blob: b3fe650ee88858ddf12640191c96c7f110432f50 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020024#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020025static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020026#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000027
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010028#define NAMESPACE_CHAR (char_u *)"abglstvw"
29
Bram Moolenaar532c7802005-01-27 14:44:31 +000030/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 * When recursively copying lists and dicts we need to remember which ones we
32 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000033 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000034 */
35static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020036
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038 * Info used by a ":for" loop.
39 */
Bram Moolenaar33570922005-01-25 22:26:29 +000040typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000041{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 int fi_semicolon; // TRUE if ending in '; var]'
43 int fi_varcount; // nr of variables in the list
44 listwatch_T fi_lw; // keep an eye on the item used.
45 list_T *fi_list; // list being used
46 int fi_bi; // index of blob
47 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000048} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000049
Bram Moolenaar48e697e2016-01-23 22:17:30 +010050static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar32e35112020-05-14 22:41:15 +020051static int eval2(char_u **arg, typval_T *rettv, int flags);
52static int eval3(char_u **arg, typval_T *rettv, int flags);
53static int eval4(char_u **arg, typval_T *rettv, int flags);
54static int eval5(char_u **arg, typval_T *rettv, int flags);
55static int eval6(char_u **arg, typval_T *rettv, int flags, int want_string);
56static int eval7(char_u **arg, typval_T *rettv, int flags, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020057static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000058
Bram Moolenaar48e697e2016-01-23 22:17:30 +010059static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020060static 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 +010061static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020062
Bram Moolenaare21c1582019-03-02 11:57:09 +010063/*
64 * Return "n1" divided by "n2", taking care of dividing by zero.
65 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020066 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010067num_divide(varnumber_T n1, varnumber_T n2)
68{
69 varnumber_T result;
70
71 if (n2 == 0) // give an error message?
72 {
73 if (n1 == 0)
74 result = VARNUM_MIN; // similar to NaN
75 else if (n1 < 0)
76 result = -VARNUM_MAX;
77 else
78 result = VARNUM_MAX;
79 }
80 else
81 result = n1 / n2;
82
83 return result;
84}
85
86/*
87 * Return "n1" modulus "n2", taking care of dividing by zero.
88 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020089 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010090num_modulus(varnumber_T n1, varnumber_T n2)
91{
92 // Give an error when n2 is 0?
93 return (n2 == 0) ? 0 : (n1 % n2);
94}
95
Bram Moolenaara1fa8922017-01-12 20:06:33 +010096#if defined(EBCDIC) || defined(PROTO)
97/*
98 * Compare struct fst by function name.
99 */
100 static int
101compare_func_name(const void *s1, const void *s2)
102{
103 struct fst *p1 = (struct fst *)s1;
104 struct fst *p2 = (struct fst *)s2;
105
106 return STRCMP(p1->f_name, p2->f_name);
107}
108
109/*
110 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100111 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100112 * On machines using EBCDIC we have to sort it.
113 */
114 static void
115sortFunctions(void)
116{
117 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
118
119 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
120}
121#endif
122
Bram Moolenaar33570922005-01-25 22:26:29 +0000123/*
124 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000125 */
126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100127eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000128{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200129 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200130 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000131
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132#ifdef EBCDIC
133 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100134 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200135 */
136 sortFunctions();
137#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000138}
139
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140#if defined(EXITFREE) || defined(PROTO)
141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200144 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100145 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200146 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000147
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200148 // autoloaded script names
149 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000150
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200151 // unreferenced lists and dicts
152 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200153
154 // functions not garbage collected
155 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000156}
157#endif
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Top level evaluation function, returning a boolean.
161 * Sets "error" to TRUE if there was an error.
162 * Return TRUE or FALSE.
163 */
164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100165eval_to_bool(
166 char_u *arg,
167 int *error,
168 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaar33570922005-01-25 22:26:29 +0000171 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200172 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 if (skip)
175 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200176 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 *error = FALSE;
181 if (!skip)
182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100183 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000184 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 }
186 }
187 if (skip)
188 --emsg_skip;
189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
197eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
203
Bram Moolenaar32e35112020-05-14 22:41:15 +0200204 ret = eval1(arg, rettv, evaluate ? EVAL_EVALUATE : 0);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
215 return ret;
216}
217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218/*
219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe.evaluate = TRUE;
237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0)
248 {
249 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL)
250 return FAIL;
251 }
252 else
253 {
254 s = partial_name(partial);
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 funcexe.evaluate = TRUE;
259 funcexe.partial = partial;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
261 return FAIL;
262 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 }
264 else
265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100266 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 if (s == NULL)
268 return FAIL;
269 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100270 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100272 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200274 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100275 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100276 return FAIL;
277 }
278 }
279 return OK;
280}
281
282/*
283 * Like eval_to_bool() but using a typval_T instead of a string.
284 * Works for string, funcref and partial.
285 */
286 int
287eval_expr_to_bool(typval_T *expr, int *error)
288{
289 typval_T rettv;
290 int res;
291
292 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
293 {
294 *error = TRUE;
295 return FALSE;
296 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 clear_tv(&rettv);
299 return res;
300}
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302/*
303 * Top level evaluation function, returning a string. If "skip" is TRUE,
304 * only parsing to "nextcmd" is done, without reporting errors. Return
305 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
306 */
307 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100308eval_to_string_skip(
309 char_u *arg,
310 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100311 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
Bram Moolenaar33570922005-01-25 22:26:29 +0000313 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 char_u *retval;
315
316 if (skip)
317 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200318 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 retval = NULL;
320 else
321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100322 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000323 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 if (skip)
326 --emsg_skip;
327
328 return retval;
329}
330
331/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000332 * Skip over an expression at "*pp".
333 * Return FAIL for an error, OK otherwise.
334 */
335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000337{
Bram Moolenaar33570922005-01-25 22:26:29 +0000338 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000339
340 *pp = skipwhite(*pp);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200341 return eval1(pp, &rettv, 0);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342}
343
344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000346 * When "convert" is TRUE convert a List into a sequence of lines and convert
347 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 * Return pointer to allocated memory, or NULL for failure.
349 */
350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100351eval_to_string(
352 char_u *arg,
353 char_u **nextcmd,
354 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
Bram Moolenaar33570922005-01-25 22:26:29 +0000356 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000358 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000359#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar32e35112020-05-14 22:41:15 +0200363 if (eval0(arg, &tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 retval = NULL;
365 else
366 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000367 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 {
369 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000370 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200371 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200372 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200373 if (tv.vval.v_list->lv_len > 0)
374 ga_append(&ga, NL);
375 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000376 ga_append(&ga, NUL);
377 retval = (char_u *)ga.ga_data;
378 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000379#ifdef FEAT_FLOAT
380 else if (convert && tv.v_type == VAR_FLOAT)
381 {
382 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
383 retval = vim_strsave(numbuf);
384 }
385#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000386 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100387 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000388 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390
391 return retval;
392}
393
394/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000395 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200396 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399eval_to_string_safe(
400 char_u *arg,
401 char_u **nextcmd,
402 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
404 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200405 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000408 if (use_sandbox)
409 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200410 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000411 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000412 if (use_sandbox)
413 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200414 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200415 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return retval;
417}
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419/*
420 * Top level evaluation function, returning a number.
421 * Evaluates "expr" silently.
422 * Returns -1 for an error.
423 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200424 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100425eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200428 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000429 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431 ++emsg_off;
432
Bram Moolenaar32e35112020-05-14 22:41:15 +0200433 if (eval1(&p, &rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 retval = -1;
435 else
436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100437 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 }
440 --emsg_off;
441
442 return retval;
443}
444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000445/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000446 * Top level evaluation function.
447 * Returns an allocated typval_T with the result.
448 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000449 */
450 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000452{
453 typval_T *tv;
454
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200455 tv = ALLOC_ONE(typval_T);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200456 if (tv != NULL && eval0(arg, tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000458
459 return tv;
460}
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100463 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200464 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
465 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000466 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100469call_vim_function(
470 char_u *func,
471 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200472 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200473 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000475 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200476 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100478 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200479 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200480 funcexe.firstline = curwin->w_cursor.lnum;
481 funcexe.lastline = curwin->w_cursor.lnum;
482 funcexe.evaluate = TRUE;
483 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000484 if (ret == FAIL)
485 clear_tv(rettv);
486
487 return ret;
488}
489
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100490/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100491 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200493 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
494 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100495 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200496 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100497call_func_retnr(
498 char_u *func,
499 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200500 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501{
502 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200503 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100504
Bram Moolenaarded27a12018-08-01 19:06:03 +0200505 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506 return -1;
507
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100508 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100509 clear_tv(&rettv);
510 return retval;
511}
512
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000513/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100514 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000515 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200516 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
517 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000518 */
519 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100520call_func_retstr(
521 char_u *func,
522 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200523 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524{
525 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000526 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527
Bram Moolenaarded27a12018-08-01 19:06:03 +0200528 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 return NULL;
530
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100531 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return retval;
534}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000535
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000536/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100537 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200538 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
539 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000540 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000541 */
542 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100543call_func_retlist(
544 char_u *func,
545 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200546 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000547{
548 typval_T rettv;
549
Bram Moolenaarded27a12018-08-01 19:06:03 +0200550 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000551 return NULL;
552
553 if (rettv.v_type != VAR_LIST)
554 {
555 clear_tv(&rettv);
556 return NULL;
557 }
558
559 return rettv.vval.v_list;
560}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef FEAT_FOLDING
563/*
564 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
565 * it in "*cp". Doesn't give error messages.
566 */
567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
Bram Moolenaar33570922005-01-25 22:26:29 +0000570 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200571 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000573 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
574 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200579 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 *cp = NUL;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200581 if (eval0(arg, &tv, NULL, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = 0;
583 else
584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100585 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 if (tv.v_type == VAR_NUMBER)
587 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000588 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 retval = 0;
590 else
591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100592 // If the result is a string, check if there is a non-digit before
593 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (!VIM_ISDIGIT(*s) && *s != '-')
596 *cp = *s++;
597 retval = atol((char *)s);
598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000599 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 if (use_sandbox)
603 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200604 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200606 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607}
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000611 * Get an lval: variable, Dict item or List item that can be assigned a value
612 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
613 * "name.key", "name.key[expr]" etc.
614 * Indexing only works if "name" is an existing List or Dictionary.
615 * "name" points to the start of the name.
616 * If "rettv" is not NULL it points to the value to be assigned.
617 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
618 * wrong; must end in space or cmd separator.
619 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100620 * flags:
621 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100622 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100623 * GLV_NO_AUTOLOAD: do not use script autoloading
624 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000625 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000626 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000627 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200629 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630get_lval(
631 char_u *name,
632 typval_T *rettv,
633 lval_T *lp,
634 int unlet,
635 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100636 int flags, // GLV_ values
637 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000640 char_u *expr_start, *expr_end;
641 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 dictitem_T *v;
643 typval_T var1;
644 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000645 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000646 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000647 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000648 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000649 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100650 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000654
655 if (skip)
656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100657 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000658 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000659 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 }
661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000663 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000665 if (expr_start != NULL)
666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100667 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100668 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 && *p != '[' && *p != '.')
670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000672 return NULL;
673 }
674
675 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
676 if (lp->ll_exp_name == NULL)
677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100678 // Report an invalid expression in braces, unless the
679 // expression evaluation has been cancelled due to an
680 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000681 if (!aborting() && !quiet)
682 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000683 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100684 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000685 return NULL;
686 }
687 }
688 lp->ll_name = lp->ll_exp_name;
689 }
690 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000692 lp->ll_name = name;
693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
695 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100696 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 char_u *tp = skipwhite(p + 1);
698
699 // parse the type after the name
700 lp->ll_type = parse_type(&tp, &si->sn_type_list);
701 lp->ll_name_end = tp;
702 }
703 }
704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100705 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000706 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
707 return p;
708
709 cc = *p;
710 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100711 // Only pass &ht when we would write to the variable, it prevents autoload
712 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100713 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
714 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100716 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 if (v == NULL)
719 return NULL;
720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 /*
722 * Loop until no more [idx] or .key is following.
723 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100725 var1.v_type = VAR_UNKNOWN;
726 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000729 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
730 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100731 && lp->ll_tv->vval.v_dict != NULL)
732 && !(lp->ll_tv->v_type == VAR_BLOB
733 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000745
Bram Moolenaar8c711452005-01-14 21:53:12 +0000746 len = -1;
747 if (*p == '.')
748 {
749 key = p + 1;
750 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
751 ;
752 if (len == 0)
753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000757 }
758 p = key + len;
759 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000760 else
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000763 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000764 if (*p == ':')
765 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000766 else
767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000768 empty1 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200769 if (eval1(&p, &var1, EVAL_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100771 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000774 clear_tv(&var1);
775 return NULL;
776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 }
778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100779 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 if (*p == ':')
781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000782 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000788 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100789 if (rettv != NULL
790 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100791 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100792 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100793 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000799 }
800 p = skipwhite(p + 1);
801 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 else
804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200806 // recursive!
807 if (eval1(&p, &var2, EVAL_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000811 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100812 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100815 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816 clear_tv(&var2);
817 return NULL;
818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000819 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000822 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000824
Bram Moolenaar8c711452005-01-14 21:53:12 +0000825 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100829 clear_tv(&var1);
830 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 }
833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000835 ++p;
836 }
837
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000839 {
840 if (len == -1)
841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 // "[key]": get key from "var1"
843 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200844 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000848 }
849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000850 lp->ll_list = NULL;
851 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000852 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // When assigning to a scope dictionary check that a function and
855 // variable name is valid (only variable name unless it is l: or
856 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200857 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200858 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200859 int prevval;
860 int wrong;
861
862 if (len != -1)
863 {
864 prevval = key[len];
865 key[len] = NUL;
866 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200867 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200869 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
870 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200871 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200872 || !valid_varname(key);
873 if (len != -1)
874 key[len] = prevval;
875 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200876 {
877 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200878 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200879 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200880 }
881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000883 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100884 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200885 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100886 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100888 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100889 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200890 return NULL;
891 }
892
Bram Moolenaar31b81602019-02-10 22:14:27 +0100893 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100897 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100898 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000900 }
901 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000903 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100905 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000906 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 p = NULL;
908 break;
909 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100911 else if ((flags & GLV_READ_ONLY) == 0
912 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100913 {
914 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200915 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100916 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200917
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100918 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000920 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100921 else if (lp->ll_tv->v_type == VAR_BLOB)
922 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100923 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
924
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100925 /*
926 * Get the number and item for the only or first index of the List.
927 */
928 if (empty1)
929 lp->ll_n1 = 0;
930 else
931 // is number or string
932 lp->ll_n1 = (long)tv_get_number(&var1);
933 clear_tv(&var1);
934
935 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100936 || lp->ll_n1 > bloblen
937 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100938 {
939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100941 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100942 return NULL;
943 }
944 if (lp->ll_range && !lp->ll_empty2)
945 {
946 lp->ll_n2 = (long)tv_get_number(&var2);
947 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100948 if (lp->ll_n2 < 0
949 || lp->ll_n2 >= bloblen
950 || lp->ll_n2 < lp->ll_n1)
951 {
952 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100953 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100954 return NULL;
955 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100956 }
957 lp->ll_blob = lp->ll_tv->vval.v_blob;
958 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100959 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 else
962 {
963 /*
964 * Get the number and item for the only or first index of the List.
965 */
966 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100970 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
972
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000973 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 lp->ll_list = lp->ll_tv->vval.v_list;
975 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
976 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000978 if (lp->ll_n1 < 0)
979 {
980 lp->ll_n1 = 0;
981 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
982 }
983 }
984 if (lp->ll_li == NULL)
985 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100986 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200987 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100988 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 }
991
992 /*
993 * May need to find the item or absolute index for the second
994 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 * When no index given: "lp->ll_empty2" is TRUE.
996 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001000 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001001 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001007 {
1008 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001009 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 }
1014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001015 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 if (lp->ll_n1 < 0)
1017 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1018 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001019 {
1020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001023 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001024 }
1025
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001028 }
1029
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001030 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 return p;
1033}
1034
1035/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040{
1041 vim_free(lp->ll_exp_name);
1042 vim_free(lp->ll_newkey);
1043}
1044
1045/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001046 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001048 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1049 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001052set_var_lval(
1053 lval_T *lp,
1054 char_u *endp,
1055 typval_T *rettv,
1056 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001058 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059{
1060 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001061 listitem_T *ri;
1062 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063
1064 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001065 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001066 cc = *endp;
1067 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001068 if (lp->ll_blob != NULL)
1069 {
1070 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001071
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001072 if (op != NULL && *op != '=')
1073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001074 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001075 return;
1076 }
1077
1078 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1079 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 if (lp->ll_empty2)
1083 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1084
1085 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001087 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001088 return;
1089 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001090 if (lp->ll_empty2)
1091 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001092
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001093 ir = 0;
1094 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1095 blob_set(lp->ll_blob, il,
1096 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001097 }
1098 else
1099 {
1100 val = (int)tv_get_number_chk(rettv, &error);
1101 if (!error)
1102 {
1103 garray_T *gap = &lp->ll_blob->bv_ga;
1104
1105 // Allow for appending a byte. Setting a byte beyond
1106 // the end is an error otherwise.
1107 if (lp->ll_n1 < gap->ga_len
1108 || (lp->ll_n1 == gap->ga_len
1109 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1110 {
1111 blob_set(lp->ll_blob, lp->ll_n1, val);
1112 if (lp->ll_n1 == gap->ga_len)
1113 ++gap->ga_len;
1114 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001116 }
1117 }
1118 }
1119 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001121 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001124 {
1125 emsg(_(e_cannot_mod));
1126 *endp = cc;
1127 return;
1128 }
1129
Bram Moolenaarff697e62019-02-12 22:28:33 +01001130 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001131 di = NULL;
1132 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1133 &tv, &di, TRUE, FALSE) == OK)
1134 {
1135 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001136 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1137 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001138 && tv_op(&tv, rettv, op) == OK)
1139 set_var(lp->ll_name, &tv, FALSE);
1140 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001143 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001145 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001147 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001148 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001149 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001150 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 else if (lp->ll_range)
1152 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001153 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001154 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001157 {
1158 emsg(_("E996: Cannot lock a range"));
1159 return;
1160 }
1161
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001162 /*
1163 * Check whether any of the list items is locked
1164 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001165 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001166 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001167 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001168 return;
1169 ri = ri->li_next;
1170 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1171 break;
1172 ll_li = ll_li->li_next;
1173 ++ll_n1;
1174 }
1175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 /*
1177 * Assign the List values to the list items.
1178 */
1179 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001180 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001181 if (op != NULL && *op != '=')
1182 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1183 else
1184 {
1185 clear_tv(&lp->ll_li->li_tv);
1186 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1187 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 ri = ri->li_next;
1189 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1190 break;
1191 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001194 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001195 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 ri = NULL;
1197 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001198 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 lp->ll_li = lp->ll_li->li_next;
1201 ++lp->ll_n1;
1202 }
1203 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001205 else if (lp->ll_empty2
1206 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 }
1210 else
1211 {
1212 /*
1213 * Assign to a List or Dictionary item.
1214 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001216 {
1217 emsg(_("E996: Cannot lock a list or dict"));
1218 return;
1219 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 if (lp->ll_newkey != NULL)
1221 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 if (op != NULL && *op != '=')
1223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001225 return;
1226 }
1227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001229 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (di == NULL)
1231 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001232 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1233 {
1234 vim_free(di);
1235 return;
1236 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001238 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001239 else if (op != NULL && *op != '=')
1240 {
1241 tv_op(lp->ll_tv, rettv, op);
1242 return;
1243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 /*
1248 * Assign the value to the variable or list item.
1249 */
1250 if (copy)
1251 copy_tv(rettv, lp->ll_tv);
1252 else
1253 {
1254 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001255 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257 }
1258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259}
1260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001261/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001262 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1263 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001264 * Returns OK or FAIL.
1265 */
1266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001267tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001269 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270 char_u numbuf[NUMBUFLEN];
1271 char_u *s;
1272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001273 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001274 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001275 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 {
1277 switch (tv1->v_type)
1278 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001279 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001280 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001282 case VAR_DICT:
1283 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001284 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001285 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001286 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001287 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001288 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 break;
1290
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 case VAR_BLOB:
1292 if (*op != '+' || tv2->v_type != VAR_BLOB)
1293 break;
1294 // BLOB += BLOB
1295 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1296 {
1297 blob_T *b1 = tv1->vval.v_blob;
1298 blob_T *b2 = tv2->vval.v_blob;
1299 int i, len = blob_len(b2);
1300 for (i = 0; i < len; i++)
1301 ga_append(&b1->bv_ga, blob_get(b2, i));
1302 }
1303 return OK;
1304
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001305 case VAR_LIST:
1306 if (*op != '+' || tv2->v_type != VAR_LIST)
1307 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001308 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001309 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1310 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1311 return OK;
1312
1313 case VAR_NUMBER:
1314 case VAR_STRING:
1315 if (tv2->v_type == VAR_LIST)
1316 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001317 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001318 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001319 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001320 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001321#ifdef FEAT_FLOAT
1322 if (tv2->v_type == VAR_FLOAT)
1323 {
1324 float_T f = n;
1325
Bram Moolenaarff697e62019-02-12 22:28:33 +01001326 if (*op == '%')
1327 break;
1328 switch (*op)
1329 {
1330 case '+': f += tv2->vval.v_float; break;
1331 case '-': f -= tv2->vval.v_float; break;
1332 case '*': f *= tv2->vval.v_float; break;
1333 case '/': f /= tv2->vval.v_float; break;
1334 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001335 clear_tv(tv1);
1336 tv1->v_type = VAR_FLOAT;
1337 tv1->vval.v_float = f;
1338 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001340#endif
1341 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001342 switch (*op)
1343 {
1344 case '+': n += tv_get_number(tv2); break;
1345 case '-': n -= tv_get_number(tv2); break;
1346 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001347 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1348 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001350 clear_tv(tv1);
1351 tv1->v_type = VAR_NUMBER;
1352 tv1->vval.v_number = n;
1353 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 }
1355 else
1356 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001357 if (tv2->v_type == VAR_FLOAT)
1358 break;
1359
Bram Moolenaarff697e62019-02-12 22:28:33 +01001360 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001361 s = tv_get_string(tv1);
1362 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363 clear_tv(tv1);
1364 tv1->v_type = VAR_STRING;
1365 tv1->vval.v_string = s;
1366 }
1367 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001368
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001369 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001370#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001371 {
1372 float_T f;
1373
Bram Moolenaarff697e62019-02-12 22:28:33 +01001374 if (*op == '%' || *op == '.'
1375 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001376 && tv2->v_type != VAR_NUMBER
1377 && tv2->v_type != VAR_STRING))
1378 break;
1379 if (tv2->v_type == VAR_FLOAT)
1380 f = tv2->vval.v_float;
1381 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001382 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001383 switch (*op)
1384 {
1385 case '+': tv1->vval.v_float += f; break;
1386 case '-': tv1->vval.v_float -= f; break;
1387 case '*': tv1->vval.v_float *= f; break;
1388 case '/': tv1->vval.v_float /= f; break;
1389 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001390 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001391#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001392 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 }
1394 }
1395
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001396 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397 return FAIL;
1398}
1399
1400/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 * Evaluate the expression used in a ":for var in expr" command.
1402 * "arg" points to "var".
1403 * Set "*errp" to TRUE for an error, FALSE otherwise;
1404 * Return a pointer that holds the info. Null when there is an error.
1405 */
1406 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001407eval_for_line(
1408 char_u *arg,
1409 int *errp,
1410 char_u **nextcmdp,
1411 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 typval_T tv;
1416 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001418 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001420 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 if (fi == NULL)
1422 return NULL;
1423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425 if (expr == NULL)
1426 return fi;
1427
1428 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001429 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001432 return fi;
1433 }
1434
1435 if (skip)
1436 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001437 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, skip ? 0 : EVAL_EVALUATE)
1438 == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001439 {
1440 *errp = FALSE;
1441 if (!skip)
1442 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001444 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001445 l = tv.vval.v_list;
1446 if (l == NULL)
1447 {
1448 // a null list is like an empty list: do nothing
1449 clear_tv(&tv);
1450 }
1451 else
1452 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001454 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 // No need to increment the refcount, it's already set for
1457 // the list being used in "tv".
1458 fi->fi_list = l;
1459 list_add_watch(l, &fi->fi_lw);
1460 fi->fi_lw.lw_item = l->lv_first;
1461 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001462 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001463 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001464 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001465 fi->fi_bi = 0;
1466 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001468 typval_T btv;
1469
1470 // Make a copy, so that the iteration still works when the
1471 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001473 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001475 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001476 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 else
1478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001479 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 }
1482 }
1483 }
1484 if (skip)
1485 --emsg_skip;
1486
1487 return fi;
1488}
1489
1490/*
1491 * Use the first item in a ":for" list. Advance to the next.
1492 * Assign the values to the variable (list). "arg" points to the first one.
1493 * Return TRUE when a valid item was found, FALSE when at end of list or
1494 * something wrong.
1495 */
1496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001499 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001501 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1502 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001503 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001505 if (fi->fi_blob != NULL)
1506 {
1507 typval_T tv;
1508
1509 if (fi->fi_bi >= blob_len(fi->fi_blob))
1510 return FALSE;
1511 tv.v_type = VAR_NUMBER;
1512 tv.v_lock = VAR_FIXED;
1513 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1514 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001515 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001516 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001517 }
1518
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001519 item = fi->fi_lw.lw_item;
1520 if (item == NULL)
1521 result = FALSE;
1522 else
1523 {
1524 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001525 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001526 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 }
1528 return result;
1529}
1530
1531/*
1532 * Free the structure used to store info used by ":for".
1533 */
1534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536{
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001539 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001540 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001542 list_unref(fi->fi_list);
1543 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001544 if (fi != NULL && fi->fi_blob != NULL)
1545 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 vim_free(fi);
1547}
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001550set_context_for_expression(
1551 expand_T *xp,
1552 char_u *arg,
1553 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 int got_eq = FALSE;
1556 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001559 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 {
1561 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001564 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001565 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001566 {
1567 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001568 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001569 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 break;
1571 }
1572 return;
1573 }
1574 }
1575 else
1576 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1577 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 while ((xp->xp_pattern = vim_strpbrk(arg,
1579 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1580 {
1581 c = *xp->xp_pattern;
1582 if (c == '&')
1583 {
1584 c = xp->xp_pattern[1];
1585 if (c == '&')
1586 {
1587 ++xp->xp_pattern;
1588 xp->xp_context = cmdidx != CMD_let || got_eq
1589 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1590 }
1591 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001594 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1595 xp->xp_pattern += 2;
1596
1597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else if (c == '$')
1600 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001601 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 xp->xp_context = EXPAND_ENV_VARS;
1603 }
1604 else if (c == '=')
1605 {
1606 got_eq = TRUE;
1607 xp->xp_context = EXPAND_EXPRESSION;
1608 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001609 else if (c == '#'
1610 && xp->xp_context == EXPAND_EXPRESSION)
1611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001612 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001613 break;
1614 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001615 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 && xp->xp_context == EXPAND_FUNCTIONS
1617 && vim_strchr(xp->xp_pattern, '(') == NULL)
1618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001619 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 break;
1621 }
1622 else if (cmdidx != CMD_let || got_eq)
1623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001624 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1627 if (c == '\\' && xp->xp_pattern[1] != NUL)
1628 ++xp->xp_pattern;
1629 xp->xp_context = EXPAND_NOTHING;
1630 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1635 /* skip */ ;
1636 xp->xp_context = EXPAND_NOTHING;
1637 }
1638 else if (c == '|')
1639 {
1640 if (xp->xp_pattern[1] == '|')
1641 {
1642 ++xp->xp_pattern;
1643 xp->xp_context = EXPAND_EXPRESSION;
1644 }
1645 else
1646 xp->xp_context = EXPAND_COMMANDS;
1647 }
1648 else
1649 xp->xp_context = EXPAND_EXPRESSION;
1650 }
1651 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001652 // Doesn't look like something valid, expand as an expression
1653 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 arg = xp->xp_pattern;
1656 if (*arg != NUL)
1657 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1658 /* skip */ ;
1659 }
1660 xp->xp_pattern = arg;
1661}
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001664 * Return TRUE if "pat" matches "text".
1665 * Does not use 'cpo' and always uses 'magic'.
1666 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001667 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001668pattern_match(char_u *pat, char_u *text, int ic)
1669{
1670 int matches = FALSE;
1671 char_u *save_cpo;
1672 regmatch_T regmatch;
1673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001674 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001675 save_cpo = p_cpo;
1676 p_cpo = (char_u *)"";
1677 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1678 if (regmatch.regprog != NULL)
1679 {
1680 regmatch.rm_ic = ic;
1681 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1682 vim_regfree(regmatch.regprog);
1683 }
1684 p_cpo = save_cpo;
1685 return matches;
1686}
1687
1688/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001689 * Handle a name followed by "(". Both for just "name(arg)" and for
1690 * "expr->name(arg)".
1691 * Returns OK or FAIL.
1692 */
1693 static int
1694eval_func(
1695 char_u **arg, // points to "(", will be advanced
1696 char_u *name,
1697 int name_len,
1698 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001699 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001700 typval_T *basetv) // "expr" for "expr->name(arg)"
1701{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001702 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001703 char_u *s = name;
1704 int len = name_len;
1705 partial_T *partial;
1706 int ret = OK;
1707
1708 if (!evaluate)
1709 check_vars(s, len);
1710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 // If "s" is the name of a variable of type VAR_FUNC
1712 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001713 s = deref_func_name(s, &len, &partial, !evaluate);
1714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001715 // Need to make a copy, in case evaluating the arguments makes
1716 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001717 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001718 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001719 ret = FAIL;
1720 else
1721 {
1722 funcexe_T funcexe;
1723
1724 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001725 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001726 funcexe.firstline = curwin->w_cursor.lnum;
1727 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001728 funcexe.evaluate = evaluate;
1729 funcexe.partial = partial;
1730 funcexe.basetv = basetv;
1731 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1732 }
1733 vim_free(s);
1734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001735 // If evaluate is FALSE rettv->v_type was not set in
1736 // get_func_tv, but it's needed in handle_subscript() to parse
1737 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001738 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1739 {
1740 rettv->vval.v_string = NULL;
1741 rettv->v_type = VAR_FUNC;
1742 }
1743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Stop the expression evaluation when immediately
1745 // aborting on error, or when an interrupt occurred or
1746 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001747 if (evaluate && aborting())
1748 {
1749 if (ret == OK)
1750 clear_tv(rettv);
1751 ret = FAIL;
1752 }
1753 return ret;
1754}
1755
1756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1760 */
1761
1762/*
1763 * Handle zero level expression.
1764 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001766 * Note: "rettv.v_lock" is not set.
Bram Moolenaar32e35112020-05-14 22:41:15 +02001767 * "flags" has EVAL_EVALUATE and similar flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Return OK or FAIL.
1769 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771eval0(
1772 char_u *arg,
1773 typval_T *rettv,
1774 char_u **nextcmd,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001775 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 int ret;
1778 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001779 int did_emsg_before = did_emsg;
1780 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 p = skipwhite(arg);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001783 ret = eval1(&p, rettv, flags);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001784 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
1786 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 /*
1789 * Report the invalid expression unless the expression evaluation has
1790 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001791 * exception, or we already gave a more specific error.
1792 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001794 if (!aborting()
1795 && did_emsg == did_emsg_before
1796 && called_emsg == called_emsg_before
1797 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001798 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 ret = FAIL;
1800 }
1801 if (nextcmd != NULL)
1802 *nextcmd = check_nextcmd(p);
1803
1804 return ret;
1805}
1806
1807/*
1808 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001809 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 *
1811 * "arg" must point to the first non-white of the expression.
1812 * "arg" is advanced to the next non-white after the recognized expression.
1813 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001814 * Note: "rettv.v_lock" is not set.
1815 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 * Return OK or FAIL.
1817 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001818 int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001819eval1(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
1821 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 /*
1825 * Get the first variable.
1826 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001827 if (eval2(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 return FAIL;
1829
1830 if ((*arg)[0] == '?')
1831 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001832 int evaluate = flags & EVAL_EVALUATE;
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 result = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001835 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001837 int error = FALSE;
1838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001839 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001842 if (error)
1843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845
1846 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001847 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001850 if (eval1(arg, rettv, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 return FAIL;
1852
1853 /*
1854 * Check for the ":".
1855 */
1856 if ((*arg)[0] != ':')
1857 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 return FAIL;
1862 }
1863
1864 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001865 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 */
1867 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001868 if (eval1(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 return FAIL;
1873 }
1874 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877
1878 return OK;
1879}
1880
1881/*
1882 * Handle first level expression:
1883 * expr2 || expr2 || expr2 logical OR
1884 *
1885 * "arg" must point to the first non-white of the expression.
1886 * "arg" is advanced to the next non-white after the recognized expression.
1887 *
1888 * Return OK or FAIL.
1889 */
1890 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001891eval2(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 long result;
1895 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001896 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 /*
1899 * Get the first variable.
1900 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001901 if (eval3(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return FAIL;
1903
1904 /*
1905 * Repeat until there is no following "||".
1906 */
1907 first = TRUE;
1908 result = FALSE;
1909 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1910 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001911 int evaluate = flags & EVAL_EVALUATE;
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (evaluate && first)
1914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001918 if (error)
1919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 first = FALSE;
1921 }
1922
1923 /*
1924 * Get the second variable.
1925 */
1926 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001927 if (eval3(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE)
1928 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FAIL;
1930
1931 /*
1932 * Compute the result.
1933 */
1934 if (evaluate && !result)
1935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001936 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001939 if (error)
1940 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 if (evaluate)
1943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 rettv->v_type = VAR_NUMBER;
1945 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947 }
1948
1949 return OK;
1950}
1951
1952/*
1953 * Handle second level expression:
1954 * expr3 && expr3 && expr3 logical AND
1955 *
1956 * "arg" must point to the first non-white of the expression.
1957 * "arg" is advanced to the next non-white after the recognized expression.
1958 *
1959 * Return OK or FAIL.
1960 */
1961 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001962eval3(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 long result;
1966 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 /*
1970 * Get the first variable.
1971 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001972 if (eval4(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 return FAIL;
1974
1975 /*
1976 * Repeat until there is no following "&&".
1977 */
1978 first = TRUE;
1979 result = TRUE;
1980 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1981 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001982 int evaluate = flags & EVAL_EVALUATE;
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (evaluate && first)
1985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001986 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 if (error)
1990 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 first = FALSE;
1992 }
1993
1994 /*
1995 * Get the second variable.
1996 */
1997 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001998 if (eval4(arg, &var2, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return FAIL;
2000
2001 /*
2002 * Compute the result.
2003 */
2004 if (evaluate && result)
2005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002006 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 if (error)
2010 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 if (evaluate)
2013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 rettv->v_type = VAR_NUMBER;
2015 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 }
2018
2019 return OK;
2020}
2021
2022/*
2023 * Handle third level expression:
2024 * var1 == var2
2025 * var1 =~ var2
2026 * var1 != var2
2027 * var1 !~ var2
2028 * var1 > var2
2029 * var1 >= var2
2030 * var1 < var2
2031 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002032 * var1 is var2
2033 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 *
2035 * "arg" must point to the first non-white of the expression.
2036 * "arg" is advanced to the next non-white after the recognized expression.
2037 *
2038 * Return OK or FAIL.
2039 */
2040 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002041eval4(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
Bram Moolenaar33570922005-01-25 22:26:29 +00002043 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 char_u *p;
2045 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002046 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /*
2051 * Get the first variable.
2052 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002053 if (eval5(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 return FAIL;
2055
2056 p = *arg;
2057 switch (p[0])
2058 {
2059 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002060 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002062 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 break;
2064 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002065 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002067 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 break;
2069 case '>': if (p[1] != '=')
2070 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002071 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 len = 1;
2073 }
2074 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002075 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 break;
2077 case '<': if (p[1] != '=')
2078 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002079 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 len = 1;
2081 }
2082 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002083 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002085 case 'i': if (p[1] == 's')
2086 {
2087 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2088 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002089 i = p[len];
2090 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002091 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002092 }
2093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
2096 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002097 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002099 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002101 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (p[len] == '?')
2103 {
2104 ic = TRUE;
2105 ++len;
2106 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else if (p[len] == '#')
2109 {
2110 ic = FALSE;
2111 ++len;
2112 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002113 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
2115 ic = p_ic;
2116
2117 /*
2118 * Get the second variable.
2119 */
2120 *arg = skipwhite(p + len);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002121 if (eval5(arg, &var2, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return FAIL;
2125 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002126 if (flags & EVAL_EVALUATE)
Bram Moolenaar31988702018-02-13 12:57:42 +01002127 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002128 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002129
2130 clear_tv(&var2);
2131 return ret;
2132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134
2135 return OK;
2136}
2137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 void
2139eval_addblob(typval_T *tv1, typval_T *tv2)
2140{
2141 blob_T *b1 = tv1->vval.v_blob;
2142 blob_T *b2 = tv2->vval.v_blob;
2143 blob_T *b = blob_alloc();
2144 int i;
2145
2146 if (b != NULL)
2147 {
2148 for (i = 0; i < blob_len(b1); i++)
2149 ga_append(&b->bv_ga, blob_get(b1, i));
2150 for (i = 0; i < blob_len(b2); i++)
2151 ga_append(&b->bv_ga, blob_get(b2, i));
2152
2153 clear_tv(tv1);
2154 rettv_blob_set(tv1, b);
2155 }
2156}
2157
2158 int
2159eval_addlist(typval_T *tv1, typval_T *tv2)
2160{
2161 typval_T var3;
2162
2163 // concatenate Lists
2164 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2165 {
2166 clear_tv(tv1);
2167 clear_tv(tv2);
2168 return FAIL;
2169 }
2170 clear_tv(tv1);
2171 *tv1 = var3;
2172 return OK;
2173}
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175/*
2176 * Handle fourth level expression:
2177 * + number addition
2178 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002179 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002180 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 *
2182 * "arg" must point to the first non-white of the expression.
2183 * "arg" is advanced to the next non-white after the recognized expression.
2184 *
2185 * Return OK or FAIL.
2186 */
2187 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002188eval5(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
Bram Moolenaar33570922005-01-25 22:26:29 +00002190 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002192 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002193#ifdef FEAT_FLOAT
2194 float_T f1 = 0, f2 = 0;
2195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 char_u *s1, *s2;
2197 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2198 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002199 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 /*
2202 * Get the first variable.
2203 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002204 if (eval6(arg, rettv, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return FAIL;
2206
2207 /*
2208 * Repeat computing, until no '+', '-' or '.' is following.
2209 */
2210 for (;;)
2211 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002212 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002214 concat = op == '.'
2215 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2216 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 break;
2218
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002219 if ((op != '+' || (rettv->v_type != VAR_LIST
2220 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002221#ifdef FEAT_FLOAT
2222 && (op == '.' || rettv->v_type != VAR_FLOAT)
2223#endif
2224 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002226 // For "list + ...", an illegal use of the first operand as
2227 // a number cannot be determined before evaluating the 2nd
2228 // operand: if this is also a list, all is ok.
2229 // For "something . ...", "something - ..." or "non-list + ...",
2230 // we know that the first operand needs to be a string or number
2231 // without evaluating the 2nd operand. So check before to avoid
2232 // side effects after an error.
Bram Moolenaar32e35112020-05-14 22:41:15 +02002233 if ((flags & EVAL_EVALUATE) && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002234 {
2235 clear_tv(rettv);
2236 return FAIL;
2237 }
2238 }
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 /*
2241 * Get the second variable.
2242 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002243 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2244 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002246 if (eval6(arg, &var2, flags, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return FAIL;
2250 }
2251
Bram Moolenaar32e35112020-05-14 22:41:15 +02002252 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 /*
2255 * Compute the result.
2256 */
2257 if (op == '.')
2258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002259 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002260 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002261 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 {
2263 clear_tv(rettv);
2264 clear_tv(&var2);
2265 return FAIL;
2266 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 clear_tv(rettv);
2269 rettv->v_type = VAR_STRING;
2270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002272 else if (op == '+' && rettv->v_type == VAR_BLOB
2273 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002275 else if (op == '+' && rettv->v_type == VAR_LIST
2276 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002277 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002279 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else
2282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002283 int error = FALSE;
2284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002285#ifdef FEAT_FLOAT
2286 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002288 f1 = rettv->vval.v_float;
2289 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002291 else
2292#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002294 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002295 if (error)
2296 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002297 // This can only happen for "list + non-list". For
2298 // "non-list + ..." or "something - ...", we returned
2299 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002300 clear_tv(rettv);
2301 return FAIL;
2302 }
2303#ifdef FEAT_FLOAT
2304 if (var2.v_type == VAR_FLOAT)
2305 f1 = n1;
2306#endif
2307 }
2308#ifdef FEAT_FLOAT
2309 if (var2.v_type == VAR_FLOAT)
2310 {
2311 f2 = var2.vval.v_float;
2312 n2 = 0;
2313 }
2314 else
2315#endif
2316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002317 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002318 if (error)
2319 {
2320 clear_tv(rettv);
2321 clear_tv(&var2);
2322 return FAIL;
2323 }
2324#ifdef FEAT_FLOAT
2325 if (rettv->v_type == VAR_FLOAT)
2326 f2 = n2;
2327#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002330
2331#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002332 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002333 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2334 {
2335 if (op == '+')
2336 f1 = f1 + f2;
2337 else
2338 f1 = f1 - f2;
2339 rettv->v_type = VAR_FLOAT;
2340 rettv->vval.v_float = f1;
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002343#endif
2344 {
2345 if (op == '+')
2346 n1 = n1 + n2;
2347 else
2348 n1 = n1 - n2;
2349 rettv->v_type = VAR_NUMBER;
2350 rettv->vval.v_number = n1;
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355 }
2356 return OK;
2357}
2358
2359/*
2360 * Handle fifth level expression:
2361 * * number multiplication
2362 * / number division
2363 * % number modulo
2364 *
2365 * "arg" must point to the first non-white of the expression.
2366 * "arg" is advanced to the next non-white after the recognized expression.
2367 *
2368 * Return OK or FAIL.
2369 */
2370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002371eval6(
2372 char_u **arg,
2373 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002374 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002375 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
Bram Moolenaar33570922005-01-25 22:26:29 +00002377 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002379 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002380#ifdef FEAT_FLOAT
2381 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002382 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002383#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 /*
2387 * Get the first variable.
2388 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002389 if (eval7(arg, rettv, flags, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return FAIL;
2391
2392 /*
2393 * Repeat computing, until no '*', '/' or '%' is following.
2394 */
2395 for (;;)
2396 {
2397 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002398 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 break;
2400
Bram Moolenaar32e35112020-05-14 22:41:15 +02002401 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002403#ifdef FEAT_FLOAT
2404 if (rettv->v_type == VAR_FLOAT)
2405 {
2406 f1 = rettv->vval.v_float;
2407 use_float = TRUE;
2408 n1 = 0;
2409 }
2410 else
2411#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002412 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (error)
2415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 else
2418 n1 = 0;
2419
2420 /*
2421 * Get the second variable.
2422 */
2423 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002424 if (eval7(arg, &var2, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 return FAIL;
2426
Bram Moolenaar32e35112020-05-14 22:41:15 +02002427 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002429#ifdef FEAT_FLOAT
2430 if (var2.v_type == VAR_FLOAT)
2431 {
2432 if (!use_float)
2433 {
2434 f1 = n1;
2435 use_float = TRUE;
2436 }
2437 f2 = var2.vval.v_float;
2438 n2 = 0;
2439 }
2440 else
2441#endif
2442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002443 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444 clear_tv(&var2);
2445 if (error)
2446 return FAIL;
2447#ifdef FEAT_FLOAT
2448 if (use_float)
2449 f2 = n2;
2450#endif
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 /*
2454 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002455 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002457#ifdef FEAT_FLOAT
2458 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002460 if (op == '*')
2461 f1 = f1 * f2;
2462 else if (op == '/')
2463 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002464# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002466 if (f2 == 0.0)
2467 {
2468 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002469 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002470 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002471 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002472 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002473 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002474 }
2475 else
2476 f1 = f1 / f2;
2477# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002478 // We rely on the floating point library to handle divide
2479 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002480 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002481# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002484 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002486 return FAIL;
2487 }
2488 rettv->v_type = VAR_FLOAT;
2489 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002494 if (op == '*')
2495 n1 = n1 * n2;
2496 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002497 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002499 n1 = num_modulus(n1, n2);
2500
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002501 rettv->v_type = VAR_NUMBER;
2502 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
2505 }
2506
2507 return OK;
2508}
2509
2510/*
2511 * Handle sixth level expression:
2512 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002513 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002514 * "string" string constant
2515 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * &option-name option value
2517 * @r register contents
2518 * identifier variable value
2519 * function() function call
2520 * $VAR environment variable
2521 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002522 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002524 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002525 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 *
2527 * Also handle:
2528 * ! in front logical NOT
2529 * - in front unary minus
2530 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 * trailing [] subscript in String or List
2532 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002533 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 *
2535 * "arg" must point to the first non-white of the expression.
2536 * "arg" is advanced to the next non-white after the recognized expression.
2537 *
2538 * Return OK or FAIL.
2539 */
2540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541eval7(
2542 char_u **arg,
2543 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002544 int flags,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002547 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 int len;
2549 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 char_u *start_leader, *end_leader;
2551 int ret = OK;
2552 char_u *alias;
2553
2554 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002556 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002561 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 */
2563 start_leader = *arg;
2564 while (**arg == '!' || **arg == '-' || **arg == '+')
2565 *arg = skipwhite(*arg + 1);
2566 end_leader = *arg;
2567
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002568 if (**arg == '.' && (!isdigit(*(*arg + 1))
2569#ifdef FEAT_FLOAT
2570 || current_sctx.sc_version < 2
2571#endif
2572 ))
2573 {
2574 semsg(_(e_invexpr2), *arg);
2575 ++*arg;
2576 return FAIL;
2577 }
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 switch (**arg)
2580 {
2581 /*
2582 * Number constant.
2583 */
2584 case '0':
2585 case '1':
2586 case '2':
2587 case '3':
2588 case '4':
2589 case '5':
2590 case '6':
2591 case '7':
2592 case '8':
2593 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596
2597 /*
2598 * String constant: "string".
2599 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 break;
2602
2603 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002607 break;
2608
2609 /*
2610 * List: [expr, expr]
2611 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002612 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 break;
2614
2615 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002616 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002617 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002618 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002619 {
2620 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002621 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002622 }
2623 else
2624 ret = NOTDONE;
2625 break;
2626
2627 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002628 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002629 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002631 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2632 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002633 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 break;
2635
2636 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002637 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002639 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 break;
2641
2642 /*
2643 * Environment variable: $VAR.
2644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 break;
2647
2648 /*
2649 * Register contents: @r.
2650 */
2651 case '@': ++*arg;
2652 if (evaluate)
2653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002655 rettv->vval.v_string = get_reg_contents(**arg,
2656 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 if (**arg != NUL)
2659 ++*arg;
2660 break;
2661
2662 /*
2663 * nested expression: (expression).
2664 */
2665 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002666 ret = eval1(arg, rettv, flags); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (**arg == ')')
2668 ++*arg;
2669 else if (ret == OK)
2670 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 ret = FAIL;
2674 }
2675 break;
2676
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 break;
2679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680
2681 if (ret == NOTDONE)
2682 {
2683 /*
2684 * Must be a variable or function name.
2685 * Can also be a curly-braces kind of name: {expr}.
2686 */
2687 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002688 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (alias != NULL)
2690 s = alias;
2691
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002692 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 ret = FAIL;
2694 else
2695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002696 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002697 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002699 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002701 {
2702 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002706 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 *arg = skipwhite(*arg);
2710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002711 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2712 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002713 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002714 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002715 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 /*
2718 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2719 */
2720 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002721 ret = eval7_leader(rettv, start_leader, &end_leader);
2722 return ret;
2723}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002724
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002725/*
2726 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2727 * Adjusts "end_leaderp" until it is at "start_leader".
2728 */
2729 static int
2730eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2731{
2732 char_u *end_leader = *end_leaderp;
2733 int ret = OK;
2734 int error = FALSE;
2735 varnumber_T val = 0;
2736#ifdef FEAT_FLOAT
2737 float_T f = 0.0;
2738
2739 if (rettv->v_type == VAR_FLOAT)
2740 f = rettv->vval.v_float;
2741 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002743 val = tv_get_number_chk(rettv, &error);
2744 if (error)
2745 {
2746 clear_tv(rettv);
2747 ret = FAIL;
2748 }
2749 else
2750 {
2751 while (end_leader > start_leader)
2752 {
2753 --end_leader;
2754 if (*end_leader == '!')
2755 {
2756#ifdef FEAT_FLOAT
2757 if (rettv->v_type == VAR_FLOAT)
2758 f = !f;
2759 else
2760#endif
2761 val = !val;
2762 }
2763 else if (*end_leader == '-')
2764 {
2765#ifdef FEAT_FLOAT
2766 if (rettv->v_type == VAR_FLOAT)
2767 f = -f;
2768 else
2769#endif
2770 val = -val;
2771 }
2772 }
2773#ifdef FEAT_FLOAT
2774 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002777 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002780#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002782 clear_tv(rettv);
2783 rettv->v_type = VAR_NUMBER;
2784 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002787 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return ret;
2789}
2790
2791/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002792 * Call the function referred to in "rettv".
2793 */
2794 static int
2795call_func_rettv(
2796 char_u **arg,
2797 typval_T *rettv,
2798 int evaluate,
2799 dict_T *selfdict,
2800 typval_T *basetv)
2801{
2802 partial_T *pt = NULL;
2803 funcexe_T funcexe;
2804 typval_T functv;
2805 char_u *s;
2806 int ret;
2807
2808 // need to copy the funcref so that we can clear rettv
2809 if (evaluate)
2810 {
2811 functv = *rettv;
2812 rettv->v_type = VAR_UNKNOWN;
2813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002815 if (functv.v_type == VAR_PARTIAL)
2816 {
2817 pt = functv.vval.v_partial;
2818 s = partial_name(pt);
2819 }
2820 else
2821 s = functv.vval.v_string;
2822 }
2823 else
2824 s = (char_u *)"";
2825
Bram Moolenaara80faa82020-04-12 19:37:17 +02002826 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002827 funcexe.firstline = curwin->w_cursor.lnum;
2828 funcexe.lastline = curwin->w_cursor.lnum;
2829 funcexe.evaluate = evaluate;
2830 funcexe.partial = pt;
2831 funcexe.selfdict = selfdict;
2832 funcexe.basetv = basetv;
2833 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 // Clear the funcref afterwards, so that deleting it while
2836 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002837 if (evaluate)
2838 clear_tv(&functv);
2839
2840 return ret;
2841}
2842
2843/*
2844 * Evaluate "->method()".
2845 * "*arg" points to the '-'.
2846 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2847 */
2848 static int
2849eval_lambda(
2850 char_u **arg,
2851 typval_T *rettv,
2852 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002853 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002854{
2855 typval_T base = *rettv;
2856 int ret;
2857
2858 // Skip over the ->.
2859 *arg += 2;
2860 rettv->v_type = VAR_UNKNOWN;
2861
2862 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002863 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002864 return FAIL;
2865 else if (**arg != '(')
2866 {
2867 if (verbose)
2868 {
2869 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002870 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002871 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002873 }
2874 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002875 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002876 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002877 else
2878 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2879
2880 // Clear the funcref afterwards, so that deleting it while
2881 // evaluating the arguments is possible (see test55).
2882 if (evaluate)
2883 clear_tv(&base);
2884
2885 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002886}
2887
2888/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002889 * Evaluate "->method()".
2890 * "*arg" points to the '-'.
2891 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2892 */
2893 static int
2894eval_method(
2895 char_u **arg,
2896 typval_T *rettv,
2897 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002898 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002899{
2900 char_u *name;
2901 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002902 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002903 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002905
2906 // Skip over the ->.
2907 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002909
Bram Moolenaarac92e252019-08-03 21:58:38 +02002910 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002911 len = get_name_len(arg, &alias, evaluate, TRUE);
2912 if (alias != NULL)
2913 name = alias;
2914
2915 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002916 {
2917 if (verbose)
2918 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002919 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002920 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002921 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002923 if (**arg != '(')
2924 {
2925 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002927 ret = FAIL;
2928 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002929 else if (VIM_ISWHITE((*arg)[-1]))
2930 {
2931 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002932 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002933 ret = FAIL;
2934 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002935 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002936 ret = eval_func(arg, name, len, rettv,
2937 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002938 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002939
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002940 // Clear the funcref afterwards, so that deleting it while
2941 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002942 if (evaluate)
2943 clear_tv(&base);
2944
Bram Moolenaarac92e252019-08-03 21:58:38 +02002945 return ret;
2946}
2947
2948/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002949 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2950 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2952 */
2953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954eval_index(
2955 char_u **arg,
2956 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002957 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002959{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002960 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002961 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002962 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002964 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965 long len = -1;
2966 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002967 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002969
Bram Moolenaara03f2332016-02-06 18:09:59 +01002970 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002971 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002972 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002973 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002975 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 return FAIL;
2977 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002978#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002979 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002980 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002981 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002982#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002983 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002984 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002986 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002987 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002989 return FAIL;
2990 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002991 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002993 if (evaluate)
2994 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002995 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002996
2997 case VAR_STRING:
2998 case VAR_NUMBER:
2999 case VAR_LIST:
3000 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003001 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003002 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003003 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003005 init_tv(&var1);
3006 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003007 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003009 /*
3010 * dict.name
3011 */
3012 key = *arg + 1;
3013 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3014 ;
3015 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003016 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003017 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003018 }
3019 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021 /*
3022 * something[idx]
3023 *
3024 * Get the (first) variable from inside the [].
3025 */
3026 *arg = skipwhite(*arg + 1);
3027 if (**arg == ':')
3028 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003029 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003031 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 clear_tv(&var1);
3035 return FAIL;
3036 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003037
3038 /*
3039 * Get the second variable from inside the [:].
3040 */
3041 if (**arg == ':')
3042 {
3043 range = TRUE;
3044 *arg = skipwhite(*arg + 1);
3045 if (**arg == ']')
3046 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003047 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 if (!empty1)
3050 clear_tv(&var1);
3051 return FAIL;
3052 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003053 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 if (!empty1)
3057 clear_tv(&var1);
3058 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059 return FAIL;
3060 }
3061 }
3062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064 if (**arg != ']')
3065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003066 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003067 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 clear_tv(&var1);
3069 if (range)
3070 clear_tv(&var2);
3071 return FAIL;
3072 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003073 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003074 }
3075
3076 if (evaluate)
3077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078 n1 = 0;
3079 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003081 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003083 }
3084 if (range)
3085 {
3086 if (empty2)
3087 n2 = -1;
3088 else
3089 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003092 }
3093 }
3094
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003098 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003100 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003101 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003102 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003103 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003104 case VAR_SPECIAL:
3105 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003106 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003108
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003109 case VAR_NUMBER:
3110 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003112 len = (long)STRLEN(s);
3113 if (range)
3114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // The resulting variable is a substring. If the indexes
3116 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 if (n1 < 0)
3118 {
3119 n1 = len + n1;
3120 if (n1 < 0)
3121 n1 = 0;
3122 }
3123 if (n2 < 0)
3124 n2 = len + n2;
3125 else if (n2 >= len)
3126 n2 = len;
3127 if (n1 >= len || n2 < 0 || n1 > n2)
3128 s = NULL;
3129 else
3130 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3131 }
3132 else
3133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003134 // The resulting variable is a string of a single
3135 // character. If the index is too big or negative the
3136 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003137 if (n1 >= len || n1 < 0)
3138 s = NULL;
3139 else
3140 s = vim_strnsave(s + n1, 1);
3141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003142 clear_tv(rettv);
3143 rettv->v_type = VAR_STRING;
3144 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003145 break;
3146
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003147 case VAR_BLOB:
3148 len = blob_len(rettv->vval.v_blob);
3149 if (range)
3150 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003151 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003152 // are out of range the result is empty.
3153 if (n1 < 0)
3154 {
3155 n1 = len + n1;
3156 if (n1 < 0)
3157 n1 = 0;
3158 }
3159 if (n2 < 0)
3160 n2 = len + n2;
3161 else if (n2 >= len)
3162 n2 = len - 1;
3163 if (n1 >= len || n2 < 0 || n1 > n2)
3164 {
3165 clear_tv(rettv);
3166 rettv->v_type = VAR_BLOB;
3167 rettv->vval.v_blob = NULL;
3168 }
3169 else
3170 {
3171 blob_T *blob = blob_alloc();
3172
3173 if (blob != NULL)
3174 {
3175 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3176 {
3177 blob_free(blob);
3178 return FAIL;
3179 }
3180 blob->bv_ga.ga_len = n2 - n1 + 1;
3181 for (i = n1; i <= n2; i++)
3182 blob_set(blob, i - n1,
3183 blob_get(rettv->vval.v_blob, i));
3184
3185 clear_tv(rettv);
3186 rettv_blob_set(rettv, blob);
3187 }
3188 }
3189 }
3190 else
3191 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003192 // The resulting variable is a byte value.
3193 // If the index is too big or negative that is an error.
3194 if (n1 < 0)
3195 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003196 if (n1 < len && n1 >= 0)
3197 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003198 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003199
3200 clear_tv(rettv);
3201 rettv->v_type = VAR_NUMBER;
3202 rettv->vval.v_number = v;
3203 }
3204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003205 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003206 }
3207 break;
3208
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003209 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003211 if (n1 < 0)
3212 n1 = len + n1;
3213 if (!empty1 && (n1 < 0 || n1 >= len))
3214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003215 // For a range we allow invalid values and return an empty
3216 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003217 if (!range)
3218 {
3219 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003220 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003221 return FAIL;
3222 }
3223 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 }
3225 if (range)
3226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 list_T *l;
3228 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003229
3230 if (n2 < 0)
3231 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003232 else if (n2 >= len)
3233 n2 = len - 1;
3234 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003235 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 l = list_alloc();
3237 if (l == NULL)
3238 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 n1 <= n2; ++n1)
3241 {
3242 if (list_append_tv(l, &item->li_tv) == FAIL)
3243 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003244 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003245 return FAIL;
3246 }
3247 item = item->li_next;
3248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003249 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003250 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003251 }
3252 else
3253 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003254 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003255 clear_tv(rettv);
3256 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 }
3258 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003259
3260 case VAR_DICT:
3261 if (range)
3262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003263 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 if (len == -1)
3266 clear_tv(&var1);
3267 return FAIL;
3268 }
3269 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271
3272 if (len == -1)
3273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003274 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003275 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 clear_tv(&var1);
3278 return FAIL;
3279 }
3280 }
3281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003282 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003284 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003285 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 if (len == -1)
3287 clear_tv(&var1);
3288 if (item == NULL)
3289 return FAIL;
3290
3291 copy_tv(&item->di_tv, &var1);
3292 clear_tv(rettv);
3293 *rettv = var1;
3294 }
3295 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003296 }
3297 }
3298
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003299 return OK;
3300}
3301
3302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 * Get an option value.
3304 * "arg" points to the '&' or '+' before the option name.
3305 * "arg" is advanced to character after the option name.
3306 * Return OK or FAIL.
3307 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003309get_option_tv(
3310 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003311 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 char_u *option_end;
3315 long numval;
3316 char_u *stringval;
3317 int opt_type;
3318 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003319 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 int ret = OK;
3321 int opt_flags;
3322
3323 /*
3324 * Isolate the option name and find its value.
3325 */
3326 option_end = find_option_end(arg, &opt_flags);
3327 if (option_end == NULL)
3328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003329 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return FAIL;
3332 }
3333
3334 if (!evaluate)
3335 {
3336 *arg = option_end;
3337 return OK;
3338 }
3339
3340 c = *option_end;
3341 *option_end = NUL;
3342 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 ret = FAIL;
3350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003353 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 rettv->v_type = VAR_STRING;
3356 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 rettv->v_type = VAR_NUMBER;
3361 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003365 rettv->v_type = VAR_NUMBER;
3366 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 rettv->v_type = VAR_STRING;
3371 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
3373 }
3374 else if (working && (opt_type == -2 || opt_type == -1))
3375 ret = FAIL;
3376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003377 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 *arg = option_end;
3379
3380 return ret;
3381}
3382
3383/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3385 * Return OK or FAIL.
3386 */
3387 int
3388get_number_tv(
3389 char_u **arg,
3390 typval_T *rettv,
3391 int evaluate,
3392 int want_string UNUSED)
3393{
3394 int len;
3395#ifdef FEAT_FLOAT
3396 char_u *p;
3397 int get_float = FALSE;
3398
3399 // We accept a float when the format matches
3400 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3401 // strict to avoid backwards compatibility problems.
3402 // With script version 2 and later the leading digit can be
3403 // omitted.
3404 // Don't look for a float after the "." operator, so that
3405 // ":let vers = 1.2.3" doesn't fail.
3406 if (**arg == '.')
3407 p = *arg;
3408 else
3409 p = skipdigits(*arg + 1);
3410 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3411 {
3412 get_float = TRUE;
3413 p = skipdigits(p + 2);
3414 if (*p == 'e' || *p == 'E')
3415 {
3416 ++p;
3417 if (*p == '-' || *p == '+')
3418 ++p;
3419 if (!vim_isdigit(*p))
3420 get_float = FALSE;
3421 else
3422 p = skipdigits(p + 1);
3423 }
3424 if (ASCII_ISALPHA(*p) || *p == '.')
3425 get_float = FALSE;
3426 }
3427 if (get_float)
3428 {
3429 float_T f;
3430
3431 *arg += string2float(*arg, &f);
3432 if (evaluate)
3433 {
3434 rettv->v_type = VAR_FLOAT;
3435 rettv->vval.v_float = f;
3436 }
3437 }
3438 else
3439#endif
3440 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3441 {
3442 char_u *bp;
3443 blob_T *blob = NULL; // init for gcc
3444
3445 // Blob constant: 0z0123456789abcdef
3446 if (evaluate)
3447 blob = blob_alloc();
3448 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3449 {
3450 if (!vim_isxdigit(bp[1]))
3451 {
3452 if (blob != NULL)
3453 {
3454 emsg(_("E973: Blob literal should have an even number of hex characters"));
3455 ga_clear(&blob->bv_ga);
3456 VIM_CLEAR(blob);
3457 }
3458 return FAIL;
3459 }
3460 if (blob != NULL)
3461 ga_append(&blob->bv_ga,
3462 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3463 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3464 ++bp;
3465 }
3466 if (blob != NULL)
3467 rettv_blob_set(rettv, blob);
3468 *arg = bp;
3469 }
3470 else
3471 {
3472 varnumber_T n;
3473
3474 // decimal, hex or octal number
3475 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3476 ? STR2NR_NO_OCT + STR2NR_QUOTE
3477 : STR2NR_ALL, &n, NULL, 0, TRUE);
3478 if (len == 0)
3479 {
3480 semsg(_(e_invexpr2), *arg);
3481 return FAIL;
3482 }
3483 *arg += len;
3484 if (evaluate)
3485 {
3486 rettv->v_type = VAR_NUMBER;
3487 rettv->vval.v_number = n;
3488 }
3489 }
3490 return OK;
3491}
3492
3493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 * Allocate a variable for a string constant.
3495 * Return OK or FAIL.
3496 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003498get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 char_u *p;
3501 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 int extra = 0;
3503
3504 /*
3505 * Find the end of the string, skipping backslashed characters.
3506 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003507 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 if (*p == '\\' && p[1] != NUL)
3510 {
3511 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 // A "\<x>" form occupies at least 4 characters, and produces up
3513 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (*p == '<')
3515 extra += 2;
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518
3519 if (*p != '"')
3520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003521 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return FAIL;
3523 }
3524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003525 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (!evaluate)
3527 {
3528 *arg = p + 1;
3529 return OK;
3530 }
3531
3532 /*
3533 * Copy the string into allocated memory, handling backslashed
3534 * characters.
3535 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003536 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (name == NULL)
3538 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003539 rettv->v_type = VAR_STRING;
3540 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 if (*p == '\\')
3545 {
3546 switch (*++p)
3547 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 case 'b': *name++ = BS; ++p; break;
3549 case 'e': *name++ = ESC; ++p; break;
3550 case 'f': *name++ = FF; ++p; break;
3551 case 'n': *name++ = NL; ++p; break;
3552 case 'r': *name++ = CAR; ++p; break;
3553 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 case 'U':
3559 if (vim_isxdigit(p[1]))
3560 {
3561 int n, nr;
3562 int c = toupper(*p);
3563
3564 if (c == 'X')
3565 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003566 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003568 else
3569 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 nr = 0;
3571 while (--n >= 0 && vim_isxdigit(p[1]))
3572 {
3573 ++p;
3574 nr = (nr << 4) + hex2nr(*p);
3575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // For "\u" store the number according to
3578 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
3585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003586 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 case '0':
3588 case '1':
3589 case '2':
3590 case '3':
3591 case '4':
3592 case '5':
3593 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 case '7': *name = *p++ - '0';
3595 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 *name = (*name << 3) + *p++ - '0';
3598 if (*p >= '0' && *p <= '7')
3599 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 break;
3603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003604 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003605 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3606 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (extra != 0)
3608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003609 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 break;
3611 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003612 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 break;
3616 }
3617 }
3618 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003619 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003623 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003624 ++p;
3625 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return OK;
3628}
3629
3630/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Return OK or FAIL.
3633 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
3637 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003638 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003639 int reduce = 0;
3640
3641 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003643 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003644 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003648 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003649 break;
3650 ++reduce;
3651 ++p;
3652 }
3653 }
3654
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003657 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003658 return FAIL;
3659 }
3660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003661 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003662 if (!evaluate)
3663 {
3664 *arg = p + 1;
3665 return OK;
3666 }
3667
3668 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003670 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003671 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003672 if (str == NULL)
3673 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 rettv->v_type = VAR_STRING;
3675 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003676
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003682 break;
3683 ++p;
3684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003685 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003687 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003688 *arg = p + 1;
3689
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003690 return OK;
3691}
3692
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003694 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003695 */
3696 char_u *
3697partial_name(partial_T *pt)
3698{
3699 if (pt->pt_name != NULL)
3700 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003701 if (pt->pt_func != NULL)
3702 return pt->pt_func->uf_name;
3703 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003704}
3705
Bram Moolenaarddecc252016-04-06 22:59:37 +02003706 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003707partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003708{
3709 int i;
3710
3711 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003712 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003713 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003714 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003715 if (pt->pt_name != NULL)
3716 {
3717 func_unref(pt->pt_name);
3718 vim_free(pt->pt_name);
3719 }
3720 else
3721 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003722
3723 if (pt->pt_funcstack != NULL)
3724 {
3725 // Decrease the reference count for the context of a closure. If down
3726 // to zero free it and clear the variables on the stack.
3727 if (--pt->pt_funcstack->fs_refcount == 0)
3728 {
3729 garray_T *gap = &pt->pt_funcstack->fs_ga;
3730 typval_T *stack = gap->ga_data;
3731
3732 for (i = 0; i < gap->ga_len; ++i)
3733 clear_tv(stack + i);
3734 ga_clear(gap);
3735 vim_free(pt->pt_funcstack);
3736 }
3737 pt->pt_funcstack = NULL;
3738 }
3739
Bram Moolenaarddecc252016-04-06 22:59:37 +02003740 vim_free(pt);
3741}
3742
3743/*
3744 * Unreference a closure: decrement the reference count and free it when it
3745 * becomes zero.
3746 */
3747 void
3748partial_unref(partial_T *pt)
3749{
3750 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003751 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003752}
3753
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003754static int tv_equal_recurse_limit;
3755
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003756 static int
3757func_equal(
3758 typval_T *tv1,
3759 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003761{
3762 char_u *s1, *s2;
3763 dict_T *d1, *d2;
3764 int a1, a2;
3765 int i;
3766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003768 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003769 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003770 if (s1 != NULL && *s1 == NUL)
3771 s1 = NULL;
3772 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003774 if (s2 != NULL && *s2 == NUL)
3775 s2 = NULL;
3776 if (s1 == NULL || s2 == NULL)
3777 {
3778 if (s1 != s2)
3779 return FALSE;
3780 }
3781 else if (STRCMP(s1, s2) != 0)
3782 return FALSE;
3783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003784 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003785 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3786 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3787 if (d1 == NULL || d2 == NULL)
3788 {
3789 if (d1 != d2)
3790 return FALSE;
3791 }
3792 else if (!dict_equal(d1, d2, ic, TRUE))
3793 return FALSE;
3794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003795 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003796 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3797 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3798 if (a1 != a2)
3799 return FALSE;
3800 for (i = 0; i < a1; ++i)
3801 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3802 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3803 return FALSE;
3804
3805 return TRUE;
3806}
3807
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003808/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003809 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003810 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003811 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003812 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814tv_equal(
3815 typval_T *tv1,
3816 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 int ic, // ignore case
3818 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003819{
3820 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003822 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003823 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 // Catch lists and dicts that have an endless loop by limiting
3826 // recursiveness to a limit. We guess they are equal then.
3827 // A fixed limit has the problem of still taking an awful long time.
3828 // Reduce the limit every time running into it. That should work fine for
3829 // deeply linked structures that are not recursively linked and catch
3830 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003831 if (!recursive)
3832 tv_equal_recurse_limit = 1000;
3833 if (recursive_cnt >= tv_equal_recurse_limit)
3834 {
3835 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003836 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003837 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003839 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3840 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003841 if ((tv1->v_type == VAR_FUNC
3842 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3843 && (tv2->v_type == VAR_FUNC
3844 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3845 {
3846 ++recursive_cnt;
3847 r = func_equal(tv1, tv2, ic);
3848 --recursive_cnt;
3849 return r;
3850 }
3851
3852 if (tv1->v_type != tv2->v_type)
3853 return FALSE;
3854
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003855 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003856 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003857 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003858 ++recursive_cnt;
3859 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3860 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003861 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003862
3863 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003864 ++recursive_cnt;
3865 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3866 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003867 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003868
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003869 case VAR_BLOB:
3870 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3871
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003872 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 case VAR_BOOL:
3874 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003875 return tv1->vval.v_number == tv2->vval.v_number;
3876
3877 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003878 s1 = tv_get_string_buf(tv1, buf1);
3879 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003880 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003881
Bram Moolenaar835dc632016-02-07 14:27:38 +01003882 case VAR_FLOAT:
3883#ifdef FEAT_FLOAT
3884 return tv1->vval.v_float == tv2->vval.v_float;
3885#endif
3886 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003887#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003888 return tv1->vval.v_job == tv2->vval.v_job;
3889#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003890 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003891#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003892 return tv1->vval.v_channel == tv2->vval.v_channel;
3893#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003895 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003896 return tv1->vval.v_partial == tv2->vval.v_partial;
3897
3898 case VAR_FUNC:
3899 return tv1->vval.v_string == tv2->vval.v_string;
3900
Bram Moolenaar835dc632016-02-07 14:27:38 +01003901 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003902 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003904 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003905 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003907 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3908 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003909 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003910}
3911
3912/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003913 * Return the next (unique) copy ID.
3914 * Used for serializing nested structures.
3915 */
3916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003917get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003918{
3919 current_copyID += COPYID_INC;
3920 return current_copyID;
3921}
3922
3923/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003924 * Garbage collection for lists and dictionaries.
3925 *
3926 * We use reference counts to be able to free most items right away when they
3927 * are no longer used. But for composite items it's possible that it becomes
3928 * unused while the reference count is > 0: When there is a recursive
3929 * reference. Example:
3930 * :let l = [1, 2, 3]
3931 * :let d = {9: l}
3932 * :let l[1] = d
3933 *
3934 * Since this is quite unusual we handle this with garbage collection: every
3935 * once in a while find out which lists and dicts are not referenced from any
3936 * variable.
3937 *
3938 * Here is a good reference text about garbage collection (refers to Python
3939 * but it applies to all reference-counting mechanisms):
3940 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003941 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003942
3943/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003944 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003945 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003946 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003947 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003948 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003949garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003950{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003951 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003952 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003953 buf_T *buf;
3954 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003955 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003956 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003958 if (!testing)
3959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003960 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003961 want_garbage_collect = FALSE;
3962 may_garbage_collect = FALSE;
3963 garbage_collect_at_exit = FALSE;
3964 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003965
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003966 // The execution stack can grow big, limit the size.
3967 if (exestack.ga_maxlen - exestack.ga_len > 500)
3968 {
3969 size_t new_len;
3970 char_u *pp;
3971 int n;
3972
3973 // Keep 150% of the current size, with a minimum of the growth size.
3974 n = exestack.ga_len / 2;
3975 if (n < exestack.ga_growsize)
3976 n = exestack.ga_growsize;
3977
3978 // Don't make it bigger though.
3979 if (exestack.ga_len + n < exestack.ga_maxlen)
3980 {
3981 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3982 pp = vim_realloc(exestack.ga_data, new_len);
3983 if (pp == NULL)
3984 return FAIL;
3985 exestack.ga_maxlen = exestack.ga_len + n;
3986 exestack.ga_data = pp;
3987 }
3988 }
3989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003990 // We advance by two because we add one for items referenced through
3991 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003992 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003993
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003994 /*
3995 * 1. Go through all accessible variables and mark all lists and dicts
3996 * with copyID.
3997 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // Don't free variables in the previous_funccal list unless they are only
4000 // referenced through previous_funccal. This must be first, because if
4001 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004004 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004005 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004008 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004009 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4010 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004012 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004014 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4015 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004016 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4018 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004019#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004020 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004021 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4022 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004023 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004024 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004025 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4026 NULL, NULL);
4027#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004030 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4032 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004034 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004036 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004039 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004040 abort = abort || set_ref_in_functions(copyID);
4041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004042 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004046 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004047
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004048 // callbacks in buffers
4049 abort = abort || set_ref_in_buffers(copyID);
4050
Bram Moolenaar1dced572012-04-05 16:54:08 +02004051#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004052 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004053#endif
4054
Bram Moolenaardb913952012-06-29 12:54:53 +02004055#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004057#endif
4058
4059#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004061#endif
4062
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004063#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004064 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004065 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004066#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004067#ifdef FEAT_NETBEANS_INTG
4068 abort = abort || set_ref_in_nb_channel(copyID);
4069#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004070
Bram Moolenaare3188e22016-05-31 21:13:04 +02004071#ifdef FEAT_TIMERS
4072 abort = abort || set_ref_in_timer(copyID);
4073#endif
4074
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004075#ifdef FEAT_QUICKFIX
4076 abort = abort || set_ref_in_quickfix(copyID);
4077#endif
4078
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004079#ifdef FEAT_TERMINAL
4080 abort = abort || set_ref_in_term(copyID);
4081#endif
4082
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004083#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004084 abort = abort || set_ref_in_popups(copyID);
4085#endif
4086
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004088 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004089 /*
4090 * 2. Free lists and dictionaries that are not referenced.
4091 */
4092 did_free = free_unref_items(copyID);
4093
4094 /*
4095 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004097 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004099 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004100 else if (p_verbose > 0)
4101 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004102 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004103 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004104
4105 return did_free;
4106}
4107
4108/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004109 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004110 */
4111 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004112free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004113{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004114 int did_free = FALSE;
4115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004116 // Let all "free" functions know that we are here. This means no
4117 // dictionaries, lists, channels or jobs are to be freed, because we will
4118 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004119 in_free_unref_items = TRUE;
4120
4121 /*
4122 * PASS 1: free the contents of the items. We don't free the items
4123 * themselves yet, so that it is possible to decrement refcount counters
4124 */
4125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004126 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004127 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004129 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004130 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131
4132#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004133 // Go through the list of jobs and free items without the copyID. This
4134 // must happen before doing channels, because jobs refer to channels, but
4135 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004136 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4140#endif
4141
4142 /*
4143 * PASS 2: free the items themselves.
4144 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004145 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004146 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004147
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004148#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004149 // Go through the list of jobs and free items without the copyID. This
4150 // must happen before doing channels, because jobs refer to channels, but
4151 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004152 free_unused_jobs(copyID, COPYID_MASK);
4153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004155 free_unused_channels(copyID, COPYID_MASK);
4156#endif
4157
4158 in_free_unref_items = FALSE;
4159
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004160 return did_free;
4161}
4162
4163/*
4164 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 * "list_stack" is used to add lists to be marked. Can be NULL.
4166 *
4167 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004168 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004169 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004171{
4172 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004173 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004174 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004175 hashtab_T *cur_ht;
4176 ht_stack_T *ht_stack = NULL;
4177 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004178
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004179 cur_ht = ht;
4180 for (;;)
4181 {
4182 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004184 // Mark each item in the hashtab. If the item contains a hashtab
4185 // it is added to ht_stack, if it contains a list it is added to
4186 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004187 todo = (int)cur_ht->ht_used;
4188 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4189 if (!HASHITEM_EMPTY(hi))
4190 {
4191 --todo;
4192 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4193 &ht_stack, list_stack);
4194 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004195 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004196
4197 if (ht_stack == NULL)
4198 break;
4199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004200 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004201 cur_ht = ht_stack->ht;
4202 tempitem = ht_stack;
4203 ht_stack = ht_stack->prev;
4204 free(tempitem);
4205 }
4206
4207 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004208}
4209
4210/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004211 * Mark a dict and its items with "copyID".
4212 * Returns TRUE if setting references failed somehow.
4213 */
4214 int
4215set_ref_in_dict(dict_T *d, int copyID)
4216{
4217 if (d != NULL && d->dv_copyID != copyID)
4218 {
4219 d->dv_copyID = copyID;
4220 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4221 }
4222 return FALSE;
4223}
4224
4225/*
4226 * Mark a list and its items with "copyID".
4227 * Returns TRUE if setting references failed somehow.
4228 */
4229 int
4230set_ref_in_list(list_T *ll, int copyID)
4231{
4232 if (ll != NULL && ll->lv_copyID != copyID)
4233 {
4234 ll->lv_copyID = copyID;
4235 return set_ref_in_list_items(ll, copyID, NULL);
4236 }
4237 return FALSE;
4238}
4239
4240/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004241 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004242 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4243 *
4244 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004245 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004246 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004247set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004248{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004249 listitem_T *li;
4250 int abort = FALSE;
4251 list_T *cur_l;
4252 list_stack_T *list_stack = NULL;
4253 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004254
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004255 cur_l = l;
4256 for (;;)
4257 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004258 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004259 // Mark each item in the list. If the item contains a hashtab
4260 // it is added to ht_stack, if it contains a list it is added to
4261 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004262 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4263 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4264 ht_stack, &list_stack);
4265 if (list_stack == NULL)
4266 break;
4267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004268 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004269 cur_l = list_stack->list;
4270 tempitem = list_stack;
4271 list_stack = list_stack->prev;
4272 free(tempitem);
4273 }
4274
4275 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004276}
4277
4278/*
4279 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 * "list_stack" is used to add lists to be marked. Can be NULL.
4281 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4282 *
4283 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004284 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004286set_ref_in_item(
4287 typval_T *tv,
4288 int copyID,
4289 ht_stack_T **ht_stack,
4290 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004291{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004292 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004293
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004294 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004295 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004296 dict_T *dd = tv->vval.v_dict;
4297
Bram Moolenaara03f2332016-02-06 18:09:59 +01004298 if (dd != NULL && dd->dv_copyID != copyID)
4299 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004300 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004301 dd->dv_copyID = copyID;
4302 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004303 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004304 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4305 }
4306 else
4307 {
4308 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4309 if (newitem == NULL)
4310 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004311 else
4312 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004313 newitem->ht = &dd->dv_hashtab;
4314 newitem->prev = *ht_stack;
4315 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004316 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004317 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004318 }
4319 }
4320 else if (tv->v_type == VAR_LIST)
4321 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 list_T *ll = tv->vval.v_list;
4323
Bram Moolenaara03f2332016-02-06 18:09:59 +01004324 if (ll != NULL && ll->lv_copyID != copyID)
4325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004326 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004327 ll->lv_copyID = copyID;
4328 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004329 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004330 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004331 }
4332 else
4333 {
4334 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004335 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004336 if (newitem == NULL)
4337 abort = TRUE;
4338 else
4339 {
4340 newitem->list = ll;
4341 newitem->prev = *list_stack;
4342 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004344 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004345 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004346 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004347 else if (tv->v_type == VAR_FUNC)
4348 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004349 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004350 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004351 else if (tv->v_type == VAR_PARTIAL)
4352 {
4353 partial_T *pt = tv->vval.v_partial;
4354 int i;
4355
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004356 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004357 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004358 // Didn't see this partial yet.
4359 pt->pt_copyID = copyID;
4360
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004361 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004362
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004363 if (pt->pt_dict != NULL)
4364 {
4365 typval_T dtv;
4366
4367 dtv.v_type = VAR_DICT;
4368 dtv.vval.v_dict = pt->pt_dict;
4369 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4370 }
4371
4372 for (i = 0; i < pt->pt_argc; ++i)
4373 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4374 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004375 if (pt->pt_funcstack != NULL)
4376 {
4377 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4378
4379 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4380 abort = abort || set_ref_in_item(stack + i, copyID,
4381 ht_stack, list_stack);
4382 }
4383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384 }
4385 }
4386#ifdef FEAT_JOB_CHANNEL
4387 else if (tv->v_type == VAR_JOB)
4388 {
4389 job_T *job = tv->vval.v_job;
4390 typval_T dtv;
4391
4392 if (job != NULL && job->jv_copyID != copyID)
4393 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004394 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 if (job->jv_channel != NULL)
4396 {
4397 dtv.v_type = VAR_CHANNEL;
4398 dtv.vval.v_channel = job->jv_channel;
4399 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4400 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004401 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004402 {
4403 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004404 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004405 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4406 }
4407 }
4408 }
4409 else if (tv->v_type == VAR_CHANNEL)
4410 {
4411 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004412 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 typval_T dtv;
4414 jsonq_T *jq;
4415 cbq_T *cq;
4416
4417 if (ch != NULL && ch->ch_copyID != copyID)
4418 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004419 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004420 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004421 {
4422 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4423 jq = jq->jq_next)
4424 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4425 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4426 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004427 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 {
4429 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004430 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004431 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4432 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004433 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 {
4435 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004436 dtv.vval.v_partial =
4437 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004438 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4439 }
4440 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004441 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004442 {
4443 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004444 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004445 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4446 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004447 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004448 {
4449 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004450 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004451 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4452 }
4453 }
4454 }
4455#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004456 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004457}
4458
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 * Return a string with the string representation of a variable.
4461 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004463 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004464 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4465 * stings as "string()", otherwise does not put quotes around strings, as
4466 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004467 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4468 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004469 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004471 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004472echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473 typval_T *tv,
4474 char_u **tofree,
4475 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004476 int copyID,
4477 int echo_style,
4478 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004479 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004481 static int recurse = 0;
4482 char_u *r = NULL;
4483
Bram Moolenaar33570922005-01-25 22:26:29 +00004484 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004485 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004486 if (!did_echo_string_emsg)
4487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // Only give this message once for a recursive call to avoid
4489 // flooding the user with errors. And stop iterating over lists
4490 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004491 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004492 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004494 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004495 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004496 }
4497 ++recurse;
4498
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499 switch (tv->v_type)
4500 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004501 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004502 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004503 {
4504 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004505 r = tv->vval.v_string;
4506 if (r == NULL)
4507 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004508 }
4509 else
4510 {
4511 *tofree = string_quote(tv->vval.v_string, FALSE);
4512 r = *tofree;
4513 }
4514 break;
4515
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004517 if (echo_style)
4518 {
4519 *tofree = NULL;
4520 r = tv->vval.v_string;
4521 }
4522 else
4523 {
4524 *tofree = string_quote(tv->vval.v_string, TRUE);
4525 r = *tofree;
4526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004527 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004528
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004529 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004530 {
4531 partial_T *pt = tv->vval.v_partial;
4532 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004533 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004534 garray_T ga;
4535 int i;
4536 char_u *tf;
4537
4538 ga_init2(&ga, 1, 100);
4539 ga_concat(&ga, (char_u *)"function(");
4540 if (fname != NULL)
4541 {
4542 ga_concat(&ga, fname);
4543 vim_free(fname);
4544 }
4545 if (pt != NULL && pt->pt_argc > 0)
4546 {
4547 ga_concat(&ga, (char_u *)", [");
4548 for (i = 0; i < pt->pt_argc; ++i)
4549 {
4550 if (i > 0)
4551 ga_concat(&ga, (char_u *)", ");
4552 ga_concat(&ga,
4553 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4554 vim_free(tf);
4555 }
4556 ga_concat(&ga, (char_u *)"]");
4557 }
4558 if (pt != NULL && pt->pt_dict != NULL)
4559 {
4560 typval_T dtv;
4561
4562 ga_concat(&ga, (char_u *)", ");
4563 dtv.v_type = VAR_DICT;
4564 dtv.vval.v_dict = pt->pt_dict;
4565 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4566 vim_free(tf);
4567 }
4568 ga_concat(&ga, (char_u *)")");
4569
4570 *tofree = ga.ga_data;
4571 r = *tofree;
4572 break;
4573 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004574
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004575 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004576 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004577 break;
4578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004580 if (tv->vval.v_list == NULL)
4581 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004582 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004583 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004584 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004585 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004586 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4587 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004588 {
4589 *tofree = NULL;
4590 r = (char_u *)"[...]";
4591 }
4592 else
4593 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004594 int old_copyID = tv->vval.v_list->lv_copyID;
4595
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004596 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004597 *tofree = list2string(tv, copyID, restore_copyID);
4598 if (restore_copyID)
4599 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004600 r = *tofree;
4601 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004602 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004603
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004605 if (tv->vval.v_dict == NULL)
4606 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004607 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004608 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004609 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004610 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004611 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4612 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004613 {
4614 *tofree = NULL;
4615 r = (char_u *)"{...}";
4616 }
4617 else
4618 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004619 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004620
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004621 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004622 *tofree = dict2string(tv, copyID, restore_copyID);
4623 if (restore_copyID)
4624 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004625 r = *tofree;
4626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004630 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004631 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004633 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004634 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004635 break;
4636
Bram Moolenaar835dc632016-02-07 14:27:38 +01004637 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004638 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004639 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004640 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004641 if (composite_val)
4642 {
4643 *tofree = string_quote(r, FALSE);
4644 r = *tofree;
4645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004647
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004648 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004649#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004650 *tofree = NULL;
4651 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4652 r = numbuf;
4653 break;
4654#endif
4655
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004656 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004657 case VAR_SPECIAL:
4658 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004659 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004660 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004662
Bram Moolenaar8502c702014-06-17 12:51:16 +02004663 if (--recurse == 0)
4664 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004665 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004666}
4667
4668/*
4669 * Return a string with the string representation of a variable.
4670 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4671 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004672 * Does not put quotes around strings, as ":echo" displays values.
4673 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4674 * May return NULL.
4675 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004676 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677echo_string(
4678 typval_T *tv,
4679 char_u **tofree,
4680 char_u *numbuf,
4681 int copyID)
4682{
4683 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4684}
4685
4686/*
4687 * Return a string with the string representation of a variable.
4688 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4689 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004690 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004691 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004692 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004693 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004694tv2string(
4695 typval_T *tv,
4696 char_u **tofree,
4697 char_u *numbuf,
4698 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004699{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004700 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701}
4702
4703/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004704 * Return string "str" in ' quotes, doubling ' characters.
4705 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004707 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004709string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710{
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004712 char_u *p, *r, *s;
4713
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 len = (function ? 13 : 3);
4715 if (str != NULL)
4716 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004717 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004718 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 if (*p == '\'')
4720 ++len;
4721 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004722 s = r = alloc(len);
4723 if (r != NULL)
4724 {
4725 if (function)
4726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004728 r += 10;
4729 }
4730 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004732 if (str != NULL)
4733 for (p = str; *p != NUL; )
4734 {
4735 if (*p == '\'')
4736 *r++ = '\'';
4737 MB_COPY_CHAR(p, r);
4738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004740 if (function)
4741 *r++ = ')';
4742 *r++ = NUL;
4743 }
4744 return s;
4745}
4746
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004747#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748/*
4749 * Convert the string "text" to a floating point number.
4750 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4751 * this always uses a decimal point.
4752 * Returns the length of the text that was consumed.
4753 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755string2float(
4756 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004757 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758{
4759 char *s = (char *)text;
4760 float_T f;
4761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004763 if (STRNICMP(text, "inf", 3) == 0)
4764 {
4765 *value = INFINITY;
4766 return 3;
4767 }
4768 if (STRNICMP(text, "-inf", 3) == 0)
4769 {
4770 *value = -INFINITY;
4771 return 4;
4772 }
4773 if (STRNICMP(text, "nan", 3) == 0)
4774 {
4775 *value = NAN;
4776 return 3;
4777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778 f = strtod(s, &s);
4779 *value = f;
4780 return (int)((char_u *)s - text);
4781}
4782#endif
4783
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 * Get the value of an environment variable.
4786 * "arg" is pointing to the '$'. It is advanced to after the name.
4787 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004788 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004791get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 char_u *string = NULL;
4794 int len;
4795 int cc;
4796 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004797 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 ++*arg;
4800 name = *arg;
4801 len = get_env_len(arg);
4802 if (evaluate)
4803 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004804 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004806
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004807 cc = name[len];
4808 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004810 string = vim_getenv(name, &mustfree);
4811 if (string != NULL && *string != NUL)
4812 {
4813 if (!mustfree)
4814 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004816 else
4817 {
4818 if (mustfree)
4819 vim_free(string);
4820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004822 string = expand_env_save(name - 1);
4823 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004824 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004825 }
4826 name[len] = cc;
4827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 rettv->v_type = VAR_STRING;
4829 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831
4832 return OK;
4833}
4834
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004837 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004840var2fpos(
4841 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004842 int dollar_lnum, // TRUE when $ is last line
4843 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004845 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004847 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004850 if (varp->v_type == VAR_LIST)
4851 {
4852 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004853 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004854 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004855 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004856
4857 l = varp->vval.v_list;
4858 if (l == NULL)
4859 return NULL;
4860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004861 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004862 pos.lnum = list_find_nr(l, 0L, &error);
4863 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004867 pos.col = list_find_nr(l, 1L, &error);
4868 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004869 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004870 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004873 li = list_find(l, 1L);
4874 if (li != NULL && li->li_tv.v_type == VAR_STRING
4875 && li->li_tv.vval.v_string != NULL
4876 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4877 pos.col = len + 1;
4878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004880 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004882 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004885 pos.coladd = list_find_nr(l, 2L, &error);
4886 if (error)
4887 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004888
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004889 return &pos;
4890 }
4891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004892 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004893 if (name == NULL)
4894 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004895 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004897 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004898 {
4899 if (VIsual_active)
4900 return &VIsual;
4901 return &curwin->w_cursor;
4902 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004905 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4907 return NULL;
4908 return pp;
4909 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004910
Bram Moolenaara5525202006-03-02 22:52:09 +00004911 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004912
Bram Moolenaar477933c2007-07-17 14:32:23 +00004913 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004914 {
4915 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004917 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004918 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 // In silent Ex mode topline is zero, but that's not a valid line
4920 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004921 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004922 return &pos;
4923 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004925 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004926 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004928 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004929 return &pos;
4930 }
4931 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004934 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
4936 pos.lnum = curbuf->b_ml.ml_line_count;
4937 pos.col = 0;
4938 }
4939 else
4940 {
4941 pos.lnum = curwin->w_cursor.lnum;
4942 pos.col = (colnr_T)STRLEN(ml_get_curline());
4943 }
4944 return &pos;
4945 }
4946 return NULL;
4947}
4948
4949/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004950 * Convert list in "arg" into a position and optional file number.
4951 * When "fnump" is NULL there is no file number, only 3 items.
4952 * Note that the column is passed on as-is, the caller may want to decrement
4953 * it to use 1 for the first column.
4954 * Return FAIL when conversion is not possible, doesn't check the position for
4955 * validity.
4956 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004958list2fpos(
4959 typval_T *arg,
4960 pos_T *posp,
4961 int *fnump,
4962 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004963{
4964 list_T *l = arg->vval.v_list;
4965 long i = 0;
4966 long n;
4967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4969 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004970 if (arg->v_type != VAR_LIST
4971 || l == NULL
4972 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004973 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004974 return FAIL;
4975
4976 if (fnump != NULL)
4977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004979 if (n < 0)
4980 return FAIL;
4981 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004983 *fnump = n;
4984 }
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004987 if (n < 0)
4988 return FAIL;
4989 posp->lnum = n;
4990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004992 if (n < 0)
4993 return FAIL;
4994 posp->col = n;
4995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004997 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004998 posp->coladd = 0;
4999 else
5000 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005001
Bram Moolenaar493c1782014-05-28 14:34:46 +02005002 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005004
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005005 return OK;
5006}
5007
5008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * Get the length of an environment variable name.
5010 * Advance "arg" to the first character after the name.
5011 * Return 0 for error.
5012 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 char_u *p;
5017 int len;
5018
5019 for (p = *arg; vim_isIDc(*p); ++p)
5020 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return 0;
5023
5024 len = (int)(p - *arg);
5025 *arg = p;
5026 return len;
5027}
5028
5029/*
5030 * Get the length of the name of a function or internal variable.
5031 * "arg" is advanced to the first non-white character after the name.
5032 * Return 0 if something is wrong.
5033 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037 char_u *p;
5038 int len;
5039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005042 {
5043 if (*p == ':')
5044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // "s:" is start of "s:var", but "n:" is not and can be used in
5046 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005047 len = (int)(p - *arg);
5048 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5049 || len > 1)
5050 break;
5051 }
5052 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 return 0;
5055
5056 len = (int)(p - *arg);
5057 *arg = skipwhite(p);
5058
5059 return len;
5060}
5061
5062/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005063 * Get the length of the name of a variable or function.
5064 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005066 * Return -1 if curly braces expansion failed.
5067 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * If the name contains 'magic' {}'s, expand them and return the
5069 * expanded name in an allocated string via 'alias' - caller must free.
5070 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005072get_name_len(
5073 char_u **arg,
5074 char_u **alias,
5075 int evaluate,
5076 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *p;
5080 char_u *expr_start;
5081 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5086 && (*arg)[2] == (int)KE_SNR)
5087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 *arg += 3;
5090 return get_id_len(arg) + 3;
5091 }
5092 len = eval_fname_script(*arg);
5093 if (len > 0)
5094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 *arg += len;
5097 }
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005102 p = find_name_end(*arg, &expr_start, &expr_end,
5103 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (expr_start != NULL)
5105 {
5106 char_u *temp_string;
5107
5108 if (!evaluate)
5109 {
5110 len += (int)(p - *arg);
5111 *arg = skipwhite(p);
5112 return len;
5113 }
5114
5115 /*
5116 * Include any <SID> etc in the expanded string:
5117 * Thus the -len here.
5118 */
5119 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5120 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005121 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 *alias = temp_string;
5123 *arg = skipwhite(p);
5124 return (int)STRLEN(temp_string);
5125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005128 // Only give an error when there is something, otherwise it will be
5129 // reported at a higher level.
5130 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005131 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 return len;
5134}
5135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136/*
5137 * Find the end of a variable or function name, taking care of magic braces.
5138 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5139 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005140 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 * Return a pointer to just after the name. Equal to "arg" if there is no
5142 * valid name.
5143 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005144 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005145find_name_end(
5146 char_u *arg,
5147 char_u **expr_start,
5148 char_u **expr_end,
5149 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 int mb_nest = 0;
5152 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005154 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005155 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 *expr_start = NULL;
5160 *expr_end = NULL;
5161 }
5162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005163 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005164 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5165 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005166 return arg;
5167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 for (p = arg; *p != NUL
5169 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005170 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005171 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005173 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005175 if (*p == '\'')
5176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005177 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005178 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005179 ;
5180 if (*p == NUL)
5181 break;
5182 }
5183 else if (*p == '"')
5184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005186 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005187 if (*p == '\\' && p[1] != NUL)
5188 ++p;
5189 if (*p == NUL)
5190 break;
5191 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005192 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005194 // "s:" is start of "s:var", but "n:" is not and can be used in
5195 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005196 len = (int)(p - arg);
5197 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005198 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005199 break;
5200 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 if (*p == '[')
5205 ++br_nest;
5206 else if (*p == ']')
5207 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005209
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005210 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 if (*p == '{')
5213 {
5214 mb_nest++;
5215 if (expr_start != NULL && *expr_start == NULL)
5216 *expr_start = p;
5217 }
5218 else if (*p == '}')
5219 {
5220 mb_nest--;
5221 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5222 *expr_end = p;
5223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226
5227 return p;
5228}
5229
5230/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005231 * Expands out the 'magic' {}'s in a variable/function name.
5232 * Note that this can call itself recursively, to deal with
5233 * constructs like foo{bar}{baz}{bam}
5234 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5235 * "in_start" ^
5236 * "expr_start" ^
5237 * "expr_end" ^
5238 * "in_end" ^
5239 *
5240 * Returns a new allocated string, which the caller must free.
5241 * Returns NULL for failure.
5242 */
5243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005244make_expanded_name(
5245 char_u *in_start,
5246 char_u *expr_start,
5247 char_u *expr_end,
5248 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249{
5250 char_u c1;
5251 char_u *retval = NULL;
5252 char_u *temp_result;
5253 char_u *nextcmd = NULL;
5254
5255 if (expr_end == NULL || in_end == NULL)
5256 return NULL;
5257 *expr_start = NUL;
5258 *expr_end = NUL;
5259 c1 = *in_end;
5260 *in_end = NUL;
5261
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005262 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 if (temp_result != NULL && nextcmd == NULL)
5264 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005265 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5266 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 if (retval != NULL)
5268 {
5269 STRCPY(retval, in_start);
5270 STRCAT(retval, temp_result);
5271 STRCAT(retval, expr_end + 1);
5272 }
5273 }
5274 vim_free(temp_result);
5275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 *expr_start = '{';
5278 *expr_end = '}';
5279
5280 if (retval != NULL)
5281 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005282 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 if (expr_start != NULL)
5284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 temp_result = make_expanded_name(retval, expr_start,
5287 expr_end, temp_result);
5288 vim_free(retval);
5289 retval = temp_result;
5290 }
5291 }
5292
5293 return retval;
5294}
5295
5296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005298 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005301eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005303 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5304}
5305
5306/*
5307 * Return TRUE if character "c" can be used as the first character in a
5308 * variable or function name (excluding '{' and '}').
5309 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005311eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005312{
5313 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314}
5315
5316/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005317 * Handle:
5318 * - expr[expr], expr[expr:expr] subscript
5319 * - ".name" lookup
5320 * - function call with Funcref variable: func(expr)
5321 * - method call: var->method()
5322 *
5323 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326handle_subscript(
5327 char_u **arg,
5328 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005329 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005330 int verbose, // give error messages
5331 char_u *start_leader, // start of '!' and '-' prefixes
5332 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333{
Bram Moolenaar32e35112020-05-14 22:41:15 +02005334 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005335 int ret = OK;
5336 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337
Bram Moolenaar61343f02019-07-20 21:11:13 +02005338 // "." is ".name" lookup when we found a dict or when evaluating and
5339 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005341 && (((**arg == '['
5342 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005343 || (!evaluate
5344 && (*arg)[1] != '.'
5345 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005346 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005347 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005348 && !VIM_ISWHITE(*(*arg - 1)))
5349 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005350 {
5351 if (**arg == '(')
5352 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005353 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005354
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005355 // Stop the expression evaluation when immediately aborting on
5356 // error, or when an interrupt occurred or an exception was thrown
5357 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005358 if (aborting())
5359 {
5360 if (ret == OK)
5361 clear_tv(rettv);
5362 ret = FAIL;
5363 }
5364 dict_unref(selfdict);
5365 selfdict = NULL;
5366 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005367 else if (**arg == '-')
5368 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005369 // Expression "-1.0->method()" applies the leader "-" before
5370 // applying ->.
5371 if (evaluate && *end_leaderp > start_leader)
5372 ret = eval7_leader(rettv, start_leader, end_leaderp);
5373 if (ret == OK)
5374 {
5375 if ((*arg)[2] == '{')
5376 // expr->{lambda}()
5377 ret = eval_lambda(arg, rettv, evaluate, verbose);
5378 else
5379 // expr->name()
5380 ret = eval_method(arg, rettv, evaluate, verbose);
5381 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005382 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005384 {
5385 dict_unref(selfdict);
5386 if (rettv->v_type == VAR_DICT)
5387 {
5388 selfdict = rettv->vval.v_dict;
5389 if (selfdict != NULL)
5390 ++selfdict->dv_refcount;
5391 }
5392 else
5393 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02005394 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005395 {
5396 clear_tv(rettv);
5397 ret = FAIL;
5398 }
5399 }
5400 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5403 // Don't do this when "Func" is already a partial that was bound
5404 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005405 if (selfdict != NULL
5406 && (rettv->v_type == VAR_FUNC
5407 || (rettv->v_type == VAR_PARTIAL
5408 && (rettv->vval.v_partial->pt_auto
5409 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005412 dict_unref(selfdict);
5413 return ret;
5414}
5415
5416/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005417 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 * value).
5419 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005420 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005421alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005423 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424}
5425
5426/*
5427 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 * The string "s" must have been allocated, it is consumed.
5429 * Return NULL for out of memory, the variable otherwise.
5430 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005431 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
Bram Moolenaar33570922005-01-25 22:26:29 +00005434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 rettv = alloc_tv();
5437 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 rettv->v_type = VAR_STRING;
5440 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 else
5443 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445}
5446
5447/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 if (varp != NULL)
5454 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 switch (varp->v_type)
5456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005458 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005460 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 vim_free(varp->vval.v_string);
5462 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005463 case VAR_PARTIAL:
5464 partial_unref(varp->vval.v_partial);
5465 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005466 case VAR_BLOB:
5467 blob_unref(varp->vval.v_blob);
5468 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 case VAR_LIST:
5470 list_unref(varp->vval.v_list);
5471 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005472 case VAR_DICT:
5473 dict_unref(varp->vval.v_dict);
5474 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005477 job_unref(varp->vval.v_job);
5478 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005479#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005480 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005481#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005482 channel_unref(varp->vval.v_channel);
5483 break;
5484#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_NUMBER:
5486 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005487 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005488 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005489 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005490 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005491 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005492 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 vim_free(varp);
5495 }
5496}
5497
5498/*
5499 * Free the memory for a variable value and set the value to NULL or 0.
5500 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005502clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 if (varp != NULL)
5505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005511 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005512 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005514 case VAR_PARTIAL:
5515 partial_unref(varp->vval.v_partial);
5516 varp->vval.v_partial = NULL;
5517 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005518 case VAR_BLOB:
5519 blob_unref(varp->vval.v_blob);
5520 varp->vval.v_blob = NULL;
5521 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 case VAR_LIST:
5523 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005524 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 case VAR_DICT:
5527 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005528 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005531 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005532 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 varp->vval.v_number = 0;
5534 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005535 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005536#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005537 varp->vval.v_float = 0.0;
5538 break;
5539#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005540 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005541#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005542 job_unref(varp->vval.v_job);
5543 varp->vval.v_job = NULL;
5544#endif
5545 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005546 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005547#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005548 channel_unref(varp->vval.v_channel);
5549 varp->vval.v_channel = NULL;
5550#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005552 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005556 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558}
5559
5560/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 * Set the value of a variable to NULL without freeing items.
5562 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005564init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565{
5566 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005567 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568}
5569
5570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 * Get the number value of a variable.
5572 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005573 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005574 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005575 * caller of incompatible types: it sets *denote to TRUE if "denote"
5576 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005578 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005579tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 int error = FALSE;
5582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005584}
5585
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005586 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005587tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005588{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005589 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005594 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005595 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005596#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005597 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005598 break;
5599#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005601 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 break;
5604 case VAR_STRING:
5605 if (varp->vval.v_string != NULL)
5606 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005607 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005608 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005609 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005610 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005612 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005614 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005615 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005616 case VAR_SPECIAL:
5617 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005618 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005619#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005620 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005621 break;
5622#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005623 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005624#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005625 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005626 break;
5627#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005628 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005629 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005630 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005631 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005632 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005634 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005638 n = -1;
5639 else
5640 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005644#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005646tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005647{
5648 switch (varp->v_type)
5649 {
5650 case VAR_NUMBER:
5651 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005652 case VAR_FLOAT:
5653 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005654 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005655 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005656 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005657 break;
5658 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005659 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005660 break;
5661 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005662 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005663 break;
5664 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005666 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005667 case VAR_BOOL:
5668 emsg(_("E362: Using a boolean value as a Float"));
5669 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005670 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005671 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005672 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005673 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005674# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005675 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005676 break;
5677# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005678 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005679# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005680 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005681 break;
5682# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005683 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005684 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005685 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005686 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005687 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005689 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005690 break;
5691 }
5692 return 0;
5693}
5694#endif
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 * Get the string value of a variable.
5698 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005699 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5700 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * If the String variable has never been set, return an empty string.
5702 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005703 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005704 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005706 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005707tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 static char_u mybuf[NUMBUFLEN];
5710
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005711 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712}
5713
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005714 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005717 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005718
5719 return res != NULL ? res : (char_u *)"";
5720}
5721
Bram Moolenaar7d647822014-04-05 21:28:56 +02005722/*
5723 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5724 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005725 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005726tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005727{
5728 static char_u mybuf[NUMBUFLEN];
5729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005730 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005731}
5732
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005733 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005735{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005739 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005740 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 return buf;
5742 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005743 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005744 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 break;
5746 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005747 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 break;
5749 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005750 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005752 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005753#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005754 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005755 break;
5756#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 case VAR_STRING:
5758 if (varp->vval.v_string != NULL)
5759 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005760 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005761 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005762 case VAR_SPECIAL:
5763 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5764 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005765 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005766 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005767 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770 {
5771 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005772 char *status;
5773
5774 if (job == NULL)
5775 return (char_u *)"no process";
5776 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005777 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005778 : "run";
5779# ifdef UNIX
5780 vim_snprintf((char *)buf, NUMBUFLEN,
5781 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005782# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005783 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005784 "process %ld %s",
5785 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005786 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005788 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005789 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5790# endif
5791 return buf;
5792 }
5793#endif
5794 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005795 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005796#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005797 {
5798 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005799 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005800
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005801 if (channel == NULL)
5802 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5803 else
5804 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005805 "channel %d %s", channel->ch_id, status);
5806 return buf;
5807 }
5808#endif
5809 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005810 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005811 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005812 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005813 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005816 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005820 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5821 * string() on Dict, List, etc.
5822 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005823 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005824tv_stringify(typval_T *varp, char_u *buf)
5825{
5826 if (varp->v_type == VAR_LIST
5827 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005828 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005829 || varp->v_type == VAR_FUNC
5830 || varp->v_type == VAR_PARTIAL
5831 || varp->v_type == VAR_FLOAT)
5832 {
5833 typval_T tmp;
5834
5835 f_string(varp, &tmp);
5836 tv_get_string_buf(&tmp, buf);
5837 clear_tv(varp);
5838 *varp = tmp;
5839 return tmp.vval.v_string;
5840 }
5841 return tv_get_string_buf(varp, buf);
5842}
5843
5844/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005845 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5846 * Also give an error message, using "name" or _("name") when use_gettext is
5847 * TRUE.
5848 */
5849 static int
5850tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5851{
5852 int lock = 0;
5853
5854 switch (tv->v_type)
5855 {
5856 case VAR_BLOB:
5857 if (tv->vval.v_blob != NULL)
5858 lock = tv->vval.v_blob->bv_lock;
5859 break;
5860 case VAR_LIST:
5861 if (tv->vval.v_list != NULL)
5862 lock = tv->vval.v_list->lv_lock;
5863 break;
5864 case VAR_DICT:
5865 if (tv->vval.v_dict != NULL)
5866 lock = tv->vval.v_dict->dv_lock;
5867 break;
5868 default:
5869 break;
5870 }
5871 return var_check_lock(tv->v_lock, name, use_gettext)
5872 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5873}
5874
5875/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005878 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005879 * It is OK for "from" and "to" to point to the same item. This is used to
5880 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005883copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005886 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 switch (from->v_type)
5888 {
5889 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005890 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005891 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 to->vval.v_number = from->vval.v_number;
5893 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005896 to->vval.v_float = from->vval.v_float;
5897 break;
5898#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005899 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005901 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005902 if (to->vval.v_job != NULL)
5903 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904 break;
5905#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005906 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005907#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005908 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005909 if (to->vval.v_channel != NULL)
5910 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005911 break;
5912#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 case VAR_STRING:
5914 case VAR_FUNC:
5915 if (from->vval.v_string == NULL)
5916 to->vval.v_string = NULL;
5917 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005920 if (from->v_type == VAR_FUNC)
5921 func_ref(to->vval.v_string);
5922 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005924 case VAR_PARTIAL:
5925 if (from->vval.v_partial == NULL)
5926 to->vval.v_partial = NULL;
5927 else
5928 {
5929 to->vval.v_partial = from->vval.v_partial;
5930 ++to->vval.v_partial->pt_refcount;
5931 }
5932 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005933 case VAR_BLOB:
5934 if (from->vval.v_blob == NULL)
5935 to->vval.v_blob = NULL;
5936 else
5937 {
5938 to->vval.v_blob = from->vval.v_blob;
5939 ++to->vval.v_blob->bv_refcount;
5940 }
5941 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 case VAR_LIST:
5943 if (from->vval.v_list == NULL)
5944 to->vval.v_list = NULL;
5945 else
5946 {
5947 to->vval.v_list = from->vval.v_list;
5948 ++to->vval.v_list->lv_refcount;
5949 }
5950 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 case VAR_DICT:
5952 if (from->vval.v_dict == NULL)
5953 to->vval.v_dict = NULL;
5954 else
5955 {
5956 to->vval.v_dict = from->vval.v_dict;
5957 ++to->vval.v_dict->dv_refcount;
5958 }
5959 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005960 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005961 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005963 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 break;
5965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966}
5967
5968/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 * Make a copy of an item.
5970 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005971 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5972 * reference to an already copied list/dict can be used.
5973 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976item_copy(
5977 typval_T *from,
5978 typval_T *to,
5979 int deep,
5980 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005981{
5982 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005987 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005988 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 }
5990 ++recurse;
5991
5992 switch (from->v_type)
5993 {
5994 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005995 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996 case VAR_STRING:
5997 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005998 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005999 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006000 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006001 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006002 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003 copy_tv(from, to);
6004 break;
6005 case VAR_LIST:
6006 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 if (from->vval.v_list == NULL)
6009 to->vval.v_list = NULL;
6010 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 to->vval.v_list = from->vval.v_list->lv_copylist;
6014 ++to->vval.v_list->lv_refcount;
6015 }
6016 else
6017 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6018 if (to->vval.v_list == NULL)
6019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006021 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006023 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024 case VAR_DICT:
6025 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006026 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006027 if (from->vval.v_dict == NULL)
6028 to->vval.v_dict = NULL;
6029 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006031 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006032 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6033 ++to->vval.v_dict->dv_refcount;
6034 }
6035 else
6036 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6037 if (to->vval.v_dict == NULL)
6038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006039 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006040 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006041 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006043 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 }
6046 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048}
6049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 void
6051echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6052{
6053 char_u *tofree;
6054 char_u numbuf[NUMBUFLEN];
6055 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6056
6057 if (*atstart)
6058 {
6059 *atstart = FALSE;
6060 // Call msg_start() after eval1(), evaluating the expression
6061 // may cause a message to appear.
6062 if (with_space)
6063 {
6064 // Mark the saved text as finishing the line, so that what
6065 // follows is displayed on a new line when scrolling back
6066 // at the more prompt.
6067 msg_sb_eol();
6068 msg_start();
6069 }
6070 }
6071 else if (with_space)
6072 msg_puts_attr(" ", echo_attr);
6073
6074 if (p != NULL)
6075 for ( ; *p != NUL && !got_int; ++p)
6076 {
6077 if (*p == '\n' || *p == '\r' || *p == TAB)
6078 {
6079 if (*p != TAB && *needclr)
6080 {
6081 // remove any text still there from the command
6082 msg_clr_eos();
6083 *needclr = FALSE;
6084 }
6085 msg_putchar_attr(*p, echo_attr);
6086 }
6087 else
6088 {
6089 if (has_mbyte)
6090 {
6091 int i = (*mb_ptr2len)(p);
6092
6093 (void)msg_outtrans_len_attr(p, i, echo_attr);
6094 p += i - 1;
6095 }
6096 else
6097 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6098 }
6099 }
6100 vim_free(tofree);
6101}
6102
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * ":echo expr1 ..." print each argument separated with a space, add a
6105 * newline at the end.
6106 * ":echon expr1 ..." print each argument plain.
6107 */
6108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
6111 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 char_u *p;
6114 int needclr = TRUE;
6115 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006116 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006117 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119 if (eap->skip)
6120 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006121 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006123 // If eval1() causes an error message the text from the command may
6124 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006125 need_clr_eos = needclr;
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02006128 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
6130 /*
6131 * Report the invalid expression unless the expression evaluation
6132 * has been cancelled due to an aborting error, an interrupt, or an
6133 * exception.
6134 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006135 if (!aborting() && did_emsg == did_emsg_before
6136 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006137 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006138 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 break;
6140 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006141 need_clr_eos = FALSE;
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 arg = skipwhite(arg);
6148 }
6149 eap->nextcmd = check_nextcmd(arg);
6150
6151 if (eap->skip)
6152 --emsg_skip;
6153 else
6154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006155 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (needclr)
6157 msg_clr_eos();
6158 if (eap->cmdidx == CMD_echo)
6159 msg_end();
6160 }
6161}
6162
6163/*
6164 * ":echohl {name}".
6165 */
6166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006169 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170}
6171
6172/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006173 * Returns the :echo attribute
6174 */
6175 int
6176get_echo_attr(void)
6177{
6178 return echo_attr;
6179}
6180
6181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 * ":execute expr1 ..." execute the result of an expression.
6183 * ":echomsg expr1 ..." Print a message
6184 * ":echoerr expr1 ..." Print an error
6185 * Each gets spaces around each argument and a newline at the end for
6186 * echo commands
6187 */
6188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 int ret = OK;
6194 char_u *p;
6195 garray_T ga;
6196 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
6198 ga_init2(&ga, 1, 80);
6199
6200 if (eap->skip)
6201 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006202 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006204 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6205 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
6208 if (!eap->skip)
6209 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006210 char_u buf[NUMBUFLEN];
6211
6212 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006213 {
6214 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6215 {
6216 emsg(_(e_inval_string));
6217 p = NULL;
6218 }
6219 else
6220 p = tv_get_string_buf(&rettv, buf);
6221 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006222 else
6223 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006224 if (p == NULL)
6225 {
6226 clear_tv(&rettv);
6227 ret = FAIL;
6228 break;
6229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 len = (int)STRLEN(p);
6231 if (ga_grow(&ga, len + 2) == FAIL)
6232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 ret = FAIL;
6235 break;
6236 }
6237 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 ga.ga_len += len;
6241 }
6242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 arg = skipwhite(arg);
6245 }
6246
6247 if (ret != FAIL && ga.ga_data != NULL)
6248 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006249 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6250 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006251 // Mark the already saved text as finishing the line, so that what
6252 // follows is displayed on a new line when scrolling back at the
6253 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006254 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006255 }
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006258 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006259 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006260 out_flush();
6261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 else if (eap->cmdidx == CMD_echoerr)
6263 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006264 int save_did_emsg = did_emsg;
6265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006267 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (!force_abort)
6269 did_emsg = save_did_emsg;
6270 }
6271 else if (eap->cmdidx == CMD_execute)
6272 do_cmdline((char_u *)ga.ga_data,
6273 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6274 }
6275
6276 ga_clear(&ga);
6277
6278 if (eap->skip)
6279 --emsg_skip;
6280
6281 eap->nextcmd = check_nextcmd(arg);
6282}
6283
6284/*
6285 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6286 * "arg" points to the "&" or '+' when called, to "option" when returning.
6287 * Returns NULL when no option name found. Otherwise pointer to the char
6288 * after the option name.
6289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006290 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 char_u *p = *arg;
6294
6295 ++p;
6296 if (*p == 'g' && p[1] == ':')
6297 {
6298 *opt_flags = OPT_GLOBAL;
6299 p += 2;
6300 }
6301 else if (*p == 'l' && p[1] == ':')
6302 {
6303 *opt_flags = OPT_LOCAL;
6304 p += 2;
6305 }
6306 else
6307 *opt_flags = 0;
6308
6309 if (!ASCII_ISALPHA(*p))
6310 return NULL;
6311 *arg = p;
6312
6313 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006314 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else
6316 while (ASCII_ISALPHA(*p))
6317 ++p;
6318 return p;
6319}
6320
6321/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006322 * Display script name where an item was last set.
6323 * Should only be invoked when 'verbose' is non-zero.
6324 */
6325 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006326last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006327{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006328 char_u *p;
6329
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006330 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006331 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006332 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006333 if (p != NULL)
6334 {
6335 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006336 msg_puts(_("\n\tLast set from "));
6337 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006338 if (script_ctx.sc_lnum > 0)
6339 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006340 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006341 msg_outnum((long)script_ctx.sc_lnum);
6342 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006343 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006344 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006345 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006346 }
6347}
6348
Bram Moolenaar61be3762019-03-19 23:04:17 +01006349/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006350 * Compare "typ1" and "typ2". Put the result in "typ1".
6351 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006352 int
6353typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006354 typval_T *typ1, // first operand
6355 typval_T *typ2, // second operand
6356 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006357 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006358{
6359 int i;
6360 varnumber_T n1, n2;
6361 char_u *s1, *s2;
6362 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006363 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006364
Bram Moolenaar31988702018-02-13 12:57:42 +01006365 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006366 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006367 // For "is" a different type always means FALSE, for "notis"
6368 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006369 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006370 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006371 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6372 {
6373 if (type_is)
6374 {
6375 n1 = (typ1->v_type == typ2->v_type
6376 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006377 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006378 n1 = !n1;
6379 }
6380 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006381 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006382 {
6383 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006384 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006385 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006386 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006387 clear_tv(typ1);
6388 return FAIL;
6389 }
6390 else
6391 {
6392 // Compare two Blobs for being equal or unequal.
6393 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006394 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006395 n1 = !n1;
6396 }
6397 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006398 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6399 {
6400 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006401 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006402 n1 = (typ1->v_type == typ2->v_type
6403 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006404 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006405 n1 = !n1;
6406 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006407 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006408 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006409 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006410 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006411 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006412 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006413 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006414 clear_tv(typ1);
6415 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006416 }
6417 else
6418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006420 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6421 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006422 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006423 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006424 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006425 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006426
6427 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6428 {
6429 if (type_is)
6430 {
6431 n1 = (typ1->v_type == typ2->v_type
6432 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006433 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006434 n1 = !n1;
6435 }
6436 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006437 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006438 {
6439 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006440 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006441 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006442 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006443 clear_tv(typ1);
6444 return FAIL;
6445 }
6446 else
6447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006449 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6450 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006451 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006452 n1 = !n1;
6453 }
6454 }
6455
6456 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6457 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6458 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006459 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6460 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006462 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006463 clear_tv(typ1);
6464 return FAIL;
6465 }
6466 if ((typ1->v_type == VAR_PARTIAL
6467 && typ1->vval.v_partial == NULL)
6468 || (typ2->v_type == VAR_PARTIAL
6469 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006470 // When both partials are NULL, then they are equal.
6471 // Otherwise they are not equal.
6472 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006473 else if (type_is)
6474 {
6475 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // strings are considered the same if their value is
6477 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006478 n1 = tv_equal(typ1, typ2, ic, FALSE);
6479 else if (typ1->v_type == VAR_PARTIAL
6480 && typ2->v_type == VAR_PARTIAL)
6481 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6482 else
6483 n1 = FALSE;
6484 }
6485 else
6486 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006487 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006488 n1 = !n1;
6489 }
6490
6491#ifdef FEAT_FLOAT
6492 /*
6493 * If one of the two variables is a float, compare as a float.
6494 * When using "=~" or "!~", always compare as string.
6495 */
6496 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006497 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006498 {
6499 float_T f1, f2;
6500
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006501 f1 = tv_get_float(typ1);
6502 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006503 n1 = FALSE;
6504 switch (type)
6505 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006506 case EXPR_IS:
6507 case EXPR_EQUAL: n1 = (f1 == f2); break;
6508 case EXPR_ISNOT:
6509 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6510 case EXPR_GREATER: n1 = (f1 > f2); break;
6511 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6512 case EXPR_SMALLER: n1 = (f1 < f2); break;
6513 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6514 case EXPR_UNKNOWN:
6515 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006517 }
6518 }
6519#endif
6520
6521 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006522 * If one of the two variables is a number, compare as a number.
6523 * When using "=~" or "!~", always compare as string.
6524 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006525 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006526 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006528 n1 = tv_get_number(typ1);
6529 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006530 switch (type)
6531 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006532 case EXPR_IS:
6533 case EXPR_EQUAL: n1 = (n1 == n2); break;
6534 case EXPR_ISNOT:
6535 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6536 case EXPR_GREATER: n1 = (n1 > n2); break;
6537 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6538 case EXPR_SMALLER: n1 = (n1 < n2); break;
6539 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6540 case EXPR_UNKNOWN:
6541 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006543 }
6544 }
6545 else
6546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006547 s1 = tv_get_string_buf(typ1, buf1);
6548 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006549 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006550 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6551 else
6552 i = 0;
6553 n1 = FALSE;
6554 switch (type)
6555 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006556 case EXPR_IS:
6557 case EXPR_EQUAL: n1 = (i == 0); break;
6558 case EXPR_ISNOT:
6559 case EXPR_NEQUAL: n1 = (i != 0); break;
6560 case EXPR_GREATER: n1 = (i > 0); break;
6561 case EXPR_GEQUAL: n1 = (i >= 0); break;
6562 case EXPR_SMALLER: n1 = (i < 0); break;
6563 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006564
Bram Moolenaar87396072019-12-31 22:36:18 +01006565 case EXPR_MATCH:
6566 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006567 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006568 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006569 n1 = !n1;
6570 break;
6571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006573 }
6574 }
6575 clear_tv(typ1);
6576 typ1->v_type = VAR_NUMBER;
6577 typ1->vval.v_number = n1;
6578
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006579 return OK;
6580}
6581
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006582 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006583typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006584{
6585 char_u *tofree;
6586 char_u numbuf[NUMBUFLEN];
6587 char_u *ret = NULL;
6588
6589 if (arg == NULL)
6590 return vim_strsave((char_u *)"(does not exist)");
6591 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006592 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006593 if (ret != NULL && tofree == NULL)
6594 ret = vim_strsave(ret);
6595 return ret;
6596}
6597
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006598#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600/*
6601 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006602 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 * "flags" can be "g" to do a global substitute.
6604 * Returns an allocated string, NULL for error.
6605 */
6606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006607do_string_sub(
6608 char_u *str,
6609 char_u *pat,
6610 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006611 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 int sublen;
6615 regmatch_T regmatch;
6616 int i;
6617 int do_all;
6618 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006619 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 garray_T ga;
6621 char_u *ret;
6622 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006623 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006627 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 ga_init2(&ga, 1, 200);
6630
6631 do_all = (flags[0] == 'g');
6632
6633 regmatch.rm_ic = p_ic;
6634 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6635 if (regmatch.regprog != NULL)
6636 {
6637 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006638 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6640 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006641 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006642 if (regmatch.startp[0] == regmatch.endp[0])
6643 {
6644 if (zero_width == regmatch.startp[0])
6645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006647 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006648 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6649 (size_t)i);
6650 ga.ga_len += i;
6651 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006652 continue;
6653 }
6654 zero_width = regmatch.startp[0];
6655 }
6656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 /*
6658 * Get some space for a temporary buffer to do the substitution
6659 * into. It will contain:
6660 * - The text up to where the match is.
6661 * - The substituted text.
6662 * - The text after the match.
6663 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006664 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006665 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6667 {
6668 ga_clear(&ga);
6669 break;
6670 }
6671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006672 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 i = (int)(regmatch.startp[0] - tail);
6674 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006675 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006676 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 + ga.ga_len + i, TRUE, TRUE, FALSE);
6678 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006679 tail = regmatch.endp[0];
6680 if (*tail == NUL)
6681 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (!do_all)
6683 break;
6684 }
6685
6686 if (ga.ga_data != NULL)
6687 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6688
Bram Moolenaar473de612013-06-08 18:19:48 +02006689 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691
6692 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6693 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006694 if (p_cpo == empty_option)
6695 p_cpo = save_cpo;
6696 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006697 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006698 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700 return ret;
6701}