blob: dbc10c1b69c097323601f65665dd63e09c43da8e [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 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200249 if (call_def_function(partial->pt_func, argc, argv,
250 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251 return FAIL;
252 }
253 else
254 {
255 s = partial_name(partial);
256 if (s == NULL || *s == NUL)
257 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200258 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 funcexe.evaluate = TRUE;
260 funcexe.partial = partial;
261 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
262 return FAIL;
263 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100264 }
265 else
266 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100267 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 if (s == NULL)
269 return FAIL;
270 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100271 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100272 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100273 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100274 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200275 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100276 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 return FAIL;
278 }
279 }
280 return OK;
281}
282
283/*
284 * Like eval_to_bool() but using a typval_T instead of a string.
285 * Works for string, funcref and partial.
286 */
287 int
288eval_expr_to_bool(typval_T *expr, int *error)
289{
290 typval_T rettv;
291 int res;
292
293 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
294 {
295 *error = TRUE;
296 return FALSE;
297 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 clear_tv(&rettv);
300 return res;
301}
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * Top level evaluation function, returning a string. If "skip" is TRUE,
305 * only parsing to "nextcmd" is done, without reporting errors. Return
306 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
307 */
308 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100309eval_to_string_skip(
310 char_u *arg,
311 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100312 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313{
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 char_u *retval;
316
317 if (skip)
318 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200319 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 retval = NULL;
321 else
322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100323 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000324 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
326 if (skip)
327 --emsg_skip;
328
329 return retval;
330}
331
332/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000333 * Skip over an expression at "*pp".
334 * Return FAIL for an error, OK otherwise.
335 */
336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100337skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000338{
Bram Moolenaar33570922005-01-25 22:26:29 +0000339 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000340
341 *pp = skipwhite(*pp);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200342 return eval1(pp, &rettv, 0);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000343}
344
345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000347 * When "convert" is TRUE convert a List into a sequence of lines and convert
348 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 * Return pointer to allocated memory, or NULL for failure.
350 */
351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100352eval_to_string(
353 char_u *arg,
354 char_u **nextcmd,
355 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
Bram Moolenaar33570922005-01-25 22:26:29 +0000357 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000359 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000360#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000361 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar32e35112020-05-14 22:41:15 +0200364 if (eval0(arg, &tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 retval = NULL;
366 else
367 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000368 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000369 {
370 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000371 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200372 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200373 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200374 if (tv.vval.v_list->lv_len > 0)
375 ga_append(&ga, NL);
376 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000377 ga_append(&ga, NUL);
378 retval = (char_u *)ga.ga_data;
379 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000380#ifdef FEAT_FLOAT
381 else if (convert && tv.v_type == VAR_FLOAT)
382 {
383 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
384 retval = vim_strsave(numbuf);
385 }
386#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000387 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100388 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000389 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 }
391
392 return retval;
393}
394
395/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000396 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200397 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 */
399 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100400eval_to_string_safe(
401 char_u *arg,
402 char_u **nextcmd,
403 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
405 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200406 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200408 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000409 if (use_sandbox)
410 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200411 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000412 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000413 if (use_sandbox)
414 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200415 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200416 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 return retval;
418}
419
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420/*
421 * Top level evaluation function, returning a number.
422 * Evaluates "expr" silently.
423 * Returns -1 for an error.
424 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200425 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100426eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
Bram Moolenaar33570922005-01-25 22:26:29 +0000428 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200429 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000430 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
432 ++emsg_off;
433
Bram Moolenaar32e35112020-05-14 22:41:15 +0200434 if (eval1(&p, &rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 retval = -1;
436 else
437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100438 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
441 --emsg_off;
442
443 return retval;
444}
445
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000446/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000447 * Top level evaluation function.
448 * Returns an allocated typval_T with the result.
449 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000450 */
451 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100452eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000453{
454 typval_T *tv;
455
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200456 tv = ALLOC_ONE(typval_T);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200457 if (tv != NULL && eval0(arg, tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100458 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000459
460 return tv;
461}
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100464 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200465 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
466 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000467 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100470call_vim_function(
471 char_u *func,
472 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200473 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200474 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000476 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200477 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100479 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200480 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200481 funcexe.firstline = curwin->w_cursor.lnum;
482 funcexe.lastline = curwin->w_cursor.lnum;
483 funcexe.evaluate = TRUE;
484 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000485 if (ret == FAIL)
486 clear_tv(rettv);
487
488 return ret;
489}
490
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100491/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100492 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100493 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200494 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
495 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100496 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200497 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100498call_func_retnr(
499 char_u *func,
500 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200501 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100502{
503 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200504 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100505
Bram Moolenaarded27a12018-08-01 19:06:03 +0200506 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100507 return -1;
508
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100509 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100510 clear_tv(&rettv);
511 return retval;
512}
513
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000514/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100515 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000516 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200517 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
518 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000519 */
520 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521call_func_retstr(
522 char_u *func,
523 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200524 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000525{
526 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000527 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000528
Bram Moolenaarded27a12018-08-01 19:06:03 +0200529 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000530 return NULL;
531
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100532 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 return retval;
535}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000536
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000537/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100538 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200539 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
540 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000541 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000542 */
543 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100544call_func_retlist(
545 char_u *func,
546 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200547 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000548{
549 typval_T rettv;
550
Bram Moolenaarded27a12018-08-01 19:06:03 +0200551 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000552 return NULL;
553
554 if (rettv.v_type != VAR_LIST)
555 {
556 clear_tv(&rettv);
557 return NULL;
558 }
559
560 return rettv.vval.v_list;
561}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_FOLDING
564/*
565 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
566 * it in "*cp". Doesn't give error messages.
567 */
568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100569eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
Bram Moolenaar33570922005-01-25 22:26:29 +0000571 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200572 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000574 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
575 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000578 if (use_sandbox)
579 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200580 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 *cp = NUL;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200582 if (eval0(arg, &tv, NULL, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 retval = 0;
584 else
585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100586 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000587 if (tv.v_type == VAR_NUMBER)
588 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000589 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 retval = 0;
591 else
592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100593 // If the result is a string, check if there is a non-digit before
594 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (!VIM_ISDIGIT(*s) && *s != '-')
597 *cp = *s++;
598 retval = atol((char *)s);
599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603 if (use_sandbox)
604 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200605 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200607 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000612 * Get an lval: variable, Dict item or List item that can be assigned a value
613 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
614 * "name.key", "name.key[expr]" etc.
615 * Indexing only works if "name" is an existing List or Dictionary.
616 * "name" points to the start of the name.
617 * If "rettv" is not NULL it points to the value to be assigned.
618 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
619 * wrong; must end in space or cmd separator.
620 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100621 * flags:
622 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100623 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100624 * GLV_NO_AUTOLOAD: do not use script autoloading
625 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000626 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000627 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000628 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000629 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200630 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631get_lval(
632 char_u *name,
633 typval_T *rettv,
634 lval_T *lp,
635 int unlet,
636 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100637 int flags, // GLV_ values
638 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000640 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000641 char_u *expr_start, *expr_end;
642 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000643 dictitem_T *v;
644 typval_T var1;
645 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000646 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000647 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000648 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000649 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000650 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100651 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100653 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200654 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000655
656 if (skip)
657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000659 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000660 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000661 }
662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100663 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000664 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000666 if (expr_start != NULL)
667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100668 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100669 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000670 && *p != '[' && *p != '.')
671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000673 return NULL;
674 }
675
676 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
677 if (lp->ll_exp_name == NULL)
678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100679 // Report an invalid expression in braces, unless the
680 // expression evaluation has been cancelled due to an
681 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000682 if (!aborting() && !quiet)
683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000684 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100685 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000686 return NULL;
687 }
688 }
689 lp->ll_name = lp->ll_exp_name;
690 }
691 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000693 lp->ll_name = name;
694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
696 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100697 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 char_u *tp = skipwhite(p + 1);
699
700 // parse the type after the name
701 lp->ll_type = parse_type(&tp, &si->sn_type_list);
702 lp->ll_name_end = tp;
703 }
704 }
705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100706 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000707 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
708 return p;
709
710 cc = *p;
711 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100712 // Only pass &ht when we would write to the variable, it prevents autoload
713 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100714 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
715 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 if (v == NULL)
720 return NULL;
721
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000722 /*
723 * Loop until no more [idx] or .key is following.
724 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100726 var1.v_type = VAR_UNKNOWN;
727 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000730 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
731 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100732 && lp->ll_tv->vval.v_dict != NULL)
733 && !(lp->ll_tv->v_type == VAR_BLOB
734 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100737 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000742 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100743 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000744 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000745 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000746
Bram Moolenaar8c711452005-01-14 21:53:12 +0000747 len = -1;
748 if (*p == '.')
749 {
750 key = p + 1;
751 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
752 ;
753 if (len == 0)
754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100756 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000758 }
759 p = key + len;
760 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000761 else
762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000764 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000765 if (*p == ':')
766 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000767 else
768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000769 empty1 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200770 if (eval1(&p, &var1, EVAL_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000771 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100772 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775 clear_tv(&var1);
776 return NULL;
777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 }
779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 if (*p == ':')
782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000783 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100786 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000789 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100790 if (rettv != NULL
791 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100792 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100793 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100794 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100797 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000800 }
801 p = skipwhite(p + 1);
802 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000804 else
805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200807 // recursive!
808 if (eval1(&p, &var2, EVAL_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000812 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100813 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100816 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000817 clear_tv(&var2);
818 return NULL;
819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000825
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100830 clear_tv(&var1);
831 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000833 }
834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000836 ++p;
837 }
838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 {
841 if (len == -1)
842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // "[key]": get key from "var1"
844 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200845 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000849 }
850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000851 lp->ll_list = NULL;
852 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000853 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200854
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100855 // When assigning to a scope dictionary check that a function and
856 // variable name is valid (only variable name unless it is l: or
857 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200859 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200860 int prevval;
861 int wrong;
862
863 if (len != -1)
864 {
865 prevval = key[len];
866 key[len] = NUL;
867 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200868 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100869 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
871 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200872 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200873 || !valid_varname(key);
874 if (len != -1)
875 key[len] = prevval;
876 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200877 {
878 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200879 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200880 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200881 }
882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000884 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100885 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200886 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100887 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100889 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100890 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200891 return NULL;
892 }
893
Bram Moolenaar31b81602019-02-10 22:14:27 +0100894 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100899 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 }
902 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000904 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100906 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000908 p = NULL;
909 break;
910 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100912 else if ((flags & GLV_READ_ONLY) == 0
913 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100914 {
915 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200916 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100917 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200918
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100919 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000921 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100922 else if (lp->ll_tv->v_type == VAR_BLOB)
923 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100924 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
925
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100926 /*
927 * Get the number and item for the only or first index of the List.
928 */
929 if (empty1)
930 lp->ll_n1 = 0;
931 else
932 // is number or string
933 lp->ll_n1 = (long)tv_get_number(&var1);
934 clear_tv(&var1);
935
936 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 || lp->ll_n1 > bloblen
938 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100939 {
940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100942 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100943 return NULL;
944 }
945 if (lp->ll_range && !lp->ll_empty2)
946 {
947 lp->ll_n2 = (long)tv_get_number(&var2);
948 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100949 if (lp->ll_n2 < 0
950 || lp->ll_n2 >= bloblen
951 || lp->ll_n2 < lp->ll_n1)
952 {
953 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100954 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100955 return NULL;
956 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100957 }
958 lp->ll_blob = lp->ll_tv->vval.v_blob;
959 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100960 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100961 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000962 else
963 {
964 /*
965 * Get the number and item for the only or first index of the List.
966 */
967 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100970 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100971 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100972 clear_tv(&var1);
973
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000974 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 lp->ll_list = lp->ll_tv->vval.v_list;
976 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
977 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000979 if (lp->ll_n1 < 0)
980 {
981 lp->ll_n1 = 0;
982 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
983 }
984 }
985 if (lp->ll_li == NULL)
986 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100987 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200988 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100989 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 }
992
993 /*
994 * May need to find the item or absolute index for the second
995 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 * When no index given: "lp->ll_empty2" is TRUE.
997 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001001 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001008 {
1009 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001010 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 }
1015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001016 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (lp->ll_n1 < 0)
1018 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1019 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001020 {
1021 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001022 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001024 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001025 }
1026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001029 }
1030
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001031 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return p;
1034}
1035
1036/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041{
1042 vim_free(lp->ll_exp_name);
1043 vim_free(lp->ll_newkey);
1044}
1045
1046/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001047 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001049 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1050 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053set_var_lval(
1054 lval_T *lp,
1055 char_u *endp,
1056 typval_T *rettv,
1057 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001059 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060{
1061 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001062 listitem_T *ri;
1063 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064
1065 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001067 cc = *endp;
1068 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001069 if (lp->ll_blob != NULL)
1070 {
1071 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001072
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001073 if (op != NULL && *op != '=')
1074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001075 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001076 return;
1077 }
1078
1079 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1080 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001081 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001082
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001083 if (lp->ll_empty2)
1084 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1085
1086 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001088 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001089 return;
1090 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001091 if (lp->ll_empty2)
1092 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001093
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001094 ir = 0;
1095 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1096 blob_set(lp->ll_blob, il,
1097 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001098 }
1099 else
1100 {
1101 val = (int)tv_get_number_chk(rettv, &error);
1102 if (!error)
1103 {
1104 garray_T *gap = &lp->ll_blob->bv_ga;
1105
1106 // Allow for appending a byte. Setting a byte beyond
1107 // the end is an error otherwise.
1108 if (lp->ll_n1 < gap->ga_len
1109 || (lp->ll_n1 == gap->ga_len
1110 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1111 {
1112 blob_set(lp->ll_blob, lp->ll_n1, val);
1113 if (lp->ll_n1 == gap->ga_len)
1114 ++gap->ga_len;
1115 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001116 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001117 }
1118 }
1119 }
1120 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001122 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001125 {
1126 emsg(_(e_cannot_mod));
1127 *endp = cc;
1128 return;
1129 }
1130
Bram Moolenaarff697e62019-02-12 22:28:33 +01001131 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001132 di = NULL;
1133 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1134 &tv, &di, TRUE, FALSE) == OK)
1135 {
1136 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001137 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1138 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001139 && tv_op(&tv, rettv, op) == OK)
1140 set_var(lp->ll_name, &tv, FALSE);
1141 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001144 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001146 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001148 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001149 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001150 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001151 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 else if (lp->ll_range)
1153 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001154 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001155 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001158 {
1159 emsg(_("E996: Cannot lock a range"));
1160 return;
1161 }
1162
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001163 /*
1164 * Check whether any of the list items is locked
1165 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001166 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001167 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001168 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001169 return;
1170 ri = ri->li_next;
1171 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1172 break;
1173 ll_li = ll_li->li_next;
1174 ++ll_n1;
1175 }
1176
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 /*
1178 * Assign the List values to the list items.
1179 */
1180 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001181 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001182 if (op != NULL && *op != '=')
1183 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1184 else
1185 {
1186 clear_tv(&lp->ll_li->li_tv);
1187 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1188 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 ri = ri->li_next;
1190 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1191 break;
1192 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001194 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001195 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 ri = NULL;
1198 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001199 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001200 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_li = lp->ll_li->li_next;
1202 ++lp->ll_n1;
1203 }
1204 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001206 else if (lp->ll_empty2
1207 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 }
1211 else
1212 {
1213 /*
1214 * Assign to a List or Dictionary item.
1215 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001217 {
1218 emsg(_("E996: Cannot lock a list or dict"));
1219 return;
1220 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 if (lp->ll_newkey != NULL)
1222 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 if (op != NULL && *op != '=')
1224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001226 return;
1227 }
1228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001230 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (di == NULL)
1232 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001233 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1234 {
1235 vim_free(di);
1236 return;
1237 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001240 else if (op != NULL && *op != '=')
1241 {
1242 tv_op(lp->ll_tv, rettv, op);
1243 return;
1244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001245 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 /*
1249 * Assign the value to the variable or list item.
1250 */
1251 if (copy)
1252 copy_tv(rettv, lp->ll_tv);
1253 else
1254 {
1255 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001256 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 }
1259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260}
1261
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001262/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001263 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1264 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001265 * Returns OK or FAIL.
1266 */
1267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001269{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001270 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 char_u numbuf[NUMBUFLEN];
1272 char_u *s;
1273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001274 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001275 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001276 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 {
1278 switch (tv1->v_type)
1279 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001280 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001281 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 case VAR_DICT:
1284 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001285 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001286 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001287 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001288 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001289 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001290 break;
1291
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 case VAR_BLOB:
1293 if (*op != '+' || tv2->v_type != VAR_BLOB)
1294 break;
1295 // BLOB += BLOB
1296 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1297 {
1298 blob_T *b1 = tv1->vval.v_blob;
1299 blob_T *b2 = tv2->vval.v_blob;
1300 int i, len = blob_len(b2);
1301 for (i = 0; i < len; i++)
1302 ga_append(&b1->bv_ga, blob_get(b2, i));
1303 }
1304 return OK;
1305
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001306 case VAR_LIST:
1307 if (*op != '+' || tv2->v_type != VAR_LIST)
1308 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001309 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1311 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1312 return OK;
1313
1314 case VAR_NUMBER:
1315 case VAR_STRING:
1316 if (tv2->v_type == VAR_LIST)
1317 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001318 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001319 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001320 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001321 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001322#ifdef FEAT_FLOAT
1323 if (tv2->v_type == VAR_FLOAT)
1324 {
1325 float_T f = n;
1326
Bram Moolenaarff697e62019-02-12 22:28:33 +01001327 if (*op == '%')
1328 break;
1329 switch (*op)
1330 {
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 case '/': f /= tv2->vval.v_float; break;
1335 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001336 clear_tv(tv1);
1337 tv1->v_type = VAR_FLOAT;
1338 tv1->vval.v_float = f;
1339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001340 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001341#endif
1342 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001343 switch (*op)
1344 {
1345 case '+': n += tv_get_number(tv2); break;
1346 case '-': n -= tv_get_number(tv2); break;
1347 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001348 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1349 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001350 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001351 clear_tv(tv1);
1352 tv1->v_type = VAR_NUMBER;
1353 tv1->vval.v_number = n;
1354 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 }
1356 else
1357 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001358 if (tv2->v_type == VAR_FLOAT)
1359 break;
1360
Bram Moolenaarff697e62019-02-12 22:28:33 +01001361 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001362 s = tv_get_string(tv1);
1363 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 clear_tv(tv1);
1365 tv1->v_type = VAR_STRING;
1366 tv1->vval.v_string = s;
1367 }
1368 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001369
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001370 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001371#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001372 {
1373 float_T f;
1374
Bram Moolenaarff697e62019-02-12 22:28:33 +01001375 if (*op == '%' || *op == '.'
1376 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001377 && tv2->v_type != VAR_NUMBER
1378 && tv2->v_type != VAR_STRING))
1379 break;
1380 if (tv2->v_type == VAR_FLOAT)
1381 f = tv2->vval.v_float;
1382 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001383 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001384 switch (*op)
1385 {
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 case '/': tv1->vval.v_float /= f; break;
1390 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001391 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001392#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001393 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 }
1395 }
1396
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001397 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 return FAIL;
1399}
1400
1401/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 * Evaluate the expression used in a ":for var in expr" command.
1403 * "arg" points to "var".
1404 * Set "*errp" to TRUE for an error, FALSE otherwise;
1405 * Return a pointer that holds the info. Null when there is an error.
1406 */
1407 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001408eval_for_line(
1409 char_u *arg,
1410 int *errp,
1411 char_u **nextcmdp,
1412 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T tv;
1417 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001419 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001421 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 if (fi == NULL)
1423 return NULL;
1424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 if (expr == NULL)
1427 return fi;
1428
1429 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001430 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 return fi;
1434 }
1435
1436 if (skip)
1437 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001438 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, skip ? 0 : EVAL_EVALUATE)
1439 == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001440 {
1441 *errp = FALSE;
1442 if (!skip)
1443 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001444 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001445 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001446 l = tv.vval.v_list;
1447 if (l == NULL)
1448 {
1449 // a null list is like an empty list: do nothing
1450 clear_tv(&tv);
1451 }
1452 else
1453 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001455 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 // No need to increment the refcount, it's already set for
1458 // the list being used in "tv".
1459 fi->fi_list = l;
1460 list_add_watch(l, &fi->fi_lw);
1461 fi->fi_lw.lw_item = l->lv_first;
1462 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001463 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001464 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001465 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001466 fi->fi_bi = 0;
1467 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001469 typval_T btv;
1470
1471 // Make a copy, so that the iteration still works when the
1472 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001474 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001476 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001477 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478 else
1479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001480 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482 }
1483 }
1484 }
1485 if (skip)
1486 --emsg_skip;
1487
1488 return fi;
1489}
1490
1491/*
1492 * Use the first item in a ":for" list. Advance to the next.
1493 * Assign the values to the variable (list). "arg" points to the first one.
1494 * Return TRUE when a valid item was found, FALSE when at end of list or
1495 * something wrong.
1496 */
1497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001500 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001502 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1503 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001504 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001506 if (fi->fi_blob != NULL)
1507 {
1508 typval_T tv;
1509
1510 if (fi->fi_bi >= blob_len(fi->fi_blob))
1511 return FALSE;
1512 tv.v_type = VAR_NUMBER;
1513 tv.v_lock = VAR_FIXED;
1514 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1515 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001516 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001517 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001518 }
1519
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520 item = fi->fi_lw.lw_item;
1521 if (item == NULL)
1522 result = FALSE;
1523 else
1524 {
1525 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001526 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001527 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 }
1529 return result;
1530}
1531
1532/*
1533 * Free the structure used to store info used by ":for".
1534 */
1535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537{
Bram Moolenaar33570922005-01-25 22:26:29 +00001538 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001540 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001541 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001542 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001543 list_unref(fi->fi_list);
1544 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001545 if (fi != NULL && fi->fi_blob != NULL)
1546 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 vim_free(fi);
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001551set_context_for_expression(
1552 expand_T *xp,
1553 char_u *arg,
1554 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 int got_eq = FALSE;
1557 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001560 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
1562 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001565 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 {
1568 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001569 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001570 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571 break;
1572 }
1573 return;
1574 }
1575 }
1576 else
1577 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1578 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 while ((xp->xp_pattern = vim_strpbrk(arg,
1580 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1581 {
1582 c = *xp->xp_pattern;
1583 if (c == '&')
1584 {
1585 c = xp->xp_pattern[1];
1586 if (c == '&')
1587 {
1588 ++xp->xp_pattern;
1589 xp->xp_context = cmdidx != CMD_let || got_eq
1590 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1591 }
1592 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001595 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1596 xp->xp_pattern += 2;
1597
1598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 else if (c == '$')
1601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001602 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 xp->xp_context = EXPAND_ENV_VARS;
1604 }
1605 else if (c == '=')
1606 {
1607 got_eq = TRUE;
1608 xp->xp_context = EXPAND_EXPRESSION;
1609 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001610 else if (c == '#'
1611 && xp->xp_context == EXPAND_EXPRESSION)
1612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001613 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001614 break;
1615 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001616 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 && xp->xp_context == EXPAND_FUNCTIONS
1618 && vim_strchr(xp->xp_pattern, '(') == NULL)
1619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001620 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 break;
1622 }
1623 else if (cmdidx != CMD_let || got_eq)
1624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001625 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1628 if (c == '\\' && xp->xp_pattern[1] != NUL)
1629 ++xp->xp_pattern;
1630 xp->xp_context = EXPAND_NOTHING;
1631 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001632 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001634 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1636 /* skip */ ;
1637 xp->xp_context = EXPAND_NOTHING;
1638 }
1639 else if (c == '|')
1640 {
1641 if (xp->xp_pattern[1] == '|')
1642 {
1643 ++xp->xp_pattern;
1644 xp->xp_context = EXPAND_EXPRESSION;
1645 }
1646 else
1647 xp->xp_context = EXPAND_COMMANDS;
1648 }
1649 else
1650 xp->xp_context = EXPAND_EXPRESSION;
1651 }
1652 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001653 // Doesn't look like something valid, expand as an expression
1654 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 arg = xp->xp_pattern;
1657 if (*arg != NUL)
1658 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1659 /* skip */ ;
1660 }
1661 xp->xp_pattern = arg;
1662}
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001665 * Return TRUE if "pat" matches "text".
1666 * Does not use 'cpo' and always uses 'magic'.
1667 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001668 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001669pattern_match(char_u *pat, char_u *text, int ic)
1670{
1671 int matches = FALSE;
1672 char_u *save_cpo;
1673 regmatch_T regmatch;
1674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001676 save_cpo = p_cpo;
1677 p_cpo = (char_u *)"";
1678 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1679 if (regmatch.regprog != NULL)
1680 {
1681 regmatch.rm_ic = ic;
1682 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1683 vim_regfree(regmatch.regprog);
1684 }
1685 p_cpo = save_cpo;
1686 return matches;
1687}
1688
1689/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001690 * Handle a name followed by "(". Both for just "name(arg)" and for
1691 * "expr->name(arg)".
1692 * Returns OK or FAIL.
1693 */
1694 static int
1695eval_func(
1696 char_u **arg, // points to "(", will be advanced
1697 char_u *name,
1698 int name_len,
1699 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001700 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001701 typval_T *basetv) // "expr" for "expr->name(arg)"
1702{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001703 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001704 char_u *s = name;
1705 int len = name_len;
1706 partial_T *partial;
1707 int ret = OK;
1708
1709 if (!evaluate)
1710 check_vars(s, len);
1711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // If "s" is the name of a variable of type VAR_FUNC
1713 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001714 s = deref_func_name(s, &len, &partial, !evaluate);
1715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001716 // Need to make a copy, in case evaluating the arguments makes
1717 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001718 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001719 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001720 ret = FAIL;
1721 else
1722 {
1723 funcexe_T funcexe;
1724
1725 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001726 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001727 funcexe.firstline = curwin->w_cursor.lnum;
1728 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001729 funcexe.evaluate = evaluate;
1730 funcexe.partial = partial;
1731 funcexe.basetv = basetv;
1732 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1733 }
1734 vim_free(s);
1735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001736 // If evaluate is FALSE rettv->v_type was not set in
1737 // get_func_tv, but it's needed in handle_subscript() to parse
1738 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001739 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1740 {
1741 rettv->vval.v_string = NULL;
1742 rettv->v_type = VAR_FUNC;
1743 }
1744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001745 // Stop the expression evaluation when immediately
1746 // aborting on error, or when an interrupt occurred or
1747 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001748 if (evaluate && aborting())
1749 {
1750 if (ret == OK)
1751 clear_tv(rettv);
1752 ret = FAIL;
1753 }
1754 return ret;
1755}
1756
1757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1761 */
1762
1763/*
1764 * Handle zero level expression.
1765 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001767 * Note: "rettv.v_lock" is not set.
Bram Moolenaar32e35112020-05-14 22:41:15 +02001768 * "flags" has EVAL_EVALUATE and similar flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * Return OK or FAIL.
1770 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772eval0(
1773 char_u *arg,
1774 typval_T *rettv,
1775 char_u **nextcmd,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001776 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 int ret;
1779 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001780 int did_emsg_before = did_emsg;
1781 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 p = skipwhite(arg);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001784 ret = eval1(&p, rettv, flags);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001785 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
1787 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 /*
1790 * Report the invalid expression unless the expression evaluation has
1791 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001792 * exception, or we already gave a more specific error.
1793 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001795 if (!aborting()
1796 && did_emsg == did_emsg_before
1797 && called_emsg == called_emsg_before
1798 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001799 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 ret = FAIL;
1801 }
1802 if (nextcmd != NULL)
1803 *nextcmd = check_nextcmd(p);
1804
1805 return ret;
1806}
1807
1808/*
1809 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001810 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 *
1812 * "arg" must point to the first non-white of the expression.
1813 * "arg" is advanced to the next non-white after the recognized expression.
1814 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001815 * Note: "rettv.v_lock" is not set.
1816 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 * Return OK or FAIL.
1818 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001819 int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001820eval1(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001823 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 /*
1826 * Get the first variable.
1827 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001828 if (eval2(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 return FAIL;
1830
1831 if ((*arg)[0] == '?')
1832 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001833 int evaluate = flags & EVAL_EVALUATE;
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 result = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001836 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001838 int error = FALSE;
1839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001840 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001843 if (error)
1844 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846
1847 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001848 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 */
1850 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001851 if (eval1(arg, rettv, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 return FAIL;
1853
1854 /*
1855 * Check for the ":".
1856 */
1857 if ((*arg)[0] != ':')
1858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
1863 }
1864
1865 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001866 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001869 if (eval1(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
1871 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 return FAIL;
1874 }
1875 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878
1879 return OK;
1880}
1881
1882/*
1883 * Handle first level expression:
1884 * expr2 || expr2 || expr2 logical OR
1885 *
1886 * "arg" must point to the first non-white of the expression.
1887 * "arg" is advanced to the next non-white after the recognized expression.
1888 *
1889 * Return OK or FAIL.
1890 */
1891 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001892eval2(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 long result;
1896 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
1899 /*
1900 * Get the first variable.
1901 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001902 if (eval3(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 return FAIL;
1904
1905 /*
1906 * Repeat until there is no following "||".
1907 */
1908 first = TRUE;
1909 result = FALSE;
1910 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1911 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001912 int evaluate = flags & EVAL_EVALUATE;
1913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (evaluate && first)
1915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001919 if (error)
1920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 first = FALSE;
1922 }
1923
1924 /*
1925 * Get the second variable.
1926 */
1927 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001928 if (eval3(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE)
1929 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 return FAIL;
1931
1932 /*
1933 * Compute the result.
1934 */
1935 if (evaluate && !result)
1936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001937 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 if (error)
1941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 if (evaluate)
1944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 rettv->v_type = VAR_NUMBER;
1946 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 }
1949
1950 return OK;
1951}
1952
1953/*
1954 * Handle second level expression:
1955 * expr3 && expr3 && expr3 logical AND
1956 *
1957 * "arg" must point to the first non-white of the expression.
1958 * "arg" is advanced to the next non-white after the recognized expression.
1959 *
1960 * Return OK or FAIL.
1961 */
1962 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001963eval3(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 long result;
1967 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970 /*
1971 * Get the first variable.
1972 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001973 if (eval4(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return FAIL;
1975
1976 /*
1977 * Repeat until there is no following "&&".
1978 */
1979 first = TRUE;
1980 result = TRUE;
1981 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1982 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001983 int evaluate = flags & EVAL_EVALUATE;
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (evaluate && first)
1986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001987 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 if (error)
1991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 first = FALSE;
1993 }
1994
1995 /*
1996 * Get the second variable.
1997 */
1998 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001999 if (eval4(arg, &var2, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return FAIL;
2001
2002 /*
2003 * Compute the result.
2004 */
2005 if (evaluate && result)
2006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002007 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 if (error)
2011 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013 if (evaluate)
2014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 rettv->v_type = VAR_NUMBER;
2016 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 }
2019
2020 return OK;
2021}
2022
2023/*
2024 * Handle third level expression:
2025 * var1 == var2
2026 * var1 =~ var2
2027 * var1 != var2
2028 * var1 !~ var2
2029 * var1 > var2
2030 * var1 >= var2
2031 * var1 < var2
2032 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002033 * var1 is var2
2034 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 *
2036 * "arg" must point to the first non-white of the expression.
2037 * "arg" is advanced to the next non-white after the recognized expression.
2038 *
2039 * Return OK or FAIL.
2040 */
2041 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002042eval4(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
Bram Moolenaar33570922005-01-25 22:26:29 +00002044 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 char_u *p;
2046 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002047 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /*
2052 * Get the first variable.
2053 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002054 if (eval5(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 return FAIL;
2056
2057 p = *arg;
2058 switch (p[0])
2059 {
2060 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002061 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002063 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 break;
2065 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002066 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002068 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 break;
2070 case '>': if (p[1] != '=')
2071 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002072 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 len = 1;
2074 }
2075 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002076 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 break;
2078 case '<': if (p[1] != '=')
2079 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002080 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 len = 1;
2082 }
2083 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002084 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002086 case 'i': if (p[1] == 's')
2087 {
2088 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2089 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002090 i = p[len];
2091 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002092 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002093 }
2094 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096
2097 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002098 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002100 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002102 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (p[len] == '?')
2104 {
2105 ic = TRUE;
2106 ++len;
2107 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002108 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 else if (p[len] == '#')
2110 {
2111 ic = FALSE;
2112 ++len;
2113 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002114 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 else
2116 ic = p_ic;
2117
2118 /*
2119 * Get the second variable.
2120 */
2121 *arg = skipwhite(p + len);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002122 if (eval5(arg, &var2, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 return FAIL;
2126 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002127 if (flags & EVAL_EVALUATE)
Bram Moolenaar31988702018-02-13 12:57:42 +01002128 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002129 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002130
2131 clear_tv(&var2);
2132 return ret;
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
2136 return OK;
2137}
2138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 void
2140eval_addblob(typval_T *tv1, typval_T *tv2)
2141{
2142 blob_T *b1 = tv1->vval.v_blob;
2143 blob_T *b2 = tv2->vval.v_blob;
2144 blob_T *b = blob_alloc();
2145 int i;
2146
2147 if (b != NULL)
2148 {
2149 for (i = 0; i < blob_len(b1); i++)
2150 ga_append(&b->bv_ga, blob_get(b1, i));
2151 for (i = 0; i < blob_len(b2); i++)
2152 ga_append(&b->bv_ga, blob_get(b2, i));
2153
2154 clear_tv(tv1);
2155 rettv_blob_set(tv1, b);
2156 }
2157}
2158
2159 int
2160eval_addlist(typval_T *tv1, typval_T *tv2)
2161{
2162 typval_T var3;
2163
2164 // concatenate Lists
2165 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2166 {
2167 clear_tv(tv1);
2168 clear_tv(tv2);
2169 return FAIL;
2170 }
2171 clear_tv(tv1);
2172 *tv1 = var3;
2173 return OK;
2174}
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176/*
2177 * Handle fourth level expression:
2178 * + number addition
2179 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002180 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002181 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 *
2183 * "arg" must point to the first non-white of the expression.
2184 * "arg" is advanced to the next non-white after the recognized expression.
2185 *
2186 * Return OK or FAIL.
2187 */
2188 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002189eval5(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
Bram Moolenaar33570922005-01-25 22:26:29 +00002191 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002193 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002194#ifdef FEAT_FLOAT
2195 float_T f1 = 0, f2 = 0;
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 char_u *s1, *s2;
2198 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2199 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002200 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 /*
2203 * Get the first variable.
2204 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002205 if (eval6(arg, rettv, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 return FAIL;
2207
2208 /*
2209 * Repeat computing, until no '+', '-' or '.' is following.
2210 */
2211 for (;;)
2212 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002213 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002215 concat = op == '.'
2216 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2217 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 break;
2219
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002220 if ((op != '+' || (rettv->v_type != VAR_LIST
2221 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002222#ifdef FEAT_FLOAT
2223 && (op == '.' || rettv->v_type != VAR_FLOAT)
2224#endif
2225 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002227 // For "list + ...", an illegal use of the first operand as
2228 // a number cannot be determined before evaluating the 2nd
2229 // operand: if this is also a list, all is ok.
2230 // For "something . ...", "something - ..." or "non-list + ...",
2231 // we know that the first operand needs to be a string or number
2232 // without evaluating the 2nd operand. So check before to avoid
2233 // side effects after an error.
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234 if ((flags & EVAL_EVALUATE) && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 {
2236 clear_tv(rettv);
2237 return FAIL;
2238 }
2239 }
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 /*
2242 * Get the second variable.
2243 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002244 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2245 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002247 if (eval6(arg, &var2, flags, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return FAIL;
2251 }
2252
Bram Moolenaar32e35112020-05-14 22:41:15 +02002253 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 /*
2256 * Compute the result.
2257 */
2258 if (op == '.')
2259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002261 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002262 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002263 {
2264 clear_tv(rettv);
2265 clear_tv(&var2);
2266 return FAIL;
2267 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 clear_tv(rettv);
2270 rettv->v_type = VAR_STRING;
2271 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002273 else if (op == '+' && rettv->v_type == VAR_BLOB
2274 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002276 else if (op == '+' && rettv->v_type == VAR_LIST
2277 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002278 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002280 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
2283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002284 int error = FALSE;
2285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002286#ifdef FEAT_FLOAT
2287 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002289 f1 = rettv->vval.v_float;
2290 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002291 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002292 else
2293#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002295 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002296 if (error)
2297 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002298 // This can only happen for "list + non-list". For
2299 // "non-list + ..." or "something - ...", we returned
2300 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002301 clear_tv(rettv);
2302 return FAIL;
2303 }
2304#ifdef FEAT_FLOAT
2305 if (var2.v_type == VAR_FLOAT)
2306 f1 = n1;
2307#endif
2308 }
2309#ifdef FEAT_FLOAT
2310 if (var2.v_type == VAR_FLOAT)
2311 {
2312 f2 = var2.vval.v_float;
2313 n2 = 0;
2314 }
2315 else
2316#endif
2317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002318 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002319 if (error)
2320 {
2321 clear_tv(rettv);
2322 clear_tv(&var2);
2323 return FAIL;
2324 }
2325#ifdef FEAT_FLOAT
2326 if (rettv->v_type == VAR_FLOAT)
2327 f2 = n2;
2328#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002331
2332#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002333 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002334 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2335 {
2336 if (op == '+')
2337 f1 = f1 + f2;
2338 else
2339 f1 = f1 - f2;
2340 rettv->v_type = VAR_FLOAT;
2341 rettv->vval.v_float = f1;
2342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002344#endif
2345 {
2346 if (op == '+')
2347 n1 = n1 + n2;
2348 else
2349 n1 = n1 - n2;
2350 rettv->v_type = VAR_NUMBER;
2351 rettv->vval.v_number = n1;
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 }
2357 return OK;
2358}
2359
2360/*
2361 * Handle fifth level expression:
2362 * * number multiplication
2363 * / number division
2364 * % number modulo
2365 *
2366 * "arg" must point to the first non-white of the expression.
2367 * "arg" is advanced to the next non-white after the recognized expression.
2368 *
2369 * Return OK or FAIL.
2370 */
2371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002372eval6(
2373 char_u **arg,
2374 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002375 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar33570922005-01-25 22:26:29 +00002378 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002380 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381#ifdef FEAT_FLOAT
2382 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002383 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002384#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 /*
2388 * Get the first variable.
2389 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002390 if (eval7(arg, rettv, flags, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return FAIL;
2392
2393 /*
2394 * Repeat computing, until no '*', '/' or '%' is following.
2395 */
2396 for (;;)
2397 {
2398 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002399 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 break;
2401
Bram Moolenaar32e35112020-05-14 22:41:15 +02002402 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002404#ifdef FEAT_FLOAT
2405 if (rettv->v_type == VAR_FLOAT)
2406 {
2407 f1 = rettv->vval.v_float;
2408 use_float = TRUE;
2409 n1 = 0;
2410 }
2411 else
2412#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002413 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 if (error)
2416 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 else
2419 n1 = 0;
2420
2421 /*
2422 * Get the second variable.
2423 */
2424 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002425 if (eval7(arg, &var2, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return FAIL;
2427
Bram Moolenaar32e35112020-05-14 22:41:15 +02002428 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002430#ifdef FEAT_FLOAT
2431 if (var2.v_type == VAR_FLOAT)
2432 {
2433 if (!use_float)
2434 {
2435 f1 = n1;
2436 use_float = TRUE;
2437 }
2438 f2 = var2.vval.v_float;
2439 n2 = 0;
2440 }
2441 else
2442#endif
2443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002444 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002445 clear_tv(&var2);
2446 if (error)
2447 return FAIL;
2448#ifdef FEAT_FLOAT
2449 if (use_float)
2450 f2 = n2;
2451#endif
2452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 /*
2455 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002456 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002458#ifdef FEAT_FLOAT
2459 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002461 if (op == '*')
2462 f1 = f1 * f2;
2463 else if (op == '/')
2464 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002465# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002466 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002467 if (f2 == 0.0)
2468 {
2469 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002470 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002471 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002472 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002473 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002474 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002475 }
2476 else
2477 f1 = f1 / f2;
2478# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002479 // We rely on the floating point library to handle divide
2480 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002481 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002482# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002487 return FAIL;
2488 }
2489 rettv->v_type = VAR_FLOAT;
2490 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002495 if (op == '*')
2496 n1 = n1 * n2;
2497 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002498 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002500 n1 = num_modulus(n1, n2);
2501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002502 rettv->v_type = VAR_NUMBER;
2503 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506 }
2507
2508 return OK;
2509}
2510
2511/*
2512 * Handle sixth level expression:
2513 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002514 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002515 * "string" string constant
2516 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * &option-name option value
2518 * @r register contents
2519 * identifier variable value
2520 * function() function call
2521 * $VAR environment variable
2522 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002523 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002525 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002526 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 *
2528 * Also handle:
2529 * ! in front logical NOT
2530 * - in front unary minus
2531 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 * trailing [] subscript in String or List
2533 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002534 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 *
2536 * "arg" must point to the first non-white of the expression.
2537 * "arg" is advanced to the next non-white after the recognized expression.
2538 *
2539 * Return OK or FAIL.
2540 */
2541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542eval7(
2543 char_u **arg,
2544 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002545 int flags,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002548 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 int len;
2550 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 char_u *start_leader, *end_leader;
2552 int ret = OK;
2553 char_u *alias;
2554
2555 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002557 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
2561 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002562 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 */
2564 start_leader = *arg;
2565 while (**arg == '!' || **arg == '-' || **arg == '+')
2566 *arg = skipwhite(*arg + 1);
2567 end_leader = *arg;
2568
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002569 if (**arg == '.' && (!isdigit(*(*arg + 1))
2570#ifdef FEAT_FLOAT
2571 || current_sctx.sc_version < 2
2572#endif
2573 ))
2574 {
2575 semsg(_(e_invexpr2), *arg);
2576 ++*arg;
2577 return FAIL;
2578 }
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 switch (**arg)
2581 {
2582 /*
2583 * Number constant.
2584 */
2585 case '0':
2586 case '1':
2587 case '2':
2588 case '3':
2589 case '4':
2590 case '5':
2591 case '6':
2592 case '7':
2593 case '8':
2594 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 break;
2597
2598 /*
2599 * String constant: "string".
2600 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 break;
2603
2604 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002608 break;
2609
2610 /*
2611 * List: [expr, expr]
2612 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002613 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 break;
2615
2616 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002617 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002618 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002619 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002620 {
2621 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002622 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002623 }
2624 else
2625 ret = NOTDONE;
2626 break;
2627
2628 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002629 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002630 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002632 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2633 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002634 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 break;
2636
2637 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002638 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002640 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 break;
2642
2643 /*
2644 * Environment variable: $VAR.
2645 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648
2649 /*
2650 * Register contents: @r.
2651 */
2652 case '@': ++*arg;
2653 if (evaluate)
2654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002656 rettv->vval.v_string = get_reg_contents(**arg,
2657 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 if (**arg != NUL)
2660 ++*arg;
2661 break;
2662
2663 /*
2664 * nested expression: (expression).
2665 */
2666 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002667 ret = eval1(arg, rettv, flags); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (**arg == ')')
2669 ++*arg;
2670 else if (ret == OK)
2671 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 ret = FAIL;
2675 }
2676 break;
2677
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 break;
2680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681
2682 if (ret == NOTDONE)
2683 {
2684 /*
2685 * Must be a variable or function name.
2686 * Can also be a curly-braces kind of name: {expr}.
2687 */
2688 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002689 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (alias != NULL)
2691 s = alias;
2692
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002693 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 ret = FAIL;
2695 else
2696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002698 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002699 else if (flags & EVAL_CONSTANT)
2700 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002702 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002704 {
2705 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002709 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 *arg = skipwhite(*arg);
2713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002714 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2715 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002716 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002717 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002718 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2722 */
2723 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002724 ret = eval7_leader(rettv, start_leader, &end_leader);
2725 return ret;
2726}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002727
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002728/*
2729 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2730 * Adjusts "end_leaderp" until it is at "start_leader".
2731 */
2732 static int
2733eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2734{
2735 char_u *end_leader = *end_leaderp;
2736 int ret = OK;
2737 int error = FALSE;
2738 varnumber_T val = 0;
2739#ifdef FEAT_FLOAT
2740 float_T f = 0.0;
2741
2742 if (rettv->v_type == VAR_FLOAT)
2743 f = rettv->vval.v_float;
2744 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002745#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002746 val = tv_get_number_chk(rettv, &error);
2747 if (error)
2748 {
2749 clear_tv(rettv);
2750 ret = FAIL;
2751 }
2752 else
2753 {
2754 while (end_leader > start_leader)
2755 {
2756 --end_leader;
2757 if (*end_leader == '!')
2758 {
2759#ifdef FEAT_FLOAT
2760 if (rettv->v_type == VAR_FLOAT)
2761 f = !f;
2762 else
2763#endif
2764 val = !val;
2765 }
2766 else if (*end_leader == '-')
2767 {
2768#ifdef FEAT_FLOAT
2769 if (rettv->v_type == VAR_FLOAT)
2770 f = -f;
2771 else
2772#endif
2773 val = -val;
2774 }
2775 }
2776#ifdef FEAT_FLOAT
2777 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002780 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002782 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002783#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002785 clear_tv(rettv);
2786 rettv->v_type = VAR_NUMBER;
2787 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002790 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return ret;
2792}
2793
2794/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002795 * Call the function referred to in "rettv".
2796 */
2797 static int
2798call_func_rettv(
2799 char_u **arg,
2800 typval_T *rettv,
2801 int evaluate,
2802 dict_T *selfdict,
2803 typval_T *basetv)
2804{
2805 partial_T *pt = NULL;
2806 funcexe_T funcexe;
2807 typval_T functv;
2808 char_u *s;
2809 int ret;
2810
2811 // need to copy the funcref so that we can clear rettv
2812 if (evaluate)
2813 {
2814 functv = *rettv;
2815 rettv->v_type = VAR_UNKNOWN;
2816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002817 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002818 if (functv.v_type == VAR_PARTIAL)
2819 {
2820 pt = functv.vval.v_partial;
2821 s = partial_name(pt);
2822 }
2823 else
2824 s = functv.vval.v_string;
2825 }
2826 else
2827 s = (char_u *)"";
2828
Bram Moolenaara80faa82020-04-12 19:37:17 +02002829 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002830 funcexe.firstline = curwin->w_cursor.lnum;
2831 funcexe.lastline = curwin->w_cursor.lnum;
2832 funcexe.evaluate = evaluate;
2833 funcexe.partial = pt;
2834 funcexe.selfdict = selfdict;
2835 funcexe.basetv = basetv;
2836 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002838 // Clear the funcref afterwards, so that deleting it while
2839 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002840 if (evaluate)
2841 clear_tv(&functv);
2842
2843 return ret;
2844}
2845
2846/*
2847 * Evaluate "->method()".
2848 * "*arg" points to the '-'.
2849 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2850 */
2851 static int
2852eval_lambda(
2853 char_u **arg,
2854 typval_T *rettv,
2855 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002857{
2858 typval_T base = *rettv;
2859 int ret;
2860
2861 // Skip over the ->.
2862 *arg += 2;
2863 rettv->v_type = VAR_UNKNOWN;
2864
2865 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002866 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002867 return FAIL;
2868 else if (**arg != '(')
2869 {
2870 if (verbose)
2871 {
2872 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002873 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002874 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002876 }
2877 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002878 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002879 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002880 else
2881 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2882
2883 // Clear the funcref afterwards, so that deleting it while
2884 // evaluating the arguments is possible (see test55).
2885 if (evaluate)
2886 clear_tv(&base);
2887
2888 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002889}
2890
2891/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002892 * Evaluate "->method()".
2893 * "*arg" points to the '-'.
2894 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2895 */
2896 static int
2897eval_method(
2898 char_u **arg,
2899 typval_T *rettv,
2900 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002901 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002902{
2903 char_u *name;
2904 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002906 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002907 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002908
2909 // Skip over the ->.
2910 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002911 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002912
Bram Moolenaarac92e252019-08-03 21:58:38 +02002913 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002914 len = get_name_len(arg, &alias, evaluate, TRUE);
2915 if (alias != NULL)
2916 name = alias;
2917
2918 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002919 {
2920 if (verbose)
2921 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002922 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002923 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002924 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002925 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002926 if (**arg != '(')
2927 {
2928 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002930 ret = FAIL;
2931 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002932 else if (VIM_ISWHITE((*arg)[-1]))
2933 {
2934 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002935 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002936 ret = FAIL;
2937 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002938 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002939 ret = eval_func(arg, name, len, rettv,
2940 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002941 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002942
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002943 // Clear the funcref afterwards, so that deleting it while
2944 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002945 if (evaluate)
2946 clear_tv(&base);
2947
Bram Moolenaarac92e252019-08-03 21:58:38 +02002948 return ret;
2949}
2950
2951/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002952 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2953 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002954 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2955 */
2956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957eval_index(
2958 char_u **arg,
2959 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002960 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002961 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002962{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002963 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002964 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002966 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002967 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 long len = -1;
2969 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002970 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002971 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002972
Bram Moolenaara03f2332016-02-06 18:09:59 +01002973 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002974 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002975 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002976 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002977 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002978 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002979 return FAIL;
2980 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002981#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002982 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002983 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002984 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002985#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002986 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002987 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002988 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002989 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002990 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002992 return FAIL;
2993 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002994 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002996 if (evaluate)
2997 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002998 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002999
3000 case VAR_STRING:
3001 case VAR_NUMBER:
3002 case VAR_LIST:
3003 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003004 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003005 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003006 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003008 init_tv(&var1);
3009 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003010 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003011 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003012 /*
3013 * dict.name
3014 */
3015 key = *arg + 1;
3016 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3017 ;
3018 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003019 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003020 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003021 }
3022 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003023 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003024 /*
3025 * something[idx]
3026 *
3027 * Get the (first) variable from inside the [].
3028 */
3029 *arg = skipwhite(*arg + 1);
3030 if (**arg == ':')
3031 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003032 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003034 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003036 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 clear_tv(&var1);
3038 return FAIL;
3039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040
3041 /*
3042 * Get the second variable from inside the [:].
3043 */
3044 if (**arg == ':')
3045 {
3046 range = TRUE;
3047 *arg = skipwhite(*arg + 1);
3048 if (**arg == ']')
3049 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003050 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 if (!empty1)
3053 clear_tv(&var1);
3054 return FAIL;
3055 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003056 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003058 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003059 if (!empty1)
3060 clear_tv(&var1);
3061 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003062 return FAIL;
3063 }
3064 }
3065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003066 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003067 if (**arg != ']')
3068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003069 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003070 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071 clear_tv(&var1);
3072 if (range)
3073 clear_tv(&var2);
3074 return FAIL;
3075 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003076 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003077 }
3078
3079 if (evaluate)
3080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003081 n1 = 0;
3082 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003084 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003085 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003086 }
3087 if (range)
3088 {
3089 if (empty2)
3090 n2 = -1;
3091 else
3092 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003093 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003095 }
3096 }
3097
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003099 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003100 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003101 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003103 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003104 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003105 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003106 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003107 case VAR_SPECIAL:
3108 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003109 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003110 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003111
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003112 case VAR_NUMBER:
3113 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003115 len = (long)STRLEN(s);
3116 if (range)
3117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 // The resulting variable is a substring. If the indexes
3119 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 if (n1 < 0)
3121 {
3122 n1 = len + n1;
3123 if (n1 < 0)
3124 n1 = 0;
3125 }
3126 if (n2 < 0)
3127 n2 = len + n2;
3128 else if (n2 >= len)
3129 n2 = len;
3130 if (n1 >= len || n2 < 0 || n1 > n2)
3131 s = NULL;
3132 else
3133 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3134 }
3135 else
3136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003137 // The resulting variable is a string of a single
3138 // character. If the index is too big or negative the
3139 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003140 if (n1 >= len || n1 < 0)
3141 s = NULL;
3142 else
3143 s = vim_strnsave(s + n1, 1);
3144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003145 clear_tv(rettv);
3146 rettv->v_type = VAR_STRING;
3147 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003148 break;
3149
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003150 case VAR_BLOB:
3151 len = blob_len(rettv->vval.v_blob);
3152 if (range)
3153 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003154 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003155 // are out of range the result is empty.
3156 if (n1 < 0)
3157 {
3158 n1 = len + n1;
3159 if (n1 < 0)
3160 n1 = 0;
3161 }
3162 if (n2 < 0)
3163 n2 = len + n2;
3164 else if (n2 >= len)
3165 n2 = len - 1;
3166 if (n1 >= len || n2 < 0 || n1 > n2)
3167 {
3168 clear_tv(rettv);
3169 rettv->v_type = VAR_BLOB;
3170 rettv->vval.v_blob = NULL;
3171 }
3172 else
3173 {
3174 blob_T *blob = blob_alloc();
3175
3176 if (blob != NULL)
3177 {
3178 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3179 {
3180 blob_free(blob);
3181 return FAIL;
3182 }
3183 blob->bv_ga.ga_len = n2 - n1 + 1;
3184 for (i = n1; i <= n2; i++)
3185 blob_set(blob, i - n1,
3186 blob_get(rettv->vval.v_blob, i));
3187
3188 clear_tv(rettv);
3189 rettv_blob_set(rettv, blob);
3190 }
3191 }
3192 }
3193 else
3194 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003195 // The resulting variable is a byte value.
3196 // If the index is too big or negative that is an error.
3197 if (n1 < 0)
3198 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003199 if (n1 < len && n1 >= 0)
3200 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003201 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003202
3203 clear_tv(rettv);
3204 rettv->v_type = VAR_NUMBER;
3205 rettv->vval.v_number = v;
3206 }
3207 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003208 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003209 }
3210 break;
3211
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003212 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003213 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003214 if (n1 < 0)
3215 n1 = len + n1;
3216 if (!empty1 && (n1 < 0 || n1 >= len))
3217 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003218 // For a range we allow invalid values and return an empty
3219 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003220 if (!range)
3221 {
3222 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003223 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003224 return FAIL;
3225 }
3226 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003227 }
3228 if (range)
3229 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 list_T *l;
3231 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003232
3233 if (n2 < 0)
3234 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003235 else if (n2 >= len)
3236 n2 = len - 1;
3237 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003238 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003239 l = list_alloc();
3240 if (l == NULL)
3241 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003243 n1 <= n2; ++n1)
3244 {
3245 if (list_append_tv(l, &item->li_tv) == FAIL)
3246 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003247 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003248 return FAIL;
3249 }
3250 item = item->li_next;
3251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003252 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003253 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003254 }
3255 else
3256 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003257 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003258 clear_tv(rettv);
3259 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003260 }
3261 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262
3263 case VAR_DICT:
3264 if (range)
3265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003266 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003267 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003268 if (len == -1)
3269 clear_tv(&var1);
3270 return FAIL;
3271 }
3272 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003274
3275 if (len == -1)
3276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003277 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003278 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280 clear_tv(&var1);
3281 return FAIL;
3282 }
3283 }
3284
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003285 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003287 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003289 if (len == -1)
3290 clear_tv(&var1);
3291 if (item == NULL)
3292 return FAIL;
3293
3294 copy_tv(&item->di_tv, &var1);
3295 clear_tv(rettv);
3296 *rettv = var1;
3297 }
3298 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003299 }
3300 }
3301
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003302 return OK;
3303}
3304
3305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 * Get an option value.
3307 * "arg" points to the '&' or '+' before the option name.
3308 * "arg" is advanced to character after the option name.
3309 * Return OK or FAIL.
3310 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312get_option_tv(
3313 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003314 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 char_u *option_end;
3318 long numval;
3319 char_u *stringval;
3320 int opt_type;
3321 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003322 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 int ret = OK;
3324 int opt_flags;
3325
3326 /*
3327 * Isolate the option name and find its value.
3328 */
3329 option_end = find_option_end(arg, &opt_flags);
3330 if (option_end == NULL)
3331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003333 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 return FAIL;
3335 }
3336
3337 if (!evaluate)
3338 {
3339 *arg = option_end;
3340 return OK;
3341 }
3342
3343 c = *option_end;
3344 *option_end = NUL;
3345 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003346 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 ret = FAIL;
3353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003356 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 rettv->v_type = VAR_STRING;
3359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 rettv->v_type = VAR_NUMBER;
3364 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003368 rettv->v_type = VAR_NUMBER;
3369 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003371 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 rettv->v_type = VAR_STRING;
3374 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 }
3377 else if (working && (opt_type == -2 || opt_type == -1))
3378 ret = FAIL;
3379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003380 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 *arg = option_end;
3382
3383 return ret;
3384}
3385
3386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3388 * Return OK or FAIL.
3389 */
3390 int
3391get_number_tv(
3392 char_u **arg,
3393 typval_T *rettv,
3394 int evaluate,
3395 int want_string UNUSED)
3396{
3397 int len;
3398#ifdef FEAT_FLOAT
3399 char_u *p;
3400 int get_float = FALSE;
3401
3402 // We accept a float when the format matches
3403 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3404 // strict to avoid backwards compatibility problems.
3405 // With script version 2 and later the leading digit can be
3406 // omitted.
3407 // Don't look for a float after the "." operator, so that
3408 // ":let vers = 1.2.3" doesn't fail.
3409 if (**arg == '.')
3410 p = *arg;
3411 else
3412 p = skipdigits(*arg + 1);
3413 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3414 {
3415 get_float = TRUE;
3416 p = skipdigits(p + 2);
3417 if (*p == 'e' || *p == 'E')
3418 {
3419 ++p;
3420 if (*p == '-' || *p == '+')
3421 ++p;
3422 if (!vim_isdigit(*p))
3423 get_float = FALSE;
3424 else
3425 p = skipdigits(p + 1);
3426 }
3427 if (ASCII_ISALPHA(*p) || *p == '.')
3428 get_float = FALSE;
3429 }
3430 if (get_float)
3431 {
3432 float_T f;
3433
3434 *arg += string2float(*arg, &f);
3435 if (evaluate)
3436 {
3437 rettv->v_type = VAR_FLOAT;
3438 rettv->vval.v_float = f;
3439 }
3440 }
3441 else
3442#endif
3443 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3444 {
3445 char_u *bp;
3446 blob_T *blob = NULL; // init for gcc
3447
3448 // Blob constant: 0z0123456789abcdef
3449 if (evaluate)
3450 blob = blob_alloc();
3451 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3452 {
3453 if (!vim_isxdigit(bp[1]))
3454 {
3455 if (blob != NULL)
3456 {
3457 emsg(_("E973: Blob literal should have an even number of hex characters"));
3458 ga_clear(&blob->bv_ga);
3459 VIM_CLEAR(blob);
3460 }
3461 return FAIL;
3462 }
3463 if (blob != NULL)
3464 ga_append(&blob->bv_ga,
3465 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3466 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3467 ++bp;
3468 }
3469 if (blob != NULL)
3470 rettv_blob_set(rettv, blob);
3471 *arg = bp;
3472 }
3473 else
3474 {
3475 varnumber_T n;
3476
3477 // decimal, hex or octal number
3478 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3479 ? STR2NR_NO_OCT + STR2NR_QUOTE
3480 : STR2NR_ALL, &n, NULL, 0, TRUE);
3481 if (len == 0)
3482 {
3483 semsg(_(e_invexpr2), *arg);
3484 return FAIL;
3485 }
3486 *arg += len;
3487 if (evaluate)
3488 {
3489 rettv->v_type = VAR_NUMBER;
3490 rettv->vval.v_number = n;
3491 }
3492 }
3493 return OK;
3494}
3495
3496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Allocate a variable for a string constant.
3498 * Return OK or FAIL.
3499 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003501get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 char_u *p;
3504 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 int extra = 0;
3506
3507 /*
3508 * Find the end of the string, skipping backslashed characters.
3509 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003510 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 if (*p == '\\' && p[1] != NUL)
3513 {
3514 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003515 // A "\<x>" form occupies at least 4 characters, and produces up
3516 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (*p == '<')
3518 extra += 2;
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
3522 if (*p != '"')
3523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003524 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return FAIL;
3526 }
3527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003528 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (!evaluate)
3530 {
3531 *arg = p + 1;
3532 return OK;
3533 }
3534
3535 /*
3536 * Copy the string into allocated memory, handling backslashed
3537 * characters.
3538 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003539 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (name == NULL)
3541 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 rettv->v_type = VAR_STRING;
3543 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 if (*p == '\\')
3548 {
3549 switch (*++p)
3550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551 case 'b': *name++ = BS; ++p; break;
3552 case 'e': *name++ = ESC; ++p; break;
3553 case 'f': *name++ = FF; ++p; break;
3554 case 'n': *name++ = NL; ++p; break;
3555 case 'r': *name++ = CAR; ++p; break;
3556 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003558 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003560 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 case 'U':
3562 if (vim_isxdigit(p[1]))
3563 {
3564 int n, nr;
3565 int c = toupper(*p);
3566
3567 if (c == 'X')
3568 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003569 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003571 else
3572 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 nr = 0;
3574 while (--n >= 0 && vim_isxdigit(p[1]))
3575 {
3576 ++p;
3577 nr = (nr << 4) + hex2nr(*p);
3578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003580 // For "\u" store the number according to
3581 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 case '0':
3591 case '1':
3592 case '2':
3593 case '3':
3594 case '4':
3595 case '5':
3596 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 case '7': *name = *p++ - '0';
3598 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 *name = (*name << 3) + *p++ - '0';
3601 if (*p >= '0' && *p <= '7')
3602 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 break;
3606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003608 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3609 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (extra != 0)
3611 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003615 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
3620 }
3621 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003627 ++p;
3628 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return OK;
3631}
3632
3633/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 * Return OK or FAIL.
3636 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003641 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003642 int reduce = 0;
3643
3644 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003646 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003647 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003652 break;
3653 ++reduce;
3654 ++p;
3655 }
3656 }
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003660 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003661 return FAIL;
3662 }
3663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003664 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003665 if (!evaluate)
3666 {
3667 *arg = p + 1;
3668 return OK;
3669 }
3670
3671 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003672 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003673 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003674 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003675 if (str == NULL)
3676 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 rettv->v_type = VAR_STRING;
3678 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003679
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003685 break;
3686 ++p;
3687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003691 *arg = p + 1;
3692
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003693 return OK;
3694}
3695
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003697 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003698 */
3699 char_u *
3700partial_name(partial_T *pt)
3701{
3702 if (pt->pt_name != NULL)
3703 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003704 if (pt->pt_func != NULL)
3705 return pt->pt_func->uf_name;
3706 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003707}
3708
Bram Moolenaarddecc252016-04-06 22:59:37 +02003709 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003710partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003711{
3712 int i;
3713
3714 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003715 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003716 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003717 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003718 if (pt->pt_name != NULL)
3719 {
3720 func_unref(pt->pt_name);
3721 vim_free(pt->pt_name);
3722 }
3723 else
3724 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003725
3726 if (pt->pt_funcstack != NULL)
3727 {
3728 // Decrease the reference count for the context of a closure. If down
3729 // to zero free it and clear the variables on the stack.
3730 if (--pt->pt_funcstack->fs_refcount == 0)
3731 {
3732 garray_T *gap = &pt->pt_funcstack->fs_ga;
3733 typval_T *stack = gap->ga_data;
3734
3735 for (i = 0; i < gap->ga_len; ++i)
3736 clear_tv(stack + i);
3737 ga_clear(gap);
3738 vim_free(pt->pt_funcstack);
3739 }
3740 pt->pt_funcstack = NULL;
3741 }
3742
Bram Moolenaarddecc252016-04-06 22:59:37 +02003743 vim_free(pt);
3744}
3745
3746/*
3747 * Unreference a closure: decrement the reference count and free it when it
3748 * becomes zero.
3749 */
3750 void
3751partial_unref(partial_T *pt)
3752{
3753 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003754 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003755}
3756
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003757static int tv_equal_recurse_limit;
3758
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003759 static int
3760func_equal(
3761 typval_T *tv1,
3762 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003763 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003764{
3765 char_u *s1, *s2;
3766 dict_T *d1, *d2;
3767 int a1, a2;
3768 int i;
3769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003770 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003771 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003772 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003773 if (s1 != NULL && *s1 == NUL)
3774 s1 = NULL;
3775 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003776 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003777 if (s2 != NULL && *s2 == NUL)
3778 s2 = NULL;
3779 if (s1 == NULL || s2 == NULL)
3780 {
3781 if (s1 != s2)
3782 return FALSE;
3783 }
3784 else if (STRCMP(s1, s2) != 0)
3785 return FALSE;
3786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003787 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003788 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3789 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3790 if (d1 == NULL || d2 == NULL)
3791 {
3792 if (d1 != d2)
3793 return FALSE;
3794 }
3795 else if (!dict_equal(d1, d2, ic, TRUE))
3796 return FALSE;
3797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003798 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003799 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3800 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3801 if (a1 != a2)
3802 return FALSE;
3803 for (i = 0; i < a1; ++i)
3804 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3805 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3806 return FALSE;
3807
3808 return TRUE;
3809}
3810
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003811/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003812 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003813 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003814 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003815 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817tv_equal(
3818 typval_T *tv1,
3819 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003820 int ic, // ignore case
3821 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003822{
3823 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003826 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 // Catch lists and dicts that have an endless loop by limiting
3829 // recursiveness to a limit. We guess they are equal then.
3830 // A fixed limit has the problem of still taking an awful long time.
3831 // Reduce the limit every time running into it. That should work fine for
3832 // deeply linked structures that are not recursively linked and catch
3833 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003834 if (!recursive)
3835 tv_equal_recurse_limit = 1000;
3836 if (recursive_cnt >= tv_equal_recurse_limit)
3837 {
3838 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003839 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003840 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003842 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3843 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003844 if ((tv1->v_type == VAR_FUNC
3845 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3846 && (tv2->v_type == VAR_FUNC
3847 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3848 {
3849 ++recursive_cnt;
3850 r = func_equal(tv1, tv2, ic);
3851 --recursive_cnt;
3852 return r;
3853 }
3854
3855 if (tv1->v_type != tv2->v_type)
3856 return FALSE;
3857
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003858 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003859 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003860 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003861 ++recursive_cnt;
3862 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3863 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003864 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003865
3866 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003867 ++recursive_cnt;
3868 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3869 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003870 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003871
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003872 case VAR_BLOB:
3873 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3874
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003875 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 case VAR_BOOL:
3877 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003878 return tv1->vval.v_number == tv2->vval.v_number;
3879
3880 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 s1 = tv_get_string_buf(tv1, buf1);
3882 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003883 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003884
Bram Moolenaar835dc632016-02-07 14:27:38 +01003885 case VAR_FLOAT:
3886#ifdef FEAT_FLOAT
3887 return tv1->vval.v_float == tv2->vval.v_float;
3888#endif
3889 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003890#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003891 return tv1->vval.v_job == tv2->vval.v_job;
3892#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003893 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003894#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003895 return tv1->vval.v_channel == tv2->vval.v_channel;
3896#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003898 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003899 return tv1->vval.v_partial == tv2->vval.v_partial;
3900
3901 case VAR_FUNC:
3902 return tv1->vval.v_string == tv2->vval.v_string;
3903
Bram Moolenaar835dc632016-02-07 14:27:38 +01003904 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003905 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003907 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003908 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3911 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003912 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003913}
3914
3915/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003916 * Return the next (unique) copy ID.
3917 * Used for serializing nested structures.
3918 */
3919 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003921{
3922 current_copyID += COPYID_INC;
3923 return current_copyID;
3924}
3925
3926/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003927 * Garbage collection for lists and dictionaries.
3928 *
3929 * We use reference counts to be able to free most items right away when they
3930 * are no longer used. But for composite items it's possible that it becomes
3931 * unused while the reference count is > 0: When there is a recursive
3932 * reference. Example:
3933 * :let l = [1, 2, 3]
3934 * :let d = {9: l}
3935 * :let l[1] = d
3936 *
3937 * Since this is quite unusual we handle this with garbage collection: every
3938 * once in a while find out which lists and dicts are not referenced from any
3939 * variable.
3940 *
3941 * Here is a good reference text about garbage collection (refers to Python
3942 * but it applies to all reference-counting mechanisms):
3943 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003944 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003945
3946/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003947 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003948 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003949 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003950 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003951 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003952garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003953{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003954 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003955 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003956 buf_T *buf;
3957 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003958 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003959 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003960
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003961 if (!testing)
3962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003964 want_garbage_collect = FALSE;
3965 may_garbage_collect = FALSE;
3966 garbage_collect_at_exit = FALSE;
3967 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003968
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003969 // The execution stack can grow big, limit the size.
3970 if (exestack.ga_maxlen - exestack.ga_len > 500)
3971 {
3972 size_t new_len;
3973 char_u *pp;
3974 int n;
3975
3976 // Keep 150% of the current size, with a minimum of the growth size.
3977 n = exestack.ga_len / 2;
3978 if (n < exestack.ga_growsize)
3979 n = exestack.ga_growsize;
3980
3981 // Don't make it bigger though.
3982 if (exestack.ga_len + n < exestack.ga_maxlen)
3983 {
3984 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3985 pp = vim_realloc(exestack.ga_data, new_len);
3986 if (pp == NULL)
3987 return FAIL;
3988 exestack.ga_maxlen = exestack.ga_len + n;
3989 exestack.ga_data = pp;
3990 }
3991 }
3992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003993 // We advance by two because we add one for items referenced through
3994 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003995 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003996
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003997 /*
3998 * 1. Go through all accessible variables and mark all lists and dicts
3999 * with copyID.
4000 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004002 // Don't free variables in the previous_funccal list unless they are only
4003 // referenced through previous_funccal. This must be first, because if
4004 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004005 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004008 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004010 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004011 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004012 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4013 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4018 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004019 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4021 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004022#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004023 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004024 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4025 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004026 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004027 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004028 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4029 NULL, NULL);
4030#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004033 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004034 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4035 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004036 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004037 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004039 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004042 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004043 abort = abort || set_ref_in_functions(copyID);
4044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004049 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004050
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004051 // callbacks in buffers
4052 abort = abort || set_ref_in_buffers(copyID);
4053
Bram Moolenaar1dced572012-04-05 16:54:08 +02004054#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004055 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004056#endif
4057
Bram Moolenaardb913952012-06-29 12:54:53 +02004058#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004059 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004060#endif
4061
4062#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004063 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004064#endif
4065
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004067 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004068 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004069#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004070#ifdef FEAT_NETBEANS_INTG
4071 abort = abort || set_ref_in_nb_channel(copyID);
4072#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004073
Bram Moolenaare3188e22016-05-31 21:13:04 +02004074#ifdef FEAT_TIMERS
4075 abort = abort || set_ref_in_timer(copyID);
4076#endif
4077
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004078#ifdef FEAT_QUICKFIX
4079 abort = abort || set_ref_in_quickfix(copyID);
4080#endif
4081
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004082#ifdef FEAT_TERMINAL
4083 abort = abort || set_ref_in_term(copyID);
4084#endif
4085
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004086#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004087 abort = abort || set_ref_in_popups(copyID);
4088#endif
4089
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004090 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004091 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004092 /*
4093 * 2. Free lists and dictionaries that are not referenced.
4094 */
4095 did_free = free_unref_items(copyID);
4096
4097 /*
4098 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004099 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004101 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004102 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004103 else if (p_verbose > 0)
4104 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004105 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004106 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004107
4108 return did_free;
4109}
4110
4111/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004112 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004113 */
4114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004115free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004116{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004117 int did_free = FALSE;
4118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004119 // Let all "free" functions know that we are here. This means no
4120 // dictionaries, lists, channels or jobs are to be freed, because we will
4121 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004122 in_free_unref_items = TRUE;
4123
4124 /*
4125 * PASS 1: free the contents of the items. We don't free the items
4126 * themselves yet, so that it is possible to decrement refcount counters
4127 */
4128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004129 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004130 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004132 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004133 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004134
4135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004136 // Go through the list of jobs and free items without the copyID. This
4137 // must happen before doing channels, because jobs refer to channels, but
4138 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4143#endif
4144
4145 /*
4146 * PASS 2: free the items themselves.
4147 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004148 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004149 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004150
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004152 // Go through the list of jobs and free items without the copyID. This
4153 // must happen before doing channels, because jobs refer to channels, but
4154 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004155 free_unused_jobs(copyID, COPYID_MASK);
4156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004157 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004158 free_unused_channels(copyID, COPYID_MASK);
4159#endif
4160
4161 in_free_unref_items = FALSE;
4162
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004163 return did_free;
4164}
4165
4166/*
4167 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004168 * "list_stack" is used to add lists to be marked. Can be NULL.
4169 *
4170 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004171 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004173set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004174{
4175 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004176 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004177 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004178 hashtab_T *cur_ht;
4179 ht_stack_T *ht_stack = NULL;
4180 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004181
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004182 cur_ht = ht;
4183 for (;;)
4184 {
4185 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004187 // Mark each item in the hashtab. If the item contains a hashtab
4188 // it is added to ht_stack, if it contains a list it is added to
4189 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004190 todo = (int)cur_ht->ht_used;
4191 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4192 if (!HASHITEM_EMPTY(hi))
4193 {
4194 --todo;
4195 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4196 &ht_stack, list_stack);
4197 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004198 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004199
4200 if (ht_stack == NULL)
4201 break;
4202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004204 cur_ht = ht_stack->ht;
4205 tempitem = ht_stack;
4206 ht_stack = ht_stack->prev;
4207 free(tempitem);
4208 }
4209
4210 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004211}
4212
4213/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004214 * Mark a dict and its items with "copyID".
4215 * Returns TRUE if setting references failed somehow.
4216 */
4217 int
4218set_ref_in_dict(dict_T *d, int copyID)
4219{
4220 if (d != NULL && d->dv_copyID != copyID)
4221 {
4222 d->dv_copyID = copyID;
4223 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4224 }
4225 return FALSE;
4226}
4227
4228/*
4229 * Mark a list and its items with "copyID".
4230 * Returns TRUE if setting references failed somehow.
4231 */
4232 int
4233set_ref_in_list(list_T *ll, int copyID)
4234{
4235 if (ll != NULL && ll->lv_copyID != copyID)
4236 {
4237 ll->lv_copyID = copyID;
4238 return set_ref_in_list_items(ll, copyID, NULL);
4239 }
4240 return FALSE;
4241}
4242
4243/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004244 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004245 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4246 *
4247 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004248 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004249 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004250set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004251{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004252 listitem_T *li;
4253 int abort = FALSE;
4254 list_T *cur_l;
4255 list_stack_T *list_stack = NULL;
4256 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004257
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004258 cur_l = l;
4259 for (;;)
4260 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004261 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004262 // Mark each item in the list. If the item contains a hashtab
4263 // it is added to ht_stack, if it contains a list it is added to
4264 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004265 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4266 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4267 ht_stack, &list_stack);
4268 if (list_stack == NULL)
4269 break;
4270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004271 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004272 cur_l = list_stack->list;
4273 tempitem = list_stack;
4274 list_stack = list_stack->prev;
4275 free(tempitem);
4276 }
4277
4278 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004279}
4280
4281/*
4282 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004283 * "list_stack" is used to add lists to be marked. Can be NULL.
4284 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4285 *
4286 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004289set_ref_in_item(
4290 typval_T *tv,
4291 int copyID,
4292 ht_stack_T **ht_stack,
4293 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004294{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004295 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004296
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004297 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004298 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004299 dict_T *dd = tv->vval.v_dict;
4300
Bram Moolenaara03f2332016-02-06 18:09:59 +01004301 if (dd != NULL && dd->dv_copyID != copyID)
4302 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004303 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004304 dd->dv_copyID = copyID;
4305 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004306 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004307 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4308 }
4309 else
4310 {
4311 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4312 if (newitem == NULL)
4313 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004314 else
4315 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004316 newitem->ht = &dd->dv_hashtab;
4317 newitem->prev = *ht_stack;
4318 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004320 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004321 }
4322 }
4323 else if (tv->v_type == VAR_LIST)
4324 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004325 list_T *ll = tv->vval.v_list;
4326
Bram Moolenaara03f2332016-02-06 18:09:59 +01004327 if (ll != NULL && ll->lv_copyID != copyID)
4328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004329 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004330 ll->lv_copyID = copyID;
4331 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004332 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004333 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004334 }
4335 else
4336 {
4337 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004338 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004339 if (newitem == NULL)
4340 abort = TRUE;
4341 else
4342 {
4343 newitem->list = ll;
4344 newitem->prev = *list_stack;
4345 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004346 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004347 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004348 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004349 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004350 else if (tv->v_type == VAR_FUNC)
4351 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004352 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004353 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004354 else if (tv->v_type == VAR_PARTIAL)
4355 {
4356 partial_T *pt = tv->vval.v_partial;
4357 int i;
4358
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004359 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004360 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004361 // Didn't see this partial yet.
4362 pt->pt_copyID = copyID;
4363
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004364 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004365
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 if (pt->pt_dict != NULL)
4367 {
4368 typval_T dtv;
4369
4370 dtv.v_type = VAR_DICT;
4371 dtv.vval.v_dict = pt->pt_dict;
4372 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4373 }
4374
4375 for (i = 0; i < pt->pt_argc; ++i)
4376 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4377 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004378 if (pt->pt_funcstack != NULL)
4379 {
4380 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4381
4382 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4383 abort = abort || set_ref_in_item(stack + i, copyID,
4384 ht_stack, list_stack);
4385 }
4386
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 }
4388 }
4389#ifdef FEAT_JOB_CHANNEL
4390 else if (tv->v_type == VAR_JOB)
4391 {
4392 job_T *job = tv->vval.v_job;
4393 typval_T dtv;
4394
4395 if (job != NULL && job->jv_copyID != copyID)
4396 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004397 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004398 if (job->jv_channel != NULL)
4399 {
4400 dtv.v_type = VAR_CHANNEL;
4401 dtv.vval.v_channel = job->jv_channel;
4402 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4403 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004404 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004405 {
4406 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004407 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4409 }
4410 }
4411 }
4412 else if (tv->v_type == VAR_CHANNEL)
4413 {
4414 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004415 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004416 typval_T dtv;
4417 jsonq_T *jq;
4418 cbq_T *cq;
4419
4420 if (ch != NULL && ch->ch_copyID != copyID)
4421 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004422 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004423 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424 {
4425 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4426 jq = jq->jq_next)
4427 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4428 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4429 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004430 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004431 {
4432 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004433 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4435 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004436 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 {
4438 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004439 dtv.vval.v_partial =
4440 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004441 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4442 }
4443 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004444 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004445 {
4446 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004447 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004448 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4449 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004450 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004451 {
4452 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004453 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004454 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4455 }
4456 }
4457 }
4458#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004459 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004460}
4461
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 * Return a string with the string representation of a variable.
4464 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004466 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004467 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4468 * stings as "string()", otherwise does not put quotes around strings, as
4469 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004470 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4471 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004472 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004474 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004475echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476 typval_T *tv,
4477 char_u **tofree,
4478 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004479 int copyID,
4480 int echo_style,
4481 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004482 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004484 static int recurse = 0;
4485 char_u *r = NULL;
4486
Bram Moolenaar33570922005-01-25 22:26:29 +00004487 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004488 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004489 if (!did_echo_string_emsg)
4490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // Only give this message once for a recursive call to avoid
4492 // flooding the user with errors. And stop iterating over lists
4493 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004494 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004495 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004497 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004498 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004499 }
4500 ++recurse;
4501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 switch (tv->v_type)
4503 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004504 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004505 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004506 {
4507 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004508 r = tv->vval.v_string;
4509 if (r == NULL)
4510 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004511 }
4512 else
4513 {
4514 *tofree = string_quote(tv->vval.v_string, FALSE);
4515 r = *tofree;
4516 }
4517 break;
4518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004519 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004520 if (echo_style)
4521 {
4522 *tofree = NULL;
4523 r = tv->vval.v_string;
4524 }
4525 else
4526 {
4527 *tofree = string_quote(tv->vval.v_string, TRUE);
4528 r = *tofree;
4529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004530 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004531
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004532 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004533 {
4534 partial_T *pt = tv->vval.v_partial;
4535 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004536 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004537 garray_T ga;
4538 int i;
4539 char_u *tf;
4540
4541 ga_init2(&ga, 1, 100);
4542 ga_concat(&ga, (char_u *)"function(");
4543 if (fname != NULL)
4544 {
4545 ga_concat(&ga, fname);
4546 vim_free(fname);
4547 }
4548 if (pt != NULL && pt->pt_argc > 0)
4549 {
4550 ga_concat(&ga, (char_u *)", [");
4551 for (i = 0; i < pt->pt_argc; ++i)
4552 {
4553 if (i > 0)
4554 ga_concat(&ga, (char_u *)", ");
4555 ga_concat(&ga,
4556 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4557 vim_free(tf);
4558 }
4559 ga_concat(&ga, (char_u *)"]");
4560 }
4561 if (pt != NULL && pt->pt_dict != NULL)
4562 {
4563 typval_T dtv;
4564
4565 ga_concat(&ga, (char_u *)", ");
4566 dtv.v_type = VAR_DICT;
4567 dtv.vval.v_dict = pt->pt_dict;
4568 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4569 vim_free(tf);
4570 }
4571 ga_concat(&ga, (char_u *)")");
4572
4573 *tofree = ga.ga_data;
4574 r = *tofree;
4575 break;
4576 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004577
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004578 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004579 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004580 break;
4581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004583 if (tv->vval.v_list == NULL)
4584 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004585 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004586 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004587 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004588 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004589 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4590 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004591 {
4592 *tofree = NULL;
4593 r = (char_u *)"[...]";
4594 }
4595 else
4596 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004597 int old_copyID = tv->vval.v_list->lv_copyID;
4598
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004599 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004600 *tofree = list2string(tv, copyID, restore_copyID);
4601 if (restore_copyID)
4602 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004603 r = *tofree;
4604 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004605 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004606
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004608 if (tv->vval.v_dict == NULL)
4609 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004610 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004611 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004612 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004613 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004614 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4615 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004616 {
4617 *tofree = NULL;
4618 r = (char_u *)"{...}";
4619 }
4620 else
4621 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004622 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004623
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004624 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004625 *tofree = dict2string(tv, copyID, restore_copyID);
4626 if (restore_copyID)
4627 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004628 r = *tofree;
4629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004630 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004633 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004634 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004636 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004638 break;
4639
Bram Moolenaar835dc632016-02-07 14:27:38 +01004640 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004641 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004642 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004644 if (composite_val)
4645 {
4646 *tofree = string_quote(r, FALSE);
4647 r = *tofree;
4648 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004651 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004652#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004653 *tofree = NULL;
4654 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4655 r = numbuf;
4656 break;
4657#endif
4658
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004659 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004660 case VAR_SPECIAL:
4661 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004662 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004663 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004665
Bram Moolenaar8502c702014-06-17 12:51:16 +02004666 if (--recurse == 0)
4667 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004668 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004669}
4670
4671/*
4672 * Return a string with the string representation of a variable.
4673 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4674 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004675 * Does not put quotes around strings, as ":echo" displays values.
4676 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4677 * May return NULL.
4678 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004679 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004680echo_string(
4681 typval_T *tv,
4682 char_u **tofree,
4683 char_u *numbuf,
4684 int copyID)
4685{
4686 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4687}
4688
4689/*
4690 * Return a string with the string representation of a variable.
4691 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4692 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004693 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004694 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004695 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004696 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004697tv2string(
4698 typval_T *tv,
4699 char_u **tofree,
4700 char_u *numbuf,
4701 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004702{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004703 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704}
4705
4706/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004707 * Return string "str" in ' quotes, doubling ' characters.
4708 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004711 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004712string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004713{
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004715 char_u *p, *r, *s;
4716
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 len = (function ? 13 : 3);
4718 if (str != NULL)
4719 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004720 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004721 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 if (*p == '\'')
4723 ++len;
4724 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004725 s = r = alloc(len);
4726 if (r != NULL)
4727 {
4728 if (function)
4729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004731 r += 10;
4732 }
4733 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004735 if (str != NULL)
4736 for (p = str; *p != NUL; )
4737 {
4738 if (*p == '\'')
4739 *r++ = '\'';
4740 MB_COPY_CHAR(p, r);
4741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004743 if (function)
4744 *r++ = ')';
4745 *r++ = NUL;
4746 }
4747 return s;
4748}
4749
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004750#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751/*
4752 * Convert the string "text" to a floating point number.
4753 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4754 * this always uses a decimal point.
4755 * Returns the length of the text that was consumed.
4756 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004758string2float(
4759 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004761{
4762 char *s = (char *)text;
4763 float_T f;
4764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004766 if (STRNICMP(text, "inf", 3) == 0)
4767 {
4768 *value = INFINITY;
4769 return 3;
4770 }
4771 if (STRNICMP(text, "-inf", 3) == 0)
4772 {
4773 *value = -INFINITY;
4774 return 4;
4775 }
4776 if (STRNICMP(text, "nan", 3) == 0)
4777 {
4778 *value = NAN;
4779 return 3;
4780 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781 f = strtod(s, &s);
4782 *value = f;
4783 return (int)((char_u *)s - text);
4784}
4785#endif
4786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 * Get the value of an environment variable.
4789 * "arg" is pointing to the '$'. It is advanced to after the name.
4790 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004791 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004794get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 char_u *string = NULL;
4797 int len;
4798 int cc;
4799 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004800 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 ++*arg;
4803 name = *arg;
4804 len = get_env_len(arg);
4805 if (evaluate)
4806 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004807 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004809
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004810 cc = name[len];
4811 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004813 string = vim_getenv(name, &mustfree);
4814 if (string != NULL && *string != NUL)
4815 {
4816 if (!mustfree)
4817 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004819 else
4820 {
4821 if (mustfree)
4822 vim_free(string);
4823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004825 string = expand_env_save(name - 1);
4826 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004827 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004828 }
4829 name[len] = cc;
4830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 rettv->v_type = VAR_STRING;
4832 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834
4835 return OK;
4836}
4837
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004840 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004843var2fpos(
4844 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004845 int dollar_lnum, // TRUE when $ is last line
4846 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004848 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004850 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004853 if (varp->v_type == VAR_LIST)
4854 {
4855 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004856 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004857 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004858 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004859
4860 l = varp->vval.v_list;
4861 if (l == NULL)
4862 return NULL;
4863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004865 pos.lnum = list_find_nr(l, 0L, &error);
4866 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004870 pos.col = list_find_nr(l, 1L, &error);
4871 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004872 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004873 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004875 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004876 li = list_find(l, 1L);
4877 if (li != NULL && li->li_tv.v_type == VAR_STRING
4878 && li->li_tv.vval.v_string != NULL
4879 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4880 pos.col = len + 1;
4881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004882 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004883 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004885 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004887 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004888 pos.coladd = list_find_nr(l, 2L, &error);
4889 if (error)
4890 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004891
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004892 return &pos;
4893 }
4894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004895 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004896 if (name == NULL)
4897 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004901 {
4902 if (VIsual_active)
4903 return &VIsual;
4904 return &curwin->w_cursor;
4905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004908 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4910 return NULL;
4911 return pp;
4912 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004913
Bram Moolenaara5525202006-03-02 22:52:09 +00004914 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004915
Bram Moolenaar477933c2007-07-17 14:32:23 +00004916 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004917 {
4918 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004920 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004921 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // In silent Ex mode topline is zero, but that's not a valid line
4923 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004924 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004925 return &pos;
4926 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004928 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004929 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004931 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004932 return &pos;
4933 }
4934 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004937 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 pos.lnum = curbuf->b_ml.ml_line_count;
4940 pos.col = 0;
4941 }
4942 else
4943 {
4944 pos.lnum = curwin->w_cursor.lnum;
4945 pos.col = (colnr_T)STRLEN(ml_get_curline());
4946 }
4947 return &pos;
4948 }
4949 return NULL;
4950}
4951
4952/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004953 * Convert list in "arg" into a position and optional file number.
4954 * When "fnump" is NULL there is no file number, only 3 items.
4955 * Note that the column is passed on as-is, the caller may want to decrement
4956 * it to use 1 for the first column.
4957 * Return FAIL when conversion is not possible, doesn't check the position for
4958 * validity.
4959 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004961list2fpos(
4962 typval_T *arg,
4963 pos_T *posp,
4964 int *fnump,
4965 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004966{
4967 list_T *l = arg->vval.v_list;
4968 long i = 0;
4969 long n;
4970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4972 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004973 if (arg->v_type != VAR_LIST
4974 || l == NULL
4975 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004976 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004977 return FAIL;
4978
4979 if (fnump != NULL)
4980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004982 if (n < 0)
4983 return FAIL;
4984 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004986 *fnump = n;
4987 }
4988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004990 if (n < 0)
4991 return FAIL;
4992 posp->lnum = n;
4993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004995 if (n < 0)
4996 return FAIL;
4997 posp->col = n;
4998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005000 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005001 posp->coladd = 0;
5002 else
5003 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005004
Bram Moolenaar493c1782014-05-28 14:34:46 +02005005 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005007
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005008 return OK;
5009}
5010
5011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 * Get the length of an environment variable name.
5013 * Advance "arg" to the first character after the name.
5014 * Return 0 for error.
5015 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005017get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019 char_u *p;
5020 int len;
5021
5022 for (p = *arg; vim_isIDc(*p); ++p)
5023 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 return 0;
5026
5027 len = (int)(p - *arg);
5028 *arg = p;
5029 return len;
5030}
5031
5032/*
5033 * Get the length of the name of a function or internal variable.
5034 * "arg" is advanced to the first non-white character after the name.
5035 * Return 0 if something is wrong.
5036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039{
5040 char_u *p;
5041 int len;
5042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005045 {
5046 if (*p == ':')
5047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005048 // "s:" is start of "s:var", but "n:" is not and can be used in
5049 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005050 len = (int)(p - *arg);
5051 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5052 || len > 1)
5053 break;
5054 }
5055 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 return 0;
5058
5059 len = (int)(p - *arg);
5060 *arg = skipwhite(p);
5061
5062 return len;
5063}
5064
5065/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005066 * Get the length of the name of a variable or function.
5067 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005069 * Return -1 if curly braces expansion failed.
5070 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 * If the name contains 'magic' {}'s, expand them and return the
5072 * expanded name in an allocated string via 'alias' - caller must free.
5073 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005075get_name_len(
5076 char_u **arg,
5077 char_u **alias,
5078 int evaluate,
5079 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 char_u *p;
5083 char_u *expr_start;
5084 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005086 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5089 && (*arg)[2] == (int)KE_SNR)
5090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 *arg += 3;
5093 return get_id_len(arg) + 3;
5094 }
5095 len = eval_fname_script(*arg);
5096 if (len > 0)
5097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 *arg += len;
5100 }
5101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005105 p = find_name_end(*arg, &expr_start, &expr_end,
5106 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (expr_start != NULL)
5108 {
5109 char_u *temp_string;
5110
5111 if (!evaluate)
5112 {
5113 len += (int)(p - *arg);
5114 *arg = skipwhite(p);
5115 return len;
5116 }
5117
5118 /*
5119 * Include any <SID> etc in the expanded string:
5120 * Thus the -len here.
5121 */
5122 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5123 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005124 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 *alias = temp_string;
5126 *arg = skipwhite(p);
5127 return (int)STRLEN(temp_string);
5128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
5130 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005131 // Only give an error when there is something, otherwise it will be
5132 // reported at a higher level.
5133 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005134 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 return len;
5137}
5138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139/*
5140 * Find the end of a variable or function name, taking care of magic braces.
5141 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5142 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005143 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 * Return a pointer to just after the name. Equal to "arg" if there is no
5145 * valid name.
5146 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005148find_name_end(
5149 char_u *arg,
5150 char_u **expr_start,
5151 char_u **expr_end,
5152 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 int mb_nest = 0;
5155 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005157 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005158 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 *expr_start = NULL;
5163 *expr_end = NULL;
5164 }
5165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005167 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5168 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005169 return arg;
5170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 for (p = arg; *p != NUL
5172 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005173 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005174 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005176 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005178 if (*p == '\'')
5179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005180 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005181 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005182 ;
5183 if (*p == NUL)
5184 break;
5185 }
5186 else if (*p == '"')
5187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005189 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005190 if (*p == '\\' && p[1] != NUL)
5191 ++p;
5192 if (*p == NUL)
5193 break;
5194 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005195 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // "s:" is start of "s:var", but "n:" is not and can be used in
5198 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005199 len = (int)(p - arg);
5200 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005201 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005202 break;
5203 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 if (*p == '[')
5208 ++br_nest;
5209 else if (*p == ']')
5210 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005212
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005213 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 if (*p == '{')
5216 {
5217 mb_nest++;
5218 if (expr_start != NULL && *expr_start == NULL)
5219 *expr_start = p;
5220 }
5221 else if (*p == '}')
5222 {
5223 mb_nest--;
5224 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5225 *expr_end = p;
5226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
5230 return p;
5231}
5232
5233/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 * Expands out the 'magic' {}'s in a variable/function name.
5235 * Note that this can call itself recursively, to deal with
5236 * constructs like foo{bar}{baz}{bam}
5237 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5238 * "in_start" ^
5239 * "expr_start" ^
5240 * "expr_end" ^
5241 * "in_end" ^
5242 *
5243 * Returns a new allocated string, which the caller must free.
5244 * Returns NULL for failure.
5245 */
5246 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247make_expanded_name(
5248 char_u *in_start,
5249 char_u *expr_start,
5250 char_u *expr_end,
5251 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005252{
5253 char_u c1;
5254 char_u *retval = NULL;
5255 char_u *temp_result;
5256 char_u *nextcmd = NULL;
5257
5258 if (expr_end == NULL || in_end == NULL)
5259 return NULL;
5260 *expr_start = NUL;
5261 *expr_end = NUL;
5262 c1 = *in_end;
5263 *in_end = NUL;
5264
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005265 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 if (temp_result != NULL && nextcmd == NULL)
5267 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005268 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5269 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005270 if (retval != NULL)
5271 {
5272 STRCPY(retval, in_start);
5273 STRCAT(retval, temp_result);
5274 STRCAT(retval, expr_end + 1);
5275 }
5276 }
5277 vim_free(temp_result);
5278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280 *expr_start = '{';
5281 *expr_end = '}';
5282
5283 if (retval != NULL)
5284 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005285 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 if (expr_start != NULL)
5287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289 temp_result = make_expanded_name(retval, expr_start,
5290 expr_end, temp_result);
5291 vim_free(retval);
5292 retval = temp_result;
5293 }
5294 }
5295
5296 return retval;
5297}
5298
5299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005301 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005304eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005306 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5307}
5308
5309/*
5310 * Return TRUE if character "c" can be used as the first character in a
5311 * variable or function name (excluding '{' and '}').
5312 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005314eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005315{
5316 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
5318
5319/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005320 * Handle:
5321 * - expr[expr], expr[expr:expr] subscript
5322 * - ".name" lookup
5323 * - function call with Funcref variable: func(expr)
5324 * - method call: var->method()
5325 *
5326 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005327 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005329handle_subscript(
5330 char_u **arg,
5331 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005332 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005333 int verbose, // give error messages
5334 char_u *start_leader, // start of '!' and '-' prefixes
5335 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336{
Bram Moolenaar32e35112020-05-14 22:41:15 +02005337 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005338 int ret = OK;
5339 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340
Bram Moolenaar61343f02019-07-20 21:11:13 +02005341 // "." is ".name" lookup when we found a dict or when evaluating and
5342 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005344 && (((**arg == '['
5345 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005346 || (!evaluate
5347 && (*arg)[1] != '.'
5348 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005349 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005350 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005351 && !VIM_ISWHITE(*(*arg - 1)))
5352 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005353 {
5354 if (**arg == '(')
5355 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005356 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005357
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005358 // Stop the expression evaluation when immediately aborting on
5359 // error, or when an interrupt occurred or an exception was thrown
5360 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 if (aborting())
5362 {
5363 if (ret == OK)
5364 clear_tv(rettv);
5365 ret = FAIL;
5366 }
5367 dict_unref(selfdict);
5368 selfdict = NULL;
5369 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005370 else if (**arg == '-')
5371 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005372 // Expression "-1.0->method()" applies the leader "-" before
5373 // applying ->.
5374 if (evaluate && *end_leaderp > start_leader)
5375 ret = eval7_leader(rettv, start_leader, end_leaderp);
5376 if (ret == OK)
5377 {
5378 if ((*arg)[2] == '{')
5379 // expr->{lambda}()
5380 ret = eval_lambda(arg, rettv, evaluate, verbose);
5381 else
5382 // expr->name()
5383 ret = eval_method(arg, rettv, evaluate, verbose);
5384 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005385 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005387 {
5388 dict_unref(selfdict);
5389 if (rettv->v_type == VAR_DICT)
5390 {
5391 selfdict = rettv->vval.v_dict;
5392 if (selfdict != NULL)
5393 ++selfdict->dv_refcount;
5394 }
5395 else
5396 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02005397 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005398 {
5399 clear_tv(rettv);
5400 ret = FAIL;
5401 }
5402 }
5403 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5406 // Don't do this when "Func" is already a partial that was bound
5407 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005408 if (selfdict != NULL
5409 && (rettv->v_type == VAR_FUNC
5410 || (rettv->v_type == VAR_PARTIAL
5411 && (rettv->vval.v_partial->pt_auto
5412 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005415 dict_unref(selfdict);
5416 return ret;
5417}
5418
5419/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005420 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 * value).
5422 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005423 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005426 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427}
5428
5429/*
5430 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 * The string "s" must have been allocated, it is consumed.
5432 * Return NULL for out of memory, the variable otherwise.
5433 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005434 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar33570922005-01-25 22:26:29 +00005437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 rettv = alloc_tv();
5440 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 else
5446 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448}
5449
5450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 if (varp != NULL)
5457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 switch (varp->v_type)
5459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005461 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005463 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 vim_free(varp->vval.v_string);
5465 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005466 case VAR_PARTIAL:
5467 partial_unref(varp->vval.v_partial);
5468 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005469 case VAR_BLOB:
5470 blob_unref(varp->vval.v_blob);
5471 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 case VAR_LIST:
5473 list_unref(varp->vval.v_list);
5474 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475 case VAR_DICT:
5476 dict_unref(varp->vval.v_dict);
5477 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005478 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005479#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 job_unref(varp->vval.v_job);
5481 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005482#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005483 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005484#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005485 channel_unref(varp->vval.v_channel);
5486 break;
5487#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 case VAR_NUMBER:
5489 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005490 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005491 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005493 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005494 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005495 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 vim_free(varp);
5498 }
5499}
5500
5501/*
5502 * Free the memory for a variable value and set the value to NULL or 0.
5503 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005505clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 if (varp != NULL)
5508 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005512 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005513 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005514 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005515 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005517 case VAR_PARTIAL:
5518 partial_unref(varp->vval.v_partial);
5519 varp->vval.v_partial = NULL;
5520 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005521 case VAR_BLOB:
5522 blob_unref(varp->vval.v_blob);
5523 varp->vval.v_blob = NULL;
5524 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 case VAR_LIST:
5526 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005527 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 case VAR_DICT:
5530 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005531 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005534 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005535 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 varp->vval.v_number = 0;
5537 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005540 varp->vval.v_float = 0.0;
5541 break;
5542#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005543 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005544#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545 job_unref(varp->vval.v_job);
5546 varp->vval.v_job = NULL;
5547#endif
5548 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005549 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005551 channel_unref(varp->vval.v_channel);
5552 varp->vval.v_channel = NULL;
5553#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005555 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005559 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561}
5562
5563/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 * Set the value of a variable to NULL without freeing items.
5565 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005567init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568{
5569 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005570 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571}
5572
5573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 * Get the number value of a variable.
5575 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005577 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005578 * caller of incompatible types: it sets *denote to TRUE if "denote"
5579 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005581 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005584 int error = FALSE;
5585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005587}
5588
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005589 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005590tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005591{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005592 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005597 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005598 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005599#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005601 break;
5602#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005604 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005605 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 break;
5607 case VAR_STRING:
5608 if (varp->vval.v_string != NULL)
5609 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005610 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005611 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005614 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005615 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005616 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005617 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005618 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005619 case VAR_SPECIAL:
5620 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005621 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005622#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005623 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624 break;
5625#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005626 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005628 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005629 break;
5630#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005631 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005632 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005633 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005634 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005635 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005637 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641 n = -1;
5642 else
5643 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645}
5646
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005647#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005649tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005650{
5651 switch (varp->v_type)
5652 {
5653 case VAR_NUMBER:
5654 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005655 case VAR_FLOAT:
5656 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005657 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005658 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005659 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005660 break;
5661 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005662 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005663 break;
5664 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005666 break;
5667 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005668 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005669 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005670 case VAR_BOOL:
5671 emsg(_("E362: Using a boolean value as a Float"));
5672 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005673 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005674 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005675 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005676 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005677# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005678 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005679 break;
5680# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005681 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005682# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005683 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005684 break;
5685# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005686 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005687 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005688 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005689 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005690 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005692 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005693 break;
5694 }
5695 return 0;
5696}
5697#endif
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 * Get the string value of a variable.
5701 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005702 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5703 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 * If the String variable has never been set, return an empty string.
5705 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005706 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005707 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005709 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005710tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 static char_u mybuf[NUMBUFLEN];
5713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005714 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715}
5716
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005717 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005718tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005720 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005721
5722 return res != NULL ? res : (char_u *)"";
5723}
5724
Bram Moolenaar7d647822014-04-05 21:28:56 +02005725/*
5726 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5727 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005728 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005729tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005730{
5731 static char_u mybuf[NUMBUFLEN];
5732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005733 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005734}
5735
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005736 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005737tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005738{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005742 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005743 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 return buf;
5745 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005746 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005747 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 break;
5749 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005750 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 break;
5752 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005753 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005755 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005756#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005757 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005758 break;
5759#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 case VAR_STRING:
5761 if (varp->vval.v_string != NULL)
5762 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005763 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005764 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005765 case VAR_SPECIAL:
5766 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5767 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005768 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005769 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005770 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005772#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 {
5774 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005775 char *status;
5776
5777 if (job == NULL)
5778 return (char_u *)"no process";
5779 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005780 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005781 : "run";
5782# ifdef UNIX
5783 vim_snprintf((char *)buf, NUMBUFLEN,
5784 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005785# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005786 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005787 "process %ld %s",
5788 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005789 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005790# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005792 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5793# endif
5794 return buf;
5795 }
5796#endif
5797 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005798 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005799#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005800 {
5801 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005802 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005803
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005804 if (channel == NULL)
5805 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5806 else
5807 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005808 "channel %d %s", channel->ch_id, status);
5809 return buf;
5810 }
5811#endif
5812 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005813 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005814 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005816 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005819 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821
5822/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005823 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5824 * string() on Dict, List, etc.
5825 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005826 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005827tv_stringify(typval_T *varp, char_u *buf)
5828{
5829 if (varp->v_type == VAR_LIST
5830 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005831 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005832 || varp->v_type == VAR_FUNC
5833 || varp->v_type == VAR_PARTIAL
5834 || varp->v_type == VAR_FLOAT)
5835 {
5836 typval_T tmp;
5837
5838 f_string(varp, &tmp);
5839 tv_get_string_buf(&tmp, buf);
5840 clear_tv(varp);
5841 *varp = tmp;
5842 return tmp.vval.v_string;
5843 }
5844 return tv_get_string_buf(varp, buf);
5845}
5846
5847/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005848 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5849 * Also give an error message, using "name" or _("name") when use_gettext is
5850 * TRUE.
5851 */
5852 static int
5853tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5854{
5855 int lock = 0;
5856
5857 switch (tv->v_type)
5858 {
5859 case VAR_BLOB:
5860 if (tv->vval.v_blob != NULL)
5861 lock = tv->vval.v_blob->bv_lock;
5862 break;
5863 case VAR_LIST:
5864 if (tv->vval.v_list != NULL)
5865 lock = tv->vval.v_list->lv_lock;
5866 break;
5867 case VAR_DICT:
5868 if (tv->vval.v_dict != NULL)
5869 lock = tv->vval.v_dict->dv_lock;
5870 break;
5871 default:
5872 break;
5873 }
5874 return var_check_lock(tv->v_lock, name, use_gettext)
5875 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5876}
5877
5878/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005879 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005881 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005882 * It is OK for "from" and "to" to point to the same item. This is used to
5883 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005886copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005889 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 switch (from->v_type)
5891 {
5892 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005893 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005894 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 to->vval.v_number = from->vval.v_number;
5896 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005897 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005898#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005899 to->vval.v_float = from->vval.v_float;
5900 break;
5901#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005902 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005905 if (to->vval.v_job != NULL)
5906 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005907 break;
5908#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005909 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005910#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005911 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005912 if (to->vval.v_channel != NULL)
5913 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005914 break;
5915#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 case VAR_STRING:
5917 case VAR_FUNC:
5918 if (from->vval.v_string == NULL)
5919 to->vval.v_string = NULL;
5920 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005921 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005923 if (from->v_type == VAR_FUNC)
5924 func_ref(to->vval.v_string);
5925 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005927 case VAR_PARTIAL:
5928 if (from->vval.v_partial == NULL)
5929 to->vval.v_partial = NULL;
5930 else
5931 {
5932 to->vval.v_partial = from->vval.v_partial;
5933 ++to->vval.v_partial->pt_refcount;
5934 }
5935 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005936 case VAR_BLOB:
5937 if (from->vval.v_blob == NULL)
5938 to->vval.v_blob = NULL;
5939 else
5940 {
5941 to->vval.v_blob = from->vval.v_blob;
5942 ++to->vval.v_blob->bv_refcount;
5943 }
5944 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 case VAR_LIST:
5946 if (from->vval.v_list == NULL)
5947 to->vval.v_list = NULL;
5948 else
5949 {
5950 to->vval.v_list = from->vval.v_list;
5951 ++to->vval.v_list->lv_refcount;
5952 }
5953 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 case VAR_DICT:
5955 if (from->vval.v_dict == NULL)
5956 to->vval.v_dict = NULL;
5957 else
5958 {
5959 to->vval.v_dict = from->vval.v_dict;
5960 ++to->vval.v_dict->dv_refcount;
5961 }
5962 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005963 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005964 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005966 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 break;
5968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969}
5970
5971/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 * Make a copy of an item.
5973 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005974 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5975 * reference to an already copied list/dict can be used.
5976 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979item_copy(
5980 typval_T *from,
5981 typval_T *to,
5982 int deep,
5983 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984{
5985 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005986 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005990 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005991 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992 }
5993 ++recurse;
5994
5995 switch (from->v_type)
5996 {
5997 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005998 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 case VAR_STRING:
6000 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006001 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006002 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006003 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006004 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006005 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 copy_tv(from, to);
6007 break;
6008 case VAR_LIST:
6009 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006010 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006011 if (from->vval.v_list == NULL)
6012 to->vval.v_list = NULL;
6013 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006015 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006016 to->vval.v_list = from->vval.v_list->lv_copylist;
6017 ++to->vval.v_list->lv_refcount;
6018 }
6019 else
6020 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6021 if (to->vval.v_list == NULL)
6022 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006024 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006026 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006027 case VAR_DICT:
6028 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006029 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006030 if (from->vval.v_dict == NULL)
6031 to->vval.v_dict = NULL;
6032 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006034 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006035 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6036 ++to->vval.v_dict->dv_refcount;
6037 }
6038 else
6039 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6040 if (to->vval.v_dict == NULL)
6041 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006042 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006043 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006044 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006046 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 }
6049 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051}
6052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 void
6054echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6055{
6056 char_u *tofree;
6057 char_u numbuf[NUMBUFLEN];
6058 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6059
6060 if (*atstart)
6061 {
6062 *atstart = FALSE;
6063 // Call msg_start() after eval1(), evaluating the expression
6064 // may cause a message to appear.
6065 if (with_space)
6066 {
6067 // Mark the saved text as finishing the line, so that what
6068 // follows is displayed on a new line when scrolling back
6069 // at the more prompt.
6070 msg_sb_eol();
6071 msg_start();
6072 }
6073 }
6074 else if (with_space)
6075 msg_puts_attr(" ", echo_attr);
6076
6077 if (p != NULL)
6078 for ( ; *p != NUL && !got_int; ++p)
6079 {
6080 if (*p == '\n' || *p == '\r' || *p == TAB)
6081 {
6082 if (*p != TAB && *needclr)
6083 {
6084 // remove any text still there from the command
6085 msg_clr_eos();
6086 *needclr = FALSE;
6087 }
6088 msg_putchar_attr(*p, echo_attr);
6089 }
6090 else
6091 {
6092 if (has_mbyte)
6093 {
6094 int i = (*mb_ptr2len)(p);
6095
6096 (void)msg_outtrans_len_attr(p, i, echo_attr);
6097 p += i - 1;
6098 }
6099 else
6100 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6101 }
6102 }
6103 vim_free(tofree);
6104}
6105
Bram Moolenaare9a41262005-01-15 22:18:47 +00006106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 * ":echo expr1 ..." print each argument separated with a space, add a
6108 * newline at the end.
6109 * ":echon expr1 ..." print each argument plain.
6110 */
6111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 char_u *p;
6117 int needclr = TRUE;
6118 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006119 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006120 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (eap->skip)
6123 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006124 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006126 // If eval1() causes an error message the text from the command may
6127 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006128 need_clr_eos = needclr;
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02006131 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 /*
6134 * Report the invalid expression unless the expression evaluation
6135 * has been cancelled due to an aborting error, an interrupt, or an
6136 * exception.
6137 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006138 if (!aborting() && did_emsg == did_emsg_before
6139 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006140 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006141 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 break;
6143 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006144 need_clr_eos = FALSE;
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 arg = skipwhite(arg);
6151 }
6152 eap->nextcmd = check_nextcmd(arg);
6153
6154 if (eap->skip)
6155 --emsg_skip;
6156 else
6157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006158 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (needclr)
6160 msg_clr_eos();
6161 if (eap->cmdidx == CMD_echo)
6162 msg_end();
6163 }
6164}
6165
6166/*
6167 * ":echohl {name}".
6168 */
6169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006172 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173}
6174
6175/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006176 * Returns the :echo attribute
6177 */
6178 int
6179get_echo_attr(void)
6180{
6181 return echo_attr;
6182}
6183
6184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 * ":execute expr1 ..." execute the result of an expression.
6186 * ":echomsg expr1 ..." Print a message
6187 * ":echoerr expr1 ..." Print an error
6188 * Each gets spaces around each argument and a newline at the end for
6189 * echo commands
6190 */
6191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006192ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 int ret = OK;
6197 char_u *p;
6198 garray_T ga;
6199 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 ga_init2(&ga, 1, 80);
6202
6203 if (eap->skip)
6204 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006205 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006207 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6208 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 if (!eap->skip)
6212 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006213 char_u buf[NUMBUFLEN];
6214
6215 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006216 {
6217 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6218 {
6219 emsg(_(e_inval_string));
6220 p = NULL;
6221 }
6222 else
6223 p = tv_get_string_buf(&rettv, buf);
6224 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006225 else
6226 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006227 if (p == NULL)
6228 {
6229 clear_tv(&rettv);
6230 ret = FAIL;
6231 break;
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 len = (int)STRLEN(p);
6234 if (ga_grow(&ga, len + 2) == FAIL)
6235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 ret = FAIL;
6238 break;
6239 }
6240 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 ga.ga_len += len;
6244 }
6245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 arg = skipwhite(arg);
6248 }
6249
6250 if (ret != FAIL && ga.ga_data != NULL)
6251 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006252 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006254 // Mark the already saved text as finishing the line, so that what
6255 // follows is displayed on a new line when scrolling back at the
6256 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006257 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006258 }
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006261 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006262 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006263 out_flush();
6264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 else if (eap->cmdidx == CMD_echoerr)
6266 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006267 int save_did_emsg = did_emsg;
6268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006269 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006270 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (!force_abort)
6272 did_emsg = save_did_emsg;
6273 }
6274 else if (eap->cmdidx == CMD_execute)
6275 do_cmdline((char_u *)ga.ga_data,
6276 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6277 }
6278
6279 ga_clear(&ga);
6280
6281 if (eap->skip)
6282 --emsg_skip;
6283
6284 eap->nextcmd = check_nextcmd(arg);
6285}
6286
6287/*
6288 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6289 * "arg" points to the "&" or '+' when called, to "option" when returning.
6290 * Returns NULL when no option name found. Otherwise pointer to the char
6291 * after the option name.
6292 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
6296 char_u *p = *arg;
6297
6298 ++p;
6299 if (*p == 'g' && p[1] == ':')
6300 {
6301 *opt_flags = OPT_GLOBAL;
6302 p += 2;
6303 }
6304 else if (*p == 'l' && p[1] == ':')
6305 {
6306 *opt_flags = OPT_LOCAL;
6307 p += 2;
6308 }
6309 else
6310 *opt_flags = 0;
6311
6312 if (!ASCII_ISALPHA(*p))
6313 return NULL;
6314 *arg = p;
6315
6316 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006317 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 else
6319 while (ASCII_ISALPHA(*p))
6320 ++p;
6321 return p;
6322}
6323
6324/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006325 * Display script name where an item was last set.
6326 * Should only be invoked when 'verbose' is non-zero.
6327 */
6328 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006329last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006330{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006331 char_u *p;
6332
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006333 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006334 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006335 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006336 if (p != NULL)
6337 {
6338 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006339 msg_puts(_("\n\tLast set from "));
6340 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006341 if (script_ctx.sc_lnum > 0)
6342 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006343 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006344 msg_outnum((long)script_ctx.sc_lnum);
6345 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006346 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006347 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006348 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006349 }
6350}
6351
Bram Moolenaar61be3762019-03-19 23:04:17 +01006352/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006353 * Compare "typ1" and "typ2". Put the result in "typ1".
6354 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006355 int
6356typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006357 typval_T *typ1, // first operand
6358 typval_T *typ2, // second operand
6359 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006360 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006361{
6362 int i;
6363 varnumber_T n1, n2;
6364 char_u *s1, *s2;
6365 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006366 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006367
Bram Moolenaar31988702018-02-13 12:57:42 +01006368 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006370 // For "is" a different type always means FALSE, for "notis"
6371 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006372 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006373 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006374 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6375 {
6376 if (type_is)
6377 {
6378 n1 = (typ1->v_type == typ2->v_type
6379 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006380 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006381 n1 = !n1;
6382 }
6383 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006384 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006385 {
6386 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006387 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006388 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006389 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006390 clear_tv(typ1);
6391 return FAIL;
6392 }
6393 else
6394 {
6395 // Compare two Blobs for being equal or unequal.
6396 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006397 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006398 n1 = !n1;
6399 }
6400 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006401 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6402 {
6403 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006404 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006405 n1 = (typ1->v_type == typ2->v_type
6406 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006407 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006408 n1 = !n1;
6409 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006410 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006411 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006412 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006413 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006414 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006415 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006416 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006417 clear_tv(typ1);
6418 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006419 }
6420 else
6421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006422 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006423 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6424 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006425 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006426 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006427 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006428 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006429
6430 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6431 {
6432 if (type_is)
6433 {
6434 n1 = (typ1->v_type == typ2->v_type
6435 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006436 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006437 n1 = !n1;
6438 }
6439 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006440 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006441 {
6442 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006443 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006444 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006445 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006446 clear_tv(typ1);
6447 return FAIL;
6448 }
6449 else
6450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006451 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006452 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6453 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006454 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006455 n1 = !n1;
6456 }
6457 }
6458
6459 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6460 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6461 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006462 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6463 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006465 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006466 clear_tv(typ1);
6467 return FAIL;
6468 }
6469 if ((typ1->v_type == VAR_PARTIAL
6470 && typ1->vval.v_partial == NULL)
6471 || (typ2->v_type == VAR_PARTIAL
6472 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006473 // When both partials are NULL, then they are equal.
6474 // Otherwise they are not equal.
6475 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006476 else if (type_is)
6477 {
6478 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 // strings are considered the same if their value is
6480 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006481 n1 = tv_equal(typ1, typ2, ic, FALSE);
6482 else if (typ1->v_type == VAR_PARTIAL
6483 && typ2->v_type == VAR_PARTIAL)
6484 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6485 else
6486 n1 = FALSE;
6487 }
6488 else
6489 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006490 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006491 n1 = !n1;
6492 }
6493
6494#ifdef FEAT_FLOAT
6495 /*
6496 * If one of the two variables is a float, compare as a float.
6497 * When using "=~" or "!~", always compare as string.
6498 */
6499 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006500 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006501 {
6502 float_T f1, f2;
6503
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006504 f1 = tv_get_float(typ1);
6505 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006506 n1 = FALSE;
6507 switch (type)
6508 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006509 case EXPR_IS:
6510 case EXPR_EQUAL: n1 = (f1 == f2); break;
6511 case EXPR_ISNOT:
6512 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6513 case EXPR_GREATER: n1 = (f1 > f2); break;
6514 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6515 case EXPR_SMALLER: n1 = (f1 < f2); break;
6516 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6517 case EXPR_UNKNOWN:
6518 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006520 }
6521 }
6522#endif
6523
6524 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006525 * If one of the two variables is a number, compare as a number.
6526 * When using "=~" or "!~", always compare as string.
6527 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006528 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006529 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006531 n1 = tv_get_number(typ1);
6532 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006533 switch (type)
6534 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006535 case EXPR_IS:
6536 case EXPR_EQUAL: n1 = (n1 == n2); break;
6537 case EXPR_ISNOT:
6538 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6539 case EXPR_GREATER: n1 = (n1 > n2); break;
6540 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6541 case EXPR_SMALLER: n1 = (n1 < n2); break;
6542 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6543 case EXPR_UNKNOWN:
6544 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006546 }
6547 }
6548 else
6549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006550 s1 = tv_get_string_buf(typ1, buf1);
6551 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006552 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006553 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6554 else
6555 i = 0;
6556 n1 = FALSE;
6557 switch (type)
6558 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006559 case EXPR_IS:
6560 case EXPR_EQUAL: n1 = (i == 0); break;
6561 case EXPR_ISNOT:
6562 case EXPR_NEQUAL: n1 = (i != 0); break;
6563 case EXPR_GREATER: n1 = (i > 0); break;
6564 case EXPR_GEQUAL: n1 = (i >= 0); break;
6565 case EXPR_SMALLER: n1 = (i < 0); break;
6566 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006567
Bram Moolenaar87396072019-12-31 22:36:18 +01006568 case EXPR_MATCH:
6569 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006570 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006571 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006572 n1 = !n1;
6573 break;
6574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006576 }
6577 }
6578 clear_tv(typ1);
6579 typ1->v_type = VAR_NUMBER;
6580 typ1->vval.v_number = n1;
6581
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006582 return OK;
6583}
6584
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006585 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006586typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006587{
6588 char_u *tofree;
6589 char_u numbuf[NUMBUFLEN];
6590 char_u *ret = NULL;
6591
6592 if (arg == NULL)
6593 return vim_strsave((char_u *)"(does not exist)");
6594 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006595 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006596 if (ret != NULL && tofree == NULL)
6597 ret = vim_strsave(ret);
6598 return ret;
6599}
6600
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006601#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602
6603/*
6604 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006605 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 * "flags" can be "g" to do a global substitute.
6607 * Returns an allocated string, NULL for error.
6608 */
6609 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610do_string_sub(
6611 char_u *str,
6612 char_u *pat,
6613 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006614 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006615 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 int sublen;
6618 regmatch_T regmatch;
6619 int i;
6620 int do_all;
6621 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006622 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 garray_T ga;
6624 char_u *ret;
6625 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006626 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006628 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006630 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632 ga_init2(&ga, 1, 200);
6633
6634 do_all = (flags[0] == 'g');
6635
6636 regmatch.rm_ic = p_ic;
6637 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6638 if (regmatch.regprog != NULL)
6639 {
6640 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006641 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006645 if (regmatch.startp[0] == regmatch.endp[0])
6646 {
6647 if (zero_width == regmatch.startp[0])
6648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006650 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006651 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6652 (size_t)i);
6653 ga.ga_len += i;
6654 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006655 continue;
6656 }
6657 zero_width = regmatch.startp[0];
6658 }
6659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 /*
6661 * Get some space for a temporary buffer to do the substitution
6662 * into. It will contain:
6663 * - The text up to where the match is.
6664 * - The substituted text.
6665 * - The text after the match.
6666 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006667 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006668 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6670 {
6671 ga_clear(&ga);
6672 break;
6673 }
6674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006675 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 i = (int)(regmatch.startp[0] - tail);
6677 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006679 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 + ga.ga_len + i, TRUE, TRUE, FALSE);
6681 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006682 tail = regmatch.endp[0];
6683 if (*tail == NUL)
6684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 if (!do_all)
6686 break;
6687 }
6688
6689 if (ga.ga_data != NULL)
6690 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6691
Bram Moolenaar473de612013-06-08 18:19:48 +02006692 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 }
6694
6695 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6696 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006697 if (p_cpo == empty_option)
6698 p_cpo = save_cpo;
6699 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006700 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006701 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703 return ret;
6704}