blob: 48357d441d991d951584b8ef8cc637a871a77c50 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020025#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020026static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020027#endif
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +020028static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010030#define NAMESPACE_CHAR (char_u *)"abglstvw"
31
Bram Moolenaar532c7802005-01-27 14:44:31 +000032/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000033 * When recursively copying lists and dicts we need to remember which ones we
34 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000035 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000036 */
37static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039static int echo_attr = 0; // attributes used for ":echo"
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar071d4272004-06-13 20:20:40 +000041/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000042 * Info used by a ":for" loop.
43 */
Bram Moolenaar33570922005-01-25 22:26:29 +000044typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010046 int fi_semicolon; // TRUE if ending in '; var]'
47 int fi_varcount; // nr of variables in the list
48 listwatch_T fi_lw; // keep an eye on the item used.
49 list_T *fi_list; // list being used
50 int fi_bi; // index of blob
51 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000052} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000053
Bram Moolenaar48e697e2016-01-23 22:17:30 +010054static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int eval2(char_u **arg, typval_T *rettv, int evaluate);
56static int eval3(char_u **arg, typval_T *rettv, int evaluate);
57static int eval4(char_u **arg, typval_T *rettv, int evaluate);
58static int eval5(char_u **arg, typval_T *rettv, int evaluate);
59static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
60static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020061static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000062
Bram Moolenaar48e697e2016-01-23 22:17:30 +010063static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
64static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010065static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010066static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020067static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010068static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020069
Bram Moolenaare21c1582019-03-02 11:57:09 +010070/*
71 * Return "n1" divided by "n2", taking care of dividing by zero.
72 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020073 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010074num_divide(varnumber_T n1, varnumber_T n2)
75{
76 varnumber_T result;
77
78 if (n2 == 0) // give an error message?
79 {
80 if (n1 == 0)
81 result = VARNUM_MIN; // similar to NaN
82 else if (n1 < 0)
83 result = -VARNUM_MAX;
84 else
85 result = VARNUM_MAX;
86 }
87 else
88 result = n1 / n2;
89
90 return result;
91}
92
93/*
94 * Return "n1" modulus "n2", taking care of dividing by zero.
95 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020096 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010097num_modulus(varnumber_T n1, varnumber_T n2)
98{
99 // Give an error when n2 is 0?
100 return (n2 == 0) ? 0 : (n1 % n2);
101}
102
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100103#if defined(EBCDIC) || defined(PROTO)
104/*
105 * Compare struct fst by function name.
106 */
107 static int
108compare_func_name(const void *s1, const void *s2)
109{
110 struct fst *p1 = (struct fst *)s1;
111 struct fst *p2 = (struct fst *)s2;
112
113 return STRCMP(p1->f_name, p2->f_name);
114}
115
116/*
117 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100118 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100119 * On machines using EBCDIC we have to sort it.
120 */
121 static void
122sortFunctions(void)
123{
124 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
125
126 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
127}
128#endif
129
Bram Moolenaar33570922005-01-25 22:26:29 +0000130/*
131 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000132 */
133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100134eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000135{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200136 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200137 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000138
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200139#ifdef EBCDIC
140 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100141 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142 */
143 sortFunctions();
144#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000145}
146
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000147#if defined(EXITFREE) || defined(PROTO)
148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200151 evalvars_clear();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152
Bram Moolenaard9fba312005-06-26 22:34:35 +0000153 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * Top level evaluation function, returning a boolean.
169 * Sets "error" to TRUE if there was an error.
170 * Return TRUE or FALSE.
171 */
172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100173eval_to_bool(
174 char_u *arg,
175 int *error,
176 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100177 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178{
Bram Moolenaar33570922005-01-25 22:26:29 +0000179 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200180 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
182 if (skip)
183 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000184 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 else
187 {
188 *error = FALSE;
189 if (!skip)
190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100191 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000192 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 }
194 }
195 if (skip)
196 --emsg_skip;
197
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200198 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
200
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201/*
202 * Call eval1() and give an error message if not done at a lower level.
203 */
204 static int
205eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
206{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100207 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 int ret;
209 int did_emsg_before = did_emsg;
210 int called_emsg_before = called_emsg;
211
212 ret = eval1(arg, rettv, evaluate);
213 if (ret == FAIL)
214 {
215 // Report the invalid expression unless the expression evaluation has
216 // been cancelled due to an aborting error, an interrupt, or an
217 // exception, or we already gave a more specific error.
218 // Also check called_emsg for when using assert_fails().
219 if (!aborting() && did_emsg == did_emsg_before
220 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100221 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 }
223 return ret;
224}
225
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200226 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100227eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
228{
229 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100230 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200231 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100232
233 if (expr->v_type == VAR_FUNC)
234 {
235 s = expr->vval.v_string;
236 if (s == NULL || *s == NUL)
237 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200238 vim_memset(&funcexe, 0, sizeof(funcexe));
239 funcexe.evaluate = TRUE;
240 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100241 return FAIL;
242 }
243 else if (expr->v_type == VAR_PARTIAL)
244 {
245 partial_T *partial = expr->vval.v_partial;
246
247 s = partial_name(partial);
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 vim_memset(&funcexe, 0, sizeof(funcexe));
251 funcexe.evaluate = TRUE;
252 funcexe.partial = partial;
253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else
257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100258 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100259 if (s == NULL)
260 return FAIL;
261 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100262 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100264 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100265 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200266 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 }
271 return OK;
272}
273
274/*
275 * Like eval_to_bool() but using a typval_T instead of a string.
276 * Works for string, funcref and partial.
277 */
278 int
279eval_expr_to_bool(typval_T *expr, int *error)
280{
281 typval_T rettv;
282 int res;
283
284 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
285 {
286 *error = TRUE;
287 return FALSE;
288 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100289 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 clear_tv(&rettv);
291 return res;
292}
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294/*
295 * Top level evaluation function, returning a string. If "skip" is TRUE,
296 * only parsing to "nextcmd" is done, without reporting errors. Return
297 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
298 */
299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100300eval_to_string_skip(
301 char_u *arg,
302 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304{
Bram Moolenaar33570922005-01-25 22:26:29 +0000305 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 char_u *retval;
307
308 if (skip)
309 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000310 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 retval = NULL;
312 else
313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100314 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000315 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 }
317 if (skip)
318 --emsg_skip;
319
320 return retval;
321}
322
323/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000324 * Skip over an expression at "*pp".
325 * Return FAIL for an error, OK otherwise.
326 */
327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000329{
Bram Moolenaar33570922005-01-25 22:26:29 +0000330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000331
332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000334}
335
336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000338 * When "convert" is TRUE convert a List into a sequence of lines and convert
339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 * Return pointer to allocated memory, or NULL for failure.
341 */
342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100343eval_to_string(
344 char_u *arg,
345 char_u **nextcmd,
346 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 retval = NULL;
357 else
358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000360 {
361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200363 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200365 if (tv.vval.v_list->lv_len > 0)
366 ga_append(&ga, NL);
367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 ga_append(&ga, NUL);
369 retval = (char_u *)ga.ga_data;
370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000371#ifdef FEAT_FLOAT
372 else if (convert && tv.v_type == VAR_FLOAT)
373 {
374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
375 retval = vim_strsave(numbuf);
376 }
377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000378 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100379 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 }
382
383 return retval;
384}
385
386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000387 * Call eval_to_string() without using current local variables and using
388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 */
390 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100391eval_to_string_safe(
392 char_u *arg,
393 char_u **nextcmd,
394 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
396 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200397 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200399 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000400 if (use_sandbox)
401 ++sandbox;
402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000404 if (use_sandbox)
405 --sandbox;
406 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 return retval;
409}
410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411/*
412 * Top level evaluation function, returning a number.
413 * Evaluates "expr" silently.
414 * Returns -1 for an error.
415 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200416 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100417eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
Bram Moolenaar33570922005-01-25 22:26:29 +0000419 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200420 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000421 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423 ++emsg_off;
424
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 retval = -1;
427 else
428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100429 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000430 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 --emsg_off;
433
434 return retval;
435}
436
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000437/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000438 * Top level evaluation function.
439 * Returns an allocated typval_T with the result.
440 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000441 */
442 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100443eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000444{
445 typval_T *tv;
446
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200447 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000448 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100449 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000450
451 return tv;
452}
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100455 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200456 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
457 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000458 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100461call_vim_function(
462 char_u *func,
463 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200464 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200465 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000467 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200468 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100470 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200471 vim_memset(&funcexe, 0, sizeof(funcexe));
472 funcexe.firstline = curwin->w_cursor.lnum;
473 funcexe.lastline = curwin->w_cursor.lnum;
474 funcexe.evaluate = TRUE;
475 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000476 if (ret == FAIL)
477 clear_tv(rettv);
478
479 return ret;
480}
481
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100482/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100483 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100484 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200485 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
486 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100487 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200488 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100489call_func_retnr(
490 char_u *func,
491 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200492 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100493{
494 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200495 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100496
Bram Moolenaarded27a12018-08-01 19:06:03 +0200497 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100498 return -1;
499
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100500 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501 clear_tv(&rettv);
502 return retval;
503}
504
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000505/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100506 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000507 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200508 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
509 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000510 */
511 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100512call_func_retstr(
513 char_u *func,
514 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200515 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000516{
517 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000518 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000519
Bram Moolenaarded27a12018-08-01 19:06:03 +0200520 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000521 return NULL;
522
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100523 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 return retval;
526}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000528/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100529 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200530 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
531 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000532 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000533 */
534 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100535call_func_retlist(
536 char_u *func,
537 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200538 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000539{
540 typval_T rettv;
541
Bram Moolenaarded27a12018-08-01 19:06:03 +0200542 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000543 return NULL;
544
545 if (rettv.v_type != VAR_LIST)
546 {
547 clear_tv(&rettv);
548 return NULL;
549 }
550
551 return rettv.vval.v_list;
552}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555/*
556 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
557 * it in "*cp". Doesn't give error messages.
558 */
559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
Bram Moolenaar33570922005-01-25 22:26:29 +0000562 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200563 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000565 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
566 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000569 if (use_sandbox)
570 ++sandbox;
571 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000573 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = 0;
575 else
576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100577 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 if (tv.v_type == VAR_NUMBER)
579 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000580 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 retval = 0;
582 else
583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100584 // If the result is a string, check if there is a non-digit before
585 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (!VIM_ISDIGIT(*s) && *s != '-')
588 *cp = *s++;
589 retval = atol((char *)s);
590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000591 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000594 if (use_sandbox)
595 --sandbox;
596 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200598 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599}
600#endif
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000603 * Get an lval: variable, Dict item or List item that can be assigned a value
604 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
605 * "name.key", "name.key[expr]" etc.
606 * Indexing only works if "name" is an existing List or Dictionary.
607 * "name" points to the start of the name.
608 * If "rettv" is not NULL it points to the value to be assigned.
609 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
610 * wrong; must end in space or cmd separator.
611 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100612 * flags:
613 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100614 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100615 * GLV_NO_AUTOLOAD: do not use script autoloading
616 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000617 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000618 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000619 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000620 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200621 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100622get_lval(
623 char_u *name,
624 typval_T *rettv,
625 lval_T *lp,
626 int unlet,
627 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100628 int flags, // GLV_ values
629 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000632 char_u *expr_start, *expr_end;
633 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000634 dictitem_T *v;
635 typval_T var1;
636 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000637 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000639 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000640 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000641 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100642 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100644 // Clear everything in "lp".
Bram Moolenaar33570922005-01-25 22:26:29 +0000645 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000646
647 if (skip)
648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100649 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000650 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000652 }
653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100654 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000655 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000656 if (expr_start != NULL)
657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100659 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 && *p != '[' && *p != '.')
661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100662 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000663 return NULL;
664 }
665
666 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
667 if (lp->ll_exp_name == NULL)
668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100669 // Report an invalid expression in braces, unless the
670 // expression evaluation has been cancelled due to an
671 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000672 if (!aborting() && !quiet)
673 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000674 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000676 return NULL;
677 }
678 }
679 lp->ll_name = lp->ll_exp_name;
680 }
681 else
682 lp->ll_name = name;
683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100684 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000685 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
686 return p;
687
688 cc = *p;
689 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100690 // Only pass &ht when we would write to the variable, it prevents autoload
691 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100692 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
693 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000694 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000696 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000697 if (v == NULL)
698 return NULL;
699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000700 /*
701 * Loop until no more [idx] or .key is following.
702 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100704 var1.v_type = VAR_UNKNOWN;
705 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000706 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000708 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
709 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100710 && lp->ll_tv->vval.v_dict != NULL)
711 && !(lp->ll_tv->v_type == VAR_BLOB
712 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000714 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000720 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000722 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000724
Bram Moolenaar8c711452005-01-14 21:53:12 +0000725 len = -1;
726 if (*p == '.')
727 {
728 key = p + 1;
729 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
730 ;
731 if (len == 0)
732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000736 }
737 p = key + len;
738 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000739 else
740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100741 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000742 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000743 if (*p == ':')
744 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000745 else
746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000747 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100748 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000749 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100750 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000753 clear_tv(&var1);
754 return NULL;
755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000756 }
757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100758 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000759 if (*p == ':')
760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000763 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000766 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000767 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100768 if (rettv != NULL
769 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100770 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100771 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100772 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 }
779 p = skipwhite(p + 1);
780 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 else
783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100785 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000786 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000789 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100790 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100792 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100793 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000794 clear_tv(&var2);
795 return NULL;
796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000797 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000799 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000800 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000802
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100807 clear_tv(&var1);
808 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 }
811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000813 ++p;
814 }
815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000817 {
818 if (len == -1)
819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100820 // "[key]": get key from "var1"
821 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200822 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 }
827 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000828 lp->ll_list = NULL;
829 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000830 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // When assigning to a scope dictionary check that a function and
833 // variable name is valid (only variable name unless it is l: or
834 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200835 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200836 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200837 int prevval;
838 int wrong;
839
840 if (len != -1)
841 {
842 prevval = key[len];
843 key[len] = NUL;
844 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200845 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
848 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200849 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200850 || !valid_varname(key);
851 if (len != -1)
852 key[len] = prevval;
853 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200854 return NULL;
855 }
856
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000858 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100859 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200860 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100861 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100864 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200865 return NULL;
866 }
867
Bram Moolenaar31b81602019-02-10 22:14:27 +0100868 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100873 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 }
876 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100880 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 p = NULL;
883 break;
884 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100886 else if ((flags & GLV_READ_ONLY) == 0
887 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100888 {
889 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200890 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100891 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200892
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100893 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100896 else if (lp->ll_tv->v_type == VAR_BLOB)
897 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100898 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
899
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100900 /*
901 * Get the number and item for the only or first index of the List.
902 */
903 if (empty1)
904 lp->ll_n1 = 0;
905 else
906 // is number or string
907 lp->ll_n1 = (long)tv_get_number(&var1);
908 clear_tv(&var1);
909
910 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100911 || lp->ll_n1 > bloblen
912 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100913 {
914 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100915 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100916 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100917 return NULL;
918 }
919 if (lp->ll_range && !lp->ll_empty2)
920 {
921 lp->ll_n2 = (long)tv_get_number(&var2);
922 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100923 if (lp->ll_n2 < 0
924 || lp->ll_n2 >= bloblen
925 || lp->ll_n2 < lp->ll_n1)
926 {
927 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100929 return NULL;
930 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 }
932 lp->ll_blob = lp->ll_tv->vval.v_blob;
933 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100934 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 else
937 {
938 /*
939 * Get the number and item for the only or first index of the List.
940 */
941 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100944 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100945 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100946 clear_tv(&var1);
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_list = lp->ll_tv->vval.v_list;
950 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
951 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000953 if (lp->ll_n1 < 0)
954 {
955 lp->ll_n1 = 0;
956 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
957 }
958 }
959 if (lp->ll_li == NULL)
960 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200962 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 }
966
967 /*
968 * May need to find the item or absolute index for the second
969 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 * When no index given: "lp->ll_empty2" is TRUE.
971 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100975 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100976 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +0200982 {
983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 if (lp->ll_n1 < 0)
992 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
993 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +0200994 {
995 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000999 }
1000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001003 }
1004
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001005 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return p;
1007}
1008
1009/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001010 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014{
1015 vim_free(lp->ll_exp_name);
1016 vim_free(lp->ll_newkey);
1017}
1018
1019/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001020 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001022 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1023 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026set_var_lval(
1027 lval_T *lp,
1028 char_u *endp,
1029 typval_T *rettv,
1030 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001031 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033{
1034 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 listitem_T *ri;
1036 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037
1038 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001040 cc = *endp;
1041 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001042 if (lp->ll_blob != NULL)
1043 {
1044 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001046 if (op != NULL && *op != '=')
1047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001048 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001049 return;
1050 }
1051
1052 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1053 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001054 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001056 if (lp->ll_empty2)
1057 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1058
1059 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001062 return;
1063 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001064 if (lp->ll_empty2)
1065 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001066
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067 ir = 0;
1068 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1069 blob_set(lp->ll_blob, il,
1070 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 }
1072 else
1073 {
1074 val = (int)tv_get_number_chk(rettv, &error);
1075 if (!error)
1076 {
1077 garray_T *gap = &lp->ll_blob->bv_ga;
1078
1079 // Allow for appending a byte. Setting a byte beyond
1080 // the end is an error otherwise.
1081 if (lp->ll_n1 < gap->ga_len
1082 || (lp->ll_n1 == gap->ga_len
1083 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1084 {
1085 blob_set(lp->ll_blob, lp->ll_n1, val);
1086 if (lp->ll_n1 == gap->ga_len)
1087 ++gap->ga_len;
1088 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090 }
1091 }
1092 }
1093 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001095 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001096
Bram Moolenaar9937a052019-06-15 15:45:06 +02001097 if (is_const)
1098 {
1099 emsg(_(e_cannot_mod));
1100 *endp = cc;
1101 return;
1102 }
1103
Bram Moolenaarff697e62019-02-12 22:28:33 +01001104 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001105 di = NULL;
1106 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1107 &tv, &di, TRUE, FALSE) == OK)
1108 {
1109 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001110 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1111 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001112 && tv_op(&tv, rettv, op) == OK)
1113 set_var(lp->ll_name, &tv, FALSE);
1114 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001117 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02001118 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001119 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001121 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001122 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001123 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001124 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 else if (lp->ll_range)
1126 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001127 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001128 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001129
Bram Moolenaar9937a052019-06-15 15:45:06 +02001130 if (is_const)
1131 {
1132 emsg(_("E996: Cannot lock a range"));
1133 return;
1134 }
1135
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001136 /*
1137 * Check whether any of the list items is locked
1138 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001139 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001140 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001141 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001142 return;
1143 ri = ri->li_next;
1144 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1145 break;
1146 ll_li = ll_li->li_next;
1147 ++ll_n1;
1148 }
1149
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 /*
1151 * Assign the List values to the list items.
1152 */
1153 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001155 if (op != NULL && *op != '=')
1156 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1157 else
1158 {
1159 clear_tv(&lp->ll_li->li_tv);
1160 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1161 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 ri = ri->li_next;
1163 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1164 break;
1165 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001167 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001168 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 ri = NULL;
1171 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001172 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001173 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 lp->ll_li = lp->ll_li->li_next;
1175 ++lp->ll_n1;
1176 }
1177 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001178 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 else if (lp->ll_empty2
1180 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001182 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 }
1184 else
1185 {
1186 /*
1187 * Assign to a List or Dictionary item.
1188 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001189 if (is_const)
1190 {
1191 emsg(_("E996: Cannot lock a list or dict"));
1192 return;
1193 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_newkey != NULL)
1195 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001196 if (op != NULL && *op != '=')
1197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001199 return;
1200 }
1201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001203 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 if (di == NULL)
1205 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001206 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1207 {
1208 vim_free(di);
1209 return;
1210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001212 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001213 else if (op != NULL && *op != '=')
1214 {
1215 tv_op(lp->ll_tv, rettv, op);
1216 return;
1217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 /*
1222 * Assign the value to the variable or list item.
1223 */
1224 if (copy)
1225 copy_tv(rettv, lp->ll_tv);
1226 else
1227 {
1228 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001229 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 }
1232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233}
1234
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001235/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001236 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1237 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 * Returns OK or FAIL.
1239 */
1240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001243 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 char_u numbuf[NUMBUFLEN];
1245 char_u *s;
1246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001248 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001249 && tv2->v_type != VAR_BOOL && 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 Moolenaar9b4a15d2020-01-11 16:05:23 +01001257 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001258 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001259 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001260 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261 break;
1262
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001263 case VAR_BLOB:
1264 if (*op != '+' || tv2->v_type != VAR_BLOB)
1265 break;
1266 // BLOB += BLOB
1267 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1268 {
1269 blob_T *b1 = tv1->vval.v_blob;
1270 blob_T *b2 = tv2->vval.v_blob;
1271 int i, len = blob_len(b2);
1272 for (i = 0; i < len; i++)
1273 ga_append(&b1->bv_ga, blob_get(b2, i));
1274 }
1275 return OK;
1276
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 case VAR_LIST:
1278 if (*op != '+' || tv2->v_type != VAR_LIST)
1279 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001280 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1282 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1283 return OK;
1284
1285 case VAR_NUMBER:
1286 case VAR_STRING:
1287 if (tv2->v_type == VAR_LIST)
1288 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001289 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001290 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001291 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001292 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001293#ifdef FEAT_FLOAT
1294 if (tv2->v_type == VAR_FLOAT)
1295 {
1296 float_T f = n;
1297
Bram Moolenaarff697e62019-02-12 22:28:33 +01001298 if (*op == '%')
1299 break;
1300 switch (*op)
1301 {
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 case '/': f /= tv2->vval.v_float; break;
1306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001307 clear_tv(tv1);
1308 tv1->v_type = VAR_FLOAT;
1309 tv1->vval.v_float = f;
1310 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001312#endif
1313 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001314 switch (*op)
1315 {
1316 case '+': n += tv_get_number(tv2); break;
1317 case '-': n -= tv_get_number(tv2); break;
1318 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001319 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1320 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001322 clear_tv(tv1);
1323 tv1->v_type = VAR_NUMBER;
1324 tv1->vval.v_number = n;
1325 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001326 }
1327 else
1328 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001329 if (tv2->v_type == VAR_FLOAT)
1330 break;
1331
Bram Moolenaarff697e62019-02-12 22:28:33 +01001332 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001333 s = tv_get_string(tv1);
1334 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 clear_tv(tv1);
1336 tv1->v_type = VAR_STRING;
1337 tv1->vval.v_string = s;
1338 }
1339 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001340
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001341 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001342#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001343 {
1344 float_T f;
1345
Bram Moolenaarff697e62019-02-12 22:28:33 +01001346 if (*op == '%' || *op == '.'
1347 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001348 && tv2->v_type != VAR_NUMBER
1349 && tv2->v_type != VAR_STRING))
1350 break;
1351 if (tv2->v_type == VAR_FLOAT)
1352 f = tv2->vval.v_float;
1353 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001354 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001355 switch (*op)
1356 {
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 case '/': tv1->vval.v_float /= f; break;
1361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001362 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001363#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001364 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 }
1366 }
1367
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 return FAIL;
1370}
1371
1372/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001373 * Evaluate the expression used in a ":for var in expr" command.
1374 * "arg" points to "var".
1375 * Set "*errp" to TRUE for an error, FALSE otherwise;
1376 * Return a pointer that holds the info. Null when there is an error.
1377 */
1378 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001379eval_for_line(
1380 char_u *arg,
1381 int *errp,
1382 char_u **nextcmdp,
1383 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384{
Bram Moolenaar33570922005-01-25 22:26:29 +00001385 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
1388 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001390 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001391
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001392 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 if (fi == NULL)
1394 return NULL;
1395
1396 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1397 if (expr == NULL)
1398 return fi;
1399
1400 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001401 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001403 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001404 return fi;
1405 }
1406
1407 if (skip)
1408 ++emsg_skip;
1409 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1410 {
1411 *errp = FALSE;
1412 if (!skip)
1413 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001414 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001415 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 l = tv.vval.v_list;
1417 if (l == NULL)
1418 {
1419 // a null list is like an empty list: do nothing
1420 clear_tv(&tv);
1421 }
1422 else
1423 {
1424 // No need to increment the refcount, it's already set for
1425 // the list being used in "tv".
1426 fi->fi_list = l;
1427 list_add_watch(l, &fi->fi_lw);
1428 fi->fi_lw.lw_item = l->lv_first;
1429 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001430 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001431 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001432 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001433 fi->fi_bi = 0;
1434 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001435 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001436 typval_T btv;
1437
1438 // Make a copy, so that the iteration still works when the
1439 // blob is changed.
1440 blob_copy(&tv, &btv);
1441 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001443 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001444 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001445 else
1446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001447 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001448 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449 }
1450 }
1451 }
1452 if (skip)
1453 --emsg_skip;
1454
1455 return fi;
1456}
1457
1458/*
1459 * Use the first item in a ":for" list. Advance to the next.
1460 * Assign the values to the variable (list). "arg" points to the first one.
1461 * Return TRUE when a valid item was found, FALSE when at end of list or
1462 * something wrong.
1463 */
1464 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001465next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001466{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001467 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001468 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001469 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001471 if (fi->fi_blob != NULL)
1472 {
1473 typval_T tv;
1474
1475 if (fi->fi_bi >= blob_len(fi->fi_blob))
1476 return FALSE;
1477 tv.v_type = VAR_NUMBER;
1478 tv.v_lock = VAR_FIXED;
1479 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1480 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001481 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1482 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001483 }
1484
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485 item = fi->fi_lw.lw_item;
1486 if (item == NULL)
1487 result = FALSE;
1488 else
1489 {
1490 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001491 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1492 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493 }
1494 return result;
1495}
1496
1497/*
1498 * Free the structure used to store info used by ":for".
1499 */
1500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001501free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502{
Bram Moolenaar33570922005-01-25 22:26:29 +00001503 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001505 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001506 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001507 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001508 list_unref(fi->fi_list);
1509 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001510 if (fi != NULL && fi->fi_blob != NULL)
1511 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 vim_free(fi);
1513}
1514
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516set_context_for_expression(
1517 expand_T *xp,
1518 char_u *arg,
1519 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 int got_eq = FALSE;
1522 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001523 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001525 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001526 {
1527 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001530 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 {
1533 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001534 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001535 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 break;
1537 }
1538 return;
1539 }
1540 }
1541 else
1542 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1543 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 while ((xp->xp_pattern = vim_strpbrk(arg,
1545 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1546 {
1547 c = *xp->xp_pattern;
1548 if (c == '&')
1549 {
1550 c = xp->xp_pattern[1];
1551 if (c == '&')
1552 {
1553 ++xp->xp_pattern;
1554 xp->xp_context = cmdidx != CMD_let || got_eq
1555 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1556 }
1557 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001560 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1561 xp->xp_pattern += 2;
1562
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565 else if (c == '$')
1566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001567 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 xp->xp_context = EXPAND_ENV_VARS;
1569 }
1570 else if (c == '=')
1571 {
1572 got_eq = TRUE;
1573 xp->xp_context = EXPAND_EXPRESSION;
1574 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001575 else if (c == '#'
1576 && xp->xp_context == EXPAND_EXPRESSION)
1577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001578 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001579 break;
1580 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001581 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 && xp->xp_context == EXPAND_FUNCTIONS
1583 && vim_strchr(xp->xp_pattern, '(') == NULL)
1584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001585 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 break;
1587 }
1588 else if (cmdidx != CMD_let || got_eq)
1589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001590 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1593 if (c == '\\' && xp->xp_pattern[1] != NUL)
1594 ++xp->xp_pattern;
1595 xp->xp_context = EXPAND_NOTHING;
1596 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001597 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001599 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1601 /* skip */ ;
1602 xp->xp_context = EXPAND_NOTHING;
1603 }
1604 else if (c == '|')
1605 {
1606 if (xp->xp_pattern[1] == '|')
1607 {
1608 ++xp->xp_pattern;
1609 xp->xp_context = EXPAND_EXPRESSION;
1610 }
1611 else
1612 xp->xp_context = EXPAND_COMMANDS;
1613 }
1614 else
1615 xp->xp_context = EXPAND_EXPRESSION;
1616 }
1617 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001618 // Doesn't look like something valid, expand as an expression
1619 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 arg = xp->xp_pattern;
1622 if (*arg != NUL)
1623 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1624 /* skip */ ;
1625 }
1626 xp->xp_pattern = arg;
1627}
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001630 * Return TRUE if "pat" matches "text".
1631 * Does not use 'cpo' and always uses 'magic'.
1632 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001633 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001634pattern_match(char_u *pat, char_u *text, int ic)
1635{
1636 int matches = FALSE;
1637 char_u *save_cpo;
1638 regmatch_T regmatch;
1639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001640 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001641 save_cpo = p_cpo;
1642 p_cpo = (char_u *)"";
1643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1644 if (regmatch.regprog != NULL)
1645 {
1646 regmatch.rm_ic = ic;
1647 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1648 vim_regfree(regmatch.regprog);
1649 }
1650 p_cpo = save_cpo;
1651 return matches;
1652}
1653
1654/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001655 * Handle a name followed by "(". Both for just "name(arg)" and for
1656 * "expr->name(arg)".
1657 * Returns OK or FAIL.
1658 */
1659 static int
1660eval_func(
1661 char_u **arg, // points to "(", will be advanced
1662 char_u *name,
1663 int name_len,
1664 typval_T *rettv,
1665 int evaluate,
1666 typval_T *basetv) // "expr" for "expr->name(arg)"
1667{
1668 char_u *s = name;
1669 int len = name_len;
1670 partial_T *partial;
1671 int ret = OK;
1672
1673 if (!evaluate)
1674 check_vars(s, len);
1675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 // If "s" is the name of a variable of type VAR_FUNC
1677 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001678 s = deref_func_name(s, &len, &partial, !evaluate);
1679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001680 // Need to make a copy, in case evaluating the arguments makes
1681 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001682 s = vim_strsave(s);
1683 if (s == NULL)
1684 ret = FAIL;
1685 else
1686 {
1687 funcexe_T funcexe;
1688
1689 // Invoke the function.
1690 vim_memset(&funcexe, 0, sizeof(funcexe));
1691 funcexe.firstline = curwin->w_cursor.lnum;
1692 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001693 funcexe.evaluate = evaluate;
1694 funcexe.partial = partial;
1695 funcexe.basetv = basetv;
1696 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1697 }
1698 vim_free(s);
1699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001700 // If evaluate is FALSE rettv->v_type was not set in
1701 // get_func_tv, but it's needed in handle_subscript() to parse
1702 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001703 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1704 {
1705 rettv->vval.v_string = NULL;
1706 rettv->v_type = VAR_FUNC;
1707 }
1708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001709 // Stop the expression evaluation when immediately
1710 // aborting on error, or when an interrupt occurred or
1711 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001712 if (evaluate && aborting())
1713 {
1714 if (ret == OK)
1715 clear_tv(rettv);
1716 ret = FAIL;
1717 }
1718 return ret;
1719}
1720
1721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1725 */
1726
1727/*
1728 * Handle zero level expression.
1729 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001731 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 * Return OK or FAIL.
1733 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735eval0(
1736 char_u *arg,
1737 typval_T *rettv,
1738 char_u **nextcmd,
1739 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 int ret;
1742 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001743 int did_emsg_before = did_emsg;
1744 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (ret == FAIL || !ends_excmd(*p))
1749 {
1750 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /*
1753 * Report the invalid expression unless the expression evaluation has
1754 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001755 * exception, or we already gave a more specific error.
1756 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001758 if (!aborting() && did_emsg == did_emsg_before
1759 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001760 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 ret = FAIL;
1762 }
1763 if (nextcmd != NULL)
1764 *nextcmd = check_nextcmd(p);
1765
1766 return ret;
1767}
1768
1769/*
1770 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001771 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 *
1773 * "arg" must point to the first non-white of the expression.
1774 * "arg" is advanced to the next non-white after the recognized expression.
1775 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001776 * Note: "rettv.v_lock" is not set.
1777 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 * Return OK or FAIL.
1779 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 /*
1787 * Get the first variable.
1788 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 return FAIL;
1791
1792 if ((*arg)[0] == '?')
1793 {
1794 result = FALSE;
1795 if (evaluate)
1796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 int error = FALSE;
1798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001799 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001802 if (error)
1803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805
1806 /*
1807 * Get the second variable.
1808 */
1809 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001810 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 return FAIL;
1812
1813 /*
1814 * Check for the ":".
1815 */
1816 if ((*arg)[0] != ':')
1817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001818 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return FAIL;
1822 }
1823
1824 /*
1825 * Get the third variable.
1826 */
1827 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001828 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 return FAIL;
1833 }
1834 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837
1838 return OK;
1839}
1840
1841/*
1842 * Handle first level expression:
1843 * expr2 || expr2 || expr2 logical OR
1844 *
1845 * "arg" must point to the first non-white of the expression.
1846 * "arg" is advanced to the next non-white after the recognized expression.
1847 *
1848 * Return OK or FAIL.
1849 */
1850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 long result;
1855 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 /*
1859 * Get the first variable.
1860 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
1863
1864 /*
1865 * Repeat until there is no following "||".
1866 */
1867 first = TRUE;
1868 result = FALSE;
1869 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1870 {
1871 if (evaluate && first)
1872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001873 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001876 if (error)
1877 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 first = FALSE;
1879 }
1880
1881 /*
1882 * Get the second variable.
1883 */
1884 *arg = skipwhite(*arg + 2);
1885 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1886 return FAIL;
1887
1888 /*
1889 * Compute the result.
1890 */
1891 if (evaluate && !result)
1892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001893 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001896 if (error)
1897 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 if (evaluate)
1900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 rettv->v_type = VAR_NUMBER;
1902 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 }
1905
1906 return OK;
1907}
1908
1909/*
1910 * Handle second level expression:
1911 * expr3 && expr3 && expr3 logical AND
1912 *
1913 * "arg" must point to the first non-white of the expression.
1914 * "arg" is advanced to the next non-white after the recognized expression.
1915 *
1916 * Return OK or FAIL.
1917 */
1918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001919eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 long result;
1923 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001924 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 /*
1927 * Get the first variable.
1928 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 return FAIL;
1931
1932 /*
1933 * Repeat until there is no following "&&".
1934 */
1935 first = TRUE;
1936 result = TRUE;
1937 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1938 {
1939 if (evaluate && first)
1940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001941 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001944 if (error)
1945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 first = FALSE;
1947 }
1948
1949 /*
1950 * Get the second variable.
1951 */
1952 *arg = skipwhite(*arg + 2);
1953 if (eval4(arg, &var2, evaluate && result) == FAIL)
1954 return FAIL;
1955
1956 /*
1957 * Compute the result.
1958 */
1959 if (evaluate && result)
1960 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001961 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001964 if (error)
1965 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 if (evaluate)
1968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 rettv->v_type = VAR_NUMBER;
1970 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
1972 }
1973
1974 return OK;
1975}
1976
1977/*
1978 * Handle third level expression:
1979 * var1 == var2
1980 * var1 =~ var2
1981 * var1 != var2
1982 * var1 !~ var2
1983 * var1 > var2
1984 * var1 >= var2
1985 * var1 < var2
1986 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00001987 * var1 is var2
1988 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 *
1990 * "arg" must point to the first non-white of the expression.
1991 * "arg" is advanced to the next non-white after the recognized expression.
1992 *
1993 * Return OK or FAIL.
1994 */
1995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001996eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 char_u *p;
2000 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002001 exptype_T type = EXPR_UNKNOWN;
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] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002015 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002017 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 break;
2019 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002020 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002022 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 break;
2024 case '>': if (p[1] != '=')
2025 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002026 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 len = 1;
2028 }
2029 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002030 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 break;
2032 case '<': if (p[1] != '=')
2033 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002034 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 len = 1;
2036 }
2037 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002038 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 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 Moolenaar87396072019-12-31 22:36:18 +01002046 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002047 }
2048 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
2051 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002052 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002054 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002056 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (p[len] == '?')
2058 {
2059 ic = TRUE;
2060 ++len;
2061 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else if (p[len] == '#')
2064 {
2065 ic = FALSE;
2066 ++len;
2067 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002068 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 ic = p_ic;
2071
2072 /*
2073 * Get the second variable.
2074 */
2075 *arg = skipwhite(p + len);
2076 if (eval5(arg, &var2, evaluate) == FAIL)
2077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 return FAIL;
2080 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002081 if (evaluate)
2082 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002083 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002084
2085 clear_tv(&var2);
2086 return ret;
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
2090 return OK;
2091}
2092
2093/*
2094 * Handle fourth level expression:
2095 * + number addition
2096 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002097 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002098 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 *
2100 * "arg" must point to the first non-white of the expression.
2101 * "arg" is advanced to the next non-white after the recognized expression.
2102 *
2103 * Return OK or FAIL.
2104 */
2105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002106eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
Bram Moolenaar33570922005-01-25 22:26:29 +00002108 typval_T var2;
2109 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002111 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002112#ifdef FEAT_FLOAT
2113 float_T f1 = 0, f2 = 0;
2114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 char_u *s1, *s2;
2116 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2117 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002118 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 /*
2121 * Get the first variable.
2122 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002123 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return FAIL;
2125
2126 /*
2127 * Repeat computing, until no '+', '-' or '.' is following.
2128 */
2129 for (;;)
2130 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002131 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002133 concat = op == '.'
2134 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2135 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 break;
2137
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002138 if ((op != '+' || (rettv->v_type != VAR_LIST
2139 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002140#ifdef FEAT_FLOAT
2141 && (op == '.' || rettv->v_type != VAR_FLOAT)
2142#endif
2143 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002145 // For "list + ...", an illegal use of the first operand as
2146 // a number cannot be determined before evaluating the 2nd
2147 // operand: if this is also a list, all is ok.
2148 // For "something . ...", "something - ..." or "non-list + ...",
2149 // we know that the first operand needs to be a string or number
2150 // without evaluating the 2nd operand. So check before to avoid
2151 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002152 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 {
2154 clear_tv(rettv);
2155 return FAIL;
2156 }
2157 }
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 /*
2160 * Get the second variable.
2161 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002162 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2163 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002165 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 return FAIL;
2169 }
2170
2171 if (evaluate)
2172 {
2173 /*
2174 * Compute the result.
2175 */
2176 if (op == '.')
2177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002178 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002179 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002180 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 {
2182 clear_tv(rettv);
2183 clear_tv(&var2);
2184 return FAIL;
2185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 clear_tv(rettv);
2188 rettv->v_type = VAR_STRING;
2189 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002191 else if (op == '+' && rettv->v_type == VAR_BLOB
2192 && var2.v_type == VAR_BLOB)
2193 {
2194 blob_T *b1 = rettv->vval.v_blob;
2195 blob_T *b2 = var2.vval.v_blob;
2196 blob_T *b = blob_alloc();
2197 int i;
2198
2199 if (b != NULL)
2200 {
2201 for (i = 0; i < blob_len(b1); i++)
2202 ga_append(&b->bv_ga, blob_get(b1, i));
2203 for (i = 0; i < blob_len(b2); i++)
2204 ga_append(&b->bv_ga, blob_get(b2, i));
2205
2206 clear_tv(rettv);
2207 rettv_blob_set(rettv, b);
2208 }
2209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00002210 else if (op == '+' && rettv->v_type == VAR_LIST
2211 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // concatenate Lists
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002214 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2215 &var3) == FAIL)
2216 {
2217 clear_tv(rettv);
2218 clear_tv(&var2);
2219 return FAIL;
2220 }
2221 clear_tv(rettv);
2222 *rettv = var3;
2223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else
2225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 int error = FALSE;
2227
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002228#ifdef FEAT_FLOAT
2229 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002230 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002231 f1 = rettv->vval.v_float;
2232 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002234 else
2235#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002237 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002238 if (error)
2239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002240 // This can only happen for "list + non-list". For
2241 // "non-list + ..." or "something - ...", we returned
2242 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002243 clear_tv(rettv);
2244 return FAIL;
2245 }
2246#ifdef FEAT_FLOAT
2247 if (var2.v_type == VAR_FLOAT)
2248 f1 = n1;
2249#endif
2250 }
2251#ifdef FEAT_FLOAT
2252 if (var2.v_type == VAR_FLOAT)
2253 {
2254 f2 = var2.vval.v_float;
2255 n2 = 0;
2256 }
2257 else
2258#endif
2259 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002260 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002261 if (error)
2262 {
2263 clear_tv(rettv);
2264 clear_tv(&var2);
2265 return FAIL;
2266 }
2267#ifdef FEAT_FLOAT
2268 if (rettv->v_type == VAR_FLOAT)
2269 f2 = n2;
2270#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002273
2274#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002275 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002276 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2277 {
2278 if (op == '+')
2279 f1 = f1 + f2;
2280 else
2281 f1 = f1 - f2;
2282 rettv->v_type = VAR_FLOAT;
2283 rettv->vval.v_float = f1;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002286#endif
2287 {
2288 if (op == '+')
2289 n1 = n1 + n2;
2290 else
2291 n1 = n1 - n2;
2292 rettv->v_type = VAR_NUMBER;
2293 rettv->vval.v_number = n1;
2294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 }
2299 return OK;
2300}
2301
2302/*
2303 * Handle fifth level expression:
2304 * * number multiplication
2305 * / number division
2306 * % number modulo
2307 *
2308 * "arg" must point to the first non-white of the expression.
2309 * "arg" is advanced to the next non-white after the recognized expression.
2310 *
2311 * Return OK or FAIL.
2312 */
2313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002314eval6(
2315 char_u **arg,
2316 typval_T *rettv,
2317 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002322 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002323#ifdef FEAT_FLOAT
2324 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002325 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002326#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 /*
2330 * Get the first variable.
2331 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002332 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 return FAIL;
2334
2335 /*
2336 * Repeat computing, until no '*', '/' or '%' is following.
2337 */
2338 for (;;)
2339 {
2340 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002341 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 break;
2343
2344 if (evaluate)
2345 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002346#ifdef FEAT_FLOAT
2347 if (rettv->v_type == VAR_FLOAT)
2348 {
2349 f1 = rettv->vval.v_float;
2350 use_float = TRUE;
2351 n1 = 0;
2352 }
2353 else
2354#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002355 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002357 if (error)
2358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360 else
2361 n1 = 0;
2362
2363 /*
2364 * Get the second variable.
2365 */
2366 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002367 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 return FAIL;
2369
2370 if (evaluate)
2371 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002372#ifdef FEAT_FLOAT
2373 if (var2.v_type == VAR_FLOAT)
2374 {
2375 if (!use_float)
2376 {
2377 f1 = n1;
2378 use_float = TRUE;
2379 }
2380 f2 = var2.vval.v_float;
2381 n2 = 0;
2382 }
2383 else
2384#endif
2385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002386 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002387 clear_tv(&var2);
2388 if (error)
2389 return FAIL;
2390#ifdef FEAT_FLOAT
2391 if (use_float)
2392 f2 = n2;
2393#endif
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 /*
2397 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002400#ifdef FEAT_FLOAT
2401 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002403 if (op == '*')
2404 f1 = f1 * f2;
2405 else if (op == '/')
2406 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002407# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002408 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002409 if (f2 == 0.0)
2410 {
2411 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002412 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002413 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002414 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002415 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002416 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002417 }
2418 else
2419 f1 = f1 / f2;
2420# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002421 // We rely on the floating point library to handle divide
2422 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002423 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002424# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002428 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002429 return FAIL;
2430 }
2431 rettv->v_type = VAR_FLOAT;
2432 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002437 if (op == '*')
2438 n1 = n1 * n2;
2439 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002440 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002442 n1 = num_modulus(n1, n2);
2443
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444 rettv->v_type = VAR_NUMBER;
2445 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 }
2449
2450 return OK;
2451}
2452
2453/*
2454 * Handle sixth level expression:
2455 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002456 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002457 * "string" string constant
2458 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * &option-name option value
2460 * @r register contents
2461 * identifier variable value
2462 * function() function call
2463 * $VAR environment variable
2464 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002465 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002466 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002467 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 *
2469 * Also handle:
2470 * ! in front logical NOT
2471 * - in front unary minus
2472 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 * trailing [] subscript in String or List
2474 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002475 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 *
2477 * "arg" must point to the first non-white of the expression.
2478 * "arg" is advanced to the next non-white after the recognized expression.
2479 *
2480 * Return OK or FAIL.
2481 */
2482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002483eval7(
2484 char_u **arg,
2485 typval_T *rettv,
2486 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002487 int want_string UNUSED) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002489 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int len;
2491 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 char_u *start_leader, *end_leader;
2493 int ret = OK;
2494 char_u *alias;
2495
2496 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002498 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002503 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 */
2505 start_leader = *arg;
2506 while (**arg == '!' || **arg == '-' || **arg == '+')
2507 *arg = skipwhite(*arg + 1);
2508 end_leader = *arg;
2509
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002510 if (**arg == '.' && (!isdigit(*(*arg + 1))
2511#ifdef FEAT_FLOAT
2512 || current_sctx.sc_version < 2
2513#endif
2514 ))
2515 {
2516 semsg(_(e_invexpr2), *arg);
2517 ++*arg;
2518 return FAIL;
2519 }
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 switch (**arg)
2522 {
2523 /*
2524 * Number constant.
2525 */
2526 case '0':
2527 case '1':
2528 case '2':
2529 case '3':
2530 case '4':
2531 case '5':
2532 case '6':
2533 case '7':
2534 case '8':
2535 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002536 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 {
2538#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002539 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002540 int get_float = FALSE;
2541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002542 // We accept a float when the format matches
2543 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2544 // strict to avoid backwards compatibility problems.
2545 // With script version 2 and later the leading digit can be
2546 // omitted.
2547 // Don't look for a float after the "." operator, so that
2548 // ":let vers = 1.2.3" doesn't fail.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002549 if (**arg == '.')
2550 p = *arg;
2551 else
2552 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002553 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002555 get_float = TRUE;
2556 p = skipdigits(p + 2);
2557 if (*p == 'e' || *p == 'E')
2558 {
2559 ++p;
2560 if (*p == '-' || *p == '+')
2561 ++p;
2562 if (!vim_isdigit(*p))
2563 get_float = FALSE;
2564 else
2565 p = skipdigits(p + 1);
2566 }
2567 if (ASCII_ISALPHA(*p) || *p == '.')
2568 get_float = FALSE;
2569 }
2570 if (get_float)
2571 {
2572 float_T f;
2573
2574 *arg += string2float(*arg, &f);
2575 if (evaluate)
2576 {
2577 rettv->v_type = VAR_FLOAT;
2578 rettv->vval.v_float = f;
2579 }
2580 }
2581 else
2582#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002583 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002584 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002585 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01002586 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002587
2588 // Blob constant: 0z0123456789abcdef
2589 if (evaluate)
2590 blob = blob_alloc();
2591 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2592 {
2593 if (!vim_isxdigit(bp[1]))
2594 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002595 if (blob != NULL)
2596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002597 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002598 ga_clear(&blob->bv_ga);
2599 VIM_CLEAR(blob);
2600 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002601 ret = FAIL;
2602 break;
2603 }
2604 if (blob != NULL)
2605 ga_append(&blob->bv_ga,
2606 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01002607 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2608 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002609 }
2610 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002611 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002612 *arg = bp;
2613 }
2614 else
2615 {
2616 // decimal, hex or octal number
Bram Moolenaar60a8de22019-09-15 14:33:22 +02002617 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
2618 ? STR2NR_NO_OCT + STR2NR_QUOTE
2619 : STR2NR_ALL, &n, NULL, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002620 if (len == 0)
2621 {
2622 semsg(_(e_invexpr2), *arg);
2623 ret = FAIL;
2624 break;
2625 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002626 *arg += len;
2627 if (evaluate)
2628 {
2629 rettv->v_type = VAR_NUMBER;
2630 rettv->vval.v_number = n;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636 /*
2637 * String constant: "string".
2638 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 break;
2641
2642 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002646 break;
2647
2648 /*
2649 * List: [expr, expr]
2650 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 break;
2653
2654 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002655 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002656 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002657 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002658 {
2659 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002660 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002661 }
2662 else
2663 ret = NOTDONE;
2664 break;
2665
2666 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002667 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002668 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002670 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2671 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002672 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 break;
2674
2675 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002676 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002678 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 break;
2680
2681 /*
2682 * Environment variable: $VAR.
2683 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 break;
2686
2687 /*
2688 * Register contents: @r.
2689 */
2690 case '@': ++*arg;
2691 if (evaluate)
2692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002694 rettv->vval.v_string = get_reg_contents(**arg,
2695 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 if (**arg != NUL)
2698 ++*arg;
2699 break;
2700
2701 /*
2702 * nested expression: (expression).
2703 */
2704 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002705 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (**arg == ')')
2707 ++*arg;
2708 else if (ret == OK)
2709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002710 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 ret = FAIL;
2713 }
2714 break;
2715
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 break;
2718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719
2720 if (ret == NOTDONE)
2721 {
2722 /*
2723 * Must be a variable or function name.
2724 * Can also be a curly-braces kind of name: {expr}.
2725 */
2726 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002727 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (alias != NULL)
2729 s = alias;
2730
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002731 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 ret = FAIL;
2733 else
2734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002736 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002738 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002739 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002740 {
2741 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002742 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002745 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 *arg = skipwhite(*arg);
2749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002750 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2751 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002752 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002753 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2754 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
2757 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2758 */
2759 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002760 ret = eval7_leader(rettv, start_leader, &end_leader);
2761 return ret;
2762}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002764/*
2765 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2766 * Adjusts "end_leaderp" until it is at "start_leader".
2767 */
2768 static int
2769eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2770{
2771 char_u *end_leader = *end_leaderp;
2772 int ret = OK;
2773 int error = FALSE;
2774 varnumber_T val = 0;
2775#ifdef FEAT_FLOAT
2776 float_T f = 0.0;
2777
2778 if (rettv->v_type == VAR_FLOAT)
2779 f = rettv->vval.v_float;
2780 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002782 val = tv_get_number_chk(rettv, &error);
2783 if (error)
2784 {
2785 clear_tv(rettv);
2786 ret = FAIL;
2787 }
2788 else
2789 {
2790 while (end_leader > start_leader)
2791 {
2792 --end_leader;
2793 if (*end_leader == '!')
2794 {
2795#ifdef FEAT_FLOAT
2796 if (rettv->v_type == VAR_FLOAT)
2797 f = !f;
2798 else
2799#endif
2800 val = !val;
2801 }
2802 else if (*end_leader == '-')
2803 {
2804#ifdef FEAT_FLOAT
2805 if (rettv->v_type == VAR_FLOAT)
2806 f = -f;
2807 else
2808#endif
2809 val = -val;
2810 }
2811 }
2812#ifdef FEAT_FLOAT
2813 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002816 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002818 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002821 clear_tv(rettv);
2822 rettv->v_type = VAR_NUMBER;
2823 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002826 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 return ret;
2828}
2829
2830/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002831 * Call the function referred to in "rettv".
2832 */
2833 static int
2834call_func_rettv(
2835 char_u **arg,
2836 typval_T *rettv,
2837 int evaluate,
2838 dict_T *selfdict,
2839 typval_T *basetv)
2840{
2841 partial_T *pt = NULL;
2842 funcexe_T funcexe;
2843 typval_T functv;
2844 char_u *s;
2845 int ret;
2846
2847 // need to copy the funcref so that we can clear rettv
2848 if (evaluate)
2849 {
2850 functv = *rettv;
2851 rettv->v_type = VAR_UNKNOWN;
2852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002853 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002854 if (functv.v_type == VAR_PARTIAL)
2855 {
2856 pt = functv.vval.v_partial;
2857 s = partial_name(pt);
2858 }
2859 else
2860 s = functv.vval.v_string;
2861 }
2862 else
2863 s = (char_u *)"";
2864
2865 vim_memset(&funcexe, 0, sizeof(funcexe));
2866 funcexe.firstline = curwin->w_cursor.lnum;
2867 funcexe.lastline = curwin->w_cursor.lnum;
2868 funcexe.evaluate = evaluate;
2869 funcexe.partial = pt;
2870 funcexe.selfdict = selfdict;
2871 funcexe.basetv = basetv;
2872 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002874 // Clear the funcref afterwards, so that deleting it while
2875 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002876 if (evaluate)
2877 clear_tv(&functv);
2878
2879 return ret;
2880}
2881
2882/*
2883 * Evaluate "->method()".
2884 * "*arg" points to the '-'.
2885 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2886 */
2887 static int
2888eval_lambda(
2889 char_u **arg,
2890 typval_T *rettv,
2891 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002892 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002893{
2894 typval_T base = *rettv;
2895 int ret;
2896
2897 // Skip over the ->.
2898 *arg += 2;
2899 rettv->v_type = VAR_UNKNOWN;
2900
2901 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002902 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002903 return FAIL;
2904 else if (**arg != '(')
2905 {
2906 if (verbose)
2907 {
2908 if (*skipwhite(*arg) == '(')
2909 semsg(_(e_nowhitespace));
2910 else
2911 semsg(_(e_missingparen), "lambda");
2912 }
2913 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002914 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002915 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002916 else
2917 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2918
2919 // Clear the funcref afterwards, so that deleting it while
2920 // evaluating the arguments is possible (see test55).
2921 if (evaluate)
2922 clear_tv(&base);
2923
2924 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002925}
2926
2927/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002928 * Evaluate "->method()".
2929 * "*arg" points to the '-'.
2930 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2931 */
2932 static int
2933eval_method(
2934 char_u **arg,
2935 typval_T *rettv,
2936 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002937 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002938{
2939 char_u *name;
2940 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002941 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002942 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002943 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002944
2945 // Skip over the ->.
2946 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002947 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002948
Bram Moolenaarac92e252019-08-03 21:58:38 +02002949 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002950 len = get_name_len(arg, &alias, evaluate, TRUE);
2951 if (alias != NULL)
2952 name = alias;
2953
2954 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002955 {
2956 if (verbose)
2957 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002958 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002959 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002960 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002961 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002962 if (**arg != '(')
2963 {
2964 if (verbose)
2965 semsg(_(e_missingparen), name);
2966 ret = FAIL;
2967 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002968 else if (VIM_ISWHITE((*arg)[-1]))
2969 {
2970 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002971 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002972 ret = FAIL;
2973 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002974 else
2975 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002976 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002977
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002978 // Clear the funcref afterwards, so that deleting it while
2979 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002980 if (evaluate)
2981 clear_tv(&base);
2982
Bram Moolenaarac92e252019-08-03 21:58:38 +02002983 return ret;
2984}
2985
2986/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002987 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2988 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002989 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2990 */
2991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002992eval_index(
2993 char_u **arg,
2994 typval_T *rettv,
2995 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002996 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002997{
2998 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003000 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003001 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003002 long len = -1;
3003 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003005 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003006
Bram Moolenaara03f2332016-02-06 18:09:59 +01003007 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003008 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003009 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003010 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003011 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003012 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003013 return FAIL;
3014 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003015#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003016 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003017 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003018 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003019#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003020 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003021 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003022 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003023 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003024 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003026 return FAIL;
3027 case VAR_UNKNOWN:
3028 if (evaluate)
3029 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003031
3032 case VAR_STRING:
3033 case VAR_NUMBER:
3034 case VAR_LIST:
3035 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003036 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003037 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003038 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003039
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003040 init_tv(&var1);
3041 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003044 /*
3045 * dict.name
3046 */
3047 key = *arg + 1;
3048 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3049 ;
3050 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003051 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003053 }
3054 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003056 /*
3057 * something[idx]
3058 *
3059 * Get the (first) variable from inside the [].
3060 */
3061 *arg = skipwhite(*arg + 1);
3062 if (**arg == ':')
3063 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003064 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003065 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003066 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003068 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003069 clear_tv(&var1);
3070 return FAIL;
3071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003072
3073 /*
3074 * Get the second variable from inside the [:].
3075 */
3076 if (**arg == ':')
3077 {
3078 range = TRUE;
3079 *arg = skipwhite(*arg + 1);
3080 if (**arg == ']')
3081 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003082 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 if (!empty1)
3085 clear_tv(&var1);
3086 return FAIL;
3087 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003088 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091 if (!empty1)
3092 clear_tv(&var1);
3093 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003094 return FAIL;
3095 }
3096 }
3097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003099 if (**arg != ']')
3100 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003101 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003103 clear_tv(&var1);
3104 if (range)
3105 clear_tv(&var2);
3106 return FAIL;
3107 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003108 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003109 }
3110
3111 if (evaluate)
3112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003113 n1 = 0;
3114 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003116 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003117 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003118 }
3119 if (range)
3120 {
3121 if (empty2)
3122 n2 = -1;
3123 else
3124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003125 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003126 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003127 }
3128 }
3129
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003130 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003131 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003132 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003133 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003134 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003135 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003136 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003137 case VAR_SPECIAL:
3138 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003139 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003140 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003142 case VAR_NUMBER:
3143 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003144 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003145 len = (long)STRLEN(s);
3146 if (range)
3147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003148 // The resulting variable is a substring. If the indexes
3149 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003150 if (n1 < 0)
3151 {
3152 n1 = len + n1;
3153 if (n1 < 0)
3154 n1 = 0;
3155 }
3156 if (n2 < 0)
3157 n2 = len + n2;
3158 else if (n2 >= len)
3159 n2 = len;
3160 if (n1 >= len || n2 < 0 || n1 > n2)
3161 s = NULL;
3162 else
3163 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3164 }
3165 else
3166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003167 // The resulting variable is a string of a single
3168 // character. If the index is too big or negative the
3169 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003170 if (n1 >= len || n1 < 0)
3171 s = NULL;
3172 else
3173 s = vim_strnsave(s + n1, 1);
3174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003175 clear_tv(rettv);
3176 rettv->v_type = VAR_STRING;
3177 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003178 break;
3179
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003180 case VAR_BLOB:
3181 len = blob_len(rettv->vval.v_blob);
3182 if (range)
3183 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003184 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003185 // are out of range the result is empty.
3186 if (n1 < 0)
3187 {
3188 n1 = len + n1;
3189 if (n1 < 0)
3190 n1 = 0;
3191 }
3192 if (n2 < 0)
3193 n2 = len + n2;
3194 else if (n2 >= len)
3195 n2 = len - 1;
3196 if (n1 >= len || n2 < 0 || n1 > n2)
3197 {
3198 clear_tv(rettv);
3199 rettv->v_type = VAR_BLOB;
3200 rettv->vval.v_blob = NULL;
3201 }
3202 else
3203 {
3204 blob_T *blob = blob_alloc();
3205
3206 if (blob != NULL)
3207 {
3208 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3209 {
3210 blob_free(blob);
3211 return FAIL;
3212 }
3213 blob->bv_ga.ga_len = n2 - n1 + 1;
3214 for (i = n1; i <= n2; i++)
3215 blob_set(blob, i - n1,
3216 blob_get(rettv->vval.v_blob, i));
3217
3218 clear_tv(rettv);
3219 rettv_blob_set(rettv, blob);
3220 }
3221 }
3222 }
3223 else
3224 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003225 // The resulting variable is a byte value.
3226 // If the index is too big or negative that is an error.
3227 if (n1 < 0)
3228 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003229 if (n1 < len && n1 >= 0)
3230 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003231 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003232
3233 clear_tv(rettv);
3234 rettv->v_type = VAR_NUMBER;
3235 rettv->vval.v_number = v;
3236 }
3237 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003238 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003239 }
3240 break;
3241
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003242 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003243 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003244 if (n1 < 0)
3245 n1 = len + n1;
3246 if (!empty1 && (n1 < 0 || n1 >= len))
3247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 // For a range we allow invalid values and return an empty
3249 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003250 if (!range)
3251 {
3252 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003254 return FAIL;
3255 }
3256 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 }
3258 if (range)
3259 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 list_T *l;
3261 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003262
3263 if (n2 < 0)
3264 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003265 else if (n2 >= len)
3266 n2 = len - 1;
3267 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003268 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269 l = list_alloc();
3270 if (l == NULL)
3271 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003273 n1 <= n2; ++n1)
3274 {
3275 if (list_append_tv(l, &item->li_tv) == FAIL)
3276 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003277 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003278 return FAIL;
3279 }
3280 item = item->li_next;
3281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003283 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003284 }
3285 else
3286 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003287 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003288 clear_tv(rettv);
3289 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003290 }
3291 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292
3293 case VAR_DICT:
3294 if (range)
3295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003296 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003298 if (len == -1)
3299 clear_tv(&var1);
3300 return FAIL;
3301 }
3302 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304
3305 if (len == -1)
3306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003307 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003308 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 clear_tv(&var1);
3311 return FAIL;
3312 }
3313 }
3314
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003315 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003317 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003318 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 if (len == -1)
3320 clear_tv(&var1);
3321 if (item == NULL)
3322 return FAIL;
3323
3324 copy_tv(&item->di_tv, &var1);
3325 clear_tv(rettv);
3326 *rettv = var1;
3327 }
3328 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003329 }
3330 }
3331
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003332 return OK;
3333}
3334
3335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * Get an option value.
3337 * "arg" points to the '&' or '+' before the option name.
3338 * "arg" is advanced to character after the option name.
3339 * Return OK or FAIL.
3340 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342get_option_tv(
3343 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003344 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003345 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 char_u *option_end;
3348 long numval;
3349 char_u *stringval;
3350 int opt_type;
3351 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 int ret = OK;
3354 int opt_flags;
3355
3356 /*
3357 * Isolate the option name and find its value.
3358 */
3359 option_end = find_option_end(arg, &opt_flags);
3360 if (option_end == NULL)
3361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003363 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return FAIL;
3365 }
3366
3367 if (!evaluate)
3368 {
3369 *arg = option_end;
3370 return OK;
3371 }
3372
3373 c = *option_end;
3374 *option_end = NUL;
3375 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003376 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003378 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003381 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 ret = FAIL;
3383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003386 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003388 rettv->v_type = VAR_STRING;
3389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 rettv->v_type = VAR_NUMBER;
3394 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 rettv->v_type = VAR_NUMBER;
3399 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003401 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 rettv->v_type = VAR_STRING;
3404 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 }
3407 else if (working && (opt_type == -2 || opt_type == -1))
3408 ret = FAIL;
3409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003410 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 *arg = option_end;
3412
3413 return ret;
3414}
3415
3416/*
3417 * Allocate a variable for a string constant.
3418 * Return OK or FAIL.
3419 */
3420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003421get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u *p;
3424 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 int extra = 0;
3426
3427 /*
3428 * Find the end of the string, skipping backslashed characters.
3429 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003430 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
3432 if (*p == '\\' && p[1] != NUL)
3433 {
3434 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003435 // A "\<x>" form occupies at least 4 characters, and produces up
3436 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 if (*p == '<')
3438 extra += 2;
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441
3442 if (*p != '"')
3443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003444 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 return FAIL;
3446 }
3447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003448 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (!evaluate)
3450 {
3451 *arg = p + 1;
3452 return OK;
3453 }
3454
3455 /*
3456 * Copy the string into allocated memory, handling backslashed
3457 * characters.
3458 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003459 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (name == NULL)
3461 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 rettv->v_type = VAR_STRING;
3463 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar8c711452005-01-14 21:53:12 +00003465 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 if (*p == '\\')
3468 {
3469 switch (*++p)
3470 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003471 case 'b': *name++ = BS; ++p; break;
3472 case 'e': *name++ = ESC; ++p; break;
3473 case 'f': *name++ = FF; ++p; break;
3474 case 'n': *name++ = NL; ++p; break;
3475 case 'r': *name++ = CAR; ++p; break;
3476 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003478 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 case 'U':
3482 if (vim_isxdigit(p[1]))
3483 {
3484 int n, nr;
3485 int c = toupper(*p);
3486
3487 if (c == 'X')
3488 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003489 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003491 else
3492 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 nr = 0;
3494 while (--n >= 0 && vim_isxdigit(p[1]))
3495 {
3496 ++p;
3497 nr = (nr << 4) + hex2nr(*p);
3498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003499 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003500 // For "\u" store the number according to
3501 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003505 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 break;
3508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003509 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 case '0':
3511 case '1':
3512 case '2':
3513 case '3':
3514 case '4':
3515 case '5':
3516 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 case '7': *name = *p++ - '0';
3518 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003520 *name = (*name << 3) + *p++ - '0';
3521 if (*p >= '0' && *p <= '7')
3522 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003524 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 break;
3526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003527 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003528 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3529 TRUE, NULL);
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 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
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 Moolenaar5d18efe2019-12-01 21:11:22 +01003546 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003547 ++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 Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // 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,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003663 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003664{
3665 char_u *s1, *s2;
3666 dict_T *d1, *d2;
3667 int a1, a2;
3668 int i;
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003671 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
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003687 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003688 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
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003699 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,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 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 Moolenaar5d18efe2019-12-01 21:11:22 +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 Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // Catch lists and dicts that have an endless loop by limiting
3729 // 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.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003734 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 Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // 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
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003783 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003784 case VAR_SPECIAL:
3785 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01003786
3787 case VAR_FLOAT:
3788#ifdef FEAT_FLOAT
3789 return tv1->vval.v_float == tv2->vval.v_float;
3790#endif
3791 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003793 return tv1->vval.v_job == tv2->vval.v_job;
3794#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003795 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003796#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003797 return tv1->vval.v_channel == tv2->vval.v_channel;
3798#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003799 case VAR_FUNC:
3800 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003801 case VAR_UNKNOWN:
3802 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003803 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3806 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003807 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003808}
3809
3810/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003811 * Return the next (unique) copy ID.
3812 * Used for serializing nested structures.
3813 */
3814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003816{
3817 current_copyID += COPYID_INC;
3818 return current_copyID;
3819}
3820
3821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003822 * Garbage collection for lists and dictionaries.
3823 *
3824 * We use reference counts to be able to free most items right away when they
3825 * are no longer used. But for composite items it's possible that it becomes
3826 * unused while the reference count is > 0: When there is a recursive
3827 * reference. Example:
3828 * :let l = [1, 2, 3]
3829 * :let d = {9: l}
3830 * :let l[1] = d
3831 *
3832 * Since this is quite unusual we handle this with garbage collection: every
3833 * once in a while find out which lists and dicts are not referenced from any
3834 * variable.
3835 *
3836 * Here is a good reference text about garbage collection (refers to Python
3837 * but it applies to all reference-counting mechanisms):
3838 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003839 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003840
3841/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003842 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003843 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003844 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003845 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003846 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003847garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003848{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003849 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003850 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003851 buf_T *buf;
3852 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003853 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003854 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003855
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003856 if (!testing)
3857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003858 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003859 want_garbage_collect = FALSE;
3860 may_garbage_collect = FALSE;
3861 garbage_collect_at_exit = FALSE;
3862 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003863
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003864 // The execution stack can grow big, limit the size.
3865 if (exestack.ga_maxlen - exestack.ga_len > 500)
3866 {
3867 size_t new_len;
3868 char_u *pp;
3869 int n;
3870
3871 // Keep 150% of the current size, with a minimum of the growth size.
3872 n = exestack.ga_len / 2;
3873 if (n < exestack.ga_growsize)
3874 n = exestack.ga_growsize;
3875
3876 // Don't make it bigger though.
3877 if (exestack.ga_len + n < exestack.ga_maxlen)
3878 {
3879 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3880 pp = vim_realloc(exestack.ga_data, new_len);
3881 if (pp == NULL)
3882 return FAIL;
3883 exestack.ga_maxlen = exestack.ga_len + n;
3884 exestack.ga_data = pp;
3885 }
3886 }
3887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003888 // We advance by two because we add one for items referenced through
3889 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003890 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003892 /*
3893 * 1. Go through all accessible variables and mark all lists and dicts
3894 * with copyID.
3895 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // Don't free variables in the previous_funccal list unless they are only
3898 // referenced through previous_funccal. This must be first, because if
3899 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003900 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003903 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003906 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003907 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3908 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003911 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003912 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3913 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003914 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003915 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3916 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003917#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003918 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3919 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3920 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003921 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003922 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003923 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3924 NULL, NULL);
3925#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003927 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003928 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003929 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3930 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003931 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003932 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003934 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003935 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003937 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003938 abort = abort || set_ref_in_functions(copyID);
3939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003940 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003943 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003944 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003945
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003946 // callbacks in buffers
3947 abort = abort || set_ref_in_buffers(copyID);
3948
Bram Moolenaar1dced572012-04-05 16:54:08 +02003949#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003950 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003951#endif
3952
Bram Moolenaardb913952012-06-29 12:54:53 +02003953#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003954 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003955#endif
3956
3957#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003958 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003959#endif
3960
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003961#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003962 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003963 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003964#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003965#ifdef FEAT_NETBEANS_INTG
3966 abort = abort || set_ref_in_nb_channel(copyID);
3967#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003968
Bram Moolenaare3188e22016-05-31 21:13:04 +02003969#ifdef FEAT_TIMERS
3970 abort = abort || set_ref_in_timer(copyID);
3971#endif
3972
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003973#ifdef FEAT_QUICKFIX
3974 abort = abort || set_ref_in_quickfix(copyID);
3975#endif
3976
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003977#ifdef FEAT_TERMINAL
3978 abort = abort || set_ref_in_term(copyID);
3979#endif
3980
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003981#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003982 abort = abort || set_ref_in_popups(copyID);
3983#endif
3984
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003985 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003986 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003987 /*
3988 * 2. Free lists and dictionaries that are not referenced.
3989 */
3990 did_free = free_unref_items(copyID);
3991
3992 /*
3993 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003995 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003997 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003998 else if (p_verbose > 0)
3999 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004000 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004001 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004002
4003 return did_free;
4004}
4005
4006/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004007 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004008 */
4009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004010free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004011{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004012 int did_free = FALSE;
4013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004014 // Let all "free" functions know that we are here. This means no
4015 // dictionaries, lists, channels or jobs are to be freed, because we will
4016 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004017 in_free_unref_items = TRUE;
4018
4019 /*
4020 * PASS 1: free the contents of the items. We don't free the items
4021 * themselves yet, so that it is possible to decrement refcount counters
4022 */
4023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004025 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004027 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004028 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004029
4030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004031 // Go through the list of jobs and free items without the copyID. This
4032 // must happen before doing channels, because jobs refer to channels, but
4033 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004034 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004036 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004037 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4038#endif
4039
4040 /*
4041 * PASS 2: free the items themselves.
4042 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004043 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004044 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004045
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004046#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // Go through the list of jobs and free items without the copyID. This
4048 // must happen before doing channels, because jobs refer to channels, but
4049 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004050 free_unused_jobs(copyID, COPYID_MASK);
4051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004052 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004053 free_unused_channels(copyID, COPYID_MASK);
4054#endif
4055
4056 in_free_unref_items = FALSE;
4057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004058 return did_free;
4059}
4060
4061/*
4062 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004063 * "list_stack" is used to add lists to be marked. Can be NULL.
4064 *
4065 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004066 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004068set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004069{
4070 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004072 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 hashtab_T *cur_ht;
4074 ht_stack_T *ht_stack = NULL;
4075 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004076
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004077 cur_ht = ht;
4078 for (;;)
4079 {
4080 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004082 // Mark each item in the hashtab. If the item contains a hashtab
4083 // it is added to ht_stack, if it contains a list it is added to
4084 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004085 todo = (int)cur_ht->ht_used;
4086 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4087 if (!HASHITEM_EMPTY(hi))
4088 {
4089 --todo;
4090 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4091 &ht_stack, list_stack);
4092 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004093 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004094
4095 if (ht_stack == NULL)
4096 break;
4097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004098 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004099 cur_ht = ht_stack->ht;
4100 tempitem = ht_stack;
4101 ht_stack = ht_stack->prev;
4102 free(tempitem);
4103 }
4104
4105 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004106}
4107
4108/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004109 * Mark a dict and its items with "copyID".
4110 * Returns TRUE if setting references failed somehow.
4111 */
4112 int
4113set_ref_in_dict(dict_T *d, int copyID)
4114{
4115 if (d != NULL && d->dv_copyID != copyID)
4116 {
4117 d->dv_copyID = copyID;
4118 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4119 }
4120 return FALSE;
4121}
4122
4123/*
4124 * Mark a list and its items with "copyID".
4125 * Returns TRUE if setting references failed somehow.
4126 */
4127 int
4128set_ref_in_list(list_T *ll, int copyID)
4129{
4130 if (ll != NULL && ll->lv_copyID != copyID)
4131 {
4132 ll->lv_copyID = copyID;
4133 return set_ref_in_list_items(ll, copyID, NULL);
4134 }
4135 return FALSE;
4136}
4137
4138/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004139 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004140 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4141 *
4142 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004143 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004144 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004145set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004146{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004147 listitem_T *li;
4148 int abort = FALSE;
4149 list_T *cur_l;
4150 list_stack_T *list_stack = NULL;
4151 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004152
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 cur_l = l;
4154 for (;;)
4155 {
4156 if (!abort)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004157 // Mark each item in the list. If the item contains a hashtab
4158 // it is added to ht_stack, if it contains a list it is added to
4159 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4161 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4162 ht_stack, &list_stack);
4163 if (list_stack == NULL)
4164 break;
4165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004166 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004167 cur_l = list_stack->list;
4168 tempitem = list_stack;
4169 list_stack = list_stack->prev;
4170 free(tempitem);
4171 }
4172
4173 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004174}
4175
4176/*
4177 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004178 * "list_stack" is used to add lists to be marked. Can be NULL.
4179 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4180 *
4181 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004182 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004184set_ref_in_item(
4185 typval_T *tv,
4186 int copyID,
4187 ht_stack_T **ht_stack,
4188 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004189{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004190 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004191
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004192 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004193 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004194 dict_T *dd = tv->vval.v_dict;
4195
Bram Moolenaara03f2332016-02-06 18:09:59 +01004196 if (dd != NULL && dd->dv_copyID != copyID)
4197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004198 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004199 dd->dv_copyID = copyID;
4200 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004201 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004202 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4203 }
4204 else
4205 {
4206 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4207 if (newitem == NULL)
4208 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004209 else
4210 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004211 newitem->ht = &dd->dv_hashtab;
4212 newitem->prev = *ht_stack;
4213 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004214 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004215 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004216 }
4217 }
4218 else if (tv->v_type == VAR_LIST)
4219 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004220 list_T *ll = tv->vval.v_list;
4221
Bram Moolenaara03f2332016-02-06 18:09:59 +01004222 if (ll != NULL && ll->lv_copyID != copyID)
4223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004224 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004225 ll->lv_copyID = copyID;
4226 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004227 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004228 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004229 }
4230 else
4231 {
4232 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004233 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004234 if (newitem == NULL)
4235 abort = TRUE;
4236 else
4237 {
4238 newitem->list = ll;
4239 newitem->prev = *list_stack;
4240 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004241 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004242 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004243 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004244 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004245 else if (tv->v_type == VAR_FUNC)
4246 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004247 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004248 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004249 else if (tv->v_type == VAR_PARTIAL)
4250 {
4251 partial_T *pt = tv->vval.v_partial;
4252 int i;
4253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004254 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004255 if (pt != NULL)
4256 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004258
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004259 if (pt->pt_dict != NULL)
4260 {
4261 typval_T dtv;
4262
4263 dtv.v_type = VAR_DICT;
4264 dtv.vval.v_dict = pt->pt_dict;
4265 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4266 }
4267
4268 for (i = 0; i < pt->pt_argc; ++i)
4269 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4270 ht_stack, list_stack);
4271 }
4272 }
4273#ifdef FEAT_JOB_CHANNEL
4274 else if (tv->v_type == VAR_JOB)
4275 {
4276 job_T *job = tv->vval.v_job;
4277 typval_T dtv;
4278
4279 if (job != NULL && job->jv_copyID != copyID)
4280 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004281 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004282 if (job->jv_channel != NULL)
4283 {
4284 dtv.v_type = VAR_CHANNEL;
4285 dtv.vval.v_channel = job->jv_channel;
4286 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4287 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004288 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004289 {
4290 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004291 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004292 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4293 }
4294 }
4295 }
4296 else if (tv->v_type == VAR_CHANNEL)
4297 {
4298 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004299 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300 typval_T dtv;
4301 jsonq_T *jq;
4302 cbq_T *cq;
4303
4304 if (ch != NULL && ch->ch_copyID != copyID)
4305 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004306 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004307 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004308 {
4309 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4310 jq = jq->jq_next)
4311 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4312 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4313 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004314 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004315 {
4316 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004317 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4319 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004320 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004321 {
4322 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004323 dtv.vval.v_partial =
4324 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004325 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4326 }
4327 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004328 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004329 {
4330 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004331 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004332 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4333 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004334 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004335 {
4336 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004337 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004338 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4339 }
4340 }
4341 }
4342#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004344}
4345
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004347 * Return a string with the string representation of a variable.
4348 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004350 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004351 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4352 * stings as "string()", otherwise does not put quotes around strings, as
4353 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004354 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4355 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004356 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004358 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004359echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004360 typval_T *tv,
4361 char_u **tofree,
4362 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004363 int copyID,
4364 int echo_style,
4365 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004366 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004367{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004368 static int recurse = 0;
4369 char_u *r = NULL;
4370
Bram Moolenaar33570922005-01-25 22:26:29 +00004371 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004372 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004373 if (!did_echo_string_emsg)
4374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // Only give this message once for a recursive call to avoid
4376 // flooding the user with errors. And stop iterating over lists
4377 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004378 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004379 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004381 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004382 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004383 }
4384 ++recurse;
4385
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 switch (tv->v_type)
4387 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004388 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004389 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004390 {
4391 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004392 r = tv->vval.v_string;
4393 if (r == NULL)
4394 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004395 }
4396 else
4397 {
4398 *tofree = string_quote(tv->vval.v_string, FALSE);
4399 r = *tofree;
4400 }
4401 break;
4402
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004404 if (echo_style)
4405 {
4406 *tofree = NULL;
4407 r = tv->vval.v_string;
4408 }
4409 else
4410 {
4411 *tofree = string_quote(tv->vval.v_string, TRUE);
4412 r = *tofree;
4413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004414 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004415
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004416 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004417 {
4418 partial_T *pt = tv->vval.v_partial;
4419 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004420 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004421 garray_T ga;
4422 int i;
4423 char_u *tf;
4424
4425 ga_init2(&ga, 1, 100);
4426 ga_concat(&ga, (char_u *)"function(");
4427 if (fname != NULL)
4428 {
4429 ga_concat(&ga, fname);
4430 vim_free(fname);
4431 }
4432 if (pt != NULL && pt->pt_argc > 0)
4433 {
4434 ga_concat(&ga, (char_u *)", [");
4435 for (i = 0; i < pt->pt_argc; ++i)
4436 {
4437 if (i > 0)
4438 ga_concat(&ga, (char_u *)", ");
4439 ga_concat(&ga,
4440 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4441 vim_free(tf);
4442 }
4443 ga_concat(&ga, (char_u *)"]");
4444 }
4445 if (pt != NULL && pt->pt_dict != NULL)
4446 {
4447 typval_T dtv;
4448
4449 ga_concat(&ga, (char_u *)", ");
4450 dtv.v_type = VAR_DICT;
4451 dtv.vval.v_dict = pt->pt_dict;
4452 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4453 vim_free(tf);
4454 }
4455 ga_concat(&ga, (char_u *)")");
4456
4457 *tofree = ga.ga_data;
4458 r = *tofree;
4459 break;
4460 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004461
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004462 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004463 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004464 break;
4465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004467 if (tv->vval.v_list == NULL)
4468 {
4469 *tofree = NULL;
4470 r = NULL;
4471 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004472 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4473 && tv->vval.v_list->lv_len > 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_list->lv_copyID;
4481
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004482 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004483 *tofree = list2string(tv, copyID, restore_copyID);
4484 if (restore_copyID)
4485 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004486 r = *tofree;
4487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004488 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004489
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004491 if (tv->vval.v_dict == NULL)
4492 {
4493 *tofree = NULL;
4494 r = NULL;
4495 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004496 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4497 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004498 {
4499 *tofree = NULL;
4500 r = (char_u *)"{...}";
4501 }
4502 else
4503 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004504 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004505 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004506 *tofree = dict2string(tv, copyID, restore_copyID);
4507 if (restore_copyID)
4508 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004509 r = *tofree;
4510 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004511 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004513 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004514 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004515 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004516 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004517 break;
4518
Bram Moolenaar835dc632016-02-07 14:27:38 +01004519 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004520 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004522 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004523 if (composite_val)
4524 {
4525 *tofree = string_quote(r, FALSE);
4526 r = *tofree;
4527 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004529
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004530 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004531#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004532 *tofree = NULL;
4533 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4534 r = numbuf;
4535 break;
4536#endif
4537
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004538 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004539 case VAR_SPECIAL:
4540 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004541 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004542 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004543 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004544
Bram Moolenaar8502c702014-06-17 12:51:16 +02004545 if (--recurse == 0)
4546 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004547 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004548}
4549
4550/*
4551 * Return a string with the string representation of a variable.
4552 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4553 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004554 * Does not put quotes around strings, as ":echo" displays values.
4555 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4556 * May return NULL.
4557 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004559echo_string(
4560 typval_T *tv,
4561 char_u **tofree,
4562 char_u *numbuf,
4563 int copyID)
4564{
4565 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4566}
4567
4568/*
4569 * Return a string with the string representation of a variable.
4570 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4571 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004572 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004573 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004574 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004576tv2string(
4577 typval_T *tv,
4578 char_u **tofree,
4579 char_u *numbuf,
4580 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004581{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004582 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583}
4584
4585/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004586 * Return string "str" in ' quotes, doubling ' characters.
4587 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004589 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004590 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004591string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004592{
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004594 char_u *p, *r, *s;
4595
Bram Moolenaar33570922005-01-25 22:26:29 +00004596 len = (function ? 13 : 3);
4597 if (str != NULL)
4598 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004599 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004600 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004601 if (*p == '\'')
4602 ++len;
4603 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004604 s = r = alloc(len);
4605 if (r != NULL)
4606 {
4607 if (function)
4608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004610 r += 10;
4611 }
4612 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004614 if (str != NULL)
4615 for (p = str; *p != NUL; )
4616 {
4617 if (*p == '\'')
4618 *r++ = '\'';
4619 MB_COPY_CHAR(p, r);
4620 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004622 if (function)
4623 *r++ = ')';
4624 *r++ = NUL;
4625 }
4626 return s;
4627}
4628
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004629#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630/*
4631 * Convert the string "text" to a floating point number.
4632 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4633 * this always uses a decimal point.
4634 * Returns the length of the text that was consumed.
4635 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004637string2float(
4638 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004639 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640{
4641 char *s = (char *)text;
4642 float_T f;
4643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004645 if (STRNICMP(text, "inf", 3) == 0)
4646 {
4647 *value = INFINITY;
4648 return 3;
4649 }
4650 if (STRNICMP(text, "-inf", 3) == 0)
4651 {
4652 *value = -INFINITY;
4653 return 4;
4654 }
4655 if (STRNICMP(text, "nan", 3) == 0)
4656 {
4657 *value = NAN;
4658 return 3;
4659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660 f = strtod(s, &s);
4661 *value = f;
4662 return (int)((char_u *)s - text);
4663}
4664#endif
4665
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 * Get the value of an environment variable.
4668 * "arg" is pointing to the '$'. It is advanced to after the name.
4669 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004670 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 */
4672 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004673get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 char_u *string = NULL;
4676 int len;
4677 int cc;
4678 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004679 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 ++*arg;
4682 name = *arg;
4683 len = get_env_len(arg);
4684 if (evaluate)
4685 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004686 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004688
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004689 cc = name[len];
4690 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004692 string = vim_getenv(name, &mustfree);
4693 if (string != NULL && *string != NUL)
4694 {
4695 if (!mustfree)
4696 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004698 else
4699 {
4700 if (mustfree)
4701 vim_free(string);
4702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004703 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004704 string = expand_env_save(name - 1);
4705 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004706 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004707 }
4708 name[len] = cc;
4709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004710 rettv->v_type = VAR_STRING;
4711 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713
4714 return OK;
4715}
4716
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004719 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004721 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004722var2fpos(
4723 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004724 int dollar_lnum, // TRUE when $ is last line
4725 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004729 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004731 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004732 if (varp->v_type == VAR_LIST)
4733 {
4734 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004735 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004736 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004737 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004738
4739 l = varp->vval.v_list;
4740 if (l == NULL)
4741 return NULL;
4742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004744 pos.lnum = list_find_nr(l, 0L, &error);
4745 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004749 pos.col = list_find_nr(l, 1L, &error);
4750 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004751 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004752 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004755 li = list_find(l, 1L);
4756 if (li != NULL && li->li_tv.v_type == VAR_STRING
4757 && li->li_tv.vval.v_string != NULL
4758 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4759 pos.col = len + 1;
4760
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004761 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004762 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004764 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004767 pos.coladd = list_find_nr(l, 2L, &error);
4768 if (error)
4769 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004770
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004771 return &pos;
4772 }
4773
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004774 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 if (name == NULL)
4776 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004777 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004780 {
4781 if (VIsual_active)
4782 return &VIsual;
4783 return &curwin->w_cursor;
4784 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004787 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4789 return NULL;
4790 return pp;
4791 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004792
Bram Moolenaara5525202006-03-02 22:52:09 +00004793 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004794
Bram Moolenaar477933c2007-07-17 14:32:23 +00004795 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004796 {
4797 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004799 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004800 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // In silent Ex mode topline is zero, but that's not a valid line
4802 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004803 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004804 return &pos;
4805 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004807 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004808 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004810 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004811 return &pos;
4812 }
4813 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004816 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 pos.lnum = curbuf->b_ml.ml_line_count;
4819 pos.col = 0;
4820 }
4821 else
4822 {
4823 pos.lnum = curwin->w_cursor.lnum;
4824 pos.col = (colnr_T)STRLEN(ml_get_curline());
4825 }
4826 return &pos;
4827 }
4828 return NULL;
4829}
4830
4831/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004832 * Convert list in "arg" into a position and optional file number.
4833 * When "fnump" is NULL there is no file number, only 3 items.
4834 * Note that the column is passed on as-is, the caller may want to decrement
4835 * it to use 1 for the first column.
4836 * Return FAIL when conversion is not possible, doesn't check the position for
4837 * validity.
4838 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004840list2fpos(
4841 typval_T *arg,
4842 pos_T *posp,
4843 int *fnump,
4844 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004845{
4846 list_T *l = arg->vval.v_list;
4847 long i = 0;
4848 long n;
4849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4851 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004852 if (arg->v_type != VAR_LIST
4853 || l == NULL
4854 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004855 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004856 return FAIL;
4857
4858 if (fnump != NULL)
4859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004861 if (n < 0)
4862 return FAIL;
4863 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004865 *fnump = n;
4866 }
4867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004869 if (n < 0)
4870 return FAIL;
4871 posp->lnum = n;
4872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004874 if (n < 0)
4875 return FAIL;
4876 posp->col = n;
4877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004878 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004879 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004880 posp->coladd = 0;
4881 else
4882 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004883
Bram Moolenaar493c1782014-05-28 14:34:46 +02004884 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004886
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004887 return OK;
4888}
4889
4890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * Get the length of an environment variable name.
4892 * Advance "arg" to the first character after the name.
4893 * Return 0 for error.
4894 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004896get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
4898 char_u *p;
4899 int len;
4900
4901 for (p = *arg; vim_isIDc(*p); ++p)
4902 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return 0;
4905
4906 len = (int)(p - *arg);
4907 *arg = p;
4908 return len;
4909}
4910
4911/*
4912 * Get the length of the name of a function or internal variable.
4913 * "arg" is advanced to the first non-white character after the name.
4914 * Return 0 if something is wrong.
4915 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 char_u *p;
4920 int len;
4921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004924 {
4925 if (*p == ':')
4926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // "s:" is start of "s:var", but "n:" is not and can be used in
4928 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004929 len = (int)(p - *arg);
4930 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4931 || len > 1)
4932 break;
4933 }
4934 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 return 0;
4937
4938 len = (int)(p - *arg);
4939 *arg = skipwhite(p);
4940
4941 return len;
4942}
4943
4944/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004945 * Get the length of the name of a variable or function.
4946 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004948 * Return -1 if curly braces expansion failed.
4949 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 * If the name contains 'magic' {}'s, expand them and return the
4951 * expanded name in an allocated string via 'alias' - caller must free.
4952 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004954get_name_len(
4955 char_u **arg,
4956 char_u **alias,
4957 int evaluate,
4958 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 char_u *p;
4962 char_u *expr_start;
4963 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4968 && (*arg)[2] == (int)KE_SNR)
4969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 *arg += 3;
4972 return get_id_len(arg) + 3;
4973 }
4974 len = eval_fname_script(*arg);
4975 if (len > 0)
4976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 *arg += len;
4979 }
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004984 p = find_name_end(*arg, &expr_start, &expr_end,
4985 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (expr_start != NULL)
4987 {
4988 char_u *temp_string;
4989
4990 if (!evaluate)
4991 {
4992 len += (int)(p - *arg);
4993 *arg = skipwhite(p);
4994 return len;
4995 }
4996
4997 /*
4998 * Include any <SID> etc in the expanded string:
4999 * Thus the -len here.
5000 */
5001 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5002 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 *alias = temp_string;
5005 *arg = skipwhite(p);
5006 return (int)STRLEN(temp_string);
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005010 // Only give an error when there is something, otherwise it will be
5011 // reported at a higher level.
5012 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005013 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 return len;
5016}
5017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018/*
5019 * Find the end of a variable or function name, taking care of magic braces.
5020 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5021 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005022 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 * Return a pointer to just after the name. Equal to "arg" if there is no
5024 * valid name.
5025 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005027find_name_end(
5028 char_u *arg,
5029 char_u **expr_start,
5030 char_u **expr_end,
5031 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005033 int mb_nest = 0;
5034 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005036 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 *expr_start = NULL;
5041 *expr_end = NULL;
5042 }
5043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 // Quick check for valid starting character.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005045 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5046 return arg;
5047
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005048 for (p = arg; *p != NUL
5049 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005050 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005051 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005053 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005055 if (*p == '\'')
5056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005058 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005059 ;
5060 if (*p == NUL)
5061 break;
5062 }
5063 else if (*p == '"')
5064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005066 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005067 if (*p == '\\' && p[1] != NUL)
5068 ++p;
5069 if (*p == NUL)
5070 break;
5071 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005072 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005074 // "s:" is start of "s:var", but "n:" is not and can be used in
5075 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005076 len = (int)(p - arg);
5077 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005078 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005079 break;
5080 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 if (*p == '[')
5085 ++br_nest;
5086 else if (*p == ']')
5087 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005092 if (*p == '{')
5093 {
5094 mb_nest++;
5095 if (expr_start != NULL && *expr_start == NULL)
5096 *expr_start = p;
5097 }
5098 else if (*p == '}')
5099 {
5100 mb_nest--;
5101 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5102 *expr_end = p;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106
5107 return p;
5108}
5109
5110/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005111 * Expands out the 'magic' {}'s in a variable/function name.
5112 * Note that this can call itself recursively, to deal with
5113 * constructs like foo{bar}{baz}{bam}
5114 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5115 * "in_start" ^
5116 * "expr_start" ^
5117 * "expr_end" ^
5118 * "in_end" ^
5119 *
5120 * Returns a new allocated string, which the caller must free.
5121 * Returns NULL for failure.
5122 */
5123 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005124make_expanded_name(
5125 char_u *in_start,
5126 char_u *expr_start,
5127 char_u *expr_end,
5128 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005129{
5130 char_u c1;
5131 char_u *retval = NULL;
5132 char_u *temp_result;
5133 char_u *nextcmd = NULL;
5134
5135 if (expr_end == NULL || in_end == NULL)
5136 return NULL;
5137 *expr_start = NUL;
5138 *expr_end = NUL;
5139 c1 = *in_end;
5140 *in_end = NUL;
5141
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005142 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 if (temp_result != NULL && nextcmd == NULL)
5144 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005145 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5146 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005147 if (retval != NULL)
5148 {
5149 STRCPY(retval, in_start);
5150 STRCAT(retval, temp_result);
5151 STRCAT(retval, expr_end + 1);
5152 }
5153 }
5154 vim_free(temp_result);
5155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 *expr_start = '{';
5158 *expr_end = '}';
5159
5160 if (retval != NULL)
5161 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005162 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005163 if (expr_start != NULL)
5164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005166 temp_result = make_expanded_name(retval, expr_start,
5167 expr_end, temp_result);
5168 vim_free(retval);
5169 retval = temp_result;
5170 }
5171 }
5172
5173 return retval;
5174}
5175
5176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005178 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005181eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005183 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5184}
5185
5186/*
5187 * Return TRUE if character "c" can be used as the first character in a
5188 * variable or function name (excluding '{' and '}').
5189 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005192{
5193 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194}
5195
5196/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005197 * Handle:
5198 * - expr[expr], expr[expr:expr] subscript
5199 * - ".name" lookup
5200 * - function call with Funcref variable: func(expr)
5201 * - method call: var->method()
5202 *
5203 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005206handle_subscript(
5207 char_u **arg,
5208 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005209 int evaluate, // do more than finding the end
5210 int verbose, // give error messages
5211 char_u *start_leader, // start of '!' and '-' prefixes
5212 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213{
5214 int ret = OK;
5215 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005216
Bram Moolenaar61343f02019-07-20 21:11:13 +02005217 // "." is ".name" lookup when we found a dict or when evaluating and
5218 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005220 && (((**arg == '['
5221 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005222 || (!evaluate
5223 && (*arg)[1] != '.'
5224 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005225 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005226 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005227 && !VIM_ISWHITE(*(*arg - 1)))
5228 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 {
5230 if (**arg == '(')
5231 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005232 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005233
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005234 // Stop the expression evaluation when immediately aborting on
5235 // error, or when an interrupt occurred or an exception was thrown
5236 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (aborting())
5238 {
5239 if (ret == OK)
5240 clear_tv(rettv);
5241 ret = FAIL;
5242 }
5243 dict_unref(selfdict);
5244 selfdict = NULL;
5245 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005246 else if (**arg == '-')
5247 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005248 // Expression "-1.0->method()" applies the leader "-" before
5249 // applying ->.
5250 if (evaluate && *end_leaderp > start_leader)
5251 ret = eval7_leader(rettv, start_leader, end_leaderp);
5252 if (ret == OK)
5253 {
5254 if ((*arg)[2] == '{')
5255 // expr->{lambda}()
5256 ret = eval_lambda(arg, rettv, evaluate, verbose);
5257 else
5258 // expr->name()
5259 ret = eval_method(arg, rettv, evaluate, verbose);
5260 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005261 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 {
5264 dict_unref(selfdict);
5265 if (rettv->v_type == VAR_DICT)
5266 {
5267 selfdict = rettv->vval.v_dict;
5268 if (selfdict != NULL)
5269 ++selfdict->dv_refcount;
5270 }
5271 else
5272 selfdict = NULL;
5273 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5274 {
5275 clear_tv(rettv);
5276 ret = FAIL;
5277 }
5278 }
5279 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5282 // Don't do this when "Func" is already a partial that was bound
5283 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005284 if (selfdict != NULL
5285 && (rettv->v_type == VAR_FUNC
5286 || (rettv->v_type == VAR_PARTIAL
5287 && (rettv->vval.v_partial->pt_auto
5288 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005290
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 dict_unref(selfdict);
5292 return ret;
5293}
5294
5295/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 * value).
5298 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005299 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005300alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005302 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303}
5304
5305/*
5306 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 * The string "s" must have been allocated, it is consumed.
5308 * Return NULL for out of memory, the variable otherwise.
5309 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005310 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005311alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 rettv = alloc_tv();
5316 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005318 rettv->v_type = VAR_STRING;
5319 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 else
5322 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324}
5325
5326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
5332 if (varp != NULL)
5333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 switch (varp->v_type)
5335 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005337 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005339 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 vim_free(varp->vval.v_string);
5341 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005342 case VAR_PARTIAL:
5343 partial_unref(varp->vval.v_partial);
5344 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005345 case VAR_BLOB:
5346 blob_unref(varp->vval.v_blob);
5347 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 case VAR_LIST:
5349 list_unref(varp->vval.v_list);
5350 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005351 case VAR_DICT:
5352 dict_unref(varp->vval.v_dict);
5353 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005354 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005355#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005356 job_unref(varp->vval.v_job);
5357 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005358#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005359 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005360#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005361 channel_unref(varp->vval.v_channel);
5362 break;
5363#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005364 case VAR_NUMBER:
5365 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005366 case VAR_UNKNOWN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005367 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005368 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005369 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 vim_free(varp);
5372 }
5373}
5374
5375/*
5376 * Free the memory for a variable value and set the value to NULL or 0.
5377 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005379clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 if (varp != NULL)
5382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005386 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005388 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005389 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005391 case VAR_PARTIAL:
5392 partial_unref(varp->vval.v_partial);
5393 varp->vval.v_partial = NULL;
5394 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005395 case VAR_BLOB:
5396 blob_unref(varp->vval.v_blob);
5397 varp->vval.v_blob = NULL;
5398 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 case VAR_LIST:
5400 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005401 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 case VAR_DICT:
5404 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005405 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005408 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005409 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 varp->vval.v_number = 0;
5411 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005412 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005413#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005414 varp->vval.v_float = 0.0;
5415 break;
5416#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005417 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005418#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005419 job_unref(varp->vval.v_job);
5420 varp->vval.v_job = NULL;
5421#endif
5422 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005423 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005425 channel_unref(varp->vval.v_channel);
5426 varp->vval.v_channel = NULL;
5427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 case VAR_UNKNOWN:
5429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005431 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433}
5434
5435/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 * Set the value of a variable to NULL without freeing items.
5437 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005439init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440{
5441 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443}
5444
5445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 * Get the number value of a variable.
5447 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005448 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005449 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005450 * caller of incompatible types: it sets *denote to TRUE if "denote"
5451 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005453 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005454tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005456 int error = FALSE;
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459}
5460
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005461 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005462tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005463{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005464 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005469 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005470 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005471#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005472 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005473 break;
5474#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005476 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005477 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 break;
5479 case VAR_STRING:
5480 if (varp->vval.v_string != NULL)
5481 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005482 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005483 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005484 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005485 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005486 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005487 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005488 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005489 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005490 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005491 case VAR_SPECIAL:
5492 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005494#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005495 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005496 break;
5497#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005498 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005500 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005501 break;
5502#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005503 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005504 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005505 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005506 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005507 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511 n = -1;
5512 else
5513 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515}
5516
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005517#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005519tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005520{
5521 switch (varp->v_type)
5522 {
5523 case VAR_NUMBER:
5524 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005525 case VAR_FLOAT:
5526 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005528 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005529 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005530 break;
5531 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005532 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005533 break;
5534 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005535 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005536 break;
5537 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005538 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005539 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005540 case VAR_BOOL:
5541 emsg(_("E362: Using a boolean value as a Float"));
5542 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005544 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005545 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005546 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005547# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005548 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 break;
5550# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005551 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005552# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005553 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005554 break;
5555# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005556 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005557 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005558 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005559 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005560 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005561 break;
5562 }
5563 return 0;
5564}
5565#endif
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 * Get the string value of a variable.
5569 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005570 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5571 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * If the String variable has never been set, return an empty string.
5573 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005574 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005575 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005577 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005578tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 static char_u mybuf[NUMBUFLEN];
5581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005585 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005586tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005588 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005589
5590 return res != NULL ? res : (char_u *)"";
5591}
5592
Bram Moolenaar7d647822014-04-05 21:28:56 +02005593/*
5594 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5595 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005597tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005598{
5599 static char_u mybuf[NUMBUFLEN];
5600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005601 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005602}
5603
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005604 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005606{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005610 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005611 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 return buf;
5613 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005614 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 break;
5617 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005618 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 break;
5620 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005621 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005623 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005625 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005626 break;
5627#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 case VAR_STRING:
5629 if (varp->vval.v_string != NULL)
5630 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005631 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005632 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005633 case VAR_SPECIAL:
5634 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5635 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005636 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005637 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005638 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005639 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005640#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005641 {
5642 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005643 char *status;
5644
5645 if (job == NULL)
5646 return (char_u *)"no process";
5647 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005648 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005649 : "run";
5650# ifdef UNIX
5651 vim_snprintf((char *)buf, NUMBUFLEN,
5652 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005653# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005654 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005655 "process %ld %s",
5656 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005657 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005658# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005660 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5661# endif
5662 return buf;
5663 }
5664#endif
5665 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005666 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005667#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005668 {
5669 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005670 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005671
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005672 if (channel == NULL)
5673 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5674 else
5675 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005676 "channel %d %s", channel->ch_id, status);
5677 return buf;
5678 }
5679#endif
5680 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005681 case VAR_UNKNOWN:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005682 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005685 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686}
5687
5688/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005689 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5690 * string() on Dict, List, etc.
5691 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005692 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005693tv_stringify(typval_T *varp, char_u *buf)
5694{
5695 if (varp->v_type == VAR_LIST
5696 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005697 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005698 || varp->v_type == VAR_FUNC
5699 || varp->v_type == VAR_PARTIAL
5700 || varp->v_type == VAR_FLOAT)
5701 {
5702 typval_T tmp;
5703
5704 f_string(varp, &tmp);
5705 tv_get_string_buf(&tmp, buf);
5706 clear_tv(varp);
5707 *varp = tmp;
5708 return tmp.vval.v_string;
5709 }
5710 return tv_get_string_buf(varp, buf);
5711}
5712
5713/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005714 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5715 * Also give an error message, using "name" or _("name") when use_gettext is
5716 * TRUE.
5717 */
5718 static int
5719tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5720{
5721 int lock = 0;
5722
5723 switch (tv->v_type)
5724 {
5725 case VAR_BLOB:
5726 if (tv->vval.v_blob != NULL)
5727 lock = tv->vval.v_blob->bv_lock;
5728 break;
5729 case VAR_LIST:
5730 if (tv->vval.v_list != NULL)
5731 lock = tv->vval.v_list->lv_lock;
5732 break;
5733 case VAR_DICT:
5734 if (tv->vval.v_dict != NULL)
5735 lock = tv->vval.v_dict->dv_lock;
5736 break;
5737 default:
5738 break;
5739 }
5740 return var_check_lock(tv->v_lock, name, use_gettext)
5741 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5742}
5743
5744/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005747 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005748 * It is OK for "from" and "to" to point to the same item. This is used to
5749 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005752copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005755 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 switch (from->v_type)
5757 {
5758 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005759 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005760 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 to->vval.v_number = from->vval.v_number;
5762 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005763 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005764#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005765 to->vval.v_float = from->vval.v_float;
5766 break;
5767#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005771 if (to->vval.v_job != NULL)
5772 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 break;
5774#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005775 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005776#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005777 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005778 if (to->vval.v_channel != NULL)
5779 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005780 break;
5781#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 case VAR_STRING:
5783 case VAR_FUNC:
5784 if (from->vval.v_string == NULL)
5785 to->vval.v_string = NULL;
5786 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005787 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005789 if (from->v_type == VAR_FUNC)
5790 func_ref(to->vval.v_string);
5791 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005793 case VAR_PARTIAL:
5794 if (from->vval.v_partial == NULL)
5795 to->vval.v_partial = NULL;
5796 else
5797 {
5798 to->vval.v_partial = from->vval.v_partial;
5799 ++to->vval.v_partial->pt_refcount;
5800 }
5801 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005802 case VAR_BLOB:
5803 if (from->vval.v_blob == NULL)
5804 to->vval.v_blob = NULL;
5805 else
5806 {
5807 to->vval.v_blob = from->vval.v_blob;
5808 ++to->vval.v_blob->bv_refcount;
5809 }
5810 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 case VAR_LIST:
5812 if (from->vval.v_list == NULL)
5813 to->vval.v_list = NULL;
5814 else
5815 {
5816 to->vval.v_list = from->vval.v_list;
5817 ++to->vval.v_list->lv_refcount;
5818 }
5819 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case VAR_DICT:
5821 if (from->vval.v_dict == NULL)
5822 to->vval.v_dict = NULL;
5823 else
5824 {
5825 to->vval.v_dict = from->vval.v_dict;
5826 ++to->vval.v_dict->dv_refcount;
5827 }
5828 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005829 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005830 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 break;
5832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833}
5834
5835/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 * Make a copy of an item.
5837 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005838 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5839 * reference to an already copied list/dict can be used.
5840 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005841 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843item_copy(
5844 typval_T *from,
5845 typval_T *to,
5846 int deep,
5847 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005848{
5849 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005850 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851
Bram Moolenaar33570922005-01-25 22:26:29 +00005852 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005854 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 }
5857 ++recurse;
5858
5859 switch (from->v_type)
5860 {
5861 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005862 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 case VAR_STRING:
5864 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005865 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005866 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005867 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005868 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005869 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870 copy_tv(from, to);
5871 break;
5872 case VAR_LIST:
5873 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005874 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 if (from->vval.v_list == NULL)
5876 to->vval.v_list = NULL;
5877 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 to->vval.v_list = from->vval.v_list->lv_copylist;
5881 ++to->vval.v_list->lv_refcount;
5882 }
5883 else
5884 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5885 if (to->vval.v_list == NULL)
5886 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005888 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005889 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005890 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005891 case VAR_DICT:
5892 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005893 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 if (from->vval.v_dict == NULL)
5895 to->vval.v_dict = NULL;
5896 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005899 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5900 ++to->vval.v_dict->dv_refcount;
5901 }
5902 else
5903 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5904 if (to->vval.v_dict == NULL)
5905 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005906 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005907 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005908 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 }
5911 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005912 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913}
5914
5915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 * ":echo expr1 ..." print each argument separated with a space, add a
5917 * newline at the end.
5918 * ":echon expr1 ..." print each argument plain.
5919 */
5920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 char_u *p;
5927 int needclr = TRUE;
5928 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01005930 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005931 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 if (eap->skip)
5934 ++emsg_skip;
5935 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
5936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // If eval1() causes an error message the text from the command may
5938 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005939 need_clr_eos = needclr;
5940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005942 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {
5944 /*
5945 * Report the invalid expression unless the expression evaluation
5946 * has been cancelled due to an aborting error, an interrupt, or an
5947 * exception.
5948 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005949 if (!aborting() && did_emsg == did_emsg_before
5950 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005951 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005952 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 break;
5954 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005955 need_clr_eos = FALSE;
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (!eap->skip)
5958 {
5959 if (atstart)
5960 {
5961 atstart = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 // Call msg_start() after eval1(), evaluating the expression
5963 // may cause a message to appear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01005965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // Mark the saved text as finishing the line, so that what
5967 // follows is displayed on a new line when scrolling back
5968 // at the more prompt.
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02005969 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01005971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005974 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005975 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005976 if (p != NULL)
5977 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005979 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 if (*p != TAB && needclr)
5982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 // remove any text still there from the command
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005984 msg_clr_eos();
5985 needclr = FALSE;
5986 }
5987 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005991 if (has_mbyte)
5992 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005993 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005994
5995 (void)msg_outtrans_len_attr(p, i, echo_attr);
5996 p += i - 1;
5997 }
5998 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006004 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 arg = skipwhite(arg);
6006 }
6007 eap->nextcmd = check_nextcmd(arg);
6008
6009 if (eap->skip)
6010 --emsg_skip;
6011 else
6012 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006013 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (needclr)
6015 msg_clr_eos();
6016 if (eap->cmdidx == CMD_echo)
6017 msg_end();
6018 }
6019}
6020
6021/*
6022 * ":echohl {name}".
6023 */
6024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006027 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028}
6029
6030/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006031 * Returns the :echo attribute
6032 */
6033 int
6034get_echo_attr(void)
6035{
6036 return echo_attr;
6037}
6038
6039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 * ":execute expr1 ..." execute the result of an expression.
6041 * ":echomsg expr1 ..." Print a message
6042 * ":echoerr expr1 ..." Print an error
6043 * Each gets spaces around each argument and a newline at the end for
6044 * echo commands
6045 */
6046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006047ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int ret = OK;
6052 char_u *p;
6053 garray_T ga;
6054 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006055 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 ga_init2(&ga, 1, 80);
6058
6059 if (eap->skip)
6060 ++emsg_skip;
6061 while (*arg != NUL && *arg != '|' && *arg != '\n')
6062 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006063 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6064 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 if (!eap->skip)
6068 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006069 char_u buf[NUMBUFLEN];
6070
6071 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006072 {
6073 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6074 {
6075 emsg(_(e_inval_string));
6076 p = NULL;
6077 }
6078 else
6079 p = tv_get_string_buf(&rettv, buf);
6080 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006081 else
6082 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006083 if (p == NULL)
6084 {
6085 clear_tv(&rettv);
6086 ret = FAIL;
6087 break;
6088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 len = (int)STRLEN(p);
6090 if (ga_grow(&ga, len + 2) == FAIL)
6091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 ret = FAIL;
6094 break;
6095 }
6096 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 ga.ga_len += len;
6100 }
6101
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006102 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 arg = skipwhite(arg);
6104 }
6105
6106 if (ret != FAIL && ga.ga_data != NULL)
6107 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006108 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006110 // Mark the already saved text as finishing the line, so that what
6111 // follows is displayed on a new line when scrolling back at the
6112 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006113 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006114 }
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006117 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006118 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006119 out_flush();
6120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 else if (eap->cmdidx == CMD_echoerr)
6122 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006123 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006125 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (!force_abort)
6127 did_emsg = save_did_emsg;
6128 }
6129 else if (eap->cmdidx == CMD_execute)
6130 do_cmdline((char_u *)ga.ga_data,
6131 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6132 }
6133
6134 ga_clear(&ga);
6135
6136 if (eap->skip)
6137 --emsg_skip;
6138
6139 eap->nextcmd = check_nextcmd(arg);
6140}
6141
6142/*
6143 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6144 * "arg" points to the "&" or '+' when called, to "option" when returning.
6145 * Returns NULL when no option name found. Otherwise pointer to the char
6146 * after the option name.
6147 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006148 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 char_u *p = *arg;
6152
6153 ++p;
6154 if (*p == 'g' && p[1] == ':')
6155 {
6156 *opt_flags = OPT_GLOBAL;
6157 p += 2;
6158 }
6159 else if (*p == 'l' && p[1] == ':')
6160 {
6161 *opt_flags = OPT_LOCAL;
6162 p += 2;
6163 }
6164 else
6165 *opt_flags = 0;
6166
6167 if (!ASCII_ISALPHA(*p))
6168 return NULL;
6169 *arg = p;
6170
6171 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006172 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 else
6174 while (ASCII_ISALPHA(*p))
6175 ++p;
6176 return p;
6177}
6178
6179/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006180 * Display script name where an item was last set.
6181 * Should only be invoked when 'verbose' is non-zero.
6182 */
6183 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006184last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006185{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006186 char_u *p;
6187
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006188 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006189 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006190 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006191 if (p != NULL)
6192 {
6193 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006194 msg_puts(_("\n\tLast set from "));
6195 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006196 if (script_ctx.sc_lnum > 0)
6197 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006198 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006199 msg_outnum((long)script_ctx.sc_lnum);
6200 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006201 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006202 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006203 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006204 }
6205}
6206
Bram Moolenaar61be3762019-03-19 23:04:17 +01006207/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006208 * Compare "typ1" and "typ2". Put the result in "typ1".
6209 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006210 int
6211typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006212 typval_T *typ1, // first operand
6213 typval_T *typ2, // second operand
6214 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006216{
6217 int i;
6218 varnumber_T n1, n2;
6219 char_u *s1, *s2;
6220 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006221 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006222
Bram Moolenaar31988702018-02-13 12:57:42 +01006223 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 // For "is" a different type always means FALSE, for "notis"
6226 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006227 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006228 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006229 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6230 {
6231 if (type_is)
6232 {
6233 n1 = (typ1->v_type == typ2->v_type
6234 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006235 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006236 n1 = !n1;
6237 }
6238 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006239 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006240 {
6241 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006242 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006243 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006244 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006245 clear_tv(typ1);
6246 return FAIL;
6247 }
6248 else
6249 {
6250 // Compare two Blobs for being equal or unequal.
6251 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006252 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006253 n1 = !n1;
6254 }
6255 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006256 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6257 {
6258 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006259 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006260 n1 = (typ1->v_type == typ2->v_type
6261 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006262 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006263 n1 = !n1;
6264 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006265 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006266 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006267 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006268 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006270 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006271 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006272 clear_tv(typ1);
6273 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006274 }
6275 else
6276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006277 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006278 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6279 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006280 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006281 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006282 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006283 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006284
6285 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6286 {
6287 if (type_is)
6288 {
6289 n1 = (typ1->v_type == typ2->v_type
6290 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006291 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006292 n1 = !n1;
6293 }
6294 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006295 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006296 {
6297 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006298 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006299 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006300 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006301 clear_tv(typ1);
6302 return FAIL;
6303 }
6304 else
6305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006306 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006307 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6308 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006309 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006310 n1 = !n1;
6311 }
6312 }
6313
6314 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6315 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6316 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006317 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6318 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006320 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006321 clear_tv(typ1);
6322 return FAIL;
6323 }
6324 if ((typ1->v_type == VAR_PARTIAL
6325 && typ1->vval.v_partial == NULL)
6326 || (typ2->v_type == VAR_PARTIAL
6327 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006328 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006329 n1 = FALSE;
6330 else if (type_is)
6331 {
6332 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // strings are considered the same if their value is
6334 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006335 n1 = tv_equal(typ1, typ2, ic, FALSE);
6336 else if (typ1->v_type == VAR_PARTIAL
6337 && typ2->v_type == VAR_PARTIAL)
6338 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6339 else
6340 n1 = FALSE;
6341 }
6342 else
6343 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006344 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006345 n1 = !n1;
6346 }
6347
6348#ifdef FEAT_FLOAT
6349 /*
6350 * If one of the two variables is a float, compare as a float.
6351 * When using "=~" or "!~", always compare as string.
6352 */
6353 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006354 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006355 {
6356 float_T f1, f2;
6357
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006358 f1 = tv_get_float(typ1);
6359 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006360 n1 = FALSE;
6361 switch (type)
6362 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006363 case EXPR_IS:
6364 case EXPR_EQUAL: n1 = (f1 == f2); break;
6365 case EXPR_ISNOT:
6366 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6367 case EXPR_GREATER: n1 = (f1 > f2); break;
6368 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6369 case EXPR_SMALLER: n1 = (f1 < f2); break;
6370 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6371 case EXPR_UNKNOWN:
6372 case EXPR_MATCH:
6373 case EXPR_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006374 }
6375 }
6376#endif
6377
6378 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006379 * If one of the two variables is a number, compare as a number.
6380 * When using "=~" or "!~", always compare as string.
6381 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006382 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006383 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006385 n1 = tv_get_number(typ1);
6386 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006387 switch (type)
6388 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006389 case EXPR_IS:
6390 case EXPR_EQUAL: n1 = (n1 == n2); break;
6391 case EXPR_ISNOT:
6392 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6393 case EXPR_GREATER: n1 = (n1 > n2); break;
6394 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6395 case EXPR_SMALLER: n1 = (n1 < n2); break;
6396 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6397 case EXPR_UNKNOWN:
6398 case EXPR_MATCH:
6399 case EXPR_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006400 }
6401 }
6402 else
6403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006404 s1 = tv_get_string_buf(typ1, buf1);
6405 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006406 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006407 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6408 else
6409 i = 0;
6410 n1 = FALSE;
6411 switch (type)
6412 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006413 case EXPR_IS:
6414 case EXPR_EQUAL: n1 = (i == 0); break;
6415 case EXPR_ISNOT:
6416 case EXPR_NEQUAL: n1 = (i != 0); break;
6417 case EXPR_GREATER: n1 = (i > 0); break;
6418 case EXPR_GEQUAL: n1 = (i >= 0); break;
6419 case EXPR_SMALLER: n1 = (i < 0); break;
6420 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006421
Bram Moolenaar87396072019-12-31 22:36:18 +01006422 case EXPR_MATCH:
6423 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006424 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006425 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006426 n1 = !n1;
6427 break;
6428
Bram Moolenaar87396072019-12-31 22:36:18 +01006429 case EXPR_UNKNOWN: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006430 }
6431 }
6432 clear_tv(typ1);
6433 typ1->v_type = VAR_NUMBER;
6434 typ1->vval.v_number = n1;
6435
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006436 return OK;
6437}
6438
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006439 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006440typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006441{
6442 char_u *tofree;
6443 char_u numbuf[NUMBUFLEN];
6444 char_u *ret = NULL;
6445
6446 if (arg == NULL)
6447 return vim_strsave((char_u *)"(does not exist)");
6448 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006449 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006450 if (ret != NULL && tofree == NULL)
6451 ret = vim_strsave(ret);
6452 return ret;
6453}
6454
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006455#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
6457/*
6458 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006459 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * "flags" can be "g" to do a global substitute.
6461 * Returns an allocated string, NULL for error.
6462 */
6463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006464do_string_sub(
6465 char_u *str,
6466 char_u *pat,
6467 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006468 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 int sublen;
6472 regmatch_T regmatch;
6473 int i;
6474 int do_all;
6475 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006476 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 garray_T ga;
6478 char_u *ret;
6479 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006480 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006484 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486 ga_init2(&ga, 1, 200);
6487
6488 do_all = (flags[0] == 'g');
6489
6490 regmatch.rm_ic = p_ic;
6491 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6492 if (regmatch.regprog != NULL)
6493 {
6494 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006495 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6497 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006499 if (regmatch.startp[0] == regmatch.endp[0])
6500 {
6501 if (zero_width == regmatch.startp[0])
6502 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006503 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006504 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006505 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6506 (size_t)i);
6507 ga.ga_len += i;
6508 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006509 continue;
6510 }
6511 zero_width = regmatch.startp[0];
6512 }
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 /*
6515 * Get some space for a temporary buffer to do the substitution
6516 * into. It will contain:
6517 * - The text up to where the match is.
6518 * - The substituted text.
6519 * - The text after the match.
6520 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006521 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006522 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6524 {
6525 ga_clear(&ga);
6526 break;
6527 }
6528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006529 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 i = (int)(regmatch.startp[0] - tail);
6531 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006533 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 + ga.ga_len + i, TRUE, TRUE, FALSE);
6535 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006536 tail = regmatch.endp[0];
6537 if (*tail == NUL)
6538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (!do_all)
6540 break;
6541 }
6542
6543 if (ga.ga_data != NULL)
6544 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6545
Bram Moolenaar473de612013-06-08 18:19:48 +02006546 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548
6549 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6550 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006551 if (p_cpo == empty_option)
6552 p_cpo = save_cpo;
6553 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006555 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 return ret;
6558}