blob: fe6dee1f1b355d1dde9a2f4ad40c1f33bef790ed [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 Moolenaar7ebcba62020-01-12 17:42:55 +0100152 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200153 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000154
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200155 // autoloaded script names
156 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000157
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200158 // unreferenced lists and dicts
159 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200160
161 // functions not garbage collected
162 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000163}
164#endif
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
175 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
181 if (skip)
182 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000183 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 else
186 {
187 *error = FALSE;
188 if (!skip)
189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100190 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000191 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 }
193 }
194 if (skip)
195 --emsg_skip;
196
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200197 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200/*
201 * Call eval1() and give an error message if not done at a lower level.
202 */
203 static int
204eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
205{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100206 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 int ret;
208 int did_emsg_before = did_emsg;
209 int called_emsg_before = called_emsg;
210
211 ret = eval1(arg, rettv, evaluate);
212 if (ret == FAIL)
213 {
214 // Report the invalid expression unless the expression evaluation has
215 // been cancelled due to an aborting error, an interrupt, or an
216 // exception, or we already gave a more specific error.
217 // Also check called_emsg for when using assert_fails().
218 if (!aborting() && did_emsg == did_emsg_before
219 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100220 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 }
222 return ret;
223}
224
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200225 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100226eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
227{
228 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200230 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100231
232 if (expr->v_type == VAR_FUNC)
233 {
234 s = expr->vval.v_string;
235 if (s == NULL || *s == NUL)
236 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200237 vim_memset(&funcexe, 0, sizeof(funcexe));
238 funcexe.evaluate = TRUE;
239 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100240 return FAIL;
241 }
242 else if (expr->v_type == VAR_PARTIAL)
243 {
244 partial_T *partial = expr->vval.v_partial;
245
246 s = partial_name(partial);
247 if (s == NULL || *s == NUL)
248 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200249 vim_memset(&funcexe, 0, sizeof(funcexe));
250 funcexe.evaluate = TRUE;
251 funcexe.partial = partial;
252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else
256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100257 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100258 if (s == NULL)
259 return FAIL;
260 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100261 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100262 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100263 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100264 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200265 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100266 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 }
270 return OK;
271}
272
273/*
274 * Like eval_to_bool() but using a typval_T instead of a string.
275 * Works for string, funcref and partial.
276 */
277 int
278eval_expr_to_bool(typval_T *expr, int *error)
279{
280 typval_T rettv;
281 int res;
282
283 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
284 {
285 *error = TRUE;
286 return FALSE;
287 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100288 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 clear_tv(&rettv);
290 return res;
291}
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293/*
294 * Top level evaluation function, returning a string. If "skip" is TRUE,
295 * only parsing to "nextcmd" is done, without reporting errors. Return
296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
297 */
298 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100299eval_to_string_skip(
300 char_u *arg,
301 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100302 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303{
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 char_u *retval;
306
307 if (skip)
308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 retval = NULL;
311 else
312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100313 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 }
316 if (skip)
317 --emsg_skip;
318
319 return retval;
320}
321
322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000323 * Skip over an expression at "*pp".
324 * Return FAIL for an error, OK otherwise.
325 */
326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100327skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000328{
Bram Moolenaar33570922005-01-25 22:26:29 +0000329 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000330
331 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000332 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000333}
334
335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000337 * When "convert" is TRUE convert a List into a sequence of lines and convert
338 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 * Return pointer to allocated memory, or NULL for failure.
340 */
341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100342eval_to_string(
343 char_u *arg,
344 char_u **nextcmd,
345 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
Bram Moolenaar33570922005-01-25 22:26:29 +0000347 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000349 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000350#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000351 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000354 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 retval = NULL;
356 else
357 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000358 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000359 {
360 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000361 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200362 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200363 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200364 if (tv.vval.v_list->lv_len > 0)
365 ga_append(&ga, NL);
366 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000367 ga_append(&ga, NUL);
368 retval = (char_u *)ga.ga_data;
369 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000370#ifdef FEAT_FLOAT
371 else if (convert && tv.v_type == VAR_FLOAT)
372 {
373 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
374 retval = vim_strsave(numbuf);
375 }
376#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000377 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100378 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000379 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 }
381
382 return retval;
383}
384
385/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000386 * Call eval_to_string() without using current local variables and using
387 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 */
389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100390eval_to_string_safe(
391 char_u *arg,
392 char_u **nextcmd,
393 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200396 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200398 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000399 if (use_sandbox)
400 ++sandbox;
401 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000402 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000403 if (use_sandbox)
404 --sandbox;
405 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200406 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 return retval;
408}
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410/*
411 * Top level evaluation function, returning a number.
412 * Evaluates "expr" silently.
413 * Returns -1 for an error.
414 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200415 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100416eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
Bram Moolenaar33570922005-01-25 22:26:29 +0000418 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200419 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000420 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422 ++emsg_off;
423
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000424 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 retval = -1;
426 else
427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100428 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431 --emsg_off;
432
433 return retval;
434}
435
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000436/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000437 * Top level evaluation function.
438 * Returns an allocated typval_T with the result.
439 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000440 */
441 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100442eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000443{
444 typval_T *tv;
445
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200446 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000447 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100448 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000449
450 return tv;
451}
452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100454 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200455 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
456 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000457 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100460call_vim_function(
461 char_u *func,
462 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200463 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200464 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000466 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200467 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100469 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200470 vim_memset(&funcexe, 0, sizeof(funcexe));
471 funcexe.firstline = curwin->w_cursor.lnum;
472 funcexe.lastline = curwin->w_cursor.lnum;
473 funcexe.evaluate = TRUE;
474 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000475 if (ret == FAIL)
476 clear_tv(rettv);
477
478 return ret;
479}
480
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100481/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100482 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100483 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200484 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
485 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100486 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200487 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100488call_func_retnr(
489 char_u *func,
490 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200491 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492{
493 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200494 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100495
Bram Moolenaarded27a12018-08-01 19:06:03 +0200496 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100497 return -1;
498
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100499 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100500 clear_tv(&rettv);
501 return retval;
502}
503
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000504/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100505 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000506 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200507 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
508 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000509 */
510 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100511call_func_retstr(
512 char_u *func,
513 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200514 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000515{
516 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000517 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000518
Bram Moolenaarded27a12018-08-01 19:06:03 +0200519 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000520 return NULL;
521
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100522 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000523 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 return retval;
525}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000526
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000527/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100528 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200529 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
530 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000531 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000532 */
533 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100534call_func_retlist(
535 char_u *func,
536 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200537 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538{
539 typval_T rettv;
540
Bram Moolenaarded27a12018-08-01 19:06:03 +0200541 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000542 return NULL;
543
544 if (rettv.v_type != VAR_LIST)
545 {
546 clear_tv(&rettv);
547 return NULL;
548 }
549
550 return rettv.vval.v_list;
551}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_FOLDING
554/*
555 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
556 * it in "*cp". Doesn't give error messages.
557 */
558 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100559eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar33570922005-01-25 22:26:29 +0000561 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200562 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000564 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
565 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 ++sandbox;
570 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000572 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 retval = 0;
574 else
575 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100576 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000577 if (tv.v_type == VAR_NUMBER)
578 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000579 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 retval = 0;
581 else
582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100583 // If the result is a string, check if there is a non-digit before
584 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000585 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (!VIM_ISDIGIT(*s) && *s != '-')
587 *cp = *s++;
588 retval = atol((char *)s);
589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000590 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 }
592 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000593 if (use_sandbox)
594 --sandbox;
595 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200597 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598}
599#endif
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000602 * Get an lval: variable, Dict item or List item that can be assigned a value
603 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
604 * "name.key", "name.key[expr]" etc.
605 * Indexing only works if "name" is an existing List or Dictionary.
606 * "name" points to the start of the name.
607 * If "rettv" is not NULL it points to the value to be assigned.
608 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
609 * wrong; must end in space or cmd separator.
610 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100611 * flags:
612 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100613 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100614 * GLV_NO_AUTOLOAD: do not use script autoloading
615 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000616 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000617 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000618 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000619 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100621get_lval(
622 char_u *name,
623 typval_T *rettv,
624 lval_T *lp,
625 int unlet,
626 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100627 int flags, // GLV_ values
628 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000630 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000631 char_u *expr_start, *expr_end;
632 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000633 dictitem_T *v;
634 typval_T var1;
635 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000636 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000637 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000638 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000639 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000640 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100641 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100643 // Clear everything in "lp".
Bram Moolenaar33570922005-01-25 22:26:29 +0000644 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000645
646 if (skip)
647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100648 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000649 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000650 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000651 }
652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100653 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000654 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000655 if (expr_start != NULL)
656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100657 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100658 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000659 && *p != '[' && *p != '.')
660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100661 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000662 return NULL;
663 }
664
665 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
666 if (lp->ll_exp_name == NULL)
667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100668 // Report an invalid expression in braces, unless the
669 // expression evaluation has been cancelled due to an
670 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000671 if (!aborting() && !quiet)
672 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000673 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000675 return NULL;
676 }
677 }
678 lp->ll_name = lp->ll_exp_name;
679 }
680 else
681 lp->ll_name = name;
682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100683 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000684 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
685 return p;
686
687 cc = *p;
688 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100689 // Only pass &ht when we would write to the variable, it prevents autoload
690 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100691 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
692 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000693 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100694 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000695 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000696 if (v == NULL)
697 return NULL;
698
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000699 /*
700 * Loop until no more [idx] or .key is following.
701 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000702 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100703 var1.v_type = VAR_UNKNOWN;
704 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000705 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000707 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
708 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100709 && lp->ll_tv->vval.v_dict != NULL)
710 && !(lp->ll_tv->v_type == VAR_BLOB
711 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000713 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100714 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000719 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100720 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000723
Bram Moolenaar8c711452005-01-14 21:53:12 +0000724 len = -1;
725 if (*p == '.')
726 {
727 key = p + 1;
728 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
729 ;
730 if (len == 0)
731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100733 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000735 }
736 p = key + len;
737 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000738 else
739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100740 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000741 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000742 if (*p == ':')
743 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000744 else
745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000746 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100747 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100749 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100751 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000752 clear_tv(&var1);
753 return NULL;
754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000755 }
756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100757 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000758 if (*p == ':')
759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000760 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000762 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100763 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000766 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100767 if (rettv != NULL
768 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100769 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100770 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100771 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000773 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 }
778 p = skipwhite(p + 1);
779 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 else
782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000783 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000785 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000788 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100789 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100792 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 clear_tv(&var2);
794 return NULL;
795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000799 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000801
Bram Moolenaar8c711452005-01-14 21:53:12 +0000802 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100806 clear_tv(&var1);
807 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 }
810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100811 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000812 ++p;
813 }
814
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000816 {
817 if (len == -1)
818 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100819 // "[key]": get key from "var1"
820 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200821 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000825 }
826 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000827 lp->ll_list = NULL;
828 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000829 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100831 // When assigning to a scope dictionary check that a function and
832 // variable name is valid (only variable name unless it is l: or
833 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200834 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200835 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200836 int prevval;
837 int wrong;
838
839 if (len != -1)
840 {
841 prevval = key[len];
842 key[len] = NUL;
843 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200844 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200846 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
847 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200848 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200849 || !valid_varname(key);
850 if (len != -1)
851 key[len] = prevval;
852 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200853 return NULL;
854 }
855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100858 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200859 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100860 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100862 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100863 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200864 return NULL;
865 }
866
Bram Moolenaar31b81602019-02-10 22:14:27 +0100867 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100871 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100872 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000874 }
875 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000877 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100879 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 p = NULL;
882 break;
883 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100884 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100885 else if ((flags & GLV_READ_ONLY) == 0
886 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100887 {
888 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100890 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200891
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100892 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100895 else if (lp->ll_tv->v_type == VAR_BLOB)
896 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100897 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
898
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100899 /*
900 * Get the number and item for the only or first index of the List.
901 */
902 if (empty1)
903 lp->ll_n1 = 0;
904 else
905 // is number or string
906 lp->ll_n1 = (long)tv_get_number(&var1);
907 clear_tv(&var1);
908
909 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100910 || lp->ll_n1 > bloblen
911 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100912 {
913 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100915 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100916 return NULL;
917 }
918 if (lp->ll_range && !lp->ll_empty2)
919 {
920 lp->ll_n2 = (long)tv_get_number(&var2);
921 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100922 if (lp->ll_n2 < 0
923 || lp->ll_n2 >= bloblen
924 || lp->ll_n2 < lp->ll_n1)
925 {
926 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100928 return NULL;
929 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100930 }
931 lp->ll_blob = lp->ll_tv->vval.v_blob;
932 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100933 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000935 else
936 {
937 /*
938 * Get the number and item for the only or first index of the List.
939 */
940 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000942 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100944 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100945 clear_tv(&var1);
946
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000947 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 lp->ll_list = lp->ll_tv->vval.v_list;
949 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
950 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000952 if (lp->ll_n1 < 0)
953 {
954 lp->ll_n1 = 0;
955 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
956 }
957 }
958 if (lp->ll_li == NULL)
959 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100960 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200961 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100962 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 }
965
966 /*
967 * May need to find the item or absolute index for the second
968 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 * When no index given: "lp->ll_empty2" is TRUE.
970 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100974 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100975 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +0200981 {
982 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200985 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 }
988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 if (lp->ll_n1 < 0)
991 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
992 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +0200993 {
994 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100995 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200997 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000998 }
999
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 }
1003
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001004 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return p;
1006}
1007
1008/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013{
1014 vim_free(lp->ll_exp_name);
1015 vim_free(lp->ll_newkey);
1016}
1017
1018/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001019 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001021 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1022 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001025set_var_lval(
1026 lval_T *lp,
1027 char_u *endp,
1028 typval_T *rettv,
1029 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001030 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001031 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032{
1033 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 listitem_T *ri;
1035 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036
1037 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001038 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001039 cc = *endp;
1040 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001041 if (lp->ll_blob != NULL)
1042 {
1043 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001044
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001045 if (op != NULL && *op != '=')
1046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001047 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001048 return;
1049 }
1050
1051 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1052 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001053 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001054
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001055 if (lp->ll_empty2)
1056 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1057
1058 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001060 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001061 return;
1062 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001063 if (lp->ll_empty2)
1064 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001065
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001066 ir = 0;
1067 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1068 blob_set(lp->ll_blob, il,
1069 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001070 }
1071 else
1072 {
1073 val = (int)tv_get_number_chk(rettv, &error);
1074 if (!error)
1075 {
1076 garray_T *gap = &lp->ll_blob->bv_ga;
1077
1078 // Allow for appending a byte. Setting a byte beyond
1079 // the end is an error otherwise.
1080 if (lp->ll_n1 < gap->ga_len
1081 || (lp->ll_n1 == gap->ga_len
1082 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1083 {
1084 blob_set(lp->ll_blob, lp->ll_n1, val);
1085 if (lp->ll_n1 == gap->ga_len)
1086 ++gap->ga_len;
1087 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001088 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001089 }
1090 }
1091 }
1092 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001094 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001095
Bram Moolenaar9937a052019-06-15 15:45:06 +02001096 if (is_const)
1097 {
1098 emsg(_(e_cannot_mod));
1099 *endp = cc;
1100 return;
1101 }
1102
Bram Moolenaarff697e62019-02-12 22:28:33 +01001103 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001104 di = NULL;
1105 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1106 &tv, &di, TRUE, FALSE) == OK)
1107 {
1108 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001109 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1110 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001111 && tv_op(&tv, rettv, op) == OK)
1112 set_var(lp->ll_name, &tv, FALSE);
1113 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001115 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001116 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02001117 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001118 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001119 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001120 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001121 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001122 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001123 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 else if (lp->ll_range)
1125 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001126 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001127 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001128
Bram Moolenaar9937a052019-06-15 15:45:06 +02001129 if (is_const)
1130 {
1131 emsg(_("E996: Cannot lock a range"));
1132 return;
1133 }
1134
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001135 /*
1136 * Check whether any of the list items is locked
1137 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001138 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001139 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001140 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001141 return;
1142 ri = ri->li_next;
1143 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1144 break;
1145 ll_li = ll_li->li_next;
1146 ++ll_n1;
1147 }
1148
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 /*
1150 * Assign the List values to the list items.
1151 */
1152 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001153 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001154 if (op != NULL && *op != '=')
1155 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1156 else
1157 {
1158 clear_tv(&lp->ll_li->li_tv);
1159 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1160 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 ri = ri->li_next;
1162 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1163 break;
1164 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001166 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001167 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 ri = NULL;
1170 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001171 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001172 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 lp->ll_li = lp->ll_li->li_next;
1174 ++lp->ll_n1;
1175 }
1176 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001177 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001178 else if (lp->ll_empty2
1179 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 }
1183 else
1184 {
1185 /*
1186 * Assign to a List or Dictionary item.
1187 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001188 if (is_const)
1189 {
1190 emsg(_("E996: Cannot lock a list or dict"));
1191 return;
1192 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 if (lp->ll_newkey != NULL)
1194 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001195 if (op != NULL && *op != '=')
1196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001198 return;
1199 }
1200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001201 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001202 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203 if (di == NULL)
1204 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001205 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1206 {
1207 vim_free(di);
1208 return;
1209 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001211 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001212 else if (op != NULL && *op != '=')
1213 {
1214 tv_op(lp->ll_tv, rettv, op);
1215 return;
1216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 /*
1221 * Assign the value to the variable or list item.
1222 */
1223 if (copy)
1224 copy_tv(rettv, lp->ll_tv);
1225 else
1226 {
1227 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001228 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 }
1231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232}
1233
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001234/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001235 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1236 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 * Returns OK or FAIL.
1238 */
1239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001240tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001241{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001242 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001243 char_u numbuf[NUMBUFLEN];
1244 char_u *s;
1245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001247 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001248 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 {
1250 switch (tv1->v_type)
1251 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001252 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001253 case VAR_DICT:
1254 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001255 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001256 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001257 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001258 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001259 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 break;
1261
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001262 case VAR_BLOB:
1263 if (*op != '+' || tv2->v_type != VAR_BLOB)
1264 break;
1265 // BLOB += BLOB
1266 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1267 {
1268 blob_T *b1 = tv1->vval.v_blob;
1269 blob_T *b2 = tv2->vval.v_blob;
1270 int i, len = blob_len(b2);
1271 for (i = 0; i < len; i++)
1272 ga_append(&b1->bv_ga, blob_get(b2, i));
1273 }
1274 return OK;
1275
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 case VAR_LIST:
1277 if (*op != '+' || tv2->v_type != VAR_LIST)
1278 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001279 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1281 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1282 return OK;
1283
1284 case VAR_NUMBER:
1285 case VAR_STRING:
1286 if (tv2->v_type == VAR_LIST)
1287 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001290 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001291 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001292#ifdef FEAT_FLOAT
1293 if (tv2->v_type == VAR_FLOAT)
1294 {
1295 float_T f = n;
1296
Bram Moolenaarff697e62019-02-12 22:28:33 +01001297 if (*op == '%')
1298 break;
1299 switch (*op)
1300 {
1301 case '+': f += tv2->vval.v_float; break;
1302 case '-': f -= tv2->vval.v_float; break;
1303 case '*': f *= tv2->vval.v_float; break;
1304 case '/': f /= tv2->vval.v_float; break;
1305 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001306 clear_tv(tv1);
1307 tv1->v_type = VAR_FLOAT;
1308 tv1->vval.v_float = f;
1309 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001311#endif
1312 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001313 switch (*op)
1314 {
1315 case '+': n += tv_get_number(tv2); break;
1316 case '-': n -= tv_get_number(tv2); break;
1317 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001318 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1319 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001321 clear_tv(tv1);
1322 tv1->v_type = VAR_NUMBER;
1323 tv1->vval.v_number = n;
1324 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325 }
1326 else
1327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001328 if (tv2->v_type == VAR_FLOAT)
1329 break;
1330
Bram Moolenaarff697e62019-02-12 22:28:33 +01001331 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 s = tv_get_string(tv1);
1333 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 clear_tv(tv1);
1335 tv1->v_type = VAR_STRING;
1336 tv1->vval.v_string = s;
1337 }
1338 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001339
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001340 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001341#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001342 {
1343 float_T f;
1344
Bram Moolenaarff697e62019-02-12 22:28:33 +01001345 if (*op == '%' || *op == '.'
1346 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001347 && tv2->v_type != VAR_NUMBER
1348 && tv2->v_type != VAR_STRING))
1349 break;
1350 if (tv2->v_type == VAR_FLOAT)
1351 f = tv2->vval.v_float;
1352 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001353 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001354 switch (*op)
1355 {
1356 case '+': tv1->vval.v_float += f; break;
1357 case '-': tv1->vval.v_float -= f; break;
1358 case '*': tv1->vval.v_float *= f; break;
1359 case '/': tv1->vval.v_float /= f; break;
1360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001362#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001363 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
1365 }
1366
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001367 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 return FAIL;
1369}
1370
1371/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 * Evaluate the expression used in a ":for var in expr" command.
1373 * "arg" points to "var".
1374 * Set "*errp" to TRUE for an error, FALSE otherwise;
1375 * Return a pointer that holds the info. Null when there is an error.
1376 */
1377 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378eval_for_line(
1379 char_u *arg,
1380 int *errp,
1381 char_u **nextcmdp,
1382 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001383{
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001385 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
1387 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001390
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001391 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392 if (fi == NULL)
1393 return NULL;
1394
1395 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1396 if (expr == NULL)
1397 return fi;
1398
1399 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001400 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 return fi;
1404 }
1405
1406 if (skip)
1407 ++emsg_skip;
1408 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1409 {
1410 *errp = FALSE;
1411 if (!skip)
1412 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001413 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 l = tv.vval.v_list;
1416 if (l == NULL)
1417 {
1418 // a null list is like an empty list: do nothing
1419 clear_tv(&tv);
1420 }
1421 else
1422 {
1423 // No need to increment the refcount, it's already set for
1424 // the list being used in "tv".
1425 fi->fi_list = l;
1426 list_add_watch(l, &fi->fi_lw);
1427 fi->fi_lw.lw_item = l->lv_first;
1428 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001429 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001430 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001431 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001432 fi->fi_bi = 0;
1433 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001434 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001435 typval_T btv;
1436
1437 // Make a copy, so that the iteration still works when the
1438 // blob is changed.
1439 blob_copy(&tv, &btv);
1440 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001442 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001443 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 else
1445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001446 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001447 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001448 }
1449 }
1450 }
1451 if (skip)
1452 --emsg_skip;
1453
1454 return fi;
1455}
1456
1457/*
1458 * Use the first item in a ":for" list. Advance to the next.
1459 * Assign the values to the variable (list). "arg" points to the first one.
1460 * Return TRUE when a valid item was found, FALSE when at end of list or
1461 * something wrong.
1462 */
1463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001465{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001467 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001468 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001469
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001470 if (fi->fi_blob != NULL)
1471 {
1472 typval_T tv;
1473
1474 if (fi->fi_bi >= blob_len(fi->fi_blob))
1475 return FALSE;
1476 tv.v_type = VAR_NUMBER;
1477 tv.v_lock = VAR_FIXED;
1478 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1479 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001480 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1481 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 }
1483
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 item = fi->fi_lw.lw_item;
1485 if (item == NULL)
1486 result = FALSE;
1487 else
1488 {
1489 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001490 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1491 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 }
1493 return result;
1494}
1495
1496/*
1497 * Free the structure used to store info used by ":for".
1498 */
1499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001500free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501{
Bram Moolenaar33570922005-01-25 22:26:29 +00001502 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001504 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001505 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001507 list_unref(fi->fi_list);
1508 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001509 if (fi != NULL && fi->fi_blob != NULL)
1510 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511 vim_free(fi);
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515set_context_for_expression(
1516 expand_T *xp,
1517 char_u *arg,
1518 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 int got_eq = FALSE;
1521 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001524 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 {
1526 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001527 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001529 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001530 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531 {
1532 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001533 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001534 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 break;
1536 }
1537 return;
1538 }
1539 }
1540 else
1541 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1542 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 while ((xp->xp_pattern = vim_strpbrk(arg,
1544 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1545 {
1546 c = *xp->xp_pattern;
1547 if (c == '&')
1548 {
1549 c = xp->xp_pattern[1];
1550 if (c == '&')
1551 {
1552 ++xp->xp_pattern;
1553 xp->xp_context = cmdidx != CMD_let || got_eq
1554 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1555 }
1556 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001559 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1560 xp->xp_pattern += 2;
1561
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 else if (c == '$')
1565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 xp->xp_context = EXPAND_ENV_VARS;
1568 }
1569 else if (c == '=')
1570 {
1571 got_eq = TRUE;
1572 xp->xp_context = EXPAND_EXPRESSION;
1573 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001574 else if (c == '#'
1575 && xp->xp_context == EXPAND_EXPRESSION)
1576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001578 break;
1579 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001580 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 && xp->xp_context == EXPAND_FUNCTIONS
1582 && vim_strchr(xp->xp_pattern, '(') == NULL)
1583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001584 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 break;
1586 }
1587 else if (cmdidx != CMD_let || got_eq)
1588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001589 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1592 if (c == '\\' && xp->xp_pattern[1] != NUL)
1593 ++xp->xp_pattern;
1594 xp->xp_context = EXPAND_NOTHING;
1595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001596 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001598 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1600 /* skip */ ;
1601 xp->xp_context = EXPAND_NOTHING;
1602 }
1603 else if (c == '|')
1604 {
1605 if (xp->xp_pattern[1] == '|')
1606 {
1607 ++xp->xp_pattern;
1608 xp->xp_context = EXPAND_EXPRESSION;
1609 }
1610 else
1611 xp->xp_context = EXPAND_COMMANDS;
1612 }
1613 else
1614 xp->xp_context = EXPAND_EXPRESSION;
1615 }
1616 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Doesn't look like something valid, expand as an expression
1618 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 arg = xp->xp_pattern;
1621 if (*arg != NUL)
1622 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1623 /* skip */ ;
1624 }
1625 xp->xp_pattern = arg;
1626}
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001629 * Return TRUE if "pat" matches "text".
1630 * Does not use 'cpo' and always uses 'magic'.
1631 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001632 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001633pattern_match(char_u *pat, char_u *text, int ic)
1634{
1635 int matches = FALSE;
1636 char_u *save_cpo;
1637 regmatch_T regmatch;
1638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001639 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001640 save_cpo = p_cpo;
1641 p_cpo = (char_u *)"";
1642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1643 if (regmatch.regprog != NULL)
1644 {
1645 regmatch.rm_ic = ic;
1646 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1647 vim_regfree(regmatch.regprog);
1648 }
1649 p_cpo = save_cpo;
1650 return matches;
1651}
1652
1653/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001654 * Handle a name followed by "(". Both for just "name(arg)" and for
1655 * "expr->name(arg)".
1656 * Returns OK or FAIL.
1657 */
1658 static int
1659eval_func(
1660 char_u **arg, // points to "(", will be advanced
1661 char_u *name,
1662 int name_len,
1663 typval_T *rettv,
1664 int evaluate,
1665 typval_T *basetv) // "expr" for "expr->name(arg)"
1666{
1667 char_u *s = name;
1668 int len = name_len;
1669 partial_T *partial;
1670 int ret = OK;
1671
1672 if (!evaluate)
1673 check_vars(s, len);
1674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // If "s" is the name of a variable of type VAR_FUNC
1676 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001677 s = deref_func_name(s, &len, &partial, !evaluate);
1678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001679 // Need to make a copy, in case evaluating the arguments makes
1680 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001681 s = vim_strsave(s);
1682 if (s == NULL)
1683 ret = FAIL;
1684 else
1685 {
1686 funcexe_T funcexe;
1687
1688 // Invoke the function.
1689 vim_memset(&funcexe, 0, sizeof(funcexe));
1690 funcexe.firstline = curwin->w_cursor.lnum;
1691 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001692 funcexe.evaluate = evaluate;
1693 funcexe.partial = partial;
1694 funcexe.basetv = basetv;
1695 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1696 }
1697 vim_free(s);
1698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001699 // If evaluate is FALSE rettv->v_type was not set in
1700 // get_func_tv, but it's needed in handle_subscript() to parse
1701 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001702 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1703 {
1704 rettv->vval.v_string = NULL;
1705 rettv->v_type = VAR_FUNC;
1706 }
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Stop the expression evaluation when immediately
1709 // aborting on error, or when an interrupt occurred or
1710 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001711 if (evaluate && aborting())
1712 {
1713 if (ret == OK)
1714 clear_tv(rettv);
1715 ret = FAIL;
1716 }
1717 return ret;
1718}
1719
1720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1724 */
1725
1726/*
1727 * Handle zero level expression.
1728 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001730 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 * Return OK or FAIL.
1732 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734eval0(
1735 char_u *arg,
1736 typval_T *rettv,
1737 char_u **nextcmd,
1738 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 int ret;
1741 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001742 int did_emsg_before = did_emsg;
1743 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (ret == FAIL || !ends_excmd(*p))
1748 {
1749 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 /*
1752 * Report the invalid expression unless the expression evaluation has
1753 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001754 * exception, or we already gave a more specific error.
1755 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001757 if (!aborting() && did_emsg == did_emsg_before
1758 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001759 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ret = FAIL;
1761 }
1762 if (nextcmd != NULL)
1763 *nextcmd = check_nextcmd(p);
1764
1765 return ret;
1766}
1767
1768/*
1769 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001770 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 *
1772 * "arg" must point to the first non-white of the expression.
1773 * "arg" is advanced to the next non-white after the recognized expression.
1774 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001775 * Note: "rettv.v_lock" is not set.
1776 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 * Return OK or FAIL.
1778 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 /*
1786 * Get the first variable.
1787 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 return FAIL;
1790
1791 if ((*arg)[0] == '?')
1792 {
1793 result = FALSE;
1794 if (evaluate)
1795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001796 int error = FALSE;
1797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001798 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001801 if (error)
1802 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804
1805 /*
1806 * Get the second variable.
1807 */
1808 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001809 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 return FAIL;
1811
1812 /*
1813 * Check for the ":".
1814 */
1815 if ((*arg)[0] != ':')
1816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001817 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 return FAIL;
1821 }
1822
1823 /*
1824 * Get the third variable.
1825 */
1826 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001827 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return FAIL;
1832 }
1833 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836
1837 return OK;
1838}
1839
1840/*
1841 * Handle first level expression:
1842 * expr2 || expr2 || expr2 logical OR
1843 *
1844 * "arg" must point to the first non-white of the expression.
1845 * "arg" is advanced to the next non-white after the recognized expression.
1846 *
1847 * Return OK or FAIL.
1848 */
1849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 long result;
1854 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * Get the first variable.
1859 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 return FAIL;
1862
1863 /*
1864 * Repeat until there is no following "||".
1865 */
1866 first = TRUE;
1867 result = FALSE;
1868 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1869 {
1870 if (evaluate && first)
1871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001872 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001875 if (error)
1876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 first = FALSE;
1878 }
1879
1880 /*
1881 * Get the second variable.
1882 */
1883 *arg = skipwhite(*arg + 2);
1884 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1885 return FAIL;
1886
1887 /*
1888 * Compute the result.
1889 */
1890 if (evaluate && !result)
1891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001892 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001895 if (error)
1896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 if (evaluate)
1899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 rettv->v_type = VAR_NUMBER;
1901 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904
1905 return OK;
1906}
1907
1908/*
1909 * Handle second level expression:
1910 * expr3 && expr3 && expr3 logical AND
1911 *
1912 * "arg" must point to the first non-white of the expression.
1913 * "arg" is advanced to the next non-white after the recognized expression.
1914 *
1915 * Return OK or FAIL.
1916 */
1917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 long result;
1922 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /*
1926 * Get the first variable.
1927 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FAIL;
1930
1931 /*
1932 * Repeat until there is no following "&&".
1933 */
1934 first = TRUE;
1935 result = TRUE;
1936 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1937 {
1938 if (evaluate && first)
1939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001940 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001943 if (error)
1944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 first = FALSE;
1946 }
1947
1948 /*
1949 * Get the second variable.
1950 */
1951 *arg = skipwhite(*arg + 2);
1952 if (eval4(arg, &var2, evaluate && result) == FAIL)
1953 return FAIL;
1954
1955 /*
1956 * Compute the result.
1957 */
1958 if (evaluate && result)
1959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001960 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 if (error)
1964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 if (evaluate)
1967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 rettv->v_type = VAR_NUMBER;
1969 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
1972
1973 return OK;
1974}
1975
1976/*
1977 * Handle third level expression:
1978 * var1 == var2
1979 * var1 =~ var2
1980 * var1 != var2
1981 * var1 !~ var2
1982 * var1 > var2
1983 * var1 >= var2
1984 * var1 < var2
1985 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00001986 * var1 is var2
1987 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 *
1989 * "arg" must point to the first non-white of the expression.
1990 * "arg" is advanced to the next non-white after the recognized expression.
1991 *
1992 * Return OK or FAIL.
1993 */
1994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001995eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
Bram Moolenaar33570922005-01-25 22:26:29 +00001997 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 char_u *p;
1999 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002000 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004 /*
2005 * Get the first variable.
2006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 return FAIL;
2009
2010 p = *arg;
2011 switch (p[0])
2012 {
2013 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002014 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002016 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 break;
2018 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002019 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002021 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 break;
2023 case '>': if (p[1] != '=')
2024 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002025 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 len = 1;
2027 }
2028 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002029 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031 case '<': if (p[1] != '=')
2032 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002033 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 len = 1;
2035 }
2036 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002037 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002039 case 'i': if (p[1] == 's')
2040 {
2041 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2042 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002043 i = p[len];
2044 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002045 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002046 }
2047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049
2050 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002051 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002053 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (p[len] == '?')
2057 {
2058 ic = TRUE;
2059 ++len;
2060 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002061 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else if (p[len] == '#')
2063 {
2064 ic = FALSE;
2065 ++len;
2066 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002067 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else
2069 ic = p_ic;
2070
2071 /*
2072 * Get the second variable.
2073 */
2074 *arg = skipwhite(p + len);
2075 if (eval5(arg, &var2, evaluate) == FAIL)
2076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return FAIL;
2079 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002080 if (evaluate)
2081 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002082 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002083
2084 clear_tv(&var2);
2085 return ret;
2086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088
2089 return OK;
2090}
2091
2092/*
2093 * Handle fourth level expression:
2094 * + number addition
2095 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002096 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002097 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 *
2099 * "arg" must point to the first non-white of the expression.
2100 * "arg" is advanced to the next non-white after the recognized expression.
2101 *
2102 * Return OK or FAIL.
2103 */
2104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002105eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 typval_T var2;
2108 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002110 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002111#ifdef FEAT_FLOAT
2112 float_T f1 = 0, f2 = 0;
2113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 char_u *s1, *s2;
2115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2116 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002117 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 /*
2120 * Get the first variable.
2121 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002122 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return FAIL;
2124
2125 /*
2126 * Repeat computing, until no '+', '-' or '.' is following.
2127 */
2128 for (;;)
2129 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002130 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002132 concat = op == '.'
2133 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2134 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 break;
2136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002137 if ((op != '+' || (rettv->v_type != VAR_LIST
2138 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002139#ifdef FEAT_FLOAT
2140 && (op == '.' || rettv->v_type != VAR_FLOAT)
2141#endif
2142 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 // For "list + ...", an illegal use of the first operand as
2145 // a number cannot be determined before evaluating the 2nd
2146 // operand: if this is also a list, all is ok.
2147 // For "something . ...", "something - ..." or "non-list + ...",
2148 // we know that the first operand needs to be a string or number
2149 // without evaluating the 2nd operand. So check before to avoid
2150 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002151 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 {
2153 clear_tv(rettv);
2154 return FAIL;
2155 }
2156 }
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /*
2159 * Get the second variable.
2160 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002161 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2162 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002164 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 return FAIL;
2168 }
2169
2170 if (evaluate)
2171 {
2172 /*
2173 * Compute the result.
2174 */
2175 if (op == '.')
2176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002177 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002178 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002179 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 {
2181 clear_tv(rettv);
2182 clear_tv(&var2);
2183 return FAIL;
2184 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 clear_tv(rettv);
2187 rettv->v_type = VAR_STRING;
2188 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002190 else if (op == '+' && rettv->v_type == VAR_BLOB
2191 && var2.v_type == VAR_BLOB)
2192 {
2193 blob_T *b1 = rettv->vval.v_blob;
2194 blob_T *b2 = var2.vval.v_blob;
2195 blob_T *b = blob_alloc();
2196 int i;
2197
2198 if (b != NULL)
2199 {
2200 for (i = 0; i < blob_len(b1); i++)
2201 ga_append(&b->bv_ga, blob_get(b1, i));
2202 for (i = 0; i < blob_len(b2); i++)
2203 ga_append(&b->bv_ga, blob_get(b2, i));
2204
2205 clear_tv(rettv);
2206 rettv_blob_set(rettv, b);
2207 }
2208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00002209 else if (op == '+' && rettv->v_type == VAR_LIST
2210 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // concatenate Lists
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002213 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2214 &var3) == FAIL)
2215 {
2216 clear_tv(rettv);
2217 clear_tv(&var2);
2218 return FAIL;
2219 }
2220 clear_tv(rettv);
2221 *rettv = var3;
2222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 else
2224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002225 int error = FALSE;
2226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002227#ifdef FEAT_FLOAT
2228 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002229 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002230 f1 = rettv->vval.v_float;
2231 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002233 else
2234#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002236 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002237 if (error)
2238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002239 // This can only happen for "list + non-list". For
2240 // "non-list + ..." or "something - ...", we returned
2241 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002242 clear_tv(rettv);
2243 return FAIL;
2244 }
2245#ifdef FEAT_FLOAT
2246 if (var2.v_type == VAR_FLOAT)
2247 f1 = n1;
2248#endif
2249 }
2250#ifdef FEAT_FLOAT
2251 if (var2.v_type == VAR_FLOAT)
2252 {
2253 f2 = var2.vval.v_float;
2254 n2 = 0;
2255 }
2256 else
2257#endif
2258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002259 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002260 if (error)
2261 {
2262 clear_tv(rettv);
2263 clear_tv(&var2);
2264 return FAIL;
2265 }
2266#ifdef FEAT_FLOAT
2267 if (rettv->v_type == VAR_FLOAT)
2268 f2 = n2;
2269#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002272
2273#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002274 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002275 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2276 {
2277 if (op == '+')
2278 f1 = f1 + f2;
2279 else
2280 f1 = f1 - f2;
2281 rettv->v_type = VAR_FLOAT;
2282 rettv->vval.v_float = f1;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002285#endif
2286 {
2287 if (op == '+')
2288 n1 = n1 + n2;
2289 else
2290 n1 = n1 - n2;
2291 rettv->v_type = VAR_NUMBER;
2292 rettv->vval.v_number = n1;
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 }
2298 return OK;
2299}
2300
2301/*
2302 * Handle fifth level expression:
2303 * * number multiplication
2304 * / number division
2305 * % number modulo
2306 *
2307 * "arg" must point to the first non-white of the expression.
2308 * "arg" is advanced to the next non-white after the recognized expression.
2309 *
2310 * Return OK or FAIL.
2311 */
2312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002313eval6(
2314 char_u **arg,
2315 typval_T *rettv,
2316 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
Bram Moolenaar33570922005-01-25 22:26:29 +00002319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002321 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002322#ifdef FEAT_FLOAT
2323 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002324 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002325#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /*
2329 * Get the first variable.
2330 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002331 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return FAIL;
2333
2334 /*
2335 * Repeat computing, until no '*', '/' or '%' is following.
2336 */
2337 for (;;)
2338 {
2339 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002340 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 break;
2342
2343 if (evaluate)
2344 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002345#ifdef FEAT_FLOAT
2346 if (rettv->v_type == VAR_FLOAT)
2347 {
2348 f1 = rettv->vval.v_float;
2349 use_float = TRUE;
2350 n1 = 0;
2351 }
2352 else
2353#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002354 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (error)
2357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 else
2360 n1 = 0;
2361
2362 /*
2363 * Get the second variable.
2364 */
2365 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002366 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 return FAIL;
2368
2369 if (evaluate)
2370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371#ifdef FEAT_FLOAT
2372 if (var2.v_type == VAR_FLOAT)
2373 {
2374 if (!use_float)
2375 {
2376 f1 = n1;
2377 use_float = TRUE;
2378 }
2379 f2 = var2.vval.v_float;
2380 n2 = 0;
2381 }
2382 else
2383#endif
2384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002385 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002386 clear_tv(&var2);
2387 if (error)
2388 return FAIL;
2389#ifdef FEAT_FLOAT
2390 if (use_float)
2391 f2 = n2;
2392#endif
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 /*
2396 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002397 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399#ifdef FEAT_FLOAT
2400 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 if (op == '*')
2403 f1 = f1 * f2;
2404 else if (op == '/')
2405 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002406# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002407 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002408 if (f2 == 0.0)
2409 {
2410 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002412 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002413 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002414 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002415 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002416 }
2417 else
2418 f1 = f1 / f2;
2419# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // We rely on the floating point library to handle divide
2421 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002422 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002423# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002427 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428 return FAIL;
2429 }
2430 rettv->v_type = VAR_FLOAT;
2431 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002436 if (op == '*')
2437 n1 = n1 * n2;
2438 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002439 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002441 n1 = num_modulus(n1, n2);
2442
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443 rettv->v_type = VAR_NUMBER;
2444 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
2448
2449 return OK;
2450}
2451
2452/*
2453 * Handle sixth level expression:
2454 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002456 * "string" string constant
2457 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * &option-name option value
2459 * @r register contents
2460 * identifier variable value
2461 * function() function call
2462 * $VAR environment variable
2463 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002464 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002465 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002466 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 *
2468 * Also handle:
2469 * ! in front logical NOT
2470 * - in front unary minus
2471 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 * trailing [] subscript in String or List
2473 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002474 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 *
2476 * "arg" must point to the first non-white of the expression.
2477 * "arg" is advanced to the next non-white after the recognized expression.
2478 *
2479 * Return OK or FAIL.
2480 */
2481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482eval7(
2483 char_u **arg,
2484 typval_T *rettv,
2485 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002486 int want_string UNUSED) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002488 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int len;
2490 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 char_u *start_leader, *end_leader;
2492 int ret = OK;
2493 char_u *alias;
2494
2495 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002497 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002502 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 start_leader = *arg;
2505 while (**arg == '!' || **arg == '-' || **arg == '+')
2506 *arg = skipwhite(*arg + 1);
2507 end_leader = *arg;
2508
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002509 if (**arg == '.' && (!isdigit(*(*arg + 1))
2510#ifdef FEAT_FLOAT
2511 || current_sctx.sc_version < 2
2512#endif
2513 ))
2514 {
2515 semsg(_(e_invexpr2), *arg);
2516 ++*arg;
2517 return FAIL;
2518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 switch (**arg)
2521 {
2522 /*
2523 * Number constant.
2524 */
2525 case '0':
2526 case '1':
2527 case '2':
2528 case '3':
2529 case '4':
2530 case '5':
2531 case '6':
2532 case '7':
2533 case '8':
2534 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002535 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002536 {
2537#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002538 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002539 int get_float = FALSE;
2540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002541 // We accept a float when the format matches
2542 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2543 // strict to avoid backwards compatibility problems.
2544 // With script version 2 and later the leading digit can be
2545 // omitted.
2546 // Don't look for a float after the "." operator, so that
2547 // ":let vers = 1.2.3" doesn't fail.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002548 if (**arg == '.')
2549 p = *arg;
2550 else
2551 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002552 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002554 get_float = TRUE;
2555 p = skipdigits(p + 2);
2556 if (*p == 'e' || *p == 'E')
2557 {
2558 ++p;
2559 if (*p == '-' || *p == '+')
2560 ++p;
2561 if (!vim_isdigit(*p))
2562 get_float = FALSE;
2563 else
2564 p = skipdigits(p + 1);
2565 }
2566 if (ASCII_ISALPHA(*p) || *p == '.')
2567 get_float = FALSE;
2568 }
2569 if (get_float)
2570 {
2571 float_T f;
2572
2573 *arg += string2float(*arg, &f);
2574 if (evaluate)
2575 {
2576 rettv->v_type = VAR_FLOAT;
2577 rettv->vval.v_float = f;
2578 }
2579 }
2580 else
2581#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002582 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002583 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002584 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01002585 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002586
2587 // Blob constant: 0z0123456789abcdef
2588 if (evaluate)
2589 blob = blob_alloc();
2590 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2591 {
2592 if (!vim_isxdigit(bp[1]))
2593 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002594 if (blob != NULL)
2595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002597 ga_clear(&blob->bv_ga);
2598 VIM_CLEAR(blob);
2599 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002600 ret = FAIL;
2601 break;
2602 }
2603 if (blob != NULL)
2604 ga_append(&blob->bv_ga,
2605 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01002606 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2607 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002608 }
2609 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002610 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002611 *arg = bp;
2612 }
2613 else
2614 {
2615 // decimal, hex or octal number
Bram Moolenaar60a8de22019-09-15 14:33:22 +02002616 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
2617 ? STR2NR_NO_OCT + STR2NR_QUOTE
2618 : STR2NR_ALL, &n, NULL, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002619 if (len == 0)
2620 {
2621 semsg(_(e_invexpr2), *arg);
2622 ret = FAIL;
2623 break;
2624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002625 *arg += len;
2626 if (evaluate)
2627 {
2628 rettv->v_type = VAR_NUMBER;
2629 rettv->vval.v_number = n;
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 /*
2636 * String constant: "string".
2637 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 break;
2640
2641 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002645 break;
2646
2647 /*
2648 * List: [expr, expr]
2649 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 break;
2652
2653 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002654 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002655 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002656 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002657 {
2658 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002659 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002660 }
2661 else
2662 ret = NOTDONE;
2663 break;
2664
2665 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002666 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002667 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002669 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2670 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002671 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 break;
2673
2674 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002675 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002677 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 break;
2679
2680 /*
2681 * Environment variable: $VAR.
2682 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 break;
2685
2686 /*
2687 * Register contents: @r.
2688 */
2689 case '@': ++*arg;
2690 if (evaluate)
2691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002692 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002693 rettv->vval.v_string = get_reg_contents(**arg,
2694 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696 if (**arg != NUL)
2697 ++*arg;
2698 break;
2699
2700 /*
2701 * nested expression: (expression).
2702 */
2703 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002704 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (**arg == ')')
2706 ++*arg;
2707 else if (ret == OK)
2708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002710 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 ret = FAIL;
2712 }
2713 break;
2714
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 break;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718
2719 if (ret == NOTDONE)
2720 {
2721 /*
2722 * Must be a variable or function name.
2723 * Can also be a curly-braces kind of name: {expr}.
2724 */
2725 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002726 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (alias != NULL)
2728 s = alias;
2729
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002730 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 ret = FAIL;
2732 else
2733 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002735 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002737 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002739 {
2740 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002741 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002744 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 *arg = skipwhite(*arg);
2748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2750 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002751 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002752 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2753 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /*
2756 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2757 */
2758 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002759 ret = eval7_leader(rettv, start_leader, &end_leader);
2760 return ret;
2761}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763/*
2764 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2765 * Adjusts "end_leaderp" until it is at "start_leader".
2766 */
2767 static int
2768eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2769{
2770 char_u *end_leader = *end_leaderp;
2771 int ret = OK;
2772 int error = FALSE;
2773 varnumber_T val = 0;
2774#ifdef FEAT_FLOAT
2775 float_T f = 0.0;
2776
2777 if (rettv->v_type == VAR_FLOAT)
2778 f = rettv->vval.v_float;
2779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002780#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002781 val = tv_get_number_chk(rettv, &error);
2782 if (error)
2783 {
2784 clear_tv(rettv);
2785 ret = FAIL;
2786 }
2787 else
2788 {
2789 while (end_leader > start_leader)
2790 {
2791 --end_leader;
2792 if (*end_leader == '!')
2793 {
2794#ifdef FEAT_FLOAT
2795 if (rettv->v_type == VAR_FLOAT)
2796 f = !f;
2797 else
2798#endif
2799 val = !val;
2800 }
2801 else if (*end_leader == '-')
2802 {
2803#ifdef FEAT_FLOAT
2804 if (rettv->v_type == VAR_FLOAT)
2805 f = -f;
2806 else
2807#endif
2808 val = -val;
2809 }
2810 }
2811#ifdef FEAT_FLOAT
2812 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002814 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002815 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002819 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002820 clear_tv(rettv);
2821 rettv->v_type = VAR_NUMBER;
2822 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002825 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 return ret;
2827}
2828
2829/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002830 * Call the function referred to in "rettv".
2831 */
2832 static int
2833call_func_rettv(
2834 char_u **arg,
2835 typval_T *rettv,
2836 int evaluate,
2837 dict_T *selfdict,
2838 typval_T *basetv)
2839{
2840 partial_T *pt = NULL;
2841 funcexe_T funcexe;
2842 typval_T functv;
2843 char_u *s;
2844 int ret;
2845
2846 // need to copy the funcref so that we can clear rettv
2847 if (evaluate)
2848 {
2849 functv = *rettv;
2850 rettv->v_type = VAR_UNKNOWN;
2851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002852 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002853 if (functv.v_type == VAR_PARTIAL)
2854 {
2855 pt = functv.vval.v_partial;
2856 s = partial_name(pt);
2857 }
2858 else
2859 s = functv.vval.v_string;
2860 }
2861 else
2862 s = (char_u *)"";
2863
2864 vim_memset(&funcexe, 0, sizeof(funcexe));
2865 funcexe.firstline = curwin->w_cursor.lnum;
2866 funcexe.lastline = curwin->w_cursor.lnum;
2867 funcexe.evaluate = evaluate;
2868 funcexe.partial = pt;
2869 funcexe.selfdict = selfdict;
2870 funcexe.basetv = basetv;
2871 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 // Clear the funcref afterwards, so that deleting it while
2874 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002875 if (evaluate)
2876 clear_tv(&functv);
2877
2878 return ret;
2879}
2880
2881/*
2882 * Evaluate "->method()".
2883 * "*arg" points to the '-'.
2884 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2885 */
2886 static int
2887eval_lambda(
2888 char_u **arg,
2889 typval_T *rettv,
2890 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002892{
2893 typval_T base = *rettv;
2894 int ret;
2895
2896 // Skip over the ->.
2897 *arg += 2;
2898 rettv->v_type = VAR_UNKNOWN;
2899
2900 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002901 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002902 return FAIL;
2903 else if (**arg != '(')
2904 {
2905 if (verbose)
2906 {
2907 if (*skipwhite(*arg) == '(')
2908 semsg(_(e_nowhitespace));
2909 else
2910 semsg(_(e_missingparen), "lambda");
2911 }
2912 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002913 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002914 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002915 else
2916 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2917
2918 // Clear the funcref afterwards, so that deleting it while
2919 // evaluating the arguments is possible (see test55).
2920 if (evaluate)
2921 clear_tv(&base);
2922
2923 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002924}
2925
2926/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 * Evaluate "->method()".
2928 * "*arg" points to the '-'.
2929 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2930 */
2931 static int
2932eval_method(
2933 char_u **arg,
2934 typval_T *rettv,
2935 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002936 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002937{
2938 char_u *name;
2939 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002940 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002941 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002942 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002943
2944 // Skip over the ->.
2945 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002946 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002947
Bram Moolenaarac92e252019-08-03 21:58:38 +02002948 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002949 len = get_name_len(arg, &alias, evaluate, TRUE);
2950 if (alias != NULL)
2951 name = alias;
2952
2953 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002954 {
2955 if (verbose)
2956 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002957 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002958 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002959 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002960 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002961 if (**arg != '(')
2962 {
2963 if (verbose)
2964 semsg(_(e_missingparen), name);
2965 ret = FAIL;
2966 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002967 else if (VIM_ISWHITE((*arg)[-1]))
2968 {
2969 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002970 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002971 ret = FAIL;
2972 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002973 else
2974 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002975 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002976
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002977 // Clear the funcref afterwards, so that deleting it while
2978 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002979 if (evaluate)
2980 clear_tv(&base);
2981
Bram Moolenaarac92e252019-08-03 21:58:38 +02002982 return ret;
2983}
2984
2985/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002986 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2987 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2989 */
2990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002991eval_index(
2992 char_u **arg,
2993 typval_T *rettv,
2994 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002995 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002996{
2997 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002999 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 long len = -1;
3002 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003003 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003005
Bram Moolenaara03f2332016-02-06 18:09:59 +01003006 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003008 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003009 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003010 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003012 return FAIL;
3013 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003014#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003015 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003017 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003018#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003019 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003020 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003021 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003022 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003023 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003024 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003025 return FAIL;
3026 case VAR_UNKNOWN:
3027 if (evaluate)
3028 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003029 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003030
3031 case VAR_STRING:
3032 case VAR_NUMBER:
3033 case VAR_LIST:
3034 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003035 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003036 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003037 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003038
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003039 init_tv(&var1);
3040 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 /*
3044 * dict.name
3045 */
3046 key = *arg + 1;
3047 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3048 ;
3049 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003050 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003052 }
3053 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055 /*
3056 * something[idx]
3057 *
3058 * Get the (first) variable from inside the [].
3059 */
3060 *arg = skipwhite(*arg + 1);
3061 if (**arg == ':')
3062 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068 clear_tv(&var1);
3069 return FAIL;
3070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071
3072 /*
3073 * Get the second variable from inside the [:].
3074 */
3075 if (**arg == ':')
3076 {
3077 range = TRUE;
3078 *arg = skipwhite(*arg + 1);
3079 if (**arg == ']')
3080 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003081 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 if (!empty1)
3084 clear_tv(&var1);
3085 return FAIL;
3086 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003089 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003090 if (!empty1)
3091 clear_tv(&var1);
3092 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093 return FAIL;
3094 }
3095 }
3096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003097 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003098 if (**arg != ']')
3099 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003100 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003101 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003102 clear_tv(&var1);
3103 if (range)
3104 clear_tv(&var2);
3105 return FAIL;
3106 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003108 }
3109
3110 if (evaluate)
3111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 n1 = 0;
3113 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003116 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 }
3118 if (range)
3119 {
3120 if (empty2)
3121 n2 = -1;
3122 else
3123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003126 }
3127 }
3128
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003129 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003130 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003131 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003132 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003133 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003134 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003135 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003136 case VAR_SPECIAL:
3137 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003138 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003140
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003141 case VAR_NUMBER:
3142 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003143 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003144 len = (long)STRLEN(s);
3145 if (range)
3146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003147 // The resulting variable is a substring. If the indexes
3148 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 if (n1 < 0)
3150 {
3151 n1 = len + n1;
3152 if (n1 < 0)
3153 n1 = 0;
3154 }
3155 if (n2 < 0)
3156 n2 = len + n2;
3157 else if (n2 >= len)
3158 n2 = len;
3159 if (n1 >= len || n2 < 0 || n1 > n2)
3160 s = NULL;
3161 else
3162 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3163 }
3164 else
3165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003166 // The resulting variable is a string of a single
3167 // character. If the index is too big or negative the
3168 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003169 if (n1 >= len || n1 < 0)
3170 s = NULL;
3171 else
3172 s = vim_strnsave(s + n1, 1);
3173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 clear_tv(rettv);
3175 rettv->v_type = VAR_STRING;
3176 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003177 break;
3178
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003179 case VAR_BLOB:
3180 len = blob_len(rettv->vval.v_blob);
3181 if (range)
3182 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003183 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003184 // are out of range the result is empty.
3185 if (n1 < 0)
3186 {
3187 n1 = len + n1;
3188 if (n1 < 0)
3189 n1 = 0;
3190 }
3191 if (n2 < 0)
3192 n2 = len + n2;
3193 else if (n2 >= len)
3194 n2 = len - 1;
3195 if (n1 >= len || n2 < 0 || n1 > n2)
3196 {
3197 clear_tv(rettv);
3198 rettv->v_type = VAR_BLOB;
3199 rettv->vval.v_blob = NULL;
3200 }
3201 else
3202 {
3203 blob_T *blob = blob_alloc();
3204
3205 if (blob != NULL)
3206 {
3207 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3208 {
3209 blob_free(blob);
3210 return FAIL;
3211 }
3212 blob->bv_ga.ga_len = n2 - n1 + 1;
3213 for (i = n1; i <= n2; i++)
3214 blob_set(blob, i - n1,
3215 blob_get(rettv->vval.v_blob, i));
3216
3217 clear_tv(rettv);
3218 rettv_blob_set(rettv, blob);
3219 }
3220 }
3221 }
3222 else
3223 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003224 // The resulting variable is a byte value.
3225 // If the index is too big or negative that is an error.
3226 if (n1 < 0)
3227 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003228 if (n1 < len && n1 >= 0)
3229 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003230 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003231
3232 clear_tv(rettv);
3233 rettv->v_type = VAR_NUMBER;
3234 rettv->vval.v_number = v;
3235 }
3236 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003237 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003238 }
3239 break;
3240
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003241 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003243 if (n1 < 0)
3244 n1 = len + n1;
3245 if (!empty1 && (n1 < 0 || n1 >= len))
3246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 // For a range we allow invalid values and return an empty
3248 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003249 if (!range)
3250 {
3251 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003252 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003253 return FAIL;
3254 }
3255 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003256 }
3257 if (range)
3258 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 list_T *l;
3260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261
3262 if (n2 < 0)
3263 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003264 else if (n2 >= len)
3265 n2 = len - 1;
3266 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003267 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003268 l = list_alloc();
3269 if (l == NULL)
3270 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003271 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003272 n1 <= n2; ++n1)
3273 {
3274 if (list_append_tv(l, &item->li_tv) == FAIL)
3275 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003276 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277 return FAIL;
3278 }
3279 item = item->li_next;
3280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003281 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003282 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 }
3284 else
3285 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003286 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
3288 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003289 }
3290 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291
3292 case VAR_DICT:
3293 if (range)
3294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003295 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003296 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003297 if (len == -1)
3298 clear_tv(&var1);
3299 return FAIL;
3300 }
3301 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303
3304 if (len == -1)
3305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003306 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003307 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003308 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 clear_tv(&var1);
3310 return FAIL;
3311 }
3312 }
3313
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003314 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003315
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003316 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003317 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003318 if (len == -1)
3319 clear_tv(&var1);
3320 if (item == NULL)
3321 return FAIL;
3322
3323 copy_tv(&item->di_tv, &var1);
3324 clear_tv(rettv);
3325 *rettv = var1;
3326 }
3327 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003328 }
3329 }
3330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003331 return OK;
3332}
3333
3334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Get an option value.
3336 * "arg" points to the '&' or '+' before the option name.
3337 * "arg" is advanced to character after the option name.
3338 * Return OK or FAIL.
3339 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341get_option_tv(
3342 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003343 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 char_u *option_end;
3347 long numval;
3348 char_u *stringval;
3349 int opt_type;
3350 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003351 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 int ret = OK;
3353 int opt_flags;
3354
3355 /*
3356 * Isolate the option name and find its value.
3357 */
3358 option_end = find_option_end(arg, &opt_flags);
3359 if (option_end == NULL)
3360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003362 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 return FAIL;
3364 }
3365
3366 if (!evaluate)
3367 {
3368 *arg = option_end;
3369 return OK;
3370 }
3371
3372 c = *option_end;
3373 *option_end = NUL;
3374 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003377 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003380 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 ret = FAIL;
3382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003385 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 rettv->v_type = VAR_STRING;
3388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003390 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003392 rettv->v_type = VAR_NUMBER;
3393 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 rettv->v_type = VAR_NUMBER;
3398 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003400 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 rettv->v_type = VAR_STRING;
3403 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405 }
3406 else if (working && (opt_type == -2 || opt_type == -1))
3407 ret = FAIL;
3408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 *arg = option_end;
3411
3412 return ret;
3413}
3414
3415/*
3416 * Allocate a variable for a string constant.
3417 * Return OK or FAIL.
3418 */
3419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003420get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 char_u *p;
3423 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int extra = 0;
3425
3426 /*
3427 * Find the end of the string, skipping backslashed characters.
3428 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003429 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
3431 if (*p == '\\' && p[1] != NUL)
3432 {
3433 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003434 // A "\<x>" form occupies at least 4 characters, and produces up
3435 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (*p == '<')
3437 extra += 2;
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440
3441 if (*p != '"')
3442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003443 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 return FAIL;
3445 }
3446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003447 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (!evaluate)
3449 {
3450 *arg = p + 1;
3451 return OK;
3452 }
3453
3454 /*
3455 * Copy the string into allocated memory, handling backslashed
3456 * characters.
3457 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003458 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (name == NULL)
3460 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003461 rettv->v_type = VAR_STRING;
3462 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 if (*p == '\\')
3467 {
3468 switch (*++p)
3469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003470 case 'b': *name++ = BS; ++p; break;
3471 case 'e': *name++ = ESC; ++p; break;
3472 case 'f': *name++ = FF; ++p; break;
3473 case 'n': *name++ = NL; ++p; break;
3474 case 'r': *name++ = CAR; ++p; break;
3475 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003479 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 case 'U':
3481 if (vim_isxdigit(p[1]))
3482 {
3483 int n, nr;
3484 int c = toupper(*p);
3485
3486 if (c == 'X')
3487 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003488 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003490 else
3491 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 nr = 0;
3493 while (--n >= 0 && vim_isxdigit(p[1]))
3494 {
3495 ++p;
3496 nr = (nr << 4) + hex2nr(*p);
3497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // For "\u" store the number according to
3500 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003504 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 case '0':
3510 case '1':
3511 case '2':
3512 case '3':
3513 case '4':
3514 case '5':
3515 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003516 case '7': *name = *p++ - '0';
3517 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 *name = (*name << 3) + *p++ - '0';
3520 if (*p >= '0' && *p <= '7')
3521 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 break;
3525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003526 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003527 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3528 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (extra != 0)
3530 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003534 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar8c711452005-01-14 21:53:12 +00003536 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 break;
3538 }
3539 }
3540 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003541 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003546 ++p;
3547 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 return OK;
3550}
3551
3552/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * Return OK or FAIL.
3555 */
3556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003560 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003561 int reduce = 0;
3562
3563 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003565 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003566 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003570 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003571 break;
3572 ++reduce;
3573 ++p;
3574 }
3575 }
3576
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003579 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003580 return FAIL;
3581 }
3582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003583 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003584 if (!evaluate)
3585 {
3586 *arg = p + 1;
3587 return OK;
3588 }
3589
3590 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003592 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003593 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003594 if (str == NULL)
3595 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 rettv->v_type = VAR_STRING;
3597 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003598
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003604 break;
3605 ++p;
3606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003609 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003610 *arg = p + 1;
3611
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003612 return OK;
3613}
3614
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003615/*
3616 * Return the function name of the partial.
3617 */
3618 char_u *
3619partial_name(partial_T *pt)
3620{
3621 if (pt->pt_name != NULL)
3622 return pt->pt_name;
3623 return pt->pt_func->uf_name;
3624}
3625
Bram Moolenaarddecc252016-04-06 22:59:37 +02003626 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003627partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003628{
3629 int i;
3630
3631 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003632 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003633 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003634 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003635 if (pt->pt_name != NULL)
3636 {
3637 func_unref(pt->pt_name);
3638 vim_free(pt->pt_name);
3639 }
3640 else
3641 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003642 vim_free(pt);
3643}
3644
3645/*
3646 * Unreference a closure: decrement the reference count and free it when it
3647 * becomes zero.
3648 */
3649 void
3650partial_unref(partial_T *pt)
3651{
3652 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003653 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003654}
3655
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003656static int tv_equal_recurse_limit;
3657
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003658 static int
3659func_equal(
3660 typval_T *tv1,
3661 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003662 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003663{
3664 char_u *s1, *s2;
3665 dict_T *d1, *d2;
3666 int a1, a2;
3667 int i;
3668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003669 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003670 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003671 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003672 if (s1 != NULL && *s1 == NUL)
3673 s1 = NULL;
3674 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003675 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003676 if (s2 != NULL && *s2 == NUL)
3677 s2 = NULL;
3678 if (s1 == NULL || s2 == NULL)
3679 {
3680 if (s1 != s2)
3681 return FALSE;
3682 }
3683 else if (STRCMP(s1, s2) != 0)
3684 return FALSE;
3685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003687 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3688 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3689 if (d1 == NULL || d2 == NULL)
3690 {
3691 if (d1 != d2)
3692 return FALSE;
3693 }
3694 else if (!dict_equal(d1, d2, ic, TRUE))
3695 return FALSE;
3696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003698 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3699 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3700 if (a1 != a2)
3701 return FALSE;
3702 for (i = 0; i < a1; ++i)
3703 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3704 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3705 return FALSE;
3706
3707 return TRUE;
3708}
3709
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003710/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003711 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003712 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003714 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003716tv_equal(
3717 typval_T *tv1,
3718 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 int ic, // ignore case
3720 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003721{
3722 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003723 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003724 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003725 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003727 // Catch lists and dicts that have an endless loop by limiting
3728 // recursiveness to a limit. We guess they are equal then.
3729 // A fixed limit has the problem of still taking an awful long time.
3730 // Reduce the limit every time running into it. That should work fine for
3731 // deeply linked structures that are not recursively linked and catch
3732 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003733 if (!recursive)
3734 tv_equal_recurse_limit = 1000;
3735 if (recursive_cnt >= tv_equal_recurse_limit)
3736 {
3737 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003738 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003739 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3742 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003743 if ((tv1->v_type == VAR_FUNC
3744 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3745 && (tv2->v_type == VAR_FUNC
3746 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3747 {
3748 ++recursive_cnt;
3749 r = func_equal(tv1, tv2, ic);
3750 --recursive_cnt;
3751 return r;
3752 }
3753
3754 if (tv1->v_type != tv2->v_type)
3755 return FALSE;
3756
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003757 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003758 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003759 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003760 ++recursive_cnt;
3761 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3762 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003763 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003764
3765 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003766 ++recursive_cnt;
3767 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3768 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003769 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003770
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003771 case VAR_BLOB:
3772 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3773
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003774 case VAR_NUMBER:
3775 return tv1->vval.v_number == tv2->vval.v_number;
3776
3777 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003778 s1 = tv_get_string_buf(tv1, buf1);
3779 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003780 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003781
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003782 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003783 case VAR_SPECIAL:
3784 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01003785
3786 case VAR_FLOAT:
3787#ifdef FEAT_FLOAT
3788 return tv1->vval.v_float == tv2->vval.v_float;
3789#endif
3790 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003791#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003792 return tv1->vval.v_job == tv2->vval.v_job;
3793#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003794 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003795#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003796 return tv1->vval.v_channel == tv2->vval.v_channel;
3797#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003798 case VAR_FUNC:
3799 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003800 case VAR_UNKNOWN:
3801 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003802 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003804 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3805 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003806 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003807}
3808
3809/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003810 * Return the next (unique) copy ID.
3811 * Used for serializing nested structures.
3812 */
3813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003815{
3816 current_copyID += COPYID_INC;
3817 return current_copyID;
3818}
3819
3820/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003821 * Garbage collection for lists and dictionaries.
3822 *
3823 * We use reference counts to be able to free most items right away when they
3824 * are no longer used. But for composite items it's possible that it becomes
3825 * unused while the reference count is > 0: When there is a recursive
3826 * reference. Example:
3827 * :let l = [1, 2, 3]
3828 * :let d = {9: l}
3829 * :let l[1] = d
3830 *
3831 * Since this is quite unusual we handle this with garbage collection: every
3832 * once in a while find out which lists and dicts are not referenced from any
3833 * variable.
3834 *
3835 * Here is a good reference text about garbage collection (refers to Python
3836 * but it applies to all reference-counting mechanisms):
3837 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003838 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003839
3840/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003841 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003842 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003845 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003846garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003850 buf_T *buf;
3851 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003852 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003853 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003854
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003855 if (!testing)
3856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003857 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003858 want_garbage_collect = FALSE;
3859 may_garbage_collect = FALSE;
3860 garbage_collect_at_exit = FALSE;
3861 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003862
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003863 // The execution stack can grow big, limit the size.
3864 if (exestack.ga_maxlen - exestack.ga_len > 500)
3865 {
3866 size_t new_len;
3867 char_u *pp;
3868 int n;
3869
3870 // Keep 150% of the current size, with a minimum of the growth size.
3871 n = exestack.ga_len / 2;
3872 if (n < exestack.ga_growsize)
3873 n = exestack.ga_growsize;
3874
3875 // Don't make it bigger though.
3876 if (exestack.ga_len + n < exestack.ga_maxlen)
3877 {
3878 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3879 pp = vim_realloc(exestack.ga_data, new_len);
3880 if (pp == NULL)
3881 return FAIL;
3882 exestack.ga_maxlen = exestack.ga_len + n;
3883 exestack.ga_data = pp;
3884 }
3885 }
3886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 // We advance by two because we add one for items referenced through
3888 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003889 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003891 /*
3892 * 1. Go through all accessible variables and mark all lists and dicts
3893 * with copyID.
3894 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003896 // Don't free variables in the previous_funccal list unless they are only
3897 // referenced through previous_funccal. This must be first, because if
3898 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003901 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003902 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003905 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003906 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3907 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003911 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3912 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003913 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003914 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3915 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003916#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003917 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3918 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3919 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003920 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003921 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003922 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3923 NULL, NULL);
3924#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003926 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003927 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003928 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3929 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003930 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003931 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003933 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003936 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003937 abort = abort || set_ref_in_functions(copyID);
3938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003942 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003943 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003944
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003945 // callbacks in buffers
3946 abort = abort || set_ref_in_buffers(copyID);
3947
Bram Moolenaar1dced572012-04-05 16:54:08 +02003948#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003949 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003950#endif
3951
Bram Moolenaardb913952012-06-29 12:54:53 +02003952#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003953 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003954#endif
3955
3956#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003957 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003958#endif
3959
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003961 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003962 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003963#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003964#ifdef FEAT_NETBEANS_INTG
3965 abort = abort || set_ref_in_nb_channel(copyID);
3966#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003967
Bram Moolenaare3188e22016-05-31 21:13:04 +02003968#ifdef FEAT_TIMERS
3969 abort = abort || set_ref_in_timer(copyID);
3970#endif
3971
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003972#ifdef FEAT_QUICKFIX
3973 abort = abort || set_ref_in_quickfix(copyID);
3974#endif
3975
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003976#ifdef FEAT_TERMINAL
3977 abort = abort || set_ref_in_term(copyID);
3978#endif
3979
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003980#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003981 abort = abort || set_ref_in_popups(copyID);
3982#endif
3983
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003984 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003985 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003986 /*
3987 * 2. Free lists and dictionaries that are not referenced.
3988 */
3989 did_free = free_unref_items(copyID);
3990
3991 /*
3992 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003996 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003997 else if (p_verbose > 0)
3998 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003999 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004001
4002 return did_free;
4003}
4004
4005/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004006 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004007 */
4008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004009free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004010{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004011 int did_free = FALSE;
4012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004013 // Let all "free" functions know that we are here. This means no
4014 // dictionaries, lists, channels or jobs are to be freed, because we will
4015 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004016 in_free_unref_items = TRUE;
4017
4018 /*
4019 * PASS 1: free the contents of the items. We don't free the items
4020 * themselves yet, so that it is possible to decrement refcount counters
4021 */
4022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004024 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004026 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004027 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004028
4029#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004030 // Go through the list of jobs and free items without the copyID. This
4031 // must happen before doing channels, because jobs refer to channels, but
4032 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004033 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004035 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004036 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4037#endif
4038
4039 /*
4040 * PASS 2: free the items themselves.
4041 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004042 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004043 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004044
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004045#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004046 // Go through the list of jobs and free items without the copyID. This
4047 // must happen before doing channels, because jobs refer to channels, but
4048 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004049 free_unused_jobs(copyID, COPYID_MASK);
4050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004051 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004052 free_unused_channels(copyID, COPYID_MASK);
4053#endif
4054
4055 in_free_unref_items = FALSE;
4056
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004057 return did_free;
4058}
4059
4060/*
4061 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004062 * "list_stack" is used to add lists to be marked. Can be NULL.
4063 *
4064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004067set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004068{
4069 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004070 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004071 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004072 hashtab_T *cur_ht;
4073 ht_stack_T *ht_stack = NULL;
4074 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004075
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004076 cur_ht = ht;
4077 for (;;)
4078 {
4079 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // Mark each item in the hashtab. If the item contains a hashtab
4082 // it is added to ht_stack, if it contains a list it is added to
4083 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004084 todo = (int)cur_ht->ht_used;
4085 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4086 if (!HASHITEM_EMPTY(hi))
4087 {
4088 --todo;
4089 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4090 &ht_stack, list_stack);
4091 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004092 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004093
4094 if (ht_stack == NULL)
4095 break;
4096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004097 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004098 cur_ht = ht_stack->ht;
4099 tempitem = ht_stack;
4100 ht_stack = ht_stack->prev;
4101 free(tempitem);
4102 }
4103
4104 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004105}
4106
4107/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004108 * Mark a dict and its items with "copyID".
4109 * Returns TRUE if setting references failed somehow.
4110 */
4111 int
4112set_ref_in_dict(dict_T *d, int copyID)
4113{
4114 if (d != NULL && d->dv_copyID != copyID)
4115 {
4116 d->dv_copyID = copyID;
4117 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4118 }
4119 return FALSE;
4120}
4121
4122/*
4123 * Mark a list and its items with "copyID".
4124 * Returns TRUE if setting references failed somehow.
4125 */
4126 int
4127set_ref_in_list(list_T *ll, int copyID)
4128{
4129 if (ll != NULL && ll->lv_copyID != copyID)
4130 {
4131 ll->lv_copyID = copyID;
4132 return set_ref_in_list_items(ll, copyID, NULL);
4133 }
4134 return FALSE;
4135}
4136
4137/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004138 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4140 *
4141 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004142 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004143 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004144set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004145{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004146 listitem_T *li;
4147 int abort = FALSE;
4148 list_T *cur_l;
4149 list_stack_T *list_stack = NULL;
4150 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004151
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004152 cur_l = l;
4153 for (;;)
4154 {
4155 if (!abort)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004156 // Mark each item in the list. If the item contains a hashtab
4157 // it is added to ht_stack, if it contains a list it is added to
4158 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004159 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4160 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4161 ht_stack, &list_stack);
4162 if (list_stack == NULL)
4163 break;
4164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004166 cur_l = list_stack->list;
4167 tempitem = list_stack;
4168 list_stack = list_stack->prev;
4169 free(tempitem);
4170 }
4171
4172 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004173}
4174
4175/*
4176 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004177 * "list_stack" is used to add lists to be marked. Can be NULL.
4178 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4179 *
4180 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004181 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004182 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004183set_ref_in_item(
4184 typval_T *tv,
4185 int copyID,
4186 ht_stack_T **ht_stack,
4187 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004188{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004189 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004190
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004191 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004192 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004193 dict_T *dd = tv->vval.v_dict;
4194
Bram Moolenaara03f2332016-02-06 18:09:59 +01004195 if (dd != NULL && dd->dv_copyID != copyID)
4196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004197 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004198 dd->dv_copyID = copyID;
4199 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004200 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004201 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4202 }
4203 else
4204 {
4205 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4206 if (newitem == NULL)
4207 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004208 else
4209 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004210 newitem->ht = &dd->dv_hashtab;
4211 newitem->prev = *ht_stack;
4212 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004213 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004214 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004215 }
4216 }
4217 else if (tv->v_type == VAR_LIST)
4218 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004219 list_T *ll = tv->vval.v_list;
4220
Bram Moolenaara03f2332016-02-06 18:09:59 +01004221 if (ll != NULL && ll->lv_copyID != copyID)
4222 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004223 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004224 ll->lv_copyID = copyID;
4225 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004226 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004227 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004228 }
4229 else
4230 {
4231 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004233 if (newitem == NULL)
4234 abort = TRUE;
4235 else
4236 {
4237 newitem->list = ll;
4238 newitem->prev = *list_stack;
4239 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004241 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004242 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004243 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004244 else if (tv->v_type == VAR_FUNC)
4245 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004246 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004247 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004248 else if (tv->v_type == VAR_PARTIAL)
4249 {
4250 partial_T *pt = tv->vval.v_partial;
4251 int i;
4252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004253 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 if (pt != NULL)
4255 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004256 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004257
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004258 if (pt->pt_dict != NULL)
4259 {
4260 typval_T dtv;
4261
4262 dtv.v_type = VAR_DICT;
4263 dtv.vval.v_dict = pt->pt_dict;
4264 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4265 }
4266
4267 for (i = 0; i < pt->pt_argc; ++i)
4268 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4269 ht_stack, list_stack);
4270 }
4271 }
4272#ifdef FEAT_JOB_CHANNEL
4273 else if (tv->v_type == VAR_JOB)
4274 {
4275 job_T *job = tv->vval.v_job;
4276 typval_T dtv;
4277
4278 if (job != NULL && job->jv_copyID != copyID)
4279 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004280 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004281 if (job->jv_channel != NULL)
4282 {
4283 dtv.v_type = VAR_CHANNEL;
4284 dtv.vval.v_channel = job->jv_channel;
4285 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4286 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004287 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004288 {
4289 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004290 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004291 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4292 }
4293 }
4294 }
4295 else if (tv->v_type == VAR_CHANNEL)
4296 {
4297 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004298 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004299 typval_T dtv;
4300 jsonq_T *jq;
4301 cbq_T *cq;
4302
4303 if (ch != NULL && ch->ch_copyID != copyID)
4304 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004305 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004306 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004307 {
4308 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4309 jq = jq->jq_next)
4310 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4311 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4312 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004313 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 {
4315 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004316 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004317 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4318 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004319 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 {
4321 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004322 dtv.vval.v_partial =
4323 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004324 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4325 }
4326 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004327 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004328 {
4329 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004330 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004331 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4332 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004333 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004334 {
4335 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004336 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004337 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4338 }
4339 }
4340 }
4341#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004342 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004343}
4344
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004346 * Return a string with the string representation of a variable.
4347 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004348 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004349 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004350 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4351 * stings as "string()", otherwise does not put quotes around strings, as
4352 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004353 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4354 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004355 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004356 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004357 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004358echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004359 typval_T *tv,
4360 char_u **tofree,
4361 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004362 int copyID,
4363 int echo_style,
4364 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004365 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004366{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004367 static int recurse = 0;
4368 char_u *r = NULL;
4369
Bram Moolenaar33570922005-01-25 22:26:29 +00004370 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004371 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004372 if (!did_echo_string_emsg)
4373 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004374 // Only give this message once for a recursive call to avoid
4375 // flooding the user with errors. And stop iterating over lists
4376 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004377 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004378 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004380 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004381 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004382 }
4383 ++recurse;
4384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004385 switch (tv->v_type)
4386 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004387 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004388 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004389 {
4390 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004391 r = tv->vval.v_string;
4392 if (r == NULL)
4393 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004394 }
4395 else
4396 {
4397 *tofree = string_quote(tv->vval.v_string, FALSE);
4398 r = *tofree;
4399 }
4400 break;
4401
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004403 if (echo_style)
4404 {
4405 *tofree = NULL;
4406 r = tv->vval.v_string;
4407 }
4408 else
4409 {
4410 *tofree = string_quote(tv->vval.v_string, TRUE);
4411 r = *tofree;
4412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004413 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004414
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004415 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004416 {
4417 partial_T *pt = tv->vval.v_partial;
4418 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004419 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004420 garray_T ga;
4421 int i;
4422 char_u *tf;
4423
4424 ga_init2(&ga, 1, 100);
4425 ga_concat(&ga, (char_u *)"function(");
4426 if (fname != NULL)
4427 {
4428 ga_concat(&ga, fname);
4429 vim_free(fname);
4430 }
4431 if (pt != NULL && pt->pt_argc > 0)
4432 {
4433 ga_concat(&ga, (char_u *)", [");
4434 for (i = 0; i < pt->pt_argc; ++i)
4435 {
4436 if (i > 0)
4437 ga_concat(&ga, (char_u *)", ");
4438 ga_concat(&ga,
4439 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4440 vim_free(tf);
4441 }
4442 ga_concat(&ga, (char_u *)"]");
4443 }
4444 if (pt != NULL && pt->pt_dict != NULL)
4445 {
4446 typval_T dtv;
4447
4448 ga_concat(&ga, (char_u *)", ");
4449 dtv.v_type = VAR_DICT;
4450 dtv.vval.v_dict = pt->pt_dict;
4451 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4452 vim_free(tf);
4453 }
4454 ga_concat(&ga, (char_u *)")");
4455
4456 *tofree = ga.ga_data;
4457 r = *tofree;
4458 break;
4459 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004460
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004461 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004462 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004463 break;
4464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004466 if (tv->vval.v_list == NULL)
4467 {
4468 *tofree = NULL;
4469 r = NULL;
4470 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004471 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4472 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004473 {
4474 *tofree = NULL;
4475 r = (char_u *)"[...]";
4476 }
4477 else
4478 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004479 int old_copyID = tv->vval.v_list->lv_copyID;
4480
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004481 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004482 *tofree = list2string(tv, copyID, restore_copyID);
4483 if (restore_copyID)
4484 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004485 r = *tofree;
4486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004487 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004488
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004490 if (tv->vval.v_dict == NULL)
4491 {
4492 *tofree = NULL;
4493 r = NULL;
4494 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004495 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4496 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004497 {
4498 *tofree = NULL;
4499 r = (char_u *)"{...}";
4500 }
4501 else
4502 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004503 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004504 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004505 *tofree = dict2string(tv, copyID, restore_copyID);
4506 if (restore_copyID)
4507 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004508 r = *tofree;
4509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004510 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004513 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004514 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004515 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004516 break;
4517
Bram Moolenaar835dc632016-02-07 14:27:38 +01004518 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004519 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004520 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004521 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004522 if (composite_val)
4523 {
4524 *tofree = string_quote(r, FALSE);
4525 r = *tofree;
4526 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004528
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004529 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004530#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004531 *tofree = NULL;
4532 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4533 r = numbuf;
4534 break;
4535#endif
4536
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004537 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004538 case VAR_SPECIAL:
4539 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004540 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004541 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004542 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004543
Bram Moolenaar8502c702014-06-17 12:51:16 +02004544 if (--recurse == 0)
4545 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004546 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004547}
4548
4549/*
4550 * Return a string with the string representation of a variable.
4551 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4552 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004553 * Does not put quotes around strings, as ":echo" displays values.
4554 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4555 * May return NULL.
4556 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004558echo_string(
4559 typval_T *tv,
4560 char_u **tofree,
4561 char_u *numbuf,
4562 int copyID)
4563{
4564 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4565}
4566
4567/*
4568 * Return a string with the string representation of a variable.
4569 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4570 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004571 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004572 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004573 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004574 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004575tv2string(
4576 typval_T *tv,
4577 char_u **tofree,
4578 char_u *numbuf,
4579 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004580{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004581 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582}
4583
4584/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 * Return string "str" in ' quotes, doubling ' characters.
4586 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004588 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004589 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004590string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004591{
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004593 char_u *p, *r, *s;
4594
Bram Moolenaar33570922005-01-25 22:26:29 +00004595 len = (function ? 13 : 3);
4596 if (str != NULL)
4597 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004598 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004599 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004600 if (*p == '\'')
4601 ++len;
4602 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004603 s = r = alloc(len);
4604 if (r != NULL)
4605 {
4606 if (function)
4607 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004609 r += 10;
4610 }
4611 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004613 if (str != NULL)
4614 for (p = str; *p != NUL; )
4615 {
4616 if (*p == '\'')
4617 *r++ = '\'';
4618 MB_COPY_CHAR(p, r);
4619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004621 if (function)
4622 *r++ = ')';
4623 *r++ = NUL;
4624 }
4625 return s;
4626}
4627
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004628#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629/*
4630 * Convert the string "text" to a floating point number.
4631 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4632 * this always uses a decimal point.
4633 * Returns the length of the text that was consumed.
4634 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004636string2float(
4637 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639{
4640 char *s = (char *)text;
4641 float_T f;
4642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004644 if (STRNICMP(text, "inf", 3) == 0)
4645 {
4646 *value = INFINITY;
4647 return 3;
4648 }
4649 if (STRNICMP(text, "-inf", 3) == 0)
4650 {
4651 *value = -INFINITY;
4652 return 4;
4653 }
4654 if (STRNICMP(text, "nan", 3) == 0)
4655 {
4656 *value = NAN;
4657 return 3;
4658 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659 f = strtod(s, &s);
4660 *value = f;
4661 return (int)((char_u *)s - text);
4662}
4663#endif
4664
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 * Get the value of an environment variable.
4667 * "arg" is pointing to the '$'. It is advanced to after the name.
4668 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004669 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 */
4671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004672get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 char_u *string = NULL;
4675 int len;
4676 int cc;
4677 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004678 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 ++*arg;
4681 name = *arg;
4682 len = get_env_len(arg);
4683 if (evaluate)
4684 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004685 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004686 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004687
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004688 cc = name[len];
4689 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004691 string = vim_getenv(name, &mustfree);
4692 if (string != NULL && *string != NUL)
4693 {
4694 if (!mustfree)
4695 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004697 else
4698 {
4699 if (mustfree)
4700 vim_free(string);
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004703 string = expand_env_save(name - 1);
4704 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004705 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004706 }
4707 name[len] = cc;
4708
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 rettv->v_type = VAR_STRING;
4710 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712
4713 return OK;
4714}
4715
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004718 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004720 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721var2fpos(
4722 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 int dollar_lnum, // TRUE when $ is last line
4724 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004726 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004728 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004730 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004731 if (varp->v_type == VAR_LIST)
4732 {
4733 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004734 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004735 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004736 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004737
4738 l = varp->vval.v_list;
4739 if (l == NULL)
4740 return NULL;
4741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004743 pos.lnum = list_find_nr(l, 0L, &error);
4744 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004745 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004748 pos.col = list_find_nr(l, 1L, &error);
4749 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004750 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004751 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004754 li = list_find(l, 1L);
4755 if (li != NULL && li->li_tv.v_type == VAR_STRING
4756 && li->li_tv.vval.v_string != NULL
4757 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4758 pos.col = len + 1;
4759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004761 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004763 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004766 pos.coladd = list_find_nr(l, 2L, &error);
4767 if (error)
4768 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004769
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004770 return &pos;
4771 }
4772
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004773 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 if (name == NULL)
4775 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004779 {
4780 if (VIsual_active)
4781 return &VIsual;
4782 return &curwin->w_cursor;
4783 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004786 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4788 return NULL;
4789 return pp;
4790 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004791
Bram Moolenaara5525202006-03-02 22:52:09 +00004792 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004793
Bram Moolenaar477933c2007-07-17 14:32:23 +00004794 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004795 {
4796 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004798 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004799 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 // In silent Ex mode topline is zero, but that's not a valid line
4801 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004802 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004803 return &pos;
4804 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004806 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004807 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004809 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004810 return &pos;
4811 }
4812 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004813 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004815 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 pos.lnum = curbuf->b_ml.ml_line_count;
4818 pos.col = 0;
4819 }
4820 else
4821 {
4822 pos.lnum = curwin->w_cursor.lnum;
4823 pos.col = (colnr_T)STRLEN(ml_get_curline());
4824 }
4825 return &pos;
4826 }
4827 return NULL;
4828}
4829
4830/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004831 * Convert list in "arg" into a position and optional file number.
4832 * When "fnump" is NULL there is no file number, only 3 items.
4833 * Note that the column is passed on as-is, the caller may want to decrement
4834 * it to use 1 for the first column.
4835 * Return FAIL when conversion is not possible, doesn't check the position for
4836 * validity.
4837 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004839list2fpos(
4840 typval_T *arg,
4841 pos_T *posp,
4842 int *fnump,
4843 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004844{
4845 list_T *l = arg->vval.v_list;
4846 long i = 0;
4847 long n;
4848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4850 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004851 if (arg->v_type != VAR_LIST
4852 || l == NULL
4853 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004854 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004855 return FAIL;
4856
4857 if (fnump != NULL)
4858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004860 if (n < 0)
4861 return FAIL;
4862 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004864 *fnump = n;
4865 }
4866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004868 if (n < 0)
4869 return FAIL;
4870 posp->lnum = n;
4871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004873 if (n < 0)
4874 return FAIL;
4875 posp->col = n;
4876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004878 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004879 posp->coladd = 0;
4880 else
4881 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004882
Bram Moolenaar493c1782014-05-28 14:34:46 +02004883 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004885
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004886 return OK;
4887}
4888
4889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 * Get the length of an environment variable name.
4891 * Advance "arg" to the first character after the name.
4892 * Return 0 for error.
4893 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 char_u *p;
4898 int len;
4899
4900 for (p = *arg; vim_isIDc(*p); ++p)
4901 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 return 0;
4904
4905 len = (int)(p - *arg);
4906 *arg = p;
4907 return len;
4908}
4909
4910/*
4911 * Get the length of the name of a function or internal variable.
4912 * "arg" is advanced to the first non-white character after the name.
4913 * Return 0 if something is wrong.
4914 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004915 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004916get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 char_u *p;
4919 int len;
4920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004923 {
4924 if (*p == ':')
4925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // "s:" is start of "s:var", but "n:" is not and can be used in
4927 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004928 len = (int)(p - *arg);
4929 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4930 || len > 1)
4931 break;
4932 }
4933 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return 0;
4936
4937 len = (int)(p - *arg);
4938 *arg = skipwhite(p);
4939
4940 return len;
4941}
4942
4943/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004944 * Get the length of the name of a variable or function.
4945 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004947 * Return -1 if curly braces expansion failed.
4948 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 * If the name contains 'magic' {}'s, expand them and return the
4950 * expanded name in an allocated string via 'alias' - caller must free.
4951 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953get_name_len(
4954 char_u **arg,
4955 char_u **alias,
4956 int evaluate,
4957 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 char_u *p;
4961 char_u *expr_start;
4962 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4967 && (*arg)[2] == (int)KE_SNR)
4968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 *arg += 3;
4971 return get_id_len(arg) + 3;
4972 }
4973 len = eval_fname_script(*arg);
4974 if (len > 0)
4975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 *arg += len;
4978 }
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004983 p = find_name_end(*arg, &expr_start, &expr_end,
4984 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (expr_start != NULL)
4986 {
4987 char_u *temp_string;
4988
4989 if (!evaluate)
4990 {
4991 len += (int)(p - *arg);
4992 *arg = skipwhite(p);
4993 return len;
4994 }
4995
4996 /*
4997 * Include any <SID> etc in the expanded string:
4998 * Thus the -len here.
4999 */
5000 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5001 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005002 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 *alias = temp_string;
5004 *arg = skipwhite(p);
5005 return (int)STRLEN(temp_string);
5006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005009 // Only give an error when there is something, otherwise it will be
5010 // reported at a higher level.
5011 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005012 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 return len;
5015}
5016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017/*
5018 * Find the end of a variable or function name, taking care of magic braces.
5019 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5020 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005021 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 * Return a pointer to just after the name. Equal to "arg" if there is no
5023 * valid name.
5024 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005026find_name_end(
5027 char_u *arg,
5028 char_u **expr_start,
5029 char_u **expr_end,
5030 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 int mb_nest = 0;
5033 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005035 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 *expr_start = NULL;
5040 *expr_end = NULL;
5041 }
5042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 // Quick check for valid starting character.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005044 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5045 return arg;
5046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 for (p = arg; *p != NUL
5048 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005049 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005050 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005052 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005054 if (*p == '\'')
5055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005057 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005058 ;
5059 if (*p == NUL)
5060 break;
5061 }
5062 else if (*p == '"')
5063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005065 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005066 if (*p == '\\' && p[1] != NUL)
5067 ++p;
5068 if (*p == NUL)
5069 break;
5070 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005071 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5072 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // "s:" is start of "s:var", but "n:" is not and can be used in
5074 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005075 len = (int)(p - arg);
5076 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005077 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005078 break;
5079 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005080
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 if (*p == '[')
5084 ++br_nest;
5085 else if (*p == ']')
5086 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 if (*p == '{')
5092 {
5093 mb_nest++;
5094 if (expr_start != NULL && *expr_start == NULL)
5095 *expr_start = p;
5096 }
5097 else if (*p == '}')
5098 {
5099 mb_nest--;
5100 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5101 *expr_end = p;
5102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105
5106 return p;
5107}
5108
5109/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005110 * Expands out the 'magic' {}'s in a variable/function name.
5111 * Note that this can call itself recursively, to deal with
5112 * constructs like foo{bar}{baz}{bam}
5113 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5114 * "in_start" ^
5115 * "expr_start" ^
5116 * "expr_end" ^
5117 * "in_end" ^
5118 *
5119 * Returns a new allocated string, which the caller must free.
5120 * Returns NULL for failure.
5121 */
5122 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123make_expanded_name(
5124 char_u *in_start,
5125 char_u *expr_start,
5126 char_u *expr_end,
5127 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128{
5129 char_u c1;
5130 char_u *retval = NULL;
5131 char_u *temp_result;
5132 char_u *nextcmd = NULL;
5133
5134 if (expr_end == NULL || in_end == NULL)
5135 return NULL;
5136 *expr_start = NUL;
5137 *expr_end = NUL;
5138 c1 = *in_end;
5139 *in_end = NUL;
5140
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005141 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 if (temp_result != NULL && nextcmd == NULL)
5143 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005144 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5145 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005146 if (retval != NULL)
5147 {
5148 STRCPY(retval, in_start);
5149 STRCAT(retval, temp_result);
5150 STRCAT(retval, expr_end + 1);
5151 }
5152 }
5153 vim_free(temp_result);
5154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 *expr_start = '{';
5157 *expr_end = '}';
5158
5159 if (retval != NULL)
5160 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005161 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162 if (expr_start != NULL)
5163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005164 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 temp_result = make_expanded_name(retval, expr_start,
5166 expr_end, temp_result);
5167 vim_free(retval);
5168 retval = temp_result;
5169 }
5170 }
5171
5172 return retval;
5173}
5174
5175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005182 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5183}
5184
5185/*
5186 * Return TRUE if character "c" can be used as the first character in a
5187 * variable or function name (excluding '{' and '}').
5188 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005190eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005191{
5192 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193}
5194
5195/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005196 * Handle:
5197 * - expr[expr], expr[expr:expr] subscript
5198 * - ".name" lookup
5199 * - function call with Funcref variable: func(expr)
5200 * - method call: var->method()
5201 *
5202 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005203 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005205handle_subscript(
5206 char_u **arg,
5207 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005208 int evaluate, // do more than finding the end
5209 int verbose, // give error messages
5210 char_u *start_leader, // start of '!' and '-' prefixes
5211 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212{
5213 int ret = OK;
5214 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215
Bram Moolenaar61343f02019-07-20 21:11:13 +02005216 // "." is ".name" lookup when we found a dict or when evaluating and
5217 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005218 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005219 && (((**arg == '['
5220 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005221 || (!evaluate
5222 && (*arg)[1] != '.'
5223 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005224 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005225 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005226 && !VIM_ISWHITE(*(*arg - 1)))
5227 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 {
5229 if (**arg == '(')
5230 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005231 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005232
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005233 // Stop the expression evaluation when immediately aborting on
5234 // error, or when an interrupt occurred or an exception was thrown
5235 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 if (aborting())
5237 {
5238 if (ret == OK)
5239 clear_tv(rettv);
5240 ret = FAIL;
5241 }
5242 dict_unref(selfdict);
5243 selfdict = NULL;
5244 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005245 else if (**arg == '-')
5246 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005247 // Expression "-1.0->method()" applies the leader "-" before
5248 // applying ->.
5249 if (evaluate && *end_leaderp > start_leader)
5250 ret = eval7_leader(rettv, start_leader, end_leaderp);
5251 if (ret == OK)
5252 {
5253 if ((*arg)[2] == '{')
5254 // expr->{lambda}()
5255 ret = eval_lambda(arg, rettv, evaluate, verbose);
5256 else
5257 // expr->name()
5258 ret = eval_method(arg, rettv, evaluate, verbose);
5259 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005260 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005262 {
5263 dict_unref(selfdict);
5264 if (rettv->v_type == VAR_DICT)
5265 {
5266 selfdict = rettv->vval.v_dict;
5267 if (selfdict != NULL)
5268 ++selfdict->dv_refcount;
5269 }
5270 else
5271 selfdict = NULL;
5272 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5273 {
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5281 // Don't do this when "Func" is already a partial that was bound
5282 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005283 if (selfdict != NULL
5284 && (rettv->v_type == VAR_FUNC
5285 || (rettv->v_type == VAR_PARTIAL
5286 && (rettv->vval.v_partial->pt_auto
5287 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005288 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005289
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005290 dict_unref(selfdict);
5291 return ret;
5292}
5293
5294/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 * value).
5297 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005298 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005299alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005301 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302}
5303
5304/*
5305 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * The string "s" must have been allocated, it is consumed.
5307 * Return NULL for out of memory, the variable otherwise.
5308 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005309 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
Bram Moolenaar33570922005-01-25 22:26:29 +00005312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005314 rettv = alloc_tv();
5315 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005317 rettv->v_type = VAR_STRING;
5318 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320 else
5321 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005322 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323}
5324
5325/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005329free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
5331 if (varp != NULL)
5332 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 switch (varp->v_type)
5334 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005336 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005337 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005338 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 vim_free(varp->vval.v_string);
5340 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005341 case VAR_PARTIAL:
5342 partial_unref(varp->vval.v_partial);
5343 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005344 case VAR_BLOB:
5345 blob_unref(varp->vval.v_blob);
5346 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 case VAR_LIST:
5348 list_unref(varp->vval.v_list);
5349 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005350 case VAR_DICT:
5351 dict_unref(varp->vval.v_dict);
5352 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005353 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005354#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005355 job_unref(varp->vval.v_job);
5356 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005357#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005358 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005360 channel_unref(varp->vval.v_channel);
5361 break;
5362#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005363 case VAR_NUMBER:
5364 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005365 case VAR_UNKNOWN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005366 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005367 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005368 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 vim_free(varp);
5371 }
5372}
5373
5374/*
5375 * Free the memory for a variable value and set the value to NULL or 0.
5376 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005378clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 if (varp != NULL)
5381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005385 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005387 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005388 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005390 case VAR_PARTIAL:
5391 partial_unref(varp->vval.v_partial);
5392 varp->vval.v_partial = NULL;
5393 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005394 case VAR_BLOB:
5395 blob_unref(varp->vval.v_blob);
5396 varp->vval.v_blob = NULL;
5397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 case VAR_LIST:
5399 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005400 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 case VAR_DICT:
5403 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005404 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005407 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005408 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 varp->vval.v_number = 0;
5410 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005411 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005412#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005413 varp->vval.v_float = 0.0;
5414 break;
5415#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005416 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005417#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005418 job_unref(varp->vval.v_job);
5419 varp->vval.v_job = NULL;
5420#endif
5421 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005422 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005423#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005424 channel_unref(varp->vval.v_channel);
5425 varp->vval.v_channel = NULL;
5426#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 case VAR_UNKNOWN:
5428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005430 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432}
5433
5434/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 * Set the value of a variable to NULL without freeing items.
5436 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439{
5440 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442}
5443
5444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 * Get the number value of a variable.
5446 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005447 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005448 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005449 * caller of incompatible types: it sets *denote to TRUE if "denote"
5450 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005452 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005453tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 int error = FALSE;
5456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005458}
5459
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005460 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005461tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005463 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005468 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005469 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005470#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005471 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005472 break;
5473#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005475 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005476 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 break;
5478 case VAR_STRING:
5479 if (varp->vval.v_string != NULL)
5480 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005481 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005482 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005483 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005484 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005485 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005487 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005488 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005489 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005490 case VAR_SPECIAL:
5491 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005492 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005494 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005495 break;
5496#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005497 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005499 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005500 break;
5501#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005502 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005503 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005504 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005505 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005506 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005509 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005510 n = -1;
5511 else
5512 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
5515
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005516#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005518tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005519{
5520 switch (varp->v_type)
5521 {
5522 case VAR_NUMBER:
5523 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005524 case VAR_FLOAT:
5525 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005526 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005527 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005528 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005529 break;
5530 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005531 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005532 break;
5533 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005534 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005535 break;
5536 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005537 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005538 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005539 case VAR_BOOL:
5540 emsg(_("E362: Using a boolean value as a Float"));
5541 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005543 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005544 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005546# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005547 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005548 break;
5549# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005550 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005551# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005553 break;
5554# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005555 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005556 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005557 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005558 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005559 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005560 break;
5561 }
5562 return 0;
5563}
5564#endif
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Get the string value of a variable.
5568 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005569 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5570 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 * If the String variable has never been set, return an empty string.
5572 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005573 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005574 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005576 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005577tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 static char_u mybuf[NUMBUFLEN];
5580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005581 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005584 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005585tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005587 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005588
5589 return res != NULL ? res : (char_u *)"";
5590}
5591
Bram Moolenaar7d647822014-04-05 21:28:56 +02005592/*
5593 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5594 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005596tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005597{
5598 static char_u mybuf[NUMBUFLEN];
5599
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005600 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005601}
5602
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005603 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005604tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005609 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005610 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 return buf;
5612 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005613 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 break;
5616 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005617 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 break;
5619 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005620 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005622 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005623#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005624 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005625 break;
5626#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 case VAR_STRING:
5628 if (varp->vval.v_string != NULL)
5629 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005630 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005631 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005632 case VAR_SPECIAL:
5633 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5634 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005635 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005636 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005637 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005638 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005640 {
5641 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005642 char *status;
5643
5644 if (job == NULL)
5645 return (char_u *)"no process";
5646 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005647 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005648 : "run";
5649# ifdef UNIX
5650 vim_snprintf((char *)buf, NUMBUFLEN,
5651 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005652# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005653 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005654 "process %ld %s",
5655 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005656 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005657# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005659 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5660# endif
5661 return buf;
5662 }
5663#endif
5664 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005665 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005666#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005667 {
5668 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005669 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005670
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005671 if (channel == NULL)
5672 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5673 else
5674 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005675 "channel %d %s", channel->ch_id, status);
5676 return buf;
5677 }
5678#endif
5679 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005680 case VAR_UNKNOWN:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005681 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005684 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005688 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5689 * string() on Dict, List, etc.
5690 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005691 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005692tv_stringify(typval_T *varp, char_u *buf)
5693{
5694 if (varp->v_type == VAR_LIST
5695 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005696 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005697 || varp->v_type == VAR_FUNC
5698 || varp->v_type == VAR_PARTIAL
5699 || varp->v_type == VAR_FLOAT)
5700 {
5701 typval_T tmp;
5702
5703 f_string(varp, &tmp);
5704 tv_get_string_buf(&tmp, buf);
5705 clear_tv(varp);
5706 *varp = tmp;
5707 return tmp.vval.v_string;
5708 }
5709 return tv_get_string_buf(varp, buf);
5710}
5711
5712/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005713 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5714 * Also give an error message, using "name" or _("name") when use_gettext is
5715 * TRUE.
5716 */
5717 static int
5718tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5719{
5720 int lock = 0;
5721
5722 switch (tv->v_type)
5723 {
5724 case VAR_BLOB:
5725 if (tv->vval.v_blob != NULL)
5726 lock = tv->vval.v_blob->bv_lock;
5727 break;
5728 case VAR_LIST:
5729 if (tv->vval.v_list != NULL)
5730 lock = tv->vval.v_list->lv_lock;
5731 break;
5732 case VAR_DICT:
5733 if (tv->vval.v_dict != NULL)
5734 lock = tv->vval.v_dict->dv_lock;
5735 break;
5736 default:
5737 break;
5738 }
5739 return var_check_lock(tv->v_lock, name, use_gettext)
5740 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5741}
5742
5743/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005744 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005746 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005747 * It is OK for "from" and "to" to point to the same item. This is used to
5748 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005751copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005754 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 switch (from->v_type)
5756 {
5757 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005758 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005759 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 to->vval.v_number = from->vval.v_number;
5761 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005762 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005764 to->vval.v_float = from->vval.v_float;
5765 break;
5766#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005767 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005770 if (to->vval.v_job != NULL)
5771 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005772 break;
5773#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005774 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005776 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005777 if (to->vval.v_channel != NULL)
5778 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005779 break;
5780#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 case VAR_STRING:
5782 case VAR_FUNC:
5783 if (from->vval.v_string == NULL)
5784 to->vval.v_string = NULL;
5785 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005786 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005788 if (from->v_type == VAR_FUNC)
5789 func_ref(to->vval.v_string);
5790 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005792 case VAR_PARTIAL:
5793 if (from->vval.v_partial == NULL)
5794 to->vval.v_partial = NULL;
5795 else
5796 {
5797 to->vval.v_partial = from->vval.v_partial;
5798 ++to->vval.v_partial->pt_refcount;
5799 }
5800 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005801 case VAR_BLOB:
5802 if (from->vval.v_blob == NULL)
5803 to->vval.v_blob = NULL;
5804 else
5805 {
5806 to->vval.v_blob = from->vval.v_blob;
5807 ++to->vval.v_blob->bv_refcount;
5808 }
5809 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 case VAR_LIST:
5811 if (from->vval.v_list == NULL)
5812 to->vval.v_list = NULL;
5813 else
5814 {
5815 to->vval.v_list = from->vval.v_list;
5816 ++to->vval.v_list->lv_refcount;
5817 }
5818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 case VAR_DICT:
5820 if (from->vval.v_dict == NULL)
5821 to->vval.v_dict = NULL;
5822 else
5823 {
5824 to->vval.v_dict = from->vval.v_dict;
5825 ++to->vval.v_dict->dv_refcount;
5826 }
5827 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005828 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005829 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 break;
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
5834/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005835 * Make a copy of an item.
5836 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005837 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5838 * reference to an already copied list/dict can be used.
5839 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005840 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842item_copy(
5843 typval_T *from,
5844 typval_T *to,
5845 int deep,
5846 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005847{
5848 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005849 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005850
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005853 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005854 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855 }
5856 ++recurse;
5857
5858 switch (from->v_type)
5859 {
5860 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005861 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 case VAR_STRING:
5863 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005864 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005865 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005866 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005868 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005869 copy_tv(from, to);
5870 break;
5871 case VAR_LIST:
5872 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005873 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005874 if (from->vval.v_list == NULL)
5875 to->vval.v_list = NULL;
5876 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 to->vval.v_list = from->vval.v_list->lv_copylist;
5880 ++to->vval.v_list->lv_refcount;
5881 }
5882 else
5883 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5884 if (to->vval.v_list == NULL)
5885 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005886 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005887 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005888 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005889 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 case VAR_DICT:
5891 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005892 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005893 if (from->vval.v_dict == NULL)
5894 to->vval.v_dict = NULL;
5895 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005898 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5899 ++to->vval.v_dict->dv_refcount;
5900 }
5901 else
5902 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5903 if (to->vval.v_dict == NULL)
5904 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005906 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005907 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909 }
5910 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005911 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912}
5913
5914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 * ":echo expr1 ..." print each argument separated with a space, add a
5916 * newline at the end.
5917 * ":echon expr1 ..." print each argument plain.
5918 */
5919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 char_u *p;
5926 int needclr = TRUE;
5927 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01005929 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005930 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
5932 if (eap->skip)
5933 ++emsg_skip;
5934 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
5935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 // If eval1() causes an error message the text from the command may
5937 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005938 need_clr_eos = needclr;
5939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005941 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 /*
5944 * Report the invalid expression unless the expression evaluation
5945 * has been cancelled due to an aborting error, an interrupt, or an
5946 * exception.
5947 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005948 if (!aborting() && did_emsg == did_emsg_before
5949 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005950 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005951 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 break;
5953 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005954 need_clr_eos = FALSE;
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (!eap->skip)
5957 {
5958 if (atstart)
5959 {
5960 atstart = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 // Call msg_start() after eval1(), evaluating the expression
5962 // may cause a message to appear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01005964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005965 // Mark the saved text as finishing the line, so that what
5966 // follows is displayed on a new line when scrolling back
5967 // at the more prompt.
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02005968 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01005970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 }
5972 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005973 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005974 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 if (p != NULL)
5976 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 if (*p != TAB && needclr)
5981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // remove any text still there from the command
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 msg_clr_eos();
5984 needclr = FALSE;
5985 }
5986 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 if (has_mbyte)
5991 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005992 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993
5994 (void)msg_outtrans_len_attr(p, i, echo_attr);
5995 p += i - 1;
5996 }
5997 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006003 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 arg = skipwhite(arg);
6005 }
6006 eap->nextcmd = check_nextcmd(arg);
6007
6008 if (eap->skip)
6009 --emsg_skip;
6010 else
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 if (needclr)
6014 msg_clr_eos();
6015 if (eap->cmdidx == CMD_echo)
6016 msg_end();
6017 }
6018}
6019
6020/*
6021 * ":echohl {name}".
6022 */
6023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006026 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027}
6028
6029/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006030 * Returns the :echo attribute
6031 */
6032 int
6033get_echo_attr(void)
6034{
6035 return echo_attr;
6036}
6037
6038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * ":execute expr1 ..." execute the result of an expression.
6040 * ":echomsg expr1 ..." Print a message
6041 * ":echoerr expr1 ..." Print an error
6042 * Each gets spaces around each argument and a newline at the end for
6043 * echo commands
6044 */
6045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 int ret = OK;
6051 char_u *p;
6052 garray_T ga;
6053 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006054 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056 ga_init2(&ga, 1, 80);
6057
6058 if (eap->skip)
6059 ++emsg_skip;
6060 while (*arg != NUL && *arg != '|' && *arg != '\n')
6061 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006062 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6063 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
6066 if (!eap->skip)
6067 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006068 char_u buf[NUMBUFLEN];
6069
6070 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006071 {
6072 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6073 {
6074 emsg(_(e_inval_string));
6075 p = NULL;
6076 }
6077 else
6078 p = tv_get_string_buf(&rettv, buf);
6079 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006080 else
6081 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006082 if (p == NULL)
6083 {
6084 clear_tv(&rettv);
6085 ret = FAIL;
6086 break;
6087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 len = (int)STRLEN(p);
6089 if (ga_grow(&ga, len + 2) == FAIL)
6090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006091 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 ret = FAIL;
6093 break;
6094 }
6095 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 ga.ga_len += len;
6099 }
6100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 arg = skipwhite(arg);
6103 }
6104
6105 if (ret != FAIL && ga.ga_data != NULL)
6106 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006107 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006109 // Mark the already saved text as finishing the line, so that what
6110 // follows is displayed on a new line when scrolling back at the
6111 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006112 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006113 }
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006116 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006117 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006118 out_flush();
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 else if (eap->cmdidx == CMD_echoerr)
6121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006122 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006124 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (!force_abort)
6126 did_emsg = save_did_emsg;
6127 }
6128 else if (eap->cmdidx == CMD_execute)
6129 do_cmdline((char_u *)ga.ga_data,
6130 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6131 }
6132
6133 ga_clear(&ga);
6134
6135 if (eap->skip)
6136 --emsg_skip;
6137
6138 eap->nextcmd = check_nextcmd(arg);
6139}
6140
6141/*
6142 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6143 * "arg" points to the "&" or '+' when called, to "option" when returning.
6144 * Returns NULL when no option name found. Otherwise pointer to the char
6145 * after the option name.
6146 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006147 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 char_u *p = *arg;
6151
6152 ++p;
6153 if (*p == 'g' && p[1] == ':')
6154 {
6155 *opt_flags = OPT_GLOBAL;
6156 p += 2;
6157 }
6158 else if (*p == 'l' && p[1] == ':')
6159 {
6160 *opt_flags = OPT_LOCAL;
6161 p += 2;
6162 }
6163 else
6164 *opt_flags = 0;
6165
6166 if (!ASCII_ISALPHA(*p))
6167 return NULL;
6168 *arg = p;
6169
6170 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006171 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 else
6173 while (ASCII_ISALPHA(*p))
6174 ++p;
6175 return p;
6176}
6177
6178/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006179 * Display script name where an item was last set.
6180 * Should only be invoked when 'verbose' is non-zero.
6181 */
6182 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006183last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006184{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006185 char_u *p;
6186
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006187 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006188 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006189 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006190 if (p != NULL)
6191 {
6192 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006193 msg_puts(_("\n\tLast set from "));
6194 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006195 if (script_ctx.sc_lnum > 0)
6196 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006197 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006198 msg_outnum((long)script_ctx.sc_lnum);
6199 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006200 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006201 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006202 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006203 }
6204}
6205
Bram Moolenaar61be3762019-03-19 23:04:17 +01006206/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006207 * Compare "typ1" and "typ2". Put the result in "typ1".
6208 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006209 int
6210typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006211 typval_T *typ1, // first operand
6212 typval_T *typ2, // second operand
6213 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006215{
6216 int i;
6217 varnumber_T n1, n2;
6218 char_u *s1, *s2;
6219 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006220 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006221
Bram Moolenaar31988702018-02-13 12:57:42 +01006222 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // For "is" a different type always means FALSE, for "notis"
6225 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006226 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006227 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006228 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6229 {
6230 if (type_is)
6231 {
6232 n1 = (typ1->v_type == typ2->v_type
6233 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006234 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006235 n1 = !n1;
6236 }
6237 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006238 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006239 {
6240 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006241 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006242 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006243 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006244 clear_tv(typ1);
6245 return FAIL;
6246 }
6247 else
6248 {
6249 // Compare two Blobs for being equal or unequal.
6250 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006251 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006252 n1 = !n1;
6253 }
6254 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006255 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6256 {
6257 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006258 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006259 n1 = (typ1->v_type == typ2->v_type
6260 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006261 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006262 n1 = !n1;
6263 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006264 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006265 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006266 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006267 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006268 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006269 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006270 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006271 clear_tv(typ1);
6272 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006273 }
6274 else
6275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006276 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006277 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6278 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006279 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006280 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006281 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006282 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006283
6284 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6285 {
6286 if (type_is)
6287 {
6288 n1 = (typ1->v_type == typ2->v_type
6289 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006290 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006291 n1 = !n1;
6292 }
6293 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006294 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006295 {
6296 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006297 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006298 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006299 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006300 clear_tv(typ1);
6301 return FAIL;
6302 }
6303 else
6304 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006306 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6307 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006308 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006309 n1 = !n1;
6310 }
6311 }
6312
6313 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6314 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6315 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006316 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6317 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006319 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006320 clear_tv(typ1);
6321 return FAIL;
6322 }
6323 if ((typ1->v_type == VAR_PARTIAL
6324 && typ1->vval.v_partial == NULL)
6325 || (typ2->v_type == VAR_PARTIAL
6326 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006327 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006328 n1 = FALSE;
6329 else if (type_is)
6330 {
6331 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006332 // strings are considered the same if their value is
6333 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006334 n1 = tv_equal(typ1, typ2, ic, FALSE);
6335 else if (typ1->v_type == VAR_PARTIAL
6336 && typ2->v_type == VAR_PARTIAL)
6337 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6338 else
6339 n1 = FALSE;
6340 }
6341 else
6342 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006343 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006344 n1 = !n1;
6345 }
6346
6347#ifdef FEAT_FLOAT
6348 /*
6349 * If one of the two variables is a float, compare as a float.
6350 * When using "=~" or "!~", always compare as string.
6351 */
6352 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006353 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006354 {
6355 float_T f1, f2;
6356
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006357 f1 = tv_get_float(typ1);
6358 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006359 n1 = FALSE;
6360 switch (type)
6361 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006362 case EXPR_IS:
6363 case EXPR_EQUAL: n1 = (f1 == f2); break;
6364 case EXPR_ISNOT:
6365 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6366 case EXPR_GREATER: n1 = (f1 > f2); break;
6367 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6368 case EXPR_SMALLER: n1 = (f1 < f2); break;
6369 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6370 case EXPR_UNKNOWN:
6371 case EXPR_MATCH:
6372 case EXPR_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006373 }
6374 }
6375#endif
6376
6377 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006378 * If one of the two variables is a number, compare as a number.
6379 * When using "=~" or "!~", always compare as string.
6380 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006381 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006382 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006384 n1 = tv_get_number(typ1);
6385 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006386 switch (type)
6387 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006388 case EXPR_IS:
6389 case EXPR_EQUAL: n1 = (n1 == n2); break;
6390 case EXPR_ISNOT:
6391 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6392 case EXPR_GREATER: n1 = (n1 > n2); break;
6393 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6394 case EXPR_SMALLER: n1 = (n1 < n2); break;
6395 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6396 case EXPR_UNKNOWN:
6397 case EXPR_MATCH:
6398 case EXPR_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006399 }
6400 }
6401 else
6402 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006403 s1 = tv_get_string_buf(typ1, buf1);
6404 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006405 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006406 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6407 else
6408 i = 0;
6409 n1 = FALSE;
6410 switch (type)
6411 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006412 case EXPR_IS:
6413 case EXPR_EQUAL: n1 = (i == 0); break;
6414 case EXPR_ISNOT:
6415 case EXPR_NEQUAL: n1 = (i != 0); break;
6416 case EXPR_GREATER: n1 = (i > 0); break;
6417 case EXPR_GEQUAL: n1 = (i >= 0); break;
6418 case EXPR_SMALLER: n1 = (i < 0); break;
6419 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006420
Bram Moolenaar87396072019-12-31 22:36:18 +01006421 case EXPR_MATCH:
6422 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006423 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006424 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006425 n1 = !n1;
6426 break;
6427
Bram Moolenaar87396072019-12-31 22:36:18 +01006428 case EXPR_UNKNOWN: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006429 }
6430 }
6431 clear_tv(typ1);
6432 typ1->v_type = VAR_NUMBER;
6433 typ1->vval.v_number = n1;
6434
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006435 return OK;
6436}
6437
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006438 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006439typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006440{
6441 char_u *tofree;
6442 char_u numbuf[NUMBUFLEN];
6443 char_u *ret = NULL;
6444
6445 if (arg == NULL)
6446 return vim_strsave((char_u *)"(does not exist)");
6447 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006449 if (ret != NULL && tofree == NULL)
6450 ret = vim_strsave(ret);
6451 return ret;
6452}
6453
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006454#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456/*
6457 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006458 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 * "flags" can be "g" to do a global substitute.
6460 * Returns an allocated string, NULL for error.
6461 */
6462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463do_string_sub(
6464 char_u *str,
6465 char_u *pat,
6466 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006467 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006468 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 int sublen;
6471 regmatch_T regmatch;
6472 int i;
6473 int do_all;
6474 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006475 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 garray_T ga;
6477 char_u *ret;
6478 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006479 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006483 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
6485 ga_init2(&ga, 1, 200);
6486
6487 do_all = (flags[0] == 'g');
6488
6489 regmatch.rm_ic = p_ic;
6490 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6491 if (regmatch.regprog != NULL)
6492 {
6493 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006494 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006498 if (regmatch.startp[0] == regmatch.endp[0])
6499 {
6500 if (zero_width == regmatch.startp[0])
6501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006503 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006504 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6505 (size_t)i);
6506 ga.ga_len += i;
6507 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006508 continue;
6509 }
6510 zero_width = regmatch.startp[0];
6511 }
6512
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 /*
6514 * Get some space for a temporary buffer to do the substitution
6515 * into. It will contain:
6516 * - The text up to where the match is.
6517 * - The substituted text.
6518 * - The text after the match.
6519 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006520 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006521 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6523 {
6524 ga_clear(&ga);
6525 break;
6526 }
6527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006528 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 i = (int)(regmatch.startp[0] - tail);
6530 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006532 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 + ga.ga_len + i, TRUE, TRUE, FALSE);
6534 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006535 tail = regmatch.endp[0];
6536 if (*tail == NUL)
6537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (!do_all)
6539 break;
6540 }
6541
6542 if (ga.ga_data != NULL)
6543 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6544
Bram Moolenaar473de612013-06-08 18:19:48 +02006545 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547
6548 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6549 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006550 if (p_cpo == empty_option)
6551 p_cpo = save_cpo;
6552 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006553 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006554 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 return ret;
6557}