blob: 77359dccaa0ba4c36f3223b9d5b96e59c1365f21 [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 Moolenaar227a69d2020-05-15 18:17:28 +02002698 else if (flags & EVAL_CONSTANT)
2699 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002701 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002702 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002703 {
2704 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002708 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 *arg = skipwhite(*arg);
2712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002713 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2714 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002715 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002716 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002717 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 /*
2720 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2721 */
2722 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002723 ret = eval7_leader(rettv, start_leader, &end_leader);
2724 return ret;
2725}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002727/*
2728 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2729 * Adjusts "end_leaderp" until it is at "start_leader".
2730 */
2731 static int
2732eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2733{
2734 char_u *end_leader = *end_leaderp;
2735 int ret = OK;
2736 int error = FALSE;
2737 varnumber_T val = 0;
2738#ifdef FEAT_FLOAT
2739 float_T f = 0.0;
2740
2741 if (rettv->v_type == VAR_FLOAT)
2742 f = rettv->vval.v_float;
2743 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002744#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002745 val = tv_get_number_chk(rettv, &error);
2746 if (error)
2747 {
2748 clear_tv(rettv);
2749 ret = FAIL;
2750 }
2751 else
2752 {
2753 while (end_leader > start_leader)
2754 {
2755 --end_leader;
2756 if (*end_leader == '!')
2757 {
2758#ifdef FEAT_FLOAT
2759 if (rettv->v_type == VAR_FLOAT)
2760 f = !f;
2761 else
2762#endif
2763 val = !val;
2764 }
2765 else if (*end_leader == '-')
2766 {
2767#ifdef FEAT_FLOAT
2768 if (rettv->v_type == VAR_FLOAT)
2769 f = -f;
2770 else
2771#endif
2772 val = -val;
2773 }
2774 }
2775#ifdef FEAT_FLOAT
2776 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002779 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002782#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002783 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002784 clear_tv(rettv);
2785 rettv->v_type = VAR_NUMBER;
2786 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002789 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 return ret;
2791}
2792
2793/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002794 * Call the function referred to in "rettv".
2795 */
2796 static int
2797call_func_rettv(
2798 char_u **arg,
2799 typval_T *rettv,
2800 int evaluate,
2801 dict_T *selfdict,
2802 typval_T *basetv)
2803{
2804 partial_T *pt = NULL;
2805 funcexe_T funcexe;
2806 typval_T functv;
2807 char_u *s;
2808 int ret;
2809
2810 // need to copy the funcref so that we can clear rettv
2811 if (evaluate)
2812 {
2813 functv = *rettv;
2814 rettv->v_type = VAR_UNKNOWN;
2815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002817 if (functv.v_type == VAR_PARTIAL)
2818 {
2819 pt = functv.vval.v_partial;
2820 s = partial_name(pt);
2821 }
2822 else
2823 s = functv.vval.v_string;
2824 }
2825 else
2826 s = (char_u *)"";
2827
Bram Moolenaara80faa82020-04-12 19:37:17 +02002828 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002829 funcexe.firstline = curwin->w_cursor.lnum;
2830 funcexe.lastline = curwin->w_cursor.lnum;
2831 funcexe.evaluate = evaluate;
2832 funcexe.partial = pt;
2833 funcexe.selfdict = selfdict;
2834 funcexe.basetv = basetv;
2835 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // Clear the funcref afterwards, so that deleting it while
2838 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002839 if (evaluate)
2840 clear_tv(&functv);
2841
2842 return ret;
2843}
2844
2845/*
2846 * Evaluate "->method()".
2847 * "*arg" points to the '-'.
2848 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2849 */
2850 static int
2851eval_lambda(
2852 char_u **arg,
2853 typval_T *rettv,
2854 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002855 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002856{
2857 typval_T base = *rettv;
2858 int ret;
2859
2860 // Skip over the ->.
2861 *arg += 2;
2862 rettv->v_type = VAR_UNKNOWN;
2863
2864 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002865 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002866 return FAIL;
2867 else if (**arg != '(')
2868 {
2869 if (verbose)
2870 {
2871 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002872 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002873 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002875 }
2876 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002877 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002878 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002879 else
2880 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2881
2882 // Clear the funcref afterwards, so that deleting it while
2883 // evaluating the arguments is possible (see test55).
2884 if (evaluate)
2885 clear_tv(&base);
2886
2887 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002888}
2889
2890/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002891 * Evaluate "->method()".
2892 * "*arg" points to the '-'.
2893 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2894 */
2895 static int
2896eval_method(
2897 char_u **arg,
2898 typval_T *rettv,
2899 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002901{
2902 char_u *name;
2903 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002905 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002906 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002907
2908 // Skip over the ->.
2909 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002910 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002911
Bram Moolenaarac92e252019-08-03 21:58:38 +02002912 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002913 len = get_name_len(arg, &alias, evaluate, TRUE);
2914 if (alias != NULL)
2915 name = alias;
2916
2917 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002918 {
2919 if (verbose)
2920 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002921 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002923 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002924 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002925 if (**arg != '(')
2926 {
2927 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002929 ret = FAIL;
2930 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002931 else if (VIM_ISWHITE((*arg)[-1]))
2932 {
2933 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002934 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002935 ret = FAIL;
2936 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002937 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002938 ret = eval_func(arg, name, len, rettv,
2939 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002940 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002941
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002942 // Clear the funcref afterwards, so that deleting it while
2943 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002944 if (evaluate)
2945 clear_tv(&base);
2946
Bram Moolenaarac92e252019-08-03 21:58:38 +02002947 return ret;
2948}
2949
2950/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002951 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2952 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002953 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2954 */
2955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956eval_index(
2957 char_u **arg,
2958 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002959 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002960 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002961{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002962 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002963 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002965 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002966 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002967 long len = -1;
2968 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002969 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002970 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002971
Bram Moolenaara03f2332016-02-06 18:09:59 +01002972 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002973 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002975 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002978 return FAIL;
2979 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002980#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002981 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002982 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002983 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002984#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002985 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002986 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002987 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002988 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002989 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002991 return FAIL;
2992 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002993 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002995 if (evaluate)
2996 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002998
2999 case VAR_STRING:
3000 case VAR_NUMBER:
3001 case VAR_LIST:
3002 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003003 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003004 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003005 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003006
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003007 init_tv(&var1);
3008 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003009 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003011 /*
3012 * dict.name
3013 */
3014 key = *arg + 1;
3015 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3016 ;
3017 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003018 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003019 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003020 }
3021 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003023 /*
3024 * something[idx]
3025 *
3026 * Get the (first) variable from inside the [].
3027 */
3028 *arg = skipwhite(*arg + 1);
3029 if (**arg == ':')
3030 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003031 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003032 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003033 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003036 clear_tv(&var1);
3037 return FAIL;
3038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003039
3040 /*
3041 * Get the second variable from inside the [:].
3042 */
3043 if (**arg == ':')
3044 {
3045 range = TRUE;
3046 *arg = skipwhite(*arg + 1);
3047 if (**arg == ']')
3048 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003049 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 if (!empty1)
3052 clear_tv(&var1);
3053 return FAIL;
3054 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003055 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003057 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003058 if (!empty1)
3059 clear_tv(&var1);
3060 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061 return FAIL;
3062 }
3063 }
3064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003066 if (**arg != ']')
3067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003068 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003069 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003070 clear_tv(&var1);
3071 if (range)
3072 clear_tv(&var2);
3073 return FAIL;
3074 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 }
3077
3078 if (evaluate)
3079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003080 n1 = 0;
3081 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003083 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003085 }
3086 if (range)
3087 {
3088 if (empty2)
3089 n2 = -1;
3090 else
3091 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003092 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003094 }
3095 }
3096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003098 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003099 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003100 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003102 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003103 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003104 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003105 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003106 case VAR_SPECIAL:
3107 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003108 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003109 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003110
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003111 case VAR_NUMBER:
3112 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003113 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003114 len = (long)STRLEN(s);
3115 if (range)
3116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003117 // The resulting variable is a substring. If the indexes
3118 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003119 if (n1 < 0)
3120 {
3121 n1 = len + n1;
3122 if (n1 < 0)
3123 n1 = 0;
3124 }
3125 if (n2 < 0)
3126 n2 = len + n2;
3127 else if (n2 >= len)
3128 n2 = len;
3129 if (n1 >= len || n2 < 0 || n1 > n2)
3130 s = NULL;
3131 else
3132 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3133 }
3134 else
3135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003136 // The resulting variable is a string of a single
3137 // character. If the index is too big or negative the
3138 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003139 if (n1 >= len || n1 < 0)
3140 s = NULL;
3141 else
3142 s = vim_strnsave(s + n1, 1);
3143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 clear_tv(rettv);
3145 rettv->v_type = VAR_STRING;
3146 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003147 break;
3148
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003149 case VAR_BLOB:
3150 len = blob_len(rettv->vval.v_blob);
3151 if (range)
3152 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003153 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003154 // are out of range the result is empty.
3155 if (n1 < 0)
3156 {
3157 n1 = len + n1;
3158 if (n1 < 0)
3159 n1 = 0;
3160 }
3161 if (n2 < 0)
3162 n2 = len + n2;
3163 else if (n2 >= len)
3164 n2 = len - 1;
3165 if (n1 >= len || n2 < 0 || n1 > n2)
3166 {
3167 clear_tv(rettv);
3168 rettv->v_type = VAR_BLOB;
3169 rettv->vval.v_blob = NULL;
3170 }
3171 else
3172 {
3173 blob_T *blob = blob_alloc();
3174
3175 if (blob != NULL)
3176 {
3177 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3178 {
3179 blob_free(blob);
3180 return FAIL;
3181 }
3182 blob->bv_ga.ga_len = n2 - n1 + 1;
3183 for (i = n1; i <= n2; i++)
3184 blob_set(blob, i - n1,
3185 blob_get(rettv->vval.v_blob, i));
3186
3187 clear_tv(rettv);
3188 rettv_blob_set(rettv, blob);
3189 }
3190 }
3191 }
3192 else
3193 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003194 // The resulting variable is a byte value.
3195 // If the index is too big or negative that is an error.
3196 if (n1 < 0)
3197 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003198 if (n1 < len && n1 >= 0)
3199 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003200 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003201
3202 clear_tv(rettv);
3203 rettv->v_type = VAR_NUMBER;
3204 rettv->vval.v_number = v;
3205 }
3206 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003207 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003208 }
3209 break;
3210
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003211 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003212 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003213 if (n1 < 0)
3214 n1 = len + n1;
3215 if (!empty1 && (n1 < 0 || n1 >= len))
3216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003217 // For a range we allow invalid values and return an empty
3218 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003219 if (!range)
3220 {
3221 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003222 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003223 return FAIL;
3224 }
3225 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003226 }
3227 if (range)
3228 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 list_T *l;
3230 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003231
3232 if (n2 < 0)
3233 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003234 else if (n2 >= len)
3235 n2 = len - 1;
3236 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003237 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 l = list_alloc();
3239 if (l == NULL)
3240 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003241 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003242 n1 <= n2; ++n1)
3243 {
3244 if (list_append_tv(l, &item->li_tv) == FAIL)
3245 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003246 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003247 return FAIL;
3248 }
3249 item = item->li_next;
3250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003251 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003252 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003253 }
3254 else
3255 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003256 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003257 clear_tv(rettv);
3258 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259 }
3260 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261
3262 case VAR_DICT:
3263 if (range)
3264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003265 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003266 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 if (len == -1)
3268 clear_tv(&var1);
3269 return FAIL;
3270 }
3271 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003273
3274 if (len == -1)
3275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003276 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003277 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003278 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003279 clear_tv(&var1);
3280 return FAIL;
3281 }
3282 }
3283
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003284 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003285
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003286 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 if (len == -1)
3289 clear_tv(&var1);
3290 if (item == NULL)
3291 return FAIL;
3292
3293 copy_tv(&item->di_tv, &var1);
3294 clear_tv(rettv);
3295 *rettv = var1;
3296 }
3297 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003298 }
3299 }
3300
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003301 return OK;
3302}
3303
3304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 * Get an option value.
3306 * "arg" points to the '&' or '+' before the option name.
3307 * "arg" is advanced to character after the option name.
3308 * Return OK or FAIL.
3309 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003311get_option_tv(
3312 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003313 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 char_u *option_end;
3317 long numval;
3318 char_u *stringval;
3319 int opt_type;
3320 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003321 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 int ret = OK;
3323 int opt_flags;
3324
3325 /*
3326 * Isolate the option name and find its value.
3327 */
3328 option_end = find_option_end(arg, &opt_flags);
3329 if (option_end == NULL)
3330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003332 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 return FAIL;
3334 }
3335
3336 if (!evaluate)
3337 {
3338 *arg = option_end;
3339 return OK;
3340 }
3341
3342 c = *option_end;
3343 *option_end = NUL;
3344 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 ret = FAIL;
3352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003353 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003355 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 rettv->v_type = VAR_STRING;
3358 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 rettv->v_type = VAR_NUMBER;
3363 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003367 rettv->v_type = VAR_NUMBER;
3368 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003372 rettv->v_type = VAR_STRING;
3373 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 }
3376 else if (working && (opt_type == -2 || opt_type == -1))
3377 ret = FAIL;
3378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003379 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 *arg = option_end;
3381
3382 return ret;
3383}
3384
3385/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3387 * Return OK or FAIL.
3388 */
3389 int
3390get_number_tv(
3391 char_u **arg,
3392 typval_T *rettv,
3393 int evaluate,
3394 int want_string UNUSED)
3395{
3396 int len;
3397#ifdef FEAT_FLOAT
3398 char_u *p;
3399 int get_float = FALSE;
3400
3401 // We accept a float when the format matches
3402 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3403 // strict to avoid backwards compatibility problems.
3404 // With script version 2 and later the leading digit can be
3405 // omitted.
3406 // Don't look for a float after the "." operator, so that
3407 // ":let vers = 1.2.3" doesn't fail.
3408 if (**arg == '.')
3409 p = *arg;
3410 else
3411 p = skipdigits(*arg + 1);
3412 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3413 {
3414 get_float = TRUE;
3415 p = skipdigits(p + 2);
3416 if (*p == 'e' || *p == 'E')
3417 {
3418 ++p;
3419 if (*p == '-' || *p == '+')
3420 ++p;
3421 if (!vim_isdigit(*p))
3422 get_float = FALSE;
3423 else
3424 p = skipdigits(p + 1);
3425 }
3426 if (ASCII_ISALPHA(*p) || *p == '.')
3427 get_float = FALSE;
3428 }
3429 if (get_float)
3430 {
3431 float_T f;
3432
3433 *arg += string2float(*arg, &f);
3434 if (evaluate)
3435 {
3436 rettv->v_type = VAR_FLOAT;
3437 rettv->vval.v_float = f;
3438 }
3439 }
3440 else
3441#endif
3442 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3443 {
3444 char_u *bp;
3445 blob_T *blob = NULL; // init for gcc
3446
3447 // Blob constant: 0z0123456789abcdef
3448 if (evaluate)
3449 blob = blob_alloc();
3450 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3451 {
3452 if (!vim_isxdigit(bp[1]))
3453 {
3454 if (blob != NULL)
3455 {
3456 emsg(_("E973: Blob literal should have an even number of hex characters"));
3457 ga_clear(&blob->bv_ga);
3458 VIM_CLEAR(blob);
3459 }
3460 return FAIL;
3461 }
3462 if (blob != NULL)
3463 ga_append(&blob->bv_ga,
3464 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3465 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3466 ++bp;
3467 }
3468 if (blob != NULL)
3469 rettv_blob_set(rettv, blob);
3470 *arg = bp;
3471 }
3472 else
3473 {
3474 varnumber_T n;
3475
3476 // decimal, hex or octal number
3477 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3478 ? STR2NR_NO_OCT + STR2NR_QUOTE
3479 : STR2NR_ALL, &n, NULL, 0, TRUE);
3480 if (len == 0)
3481 {
3482 semsg(_(e_invexpr2), *arg);
3483 return FAIL;
3484 }
3485 *arg += len;
3486 if (evaluate)
3487 {
3488 rettv->v_type = VAR_NUMBER;
3489 rettv->vval.v_number = n;
3490 }
3491 }
3492 return OK;
3493}
3494
3495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 * Allocate a variable for a string constant.
3497 * Return OK or FAIL.
3498 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 char_u *p;
3503 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int extra = 0;
3505
3506 /*
3507 * Find the end of the string, skipping backslashed characters.
3508 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003509 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 if (*p == '\\' && p[1] != NUL)
3512 {
3513 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003514 // A "\<x>" form occupies at least 4 characters, and produces up
3515 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (*p == '<')
3517 extra += 2;
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520
3521 if (*p != '"')
3522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003523 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return FAIL;
3525 }
3526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003527 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (!evaluate)
3529 {
3530 *arg = p + 1;
3531 return OK;
3532 }
3533
3534 /*
3535 * Copy the string into allocated memory, handling backslashed
3536 * characters.
3537 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003538 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (name == NULL)
3540 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003541 rettv->v_type = VAR_STRING;
3542 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 if (*p == '\\')
3547 {
3548 switch (*++p)
3549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003550 case 'b': *name++ = BS; ++p; break;
3551 case 'e': *name++ = ESC; ++p; break;
3552 case 'f': *name++ = FF; ++p; break;
3553 case 'n': *name++ = NL; ++p; break;
3554 case 'r': *name++ = CAR; ++p; break;
3555 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003559 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 case 'U':
3561 if (vim_isxdigit(p[1]))
3562 {
3563 int n, nr;
3564 int c = toupper(*p);
3565
3566 if (c == 'X')
3567 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003568 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003570 else
3571 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 nr = 0;
3573 while (--n >= 0 && vim_isxdigit(p[1]))
3574 {
3575 ++p;
3576 nr = (nr << 4) + hex2nr(*p);
3577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003579 // For "\u" store the number according to
3580 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case '0':
3590 case '1':
3591 case '2':
3592 case '3':
3593 case '4':
3594 case '5':
3595 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 case '7': *name = *p++ - '0';
3597 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 *name = (*name << 3) + *p++ - '0';
3600 if (*p >= '0' && *p <= '7')
3601 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 break;
3605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003607 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3608 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (extra != 0)
3610 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 break;
3613 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003614 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 break;
3618 }
3619 }
3620 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003626 ++p;
3627 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return OK;
3630}
3631
3632/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * Return OK or FAIL.
3635 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003640 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003641 int reduce = 0;
3642
3643 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003645 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003646 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003648 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003651 break;
3652 ++reduce;
3653 ++p;
3654 }
3655 }
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003659 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003660 return FAIL;
3661 }
3662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003663 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003664 if (!evaluate)
3665 {
3666 *arg = p + 1;
3667 return OK;
3668 }
3669
3670 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003672 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003673 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003674 if (str == NULL)
3675 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676 rettv->v_type = VAR_STRING;
3677 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003678
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003683 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003684 break;
3685 ++p;
3686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003687 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003690 *arg = p + 1;
3691
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003692 return OK;
3693}
3694
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003695/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003696 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003697 */
3698 char_u *
3699partial_name(partial_T *pt)
3700{
3701 if (pt->pt_name != NULL)
3702 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003703 if (pt->pt_func != NULL)
3704 return pt->pt_func->uf_name;
3705 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003706}
3707
Bram Moolenaarddecc252016-04-06 22:59:37 +02003708 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003709partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003710{
3711 int i;
3712
3713 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003714 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003715 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003716 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003717 if (pt->pt_name != NULL)
3718 {
3719 func_unref(pt->pt_name);
3720 vim_free(pt->pt_name);
3721 }
3722 else
3723 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003724
3725 if (pt->pt_funcstack != NULL)
3726 {
3727 // Decrease the reference count for the context of a closure. If down
3728 // to zero free it and clear the variables on the stack.
3729 if (--pt->pt_funcstack->fs_refcount == 0)
3730 {
3731 garray_T *gap = &pt->pt_funcstack->fs_ga;
3732 typval_T *stack = gap->ga_data;
3733
3734 for (i = 0; i < gap->ga_len; ++i)
3735 clear_tv(stack + i);
3736 ga_clear(gap);
3737 vim_free(pt->pt_funcstack);
3738 }
3739 pt->pt_funcstack = NULL;
3740 }
3741
Bram Moolenaarddecc252016-04-06 22:59:37 +02003742 vim_free(pt);
3743}
3744
3745/*
3746 * Unreference a closure: decrement the reference count and free it when it
3747 * becomes zero.
3748 */
3749 void
3750partial_unref(partial_T *pt)
3751{
3752 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003753 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003754}
3755
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003756static int tv_equal_recurse_limit;
3757
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003758 static int
3759func_equal(
3760 typval_T *tv1,
3761 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003762 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003763{
3764 char_u *s1, *s2;
3765 dict_T *d1, *d2;
3766 int a1, a2;
3767 int i;
3768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003770 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003771 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003772 if (s1 != NULL && *s1 == NUL)
3773 s1 = NULL;
3774 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003775 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003776 if (s2 != NULL && *s2 == NUL)
3777 s2 = NULL;
3778 if (s1 == NULL || s2 == NULL)
3779 {
3780 if (s1 != s2)
3781 return FALSE;
3782 }
3783 else if (STRCMP(s1, s2) != 0)
3784 return FALSE;
3785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003787 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3788 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3789 if (d1 == NULL || d2 == NULL)
3790 {
3791 if (d1 != d2)
3792 return FALSE;
3793 }
3794 else if (!dict_equal(d1, d2, ic, TRUE))
3795 return FALSE;
3796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003797 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003798 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3799 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3800 if (a1 != a2)
3801 return FALSE;
3802 for (i = 0; i < a1; ++i)
3803 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3804 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3805 return FALSE;
3806
3807 return TRUE;
3808}
3809
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003810/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003811 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003812 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003813 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003814 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816tv_equal(
3817 typval_T *tv1,
3818 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003819 int ic, // ignore case
3820 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003821{
3822 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003824 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003825 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // Catch lists and dicts that have an endless loop by limiting
3828 // recursiveness to a limit. We guess they are equal then.
3829 // A fixed limit has the problem of still taking an awful long time.
3830 // Reduce the limit every time running into it. That should work fine for
3831 // deeply linked structures that are not recursively linked and catch
3832 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003833 if (!recursive)
3834 tv_equal_recurse_limit = 1000;
3835 if (recursive_cnt >= tv_equal_recurse_limit)
3836 {
3837 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003838 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003839 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003841 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3842 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003843 if ((tv1->v_type == VAR_FUNC
3844 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3845 && (tv2->v_type == VAR_FUNC
3846 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3847 {
3848 ++recursive_cnt;
3849 r = func_equal(tv1, tv2, ic);
3850 --recursive_cnt;
3851 return r;
3852 }
3853
3854 if (tv1->v_type != tv2->v_type)
3855 return FALSE;
3856
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003857 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003858 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003859 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003860 ++recursive_cnt;
3861 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3862 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003863 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003864
3865 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003866 ++recursive_cnt;
3867 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3868 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003869 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003870
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003871 case VAR_BLOB:
3872 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3873
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003874 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 case VAR_BOOL:
3876 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003877 return tv1->vval.v_number == tv2->vval.v_number;
3878
3879 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003880 s1 = tv_get_string_buf(tv1, buf1);
3881 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003882 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003883
Bram Moolenaar835dc632016-02-07 14:27:38 +01003884 case VAR_FLOAT:
3885#ifdef FEAT_FLOAT
3886 return tv1->vval.v_float == tv2->vval.v_float;
3887#endif
3888 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003889#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003890 return tv1->vval.v_job == tv2->vval.v_job;
3891#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003892 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003893#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003894 return tv1->vval.v_channel == tv2->vval.v_channel;
3895#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003897 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003898 return tv1->vval.v_partial == tv2->vval.v_partial;
3899
3900 case VAR_FUNC:
3901 return tv1->vval.v_string == tv2->vval.v_string;
3902
Bram Moolenaar835dc632016-02-07 14:27:38 +01003903 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003904 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003906 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003907 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3910 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003911 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003912}
3913
3914/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003915 * Return the next (unique) copy ID.
3916 * Used for serializing nested structures.
3917 */
3918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003919get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003920{
3921 current_copyID += COPYID_INC;
3922 return current_copyID;
3923}
3924
3925/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003926 * Garbage collection for lists and dictionaries.
3927 *
3928 * We use reference counts to be able to free most items right away when they
3929 * are no longer used. But for composite items it's possible that it becomes
3930 * unused while the reference count is > 0: When there is a recursive
3931 * reference. Example:
3932 * :let l = [1, 2, 3]
3933 * :let d = {9: l}
3934 * :let l[1] = d
3935 *
3936 * Since this is quite unusual we handle this with garbage collection: every
3937 * once in a while find out which lists and dicts are not referenced from any
3938 * variable.
3939 *
3940 * Here is a good reference text about garbage collection (refers to Python
3941 * but it applies to all reference-counting mechanisms):
3942 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003943 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003944
3945/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003946 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003947 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003948 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003949 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003951garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003952{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003953 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003954 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 buf_T *buf;
3956 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003957 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003958 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003960 if (!testing)
3961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003962 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003963 want_garbage_collect = FALSE;
3964 may_garbage_collect = FALSE;
3965 garbage_collect_at_exit = FALSE;
3966 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003967
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003968 // The execution stack can grow big, limit the size.
3969 if (exestack.ga_maxlen - exestack.ga_len > 500)
3970 {
3971 size_t new_len;
3972 char_u *pp;
3973 int n;
3974
3975 // Keep 150% of the current size, with a minimum of the growth size.
3976 n = exestack.ga_len / 2;
3977 if (n < exestack.ga_growsize)
3978 n = exestack.ga_growsize;
3979
3980 // Don't make it bigger though.
3981 if (exestack.ga_len + n < exestack.ga_maxlen)
3982 {
3983 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3984 pp = vim_realloc(exestack.ga_data, new_len);
3985 if (pp == NULL)
3986 return FAIL;
3987 exestack.ga_maxlen = exestack.ga_len + n;
3988 exestack.ga_data = pp;
3989 }
3990 }
3991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003992 // We advance by two because we add one for items referenced through
3993 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003994 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003995
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996 /*
3997 * 1. Go through all accessible variables and mark all lists and dicts
3998 * with copyID.
3999 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004001 // Don't free variables in the previous_funccal list unless they are only
4002 // referenced through previous_funccal. This must be first, because if
4003 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004007 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004010 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004011 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4012 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004014 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004015 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004016 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4017 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004018 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004019 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4020 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004021#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004022 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004023 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4024 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004025 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004026 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004027 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4028 NULL, NULL);
4029#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004031 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004032 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004033 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4034 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004035 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004036 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004038 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004042 abort = abort || set_ref_in_functions(copyID);
4043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004044 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004048 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004049
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004050 // callbacks in buffers
4051 abort = abort || set_ref_in_buffers(copyID);
4052
Bram Moolenaar1dced572012-04-05 16:54:08 +02004053#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004054 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004055#endif
4056
Bram Moolenaardb913952012-06-29 12:54:53 +02004057#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004058 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004059#endif
4060
4061#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004062 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004063#endif
4064
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004065#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004066 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004067 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004068#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004069#ifdef FEAT_NETBEANS_INTG
4070 abort = abort || set_ref_in_nb_channel(copyID);
4071#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004072
Bram Moolenaare3188e22016-05-31 21:13:04 +02004073#ifdef FEAT_TIMERS
4074 abort = abort || set_ref_in_timer(copyID);
4075#endif
4076
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004077#ifdef FEAT_QUICKFIX
4078 abort = abort || set_ref_in_quickfix(copyID);
4079#endif
4080
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004081#ifdef FEAT_TERMINAL
4082 abort = abort || set_ref_in_term(copyID);
4083#endif
4084
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004085#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004086 abort = abort || set_ref_in_popups(copyID);
4087#endif
4088
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004089 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004090 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004091 /*
4092 * 2. Free lists and dictionaries that are not referenced.
4093 */
4094 did_free = free_unref_items(copyID);
4095
4096 /*
4097 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004099 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004100 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004101 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004102 else if (p_verbose > 0)
4103 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004105 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004106
4107 return did_free;
4108}
4109
4110/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004111 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004112 */
4113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004115{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004116 int did_free = FALSE;
4117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004118 // Let all "free" functions know that we are here. This means no
4119 // dictionaries, lists, channels or jobs are to be freed, because we will
4120 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004121 in_free_unref_items = TRUE;
4122
4123 /*
4124 * PASS 1: free the contents of the items. We don't free the items
4125 * themselves yet, so that it is possible to decrement refcount counters
4126 */
4127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004128 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004129 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004131 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004132 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004133
4134#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004135 // Go through the list of jobs and free items without the copyID. This
4136 // must happen before doing channels, because jobs refer to channels, but
4137 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004138 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004140 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004141 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4142#endif
4143
4144 /*
4145 * PASS 2: free the items themselves.
4146 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004147 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004148 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004149
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004151 // Go through the list of jobs and free items without the copyID. This
4152 // must happen before doing channels, because jobs refer to channels, but
4153 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004154 free_unused_jobs(copyID, COPYID_MASK);
4155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004156 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157 free_unused_channels(copyID, COPYID_MASK);
4158#endif
4159
4160 in_free_unref_items = FALSE;
4161
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004162 return did_free;
4163}
4164
4165/*
4166 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004167 * "list_stack" is used to add lists to be marked. Can be NULL.
4168 *
4169 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004170 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004172set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004173{
4174 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004175 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004176 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004177 hashtab_T *cur_ht;
4178 ht_stack_T *ht_stack = NULL;
4179 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004180
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004181 cur_ht = ht;
4182 for (;;)
4183 {
4184 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004186 // Mark each item in the hashtab. If the item contains a hashtab
4187 // it is added to ht_stack, if it contains a list it is added to
4188 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004189 todo = (int)cur_ht->ht_used;
4190 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4191 if (!HASHITEM_EMPTY(hi))
4192 {
4193 --todo;
4194 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4195 &ht_stack, list_stack);
4196 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004197 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004198
4199 if (ht_stack == NULL)
4200 break;
4201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004202 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004203 cur_ht = ht_stack->ht;
4204 tempitem = ht_stack;
4205 ht_stack = ht_stack->prev;
4206 free(tempitem);
4207 }
4208
4209 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004210}
4211
4212/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004213 * Mark a dict and its items with "copyID".
4214 * Returns TRUE if setting references failed somehow.
4215 */
4216 int
4217set_ref_in_dict(dict_T *d, int copyID)
4218{
4219 if (d != NULL && d->dv_copyID != copyID)
4220 {
4221 d->dv_copyID = copyID;
4222 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4223 }
4224 return FALSE;
4225}
4226
4227/*
4228 * Mark a list and its items with "copyID".
4229 * Returns TRUE if setting references failed somehow.
4230 */
4231 int
4232set_ref_in_list(list_T *ll, int copyID)
4233{
4234 if (ll != NULL && ll->lv_copyID != copyID)
4235 {
4236 ll->lv_copyID = copyID;
4237 return set_ref_in_list_items(ll, copyID, NULL);
4238 }
4239 return FALSE;
4240}
4241
4242/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004243 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004244 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4245 *
4246 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004247 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004248 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004249set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004250{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 listitem_T *li;
4252 int abort = FALSE;
4253 list_T *cur_l;
4254 list_stack_T *list_stack = NULL;
4255 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004256
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004257 cur_l = l;
4258 for (;;)
4259 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004260 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004261 // Mark each item in the list. If the item contains a hashtab
4262 // it is added to ht_stack, if it contains a list it is added to
4263 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004264 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4265 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4266 ht_stack, &list_stack);
4267 if (list_stack == NULL)
4268 break;
4269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004270 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 cur_l = list_stack->list;
4272 tempitem = list_stack;
4273 list_stack = list_stack->prev;
4274 free(tempitem);
4275 }
4276
4277 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004278}
4279
4280/*
4281 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004282 * "list_stack" is used to add lists to be marked. Can be NULL.
4283 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4284 *
4285 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004286 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004288set_ref_in_item(
4289 typval_T *tv,
4290 int copyID,
4291 ht_stack_T **ht_stack,
4292 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004293{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004295
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004296 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004297 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004298 dict_T *dd = tv->vval.v_dict;
4299
Bram Moolenaara03f2332016-02-06 18:09:59 +01004300 if (dd != NULL && dd->dv_copyID != copyID)
4301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004302 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004303 dd->dv_copyID = copyID;
4304 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004305 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004306 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4307 }
4308 else
4309 {
4310 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4311 if (newitem == NULL)
4312 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004313 else
4314 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004315 newitem->ht = &dd->dv_hashtab;
4316 newitem->prev = *ht_stack;
4317 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004318 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004319 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004320 }
4321 }
4322 else if (tv->v_type == VAR_LIST)
4323 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004324 list_T *ll = tv->vval.v_list;
4325
Bram Moolenaara03f2332016-02-06 18:09:59 +01004326 if (ll != NULL && ll->lv_copyID != copyID)
4327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004328 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004329 ll->lv_copyID = copyID;
4330 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004331 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004332 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004333 }
4334 else
4335 {
4336 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004337 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004338 if (newitem == NULL)
4339 abort = TRUE;
4340 else
4341 {
4342 newitem->list = ll;
4343 newitem->prev = *list_stack;
4344 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004345 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004346 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004347 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004348 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004349 else if (tv->v_type == VAR_FUNC)
4350 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004352 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004353 else if (tv->v_type == VAR_PARTIAL)
4354 {
4355 partial_T *pt = tv->vval.v_partial;
4356 int i;
4357
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004358 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004359 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004360 // Didn't see this partial yet.
4361 pt->pt_copyID = copyID;
4362
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004363 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004364
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004365 if (pt->pt_dict != NULL)
4366 {
4367 typval_T dtv;
4368
4369 dtv.v_type = VAR_DICT;
4370 dtv.vval.v_dict = pt->pt_dict;
4371 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4372 }
4373
4374 for (i = 0; i < pt->pt_argc; ++i)
4375 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4376 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004377 if (pt->pt_funcstack != NULL)
4378 {
4379 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4380
4381 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4382 abort = abort || set_ref_in_item(stack + i, copyID,
4383 ht_stack, list_stack);
4384 }
4385
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004386 }
4387 }
4388#ifdef FEAT_JOB_CHANNEL
4389 else if (tv->v_type == VAR_JOB)
4390 {
4391 job_T *job = tv->vval.v_job;
4392 typval_T dtv;
4393
4394 if (job != NULL && job->jv_copyID != copyID)
4395 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004396 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 if (job->jv_channel != NULL)
4398 {
4399 dtv.v_type = VAR_CHANNEL;
4400 dtv.vval.v_channel = job->jv_channel;
4401 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4402 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004403 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004404 {
4405 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004406 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004407 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4408 }
4409 }
4410 }
4411 else if (tv->v_type == VAR_CHANNEL)
4412 {
4413 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004414 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004415 typval_T dtv;
4416 jsonq_T *jq;
4417 cbq_T *cq;
4418
4419 if (ch != NULL && ch->ch_copyID != copyID)
4420 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004421 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004422 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004423 {
4424 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4425 jq = jq->jq_next)
4426 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4427 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4428 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004429 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 {
4431 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004432 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004433 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4434 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004435 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004436 {
4437 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004438 dtv.vval.v_partial =
4439 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4441 }
4442 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004443 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004444 {
4445 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004446 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4448 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004449 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004450 {
4451 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004452 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004453 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4454 }
4455 }
4456 }
4457#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004458 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004459}
4460
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 * Return a string with the string representation of a variable.
4463 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004465 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004466 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4467 * stings as "string()", otherwise does not put quotes around strings, as
4468 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004469 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4470 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004471 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004473 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004474echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004475 typval_T *tv,
4476 char_u **tofree,
4477 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004478 int copyID,
4479 int echo_style,
4480 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004481 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004482{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004483 static int recurse = 0;
4484 char_u *r = NULL;
4485
Bram Moolenaar33570922005-01-25 22:26:29 +00004486 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004487 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004488 if (!did_echo_string_emsg)
4489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 // Only give this message once for a recursive call to avoid
4491 // flooding the user with errors. And stop iterating over lists
4492 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004493 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004494 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004496 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004497 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004498 }
4499 ++recurse;
4500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 switch (tv->v_type)
4502 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004503 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004504 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004505 {
4506 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004507 r = tv->vval.v_string;
4508 if (r == NULL)
4509 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004510 }
4511 else
4512 {
4513 *tofree = string_quote(tv->vval.v_string, FALSE);
4514 r = *tofree;
4515 }
4516 break;
4517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004518 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004519 if (echo_style)
4520 {
4521 *tofree = NULL;
4522 r = tv->vval.v_string;
4523 }
4524 else
4525 {
4526 *tofree = string_quote(tv->vval.v_string, TRUE);
4527 r = *tofree;
4528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004529 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004530
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004531 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004532 {
4533 partial_T *pt = tv->vval.v_partial;
4534 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004535 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004536 garray_T ga;
4537 int i;
4538 char_u *tf;
4539
4540 ga_init2(&ga, 1, 100);
4541 ga_concat(&ga, (char_u *)"function(");
4542 if (fname != NULL)
4543 {
4544 ga_concat(&ga, fname);
4545 vim_free(fname);
4546 }
4547 if (pt != NULL && pt->pt_argc > 0)
4548 {
4549 ga_concat(&ga, (char_u *)", [");
4550 for (i = 0; i < pt->pt_argc; ++i)
4551 {
4552 if (i > 0)
4553 ga_concat(&ga, (char_u *)", ");
4554 ga_concat(&ga,
4555 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4556 vim_free(tf);
4557 }
4558 ga_concat(&ga, (char_u *)"]");
4559 }
4560 if (pt != NULL && pt->pt_dict != NULL)
4561 {
4562 typval_T dtv;
4563
4564 ga_concat(&ga, (char_u *)", ");
4565 dtv.v_type = VAR_DICT;
4566 dtv.vval.v_dict = pt->pt_dict;
4567 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4568 vim_free(tf);
4569 }
4570 ga_concat(&ga, (char_u *)")");
4571
4572 *tofree = ga.ga_data;
4573 r = *tofree;
4574 break;
4575 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004576
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004577 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004578 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004579 break;
4580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004582 if (tv->vval.v_list == NULL)
4583 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004584 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004585 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004586 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004587 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004588 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4589 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004590 {
4591 *tofree = NULL;
4592 r = (char_u *)"[...]";
4593 }
4594 else
4595 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004596 int old_copyID = tv->vval.v_list->lv_copyID;
4597
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004598 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004599 *tofree = list2string(tv, copyID, restore_copyID);
4600 if (restore_copyID)
4601 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004602 r = *tofree;
4603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004605
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004607 if (tv->vval.v_dict == NULL)
4608 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004609 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004610 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004611 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004612 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004613 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4614 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004615 {
4616 *tofree = NULL;
4617 r = (char_u *)"{...}";
4618 }
4619 else
4620 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004621 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004622
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004623 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004624 *tofree = dict2string(tv, copyID, restore_copyID);
4625 if (restore_copyID)
4626 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004627 r = *tofree;
4628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004632 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004633 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004635 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004636 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004637 break;
4638
Bram Moolenaar835dc632016-02-07 14:27:38 +01004639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004640 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004641 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004643 if (composite_val)
4644 {
4645 *tofree = string_quote(r, FALSE);
4646 r = *tofree;
4647 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004650 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004651#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004652 *tofree = NULL;
4653 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4654 r = numbuf;
4655 break;
4656#endif
4657
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004658 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004659 case VAR_SPECIAL:
4660 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004661 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004662 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004664
Bram Moolenaar8502c702014-06-17 12:51:16 +02004665 if (--recurse == 0)
4666 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004667 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004668}
4669
4670/*
4671 * Return a string with the string representation of a variable.
4672 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4673 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004674 * Does not put quotes around strings, as ":echo" displays values.
4675 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4676 * May return NULL.
4677 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004679echo_string(
4680 typval_T *tv,
4681 char_u **tofree,
4682 char_u *numbuf,
4683 int copyID)
4684{
4685 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4686}
4687
4688/*
4689 * Return a string with the string representation of a variable.
4690 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4691 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004692 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004693 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004694 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696tv2string(
4697 typval_T *tv,
4698 char_u **tofree,
4699 char_u *numbuf,
4700 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004701{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004702 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703}
4704
4705/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 * Return string "str" in ' quotes, doubling ' characters.
4707 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004709 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004710 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004711string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004712{
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004714 char_u *p, *r, *s;
4715
Bram Moolenaar33570922005-01-25 22:26:29 +00004716 len = (function ? 13 : 3);
4717 if (str != NULL)
4718 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004719 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004720 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004721 if (*p == '\'')
4722 ++len;
4723 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004724 s = r = alloc(len);
4725 if (r != NULL)
4726 {
4727 if (function)
4728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004730 r += 10;
4731 }
4732 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004734 if (str != NULL)
4735 for (p = str; *p != NUL; )
4736 {
4737 if (*p == '\'')
4738 *r++ = '\'';
4739 MB_COPY_CHAR(p, r);
4740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004742 if (function)
4743 *r++ = ')';
4744 *r++ = NUL;
4745 }
4746 return s;
4747}
4748
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004749#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004750/*
4751 * Convert the string "text" to a floating point number.
4752 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4753 * this always uses a decimal point.
4754 * Returns the length of the text that was consumed.
4755 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004756 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004757string2float(
4758 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760{
4761 char *s = (char *)text;
4762 float_T f;
4763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004765 if (STRNICMP(text, "inf", 3) == 0)
4766 {
4767 *value = INFINITY;
4768 return 3;
4769 }
4770 if (STRNICMP(text, "-inf", 3) == 0)
4771 {
4772 *value = -INFINITY;
4773 return 4;
4774 }
4775 if (STRNICMP(text, "nan", 3) == 0)
4776 {
4777 *value = NAN;
4778 return 3;
4779 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 f = strtod(s, &s);
4781 *value = f;
4782 return (int)((char_u *)s - text);
4783}
4784#endif
4785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * Get the value of an environment variable.
4788 * "arg" is pointing to the '$'. It is advanced to after the name.
4789 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004790 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004793get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 char_u *string = NULL;
4796 int len;
4797 int cc;
4798 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004799 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 ++*arg;
4802 name = *arg;
4803 len = get_env_len(arg);
4804 if (evaluate)
4805 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004806 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004808
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004809 cc = name[len];
4810 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004811 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004812 string = vim_getenv(name, &mustfree);
4813 if (string != NULL && *string != NUL)
4814 {
4815 if (!mustfree)
4816 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004818 else
4819 {
4820 if (mustfree)
4821 vim_free(string);
4822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004824 string = expand_env_save(name - 1);
4825 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004826 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004827 }
4828 name[len] = cc;
4829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 rettv->v_type = VAR_STRING;
4831 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833
4834 return OK;
4835}
4836
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004839 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004842var2fpos(
4843 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004844 int dollar_lnum, // TRUE when $ is last line
4845 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004847 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004849 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004851 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004852 if (varp->v_type == VAR_LIST)
4853 {
4854 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004855 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004856 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004857 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004858
4859 l = varp->vval.v_list;
4860 if (l == NULL)
4861 return NULL;
4862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004864 pos.lnum = list_find_nr(l, 0L, &error);
4865 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004869 pos.col = list_find_nr(l, 1L, &error);
4870 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004871 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004872 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004875 li = list_find(l, 1L);
4876 if (li != NULL && li->li_tv.v_type == VAR_STRING
4877 && li->li_tv.vval.v_string != NULL
4878 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4879 pos.col = len + 1;
4880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004882 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004883 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004884 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004887 pos.coladd = list_find_nr(l, 2L, &error);
4888 if (error)
4889 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004890
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004891 return &pos;
4892 }
4893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 if (name == NULL)
4896 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004897 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004900 {
4901 if (VIsual_active)
4902 return &VIsual;
4903 return &curwin->w_cursor;
4904 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004907 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4909 return NULL;
4910 return pp;
4911 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004912
Bram Moolenaara5525202006-03-02 22:52:09 +00004913 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004914
Bram Moolenaar477933c2007-07-17 14:32:23 +00004915 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004916 {
4917 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004919 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004920 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // In silent Ex mode topline is zero, but that's not a valid line
4922 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004923 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004924 return &pos;
4925 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004927 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004928 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004930 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004931 return &pos;
4932 }
4933 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004936 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 pos.lnum = curbuf->b_ml.ml_line_count;
4939 pos.col = 0;
4940 }
4941 else
4942 {
4943 pos.lnum = curwin->w_cursor.lnum;
4944 pos.col = (colnr_T)STRLEN(ml_get_curline());
4945 }
4946 return &pos;
4947 }
4948 return NULL;
4949}
4950
4951/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004952 * Convert list in "arg" into a position and optional file number.
4953 * When "fnump" is NULL there is no file number, only 3 items.
4954 * Note that the column is passed on as-is, the caller may want to decrement
4955 * it to use 1 for the first column.
4956 * Return FAIL when conversion is not possible, doesn't check the position for
4957 * validity.
4958 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960list2fpos(
4961 typval_T *arg,
4962 pos_T *posp,
4963 int *fnump,
4964 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004965{
4966 list_T *l = arg->vval.v_list;
4967 long i = 0;
4968 long n;
4969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4971 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004972 if (arg->v_type != VAR_LIST
4973 || l == NULL
4974 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004975 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004976 return FAIL;
4977
4978 if (fnump != NULL)
4979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004981 if (n < 0)
4982 return FAIL;
4983 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004985 *fnump = n;
4986 }
4987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004989 if (n < 0)
4990 return FAIL;
4991 posp->lnum = n;
4992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004994 if (n < 0)
4995 return FAIL;
4996 posp->col = n;
4997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004999 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005000 posp->coladd = 0;
5001 else
5002 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005003
Bram Moolenaar493c1782014-05-28 14:34:46 +02005004 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005006
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005007 return OK;
5008}
5009
5010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * Get the length of an environment variable name.
5012 * Advance "arg" to the first character after the name.
5013 * Return 0 for error.
5014 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005016get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018 char_u *p;
5019 int len;
5020
5021 for (p = *arg; vim_isIDc(*p); ++p)
5022 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 return 0;
5025
5026 len = (int)(p - *arg);
5027 *arg = p;
5028 return len;
5029}
5030
5031/*
5032 * Get the length of the name of a function or internal variable.
5033 * "arg" is advanced to the first non-white character after the name.
5034 * Return 0 if something is wrong.
5035 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005037get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 char_u *p;
5040 int len;
5041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005044 {
5045 if (*p == ':')
5046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 // "s:" is start of "s:var", but "n:" is not and can be used in
5048 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005049 len = (int)(p - *arg);
5050 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5051 || len > 1)
5052 break;
5053 }
5054 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 return 0;
5057
5058 len = (int)(p - *arg);
5059 *arg = skipwhite(p);
5060
5061 return len;
5062}
5063
5064/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005065 * Get the length of the name of a variable or function.
5066 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 * Return -1 if curly braces expansion failed.
5069 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * If the name contains 'magic' {}'s, expand them and return the
5071 * expanded name in an allocated string via 'alias' - caller must free.
5072 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074get_name_len(
5075 char_u **arg,
5076 char_u **alias,
5077 int evaluate,
5078 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u *p;
5082 char_u *expr_start;
5083 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005085 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5088 && (*arg)[2] == (int)KE_SNR)
5089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 *arg += 3;
5092 return get_id_len(arg) + 3;
5093 }
5094 len = eval_fname_script(*arg);
5095 if (len > 0)
5096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *arg += len;
5099 }
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005104 p = find_name_end(*arg, &expr_start, &expr_end,
5105 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (expr_start != NULL)
5107 {
5108 char_u *temp_string;
5109
5110 if (!evaluate)
5111 {
5112 len += (int)(p - *arg);
5113 *arg = skipwhite(p);
5114 return len;
5115 }
5116
5117 /*
5118 * Include any <SID> etc in the expanded string:
5119 * Thus the -len here.
5120 */
5121 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5122 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 *alias = temp_string;
5125 *arg = skipwhite(p);
5126 return (int)STRLEN(temp_string);
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005130 // Only give an error when there is something, otherwise it will be
5131 // reported at a higher level.
5132 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 return len;
5136}
5137
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138/*
5139 * Find the end of a variable or function name, taking care of magic braces.
5140 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5141 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005142 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 * Return a pointer to just after the name. Equal to "arg" if there is no
5144 * valid name.
5145 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147find_name_end(
5148 char_u *arg,
5149 char_u **expr_start,
5150 char_u **expr_end,
5151 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 int mb_nest = 0;
5154 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005156 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005157 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 *expr_start = NULL;
5162 *expr_end = NULL;
5163 }
5164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005166 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5167 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005168 return arg;
5169
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 for (p = arg; *p != NUL
5171 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005172 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005173 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005175 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005177 if (*p == '\'')
5178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005179 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005180 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005181 ;
5182 if (*p == NUL)
5183 break;
5184 }
5185 else if (*p == '"')
5186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005188 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005189 if (*p == '\\' && p[1] != NUL)
5190 ++p;
5191 if (*p == NUL)
5192 break;
5193 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005194 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // "s:" is start of "s:var", but "n:" is not and can be used in
5197 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005198 len = (int)(p - arg);
5199 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005200 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005201 break;
5202 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 if (*p == '[')
5207 ++br_nest;
5208 else if (*p == ']')
5209 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005212 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 if (*p == '{')
5215 {
5216 mb_nest++;
5217 if (expr_start != NULL && *expr_start == NULL)
5218 *expr_start = p;
5219 }
5220 else if (*p == '}')
5221 {
5222 mb_nest--;
5223 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5224 *expr_end = p;
5225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228
5229 return p;
5230}
5231
5232/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 * Expands out the 'magic' {}'s in a variable/function name.
5234 * Note that this can call itself recursively, to deal with
5235 * constructs like foo{bar}{baz}{bam}
5236 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5237 * "in_start" ^
5238 * "expr_start" ^
5239 * "expr_end" ^
5240 * "in_end" ^
5241 *
5242 * Returns a new allocated string, which the caller must free.
5243 * Returns NULL for failure.
5244 */
5245 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246make_expanded_name(
5247 char_u *in_start,
5248 char_u *expr_start,
5249 char_u *expr_end,
5250 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251{
5252 char_u c1;
5253 char_u *retval = NULL;
5254 char_u *temp_result;
5255 char_u *nextcmd = NULL;
5256
5257 if (expr_end == NULL || in_end == NULL)
5258 return NULL;
5259 *expr_start = NUL;
5260 *expr_end = NUL;
5261 c1 = *in_end;
5262 *in_end = NUL;
5263
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005264 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 if (temp_result != NULL && nextcmd == NULL)
5266 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005267 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5268 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 if (retval != NULL)
5270 {
5271 STRCPY(retval, in_start);
5272 STRCAT(retval, temp_result);
5273 STRCAT(retval, expr_end + 1);
5274 }
5275 }
5276 vim_free(temp_result);
5277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 *expr_start = '{';
5280 *expr_end = '}';
5281
5282 if (retval != NULL)
5283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005284 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005285 if (expr_start != NULL)
5286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 temp_result = make_expanded_name(retval, expr_start,
5289 expr_end, temp_result);
5290 vim_free(retval);
5291 retval = temp_result;
5292 }
5293 }
5294
5295 return retval;
5296}
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005300 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005303eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005305 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5306}
5307
5308/*
5309 * Return TRUE if character "c" can be used as the first character in a
5310 * variable or function name (excluding '{' and '}').
5311 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005314{
5315 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316}
5317
5318/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005319 * Handle:
5320 * - expr[expr], expr[expr:expr] subscript
5321 * - ".name" lookup
5322 * - function call with Funcref variable: func(expr)
5323 * - method call: var->method()
5324 *
5325 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005326 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005328handle_subscript(
5329 char_u **arg,
5330 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005331 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005332 int verbose, // give error messages
5333 char_u *start_leader, // start of '!' and '-' prefixes
5334 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005335{
Bram Moolenaar32e35112020-05-14 22:41:15 +02005336 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337 int ret = OK;
5338 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339
Bram Moolenaar61343f02019-07-20 21:11:13 +02005340 // "." is ".name" lookup when we found a dict or when evaluating and
5341 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005342 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005343 && (((**arg == '['
5344 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005345 || (!evaluate
5346 && (*arg)[1] != '.'
5347 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005348 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005349 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005350 && !VIM_ISWHITE(*(*arg - 1)))
5351 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005352 {
5353 if (**arg == '(')
5354 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005355 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005356
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005357 // Stop the expression evaluation when immediately aborting on
5358 // error, or when an interrupt occurred or an exception was thrown
5359 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (aborting())
5361 {
5362 if (ret == OK)
5363 clear_tv(rettv);
5364 ret = FAIL;
5365 }
5366 dict_unref(selfdict);
5367 selfdict = NULL;
5368 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005369 else if (**arg == '-')
5370 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005371 // Expression "-1.0->method()" applies the leader "-" before
5372 // applying ->.
5373 if (evaluate && *end_leaderp > start_leader)
5374 ret = eval7_leader(rettv, start_leader, end_leaderp);
5375 if (ret == OK)
5376 {
5377 if ((*arg)[2] == '{')
5378 // expr->{lambda}()
5379 ret = eval_lambda(arg, rettv, evaluate, verbose);
5380 else
5381 // expr->name()
5382 ret = eval_method(arg, rettv, evaluate, verbose);
5383 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005384 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005385 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005386 {
5387 dict_unref(selfdict);
5388 if (rettv->v_type == VAR_DICT)
5389 {
5390 selfdict = rettv->vval.v_dict;
5391 if (selfdict != NULL)
5392 ++selfdict->dv_refcount;
5393 }
5394 else
5395 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02005396 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005397 {
5398 clear_tv(rettv);
5399 ret = FAIL;
5400 }
5401 }
5402 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5405 // Don't do this when "Func" is already a partial that was bound
5406 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005407 if (selfdict != NULL
5408 && (rettv->v_type == VAR_FUNC
5409 || (rettv->v_type == VAR_PARTIAL
5410 && (rettv->vval.v_partial->pt_auto
5411 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005414 dict_unref(selfdict);
5415 return ret;
5416}
5417
5418/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005419 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 * value).
5421 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005422 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005425 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426}
5427
5428/*
5429 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * The string "s" must have been allocated, it is consumed.
5431 * Return NULL for out of memory, the variable otherwise.
5432 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005433 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 rettv = alloc_tv();
5439 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 rettv->v_type = VAR_STRING;
5442 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 else
5445 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448
5449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005453free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 if (varp != NULL)
5456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 switch (varp->v_type)
5458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005460 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005462 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 vim_free(varp->vval.v_string);
5464 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005465 case VAR_PARTIAL:
5466 partial_unref(varp->vval.v_partial);
5467 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005468 case VAR_BLOB:
5469 blob_unref(varp->vval.v_blob);
5470 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 case VAR_LIST:
5472 list_unref(varp->vval.v_list);
5473 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005474 case VAR_DICT:
5475 dict_unref(varp->vval.v_dict);
5476 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005477 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479 job_unref(varp->vval.v_job);
5480 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005481#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005482 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005483#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005484 channel_unref(varp->vval.v_channel);
5485 break;
5486#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487 case VAR_NUMBER:
5488 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005489 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005490 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005492 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005493 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005494 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 vim_free(varp);
5497 }
5498}
5499
5500/*
5501 * Free the memory for a variable value and set the value to NULL or 0.
5502 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005504clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 if (varp != NULL)
5507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005511 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005512 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005513 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005514 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005516 case VAR_PARTIAL:
5517 partial_unref(varp->vval.v_partial);
5518 varp->vval.v_partial = NULL;
5519 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005520 case VAR_BLOB:
5521 blob_unref(varp->vval.v_blob);
5522 varp->vval.v_blob = NULL;
5523 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 case VAR_LIST:
5525 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005526 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 case VAR_DICT:
5529 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005530 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005533 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005534 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 varp->vval.v_number = 0;
5536 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005537 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005538#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005539 varp->vval.v_float = 0.0;
5540 break;
5541#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005542 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005543#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005544 job_unref(varp->vval.v_job);
5545 varp->vval.v_job = NULL;
5546#endif
5547 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005548 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005550 channel_unref(varp->vval.v_channel);
5551 varp->vval.v_channel = NULL;
5552#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005554 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005558 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560}
5561
5562/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 * Set the value of a variable to NULL without freeing items.
5564 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567{
5568 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005569 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570}
5571
5572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 * Get the number value of a variable.
5574 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005575 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005576 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005577 * caller of incompatible types: it sets *denote to TRUE if "denote"
5578 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005580 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005581tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005583 int error = FALSE;
5584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005586}
5587
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005588 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005589tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005591 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005596 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005597 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005598#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005599 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005600 break;
5601#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005603 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005604 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 break;
5606 case VAR_STRING:
5607 if (varp->vval.v_string != NULL)
5608 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005609 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005610 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005612 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005613 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005614 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005616 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005617 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005618 case VAR_SPECIAL:
5619 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005620 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005621#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005622 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005623 break;
5624#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005625 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005626#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005627 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005628 break;
5629#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005630 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005631 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005632 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005633 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005634 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005636 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005640 n = -1;
5641 else
5642 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005646#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005648tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005649{
5650 switch (varp->v_type)
5651 {
5652 case VAR_NUMBER:
5653 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005654 case VAR_FLOAT:
5655 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005656 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005657 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005658 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005659 break;
5660 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005661 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005662 break;
5663 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005665 break;
5666 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005667 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005668 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005669 case VAR_BOOL:
5670 emsg(_("E362: Using a boolean value as a Float"));
5671 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005672 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005673 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005674 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005675 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005676# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005677 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005678 break;
5679# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005680 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005681# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005682 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005683 break;
5684# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005685 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005686 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005687 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005688 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005689 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005691 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005692 break;
5693 }
5694 return 0;
5695}
5696#endif
5697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * Get the string value of a variable.
5700 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005701 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5702 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 * If the String variable has never been set, return an empty string.
5704 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005705 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005706 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005708 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005709tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 static char_u mybuf[NUMBUFLEN];
5712
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005713 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
5715
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005716 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005717tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005719 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005720
5721 return res != NULL ? res : (char_u *)"";
5722}
5723
Bram Moolenaar7d647822014-04-05 21:28:56 +02005724/*
5725 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5726 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005727 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005728tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005729{
5730 static char_u mybuf[NUMBUFLEN];
5731
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005732 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005733}
5734
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005735 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005736tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005737{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005741 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005742 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 return buf;
5744 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005745 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005746 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 break;
5748 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005749 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 break;
5751 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005752 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005754 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005755#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005756 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005757 break;
5758#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 case VAR_STRING:
5760 if (varp->vval.v_string != NULL)
5761 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005762 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005763 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005764 case VAR_SPECIAL:
5765 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5766 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005767 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005768 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005769 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005771#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005772 {
5773 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005774 char *status;
5775
5776 if (job == NULL)
5777 return (char_u *)"no process";
5778 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005779 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005780 : "run";
5781# ifdef UNIX
5782 vim_snprintf((char *)buf, NUMBUFLEN,
5783 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005784# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005785 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005786 "process %ld %s",
5787 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005788 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005789# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005790 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005791 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5792# endif
5793 return buf;
5794 }
5795#endif
5796 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005797 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005798#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005799 {
5800 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005801 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005802
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005803 if (channel == NULL)
5804 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5805 else
5806 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005807 "channel %d %s", channel->ch_id, status);
5808 return buf;
5809 }
5810#endif
5811 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005812 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005813 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005815 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005818 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819}
5820
5821/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005822 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5823 * string() on Dict, List, etc.
5824 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005825 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005826tv_stringify(typval_T *varp, char_u *buf)
5827{
5828 if (varp->v_type == VAR_LIST
5829 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005830 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005831 || varp->v_type == VAR_FUNC
5832 || varp->v_type == VAR_PARTIAL
5833 || varp->v_type == VAR_FLOAT)
5834 {
5835 typval_T tmp;
5836
5837 f_string(varp, &tmp);
5838 tv_get_string_buf(&tmp, buf);
5839 clear_tv(varp);
5840 *varp = tmp;
5841 return tmp.vval.v_string;
5842 }
5843 return tv_get_string_buf(varp, buf);
5844}
5845
5846/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005847 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5848 * Also give an error message, using "name" or _("name") when use_gettext is
5849 * TRUE.
5850 */
5851 static int
5852tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5853{
5854 int lock = 0;
5855
5856 switch (tv->v_type)
5857 {
5858 case VAR_BLOB:
5859 if (tv->vval.v_blob != NULL)
5860 lock = tv->vval.v_blob->bv_lock;
5861 break;
5862 case VAR_LIST:
5863 if (tv->vval.v_list != NULL)
5864 lock = tv->vval.v_list->lv_lock;
5865 break;
5866 case VAR_DICT:
5867 if (tv->vval.v_dict != NULL)
5868 lock = tv->vval.v_dict->dv_lock;
5869 break;
5870 default:
5871 break;
5872 }
5873 return var_check_lock(tv->v_lock, name, use_gettext)
5874 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5875}
5876
5877/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005880 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005881 * It is OK for "from" and "to" to point to the same item. This is used to
5882 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005888 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 switch (from->v_type)
5890 {
5891 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005892 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005893 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 to->vval.v_number = from->vval.v_number;
5895 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005896 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005897#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005898 to->vval.v_float = from->vval.v_float;
5899 break;
5900#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005901 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005902#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005903 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005904 if (to->vval.v_job != NULL)
5905 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005906 break;
5907#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005908 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005910 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005911 if (to->vval.v_channel != NULL)
5912 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005913 break;
5914#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 case VAR_STRING:
5916 case VAR_FUNC:
5917 if (from->vval.v_string == NULL)
5918 to->vval.v_string = NULL;
5919 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005922 if (from->v_type == VAR_FUNC)
5923 func_ref(to->vval.v_string);
5924 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005926 case VAR_PARTIAL:
5927 if (from->vval.v_partial == NULL)
5928 to->vval.v_partial = NULL;
5929 else
5930 {
5931 to->vval.v_partial = from->vval.v_partial;
5932 ++to->vval.v_partial->pt_refcount;
5933 }
5934 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005935 case VAR_BLOB:
5936 if (from->vval.v_blob == NULL)
5937 to->vval.v_blob = NULL;
5938 else
5939 {
5940 to->vval.v_blob = from->vval.v_blob;
5941 ++to->vval.v_blob->bv_refcount;
5942 }
5943 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 case VAR_LIST:
5945 if (from->vval.v_list == NULL)
5946 to->vval.v_list = NULL;
5947 else
5948 {
5949 to->vval.v_list = from->vval.v_list;
5950 ++to->vval.v_list->lv_refcount;
5951 }
5952 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 case VAR_DICT:
5954 if (from->vval.v_dict == NULL)
5955 to->vval.v_dict = NULL;
5956 else
5957 {
5958 to->vval.v_dict = from->vval.v_dict;
5959 ++to->vval.v_dict->dv_refcount;
5960 }
5961 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005962 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005963 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005965 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 break;
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 * Make a copy of an item.
5972 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005973 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5974 * reference to an already copied list/dict can be used.
5975 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978item_copy(
5979 typval_T *from,
5980 typval_T *to,
5981 int deep,
5982 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983{
5984 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005985 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005986
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005989 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991 }
5992 ++recurse;
5993
5994 switch (from->v_type)
5995 {
5996 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005997 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005998 case VAR_STRING:
5999 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006000 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006001 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006002 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006003 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006004 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 copy_tv(from, to);
6006 break;
6007 case VAR_LIST:
6008 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006009 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006010 if (from->vval.v_list == NULL)
6011 to->vval.v_list = NULL;
6012 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6013 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006014 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006015 to->vval.v_list = from->vval.v_list->lv_copylist;
6016 ++to->vval.v_list->lv_refcount;
6017 }
6018 else
6019 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6020 if (to->vval.v_list == NULL)
6021 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006023 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006025 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 case VAR_DICT:
6027 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006028 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 if (from->vval.v_dict == NULL)
6030 to->vval.v_dict = NULL;
6031 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6035 ++to->vval.v_dict->dv_refcount;
6036 }
6037 else
6038 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6039 if (to->vval.v_dict == NULL)
6040 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006042 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006043 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006045 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047 }
6048 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006049 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050}
6051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 void
6053echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6054{
6055 char_u *tofree;
6056 char_u numbuf[NUMBUFLEN];
6057 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6058
6059 if (*atstart)
6060 {
6061 *atstart = FALSE;
6062 // Call msg_start() after eval1(), evaluating the expression
6063 // may cause a message to appear.
6064 if (with_space)
6065 {
6066 // Mark the saved text as finishing the line, so that what
6067 // follows is displayed on a new line when scrolling back
6068 // at the more prompt.
6069 msg_sb_eol();
6070 msg_start();
6071 }
6072 }
6073 else if (with_space)
6074 msg_puts_attr(" ", echo_attr);
6075
6076 if (p != NULL)
6077 for ( ; *p != NUL && !got_int; ++p)
6078 {
6079 if (*p == '\n' || *p == '\r' || *p == TAB)
6080 {
6081 if (*p != TAB && *needclr)
6082 {
6083 // remove any text still there from the command
6084 msg_clr_eos();
6085 *needclr = FALSE;
6086 }
6087 msg_putchar_attr(*p, echo_attr);
6088 }
6089 else
6090 {
6091 if (has_mbyte)
6092 {
6093 int i = (*mb_ptr2len)(p);
6094
6095 (void)msg_outtrans_len_attr(p, i, echo_attr);
6096 p += i - 1;
6097 }
6098 else
6099 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6100 }
6101 }
6102 vim_free(tofree);
6103}
6104
Bram Moolenaare9a41262005-01-15 22:18:47 +00006105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 * ":echo expr1 ..." print each argument separated with a space, add a
6107 * newline at the end.
6108 * ":echon expr1 ..." print each argument plain.
6109 */
6110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
6113 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 char_u *p;
6116 int needclr = TRUE;
6117 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006118 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006119 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 if (eap->skip)
6122 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006123 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006125 // If eval1() causes an error message the text from the command may
6126 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006127 need_clr_eos = needclr;
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02006130 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 /*
6133 * Report the invalid expression unless the expression evaluation
6134 * has been cancelled due to an aborting error, an interrupt, or an
6135 * exception.
6136 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006137 if (!aborting() && did_emsg == did_emsg_before
6138 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006139 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006140 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 break;
6142 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006143 need_clr_eos = FALSE;
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006147
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 arg = skipwhite(arg);
6150 }
6151 eap->nextcmd = check_nextcmd(arg);
6152
6153 if (eap->skip)
6154 --emsg_skip;
6155 else
6156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006157 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (needclr)
6159 msg_clr_eos();
6160 if (eap->cmdidx == CMD_echo)
6161 msg_end();
6162 }
6163}
6164
6165/*
6166 * ":echohl {name}".
6167 */
6168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006171 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172}
6173
6174/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006175 * Returns the :echo attribute
6176 */
6177 int
6178get_echo_attr(void)
6179{
6180 return echo_attr;
6181}
6182
6183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 * ":execute expr1 ..." execute the result of an expression.
6185 * ":echomsg expr1 ..." Print a message
6186 * ":echoerr expr1 ..." Print an error
6187 * Each gets spaces around each argument and a newline at the end for
6188 * echo commands
6189 */
6190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006191ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 int ret = OK;
6196 char_u *p;
6197 garray_T ga;
6198 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
6200 ga_init2(&ga, 1, 80);
6201
6202 if (eap->skip)
6203 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006204 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006206 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6207 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 if (!eap->skip)
6211 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006212 char_u buf[NUMBUFLEN];
6213
6214 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006215 {
6216 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6217 {
6218 emsg(_(e_inval_string));
6219 p = NULL;
6220 }
6221 else
6222 p = tv_get_string_buf(&rettv, buf);
6223 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006224 else
6225 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006226 if (p == NULL)
6227 {
6228 clear_tv(&rettv);
6229 ret = FAIL;
6230 break;
6231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 len = (int)STRLEN(p);
6233 if (ga_grow(&ga, len + 2) == FAIL)
6234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006235 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 ret = FAIL;
6237 break;
6238 }
6239 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 ga.ga_len += len;
6243 }
6244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006245 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 arg = skipwhite(arg);
6247 }
6248
6249 if (ret != FAIL && ga.ga_data != NULL)
6250 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006251 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006253 // Mark the already saved text as finishing the line, so that what
6254 // follows is displayed on a new line when scrolling back at the
6255 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006256 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006257 }
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006260 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006261 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006262 out_flush();
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 else if (eap->cmdidx == CMD_echoerr)
6265 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006266 int save_did_emsg = did_emsg;
6267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 if (!force_abort)
6271 did_emsg = save_did_emsg;
6272 }
6273 else if (eap->cmdidx == CMD_execute)
6274 do_cmdline((char_u *)ga.ga_data,
6275 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6276 }
6277
6278 ga_clear(&ga);
6279
6280 if (eap->skip)
6281 --emsg_skip;
6282
6283 eap->nextcmd = check_nextcmd(arg);
6284}
6285
6286/*
6287 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6288 * "arg" points to the "&" or '+' when called, to "option" when returning.
6289 * Returns NULL when no option name found. Otherwise pointer to the char
6290 * after the option name.
6291 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006292 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 char_u *p = *arg;
6296
6297 ++p;
6298 if (*p == 'g' && p[1] == ':')
6299 {
6300 *opt_flags = OPT_GLOBAL;
6301 p += 2;
6302 }
6303 else if (*p == 'l' && p[1] == ':')
6304 {
6305 *opt_flags = OPT_LOCAL;
6306 p += 2;
6307 }
6308 else
6309 *opt_flags = 0;
6310
6311 if (!ASCII_ISALPHA(*p))
6312 return NULL;
6313 *arg = p;
6314
6315 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 else
6318 while (ASCII_ISALPHA(*p))
6319 ++p;
6320 return p;
6321}
6322
6323/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006324 * Display script name where an item was last set.
6325 * Should only be invoked when 'verbose' is non-zero.
6326 */
6327 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006328last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006329{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006330 char_u *p;
6331
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006332 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006333 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006334 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006335 if (p != NULL)
6336 {
6337 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006338 msg_puts(_("\n\tLast set from "));
6339 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006340 if (script_ctx.sc_lnum > 0)
6341 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006342 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006343 msg_outnum((long)script_ctx.sc_lnum);
6344 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006345 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006346 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006347 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006348 }
6349}
6350
Bram Moolenaar61be3762019-03-19 23:04:17 +01006351/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006352 * Compare "typ1" and "typ2". Put the result in "typ1".
6353 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006354 int
6355typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006356 typval_T *typ1, // first operand
6357 typval_T *typ2, // second operand
6358 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006359 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006360{
6361 int i;
6362 varnumber_T n1, n2;
6363 char_u *s1, *s2;
6364 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006365 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006366
Bram Moolenaar31988702018-02-13 12:57:42 +01006367 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006369 // For "is" a different type always means FALSE, for "notis"
6370 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006371 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006372 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006373 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6374 {
6375 if (type_is)
6376 {
6377 n1 = (typ1->v_type == typ2->v_type
6378 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006379 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006380 n1 = !n1;
6381 }
6382 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006383 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006384 {
6385 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006386 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006387 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006388 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006389 clear_tv(typ1);
6390 return FAIL;
6391 }
6392 else
6393 {
6394 // Compare two Blobs for being equal or unequal.
6395 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006396 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006397 n1 = !n1;
6398 }
6399 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006400 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6401 {
6402 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006403 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006404 n1 = (typ1->v_type == typ2->v_type
6405 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006406 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006407 n1 = !n1;
6408 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006409 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006410 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006411 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006412 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006413 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006414 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006415 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006416 clear_tv(typ1);
6417 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006418 }
6419 else
6420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006421 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006422 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6423 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006424 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006425 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006426 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006427 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006428
6429 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6430 {
6431 if (type_is)
6432 {
6433 n1 = (typ1->v_type == typ2->v_type
6434 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006435 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006436 n1 = !n1;
6437 }
6438 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006439 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006440 {
6441 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006442 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006443 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006444 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006445 clear_tv(typ1);
6446 return FAIL;
6447 }
6448 else
6449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006450 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006451 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6452 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006453 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006454 n1 = !n1;
6455 }
6456 }
6457
6458 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6459 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6460 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006461 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6462 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006464 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006465 clear_tv(typ1);
6466 return FAIL;
6467 }
6468 if ((typ1->v_type == VAR_PARTIAL
6469 && typ1->vval.v_partial == NULL)
6470 || (typ2->v_type == VAR_PARTIAL
6471 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006472 // When both partials are NULL, then they are equal.
6473 // Otherwise they are not equal.
6474 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006475 else if (type_is)
6476 {
6477 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 // strings are considered the same if their value is
6479 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006480 n1 = tv_equal(typ1, typ2, ic, FALSE);
6481 else if (typ1->v_type == VAR_PARTIAL
6482 && typ2->v_type == VAR_PARTIAL)
6483 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6484 else
6485 n1 = FALSE;
6486 }
6487 else
6488 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006489 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006490 n1 = !n1;
6491 }
6492
6493#ifdef FEAT_FLOAT
6494 /*
6495 * If one of the two variables is a float, compare as a float.
6496 * When using "=~" or "!~", always compare as string.
6497 */
6498 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006499 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006500 {
6501 float_T f1, f2;
6502
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006503 f1 = tv_get_float(typ1);
6504 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006505 n1 = FALSE;
6506 switch (type)
6507 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006508 case EXPR_IS:
6509 case EXPR_EQUAL: n1 = (f1 == f2); break;
6510 case EXPR_ISNOT:
6511 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6512 case EXPR_GREATER: n1 = (f1 > f2); break;
6513 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6514 case EXPR_SMALLER: n1 = (f1 < f2); break;
6515 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6516 case EXPR_UNKNOWN:
6517 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006519 }
6520 }
6521#endif
6522
6523 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006524 * If one of the two variables is a number, compare as a number.
6525 * When using "=~" or "!~", always compare as string.
6526 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006527 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006528 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006530 n1 = tv_get_number(typ1);
6531 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006532 switch (type)
6533 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006534 case EXPR_IS:
6535 case EXPR_EQUAL: n1 = (n1 == n2); break;
6536 case EXPR_ISNOT:
6537 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6538 case EXPR_GREATER: n1 = (n1 > n2); break;
6539 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6540 case EXPR_SMALLER: n1 = (n1 < n2); break;
6541 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6542 case EXPR_UNKNOWN:
6543 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006545 }
6546 }
6547 else
6548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006549 s1 = tv_get_string_buf(typ1, buf1);
6550 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006551 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006552 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6553 else
6554 i = 0;
6555 n1 = FALSE;
6556 switch (type)
6557 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006558 case EXPR_IS:
6559 case EXPR_EQUAL: n1 = (i == 0); break;
6560 case EXPR_ISNOT:
6561 case EXPR_NEQUAL: n1 = (i != 0); break;
6562 case EXPR_GREATER: n1 = (i > 0); break;
6563 case EXPR_GEQUAL: n1 = (i >= 0); break;
6564 case EXPR_SMALLER: n1 = (i < 0); break;
6565 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006566
Bram Moolenaar87396072019-12-31 22:36:18 +01006567 case EXPR_MATCH:
6568 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006569 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006570 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006571 n1 = !n1;
6572 break;
6573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006575 }
6576 }
6577 clear_tv(typ1);
6578 typ1->v_type = VAR_NUMBER;
6579 typ1->vval.v_number = n1;
6580
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006581 return OK;
6582}
6583
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006584 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006585typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006586{
6587 char_u *tofree;
6588 char_u numbuf[NUMBUFLEN];
6589 char_u *ret = NULL;
6590
6591 if (arg == NULL)
6592 return vim_strsave((char_u *)"(does not exist)");
6593 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006595 if (ret != NULL && tofree == NULL)
6596 ret = vim_strsave(ret);
6597 return ret;
6598}
6599
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006600#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602/*
6603 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006604 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 * "flags" can be "g" to do a global substitute.
6606 * Returns an allocated string, NULL for error.
6607 */
6608 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609do_string_sub(
6610 char_u *str,
6611 char_u *pat,
6612 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006613 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 int sublen;
6617 regmatch_T regmatch;
6618 int i;
6619 int do_all;
6620 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006621 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 garray_T ga;
6623 char_u *ret;
6624 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006625 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006627 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006629 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631 ga_init2(&ga, 1, 200);
6632
6633 do_all = (flags[0] == 'g');
6634
6635 regmatch.rm_ic = p_ic;
6636 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6637 if (regmatch.regprog != NULL)
6638 {
6639 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006640 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006644 if (regmatch.startp[0] == regmatch.endp[0])
6645 {
6646 if (zero_width == regmatch.startp[0])
6647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006649 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006650 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6651 (size_t)i);
6652 ga.ga_len += i;
6653 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006654 continue;
6655 }
6656 zero_width = regmatch.startp[0];
6657 }
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 /*
6660 * Get some space for a temporary buffer to do the substitution
6661 * into. It will contain:
6662 * - The text up to where the match is.
6663 * - The substituted text.
6664 * - The text after the match.
6665 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006666 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006667 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6669 {
6670 ga_clear(&ga);
6671 break;
6672 }
6673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 i = (int)(regmatch.startp[0] - tail);
6676 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006677 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006678 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 + ga.ga_len + i, TRUE, TRUE, FALSE);
6680 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006681 tail = regmatch.endp[0];
6682 if (*tail == NUL)
6683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (!do_all)
6685 break;
6686 }
6687
6688 if (ga.ga_data != NULL)
6689 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6690
Bram Moolenaar473de612013-06-08 18:19:48 +02006691 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693
6694 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6695 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006696 if (p_cpo == empty_option)
6697 p_cpo = save_cpo;
6698 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006700 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 return ret;
6703}