blob: 00b6c59df2a83ee8e66ed4ca988004939b815d5b [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;
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003506 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 /*
3509 * Find the end of the string, skipping backslashed characters.
3510 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003511 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 if (*p == '\\' && p[1] != NUL)
3514 {
3515 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003516 // A "\<x>" form occupies at least 4 characters, and produces up
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003517 // to 9 characters (6 for the char and 3 for a modifier): reserve
3518 // space for 5 extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (*p == '<')
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003520 extra += 5;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523
3524 if (*p != '"')
3525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003526 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 return FAIL;
3528 }
3529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (!evaluate)
3532 {
3533 *arg = p + 1;
3534 return OK;
3535 }
3536
3537 /*
3538 * Copy the string into allocated memory, handling backslashed
3539 * characters.
3540 */
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003541 len = (int)(p - *arg + extra);
3542 name = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (name == NULL)
3544 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545 rettv->v_type = VAR_STRING;
3546 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 if (*p == '\\')
3551 {
3552 switch (*++p)
3553 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003554 case 'b': *name++ = BS; ++p; break;
3555 case 'e': *name++ = ESC; ++p; break;
3556 case 'f': *name++ = FF; ++p; break;
3557 case 'n': *name++ = NL; ++p; break;
3558 case 'r': *name++ = CAR; ++p; break;
3559 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003563 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case 'U':
3565 if (vim_isxdigit(p[1]))
3566 {
3567 int n, nr;
3568 int c = toupper(*p);
3569
3570 if (c == 'X')
3571 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003572 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003574 else
3575 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 nr = 0;
3577 while (--n >= 0 && vim_isxdigit(p[1]))
3578 {
3579 ++p;
3580 nr = (nr << 4) + hex2nr(*p);
3581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003583 // For "\u" store the number according to
3584 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003588 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 break;
3591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003592 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 case '0':
3594 case '1':
3595 case '2':
3596 case '3':
3597 case '4':
3598 case '5':
3599 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 case '7': *name = *p++ - '0';
3601 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 *name = (*name << 3) + *p++ - '0';
3604 if (*p >= '0' && *p <= '7')
3605 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 break;
3609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003610 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003611 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3612 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (extra != 0)
3614 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 name += extra;
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003616 if (name >= rettv->vval.v_string + len)
3617 iemsg("get_string_tv() used more space than allocated");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003620 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 break;
3624 }
3625 }
3626 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003632 ++p;
3633 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 return OK;
3636}
3637
3638/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Return OK or FAIL.
3641 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003643get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003646 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003647 int reduce = 0;
3648
3649 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003651 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003652 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003656 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003657 break;
3658 ++reduce;
3659 ++p;
3660 }
3661 }
3662
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003665 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003666 return FAIL;
3667 }
3668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003669 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003670 if (!evaluate)
3671 {
3672 *arg = p + 1;
3673 return OK;
3674 }
3675
3676 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003678 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003679 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003680 if (str == NULL)
3681 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 rettv->v_type = VAR_STRING;
3683 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003684
Bram Moolenaar8c711452005-01-14 21:53:12 +00003685 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003687 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003690 break;
3691 ++p;
3692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003696 *arg = p + 1;
3697
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003698 return OK;
3699}
3700
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003701/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003702 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003703 */
3704 char_u *
3705partial_name(partial_T *pt)
3706{
3707 if (pt->pt_name != NULL)
3708 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003709 if (pt->pt_func != NULL)
3710 return pt->pt_func->uf_name;
3711 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712}
3713
Bram Moolenaarddecc252016-04-06 22:59:37 +02003714 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003715partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003716{
3717 int i;
3718
3719 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003720 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003721 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003722 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723 if (pt->pt_name != NULL)
3724 {
3725 func_unref(pt->pt_name);
3726 vim_free(pt->pt_name);
3727 }
3728 else
3729 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003730
3731 if (pt->pt_funcstack != NULL)
3732 {
3733 // Decrease the reference count for the context of a closure. If down
3734 // to zero free it and clear the variables on the stack.
3735 if (--pt->pt_funcstack->fs_refcount == 0)
3736 {
3737 garray_T *gap = &pt->pt_funcstack->fs_ga;
3738 typval_T *stack = gap->ga_data;
3739
3740 for (i = 0; i < gap->ga_len; ++i)
3741 clear_tv(stack + i);
3742 ga_clear(gap);
3743 vim_free(pt->pt_funcstack);
3744 }
3745 pt->pt_funcstack = NULL;
3746 }
3747
Bram Moolenaarddecc252016-04-06 22:59:37 +02003748 vim_free(pt);
3749}
3750
3751/*
3752 * Unreference a closure: decrement the reference count and free it when it
3753 * becomes zero.
3754 */
3755 void
3756partial_unref(partial_T *pt)
3757{
3758 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003759 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003760}
3761
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003762static int tv_equal_recurse_limit;
3763
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003764 static int
3765func_equal(
3766 typval_T *tv1,
3767 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003769{
3770 char_u *s1, *s2;
3771 dict_T *d1, *d2;
3772 int a1, a2;
3773 int i;
3774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003775 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003776 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003777 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003778 if (s1 != NULL && *s1 == NUL)
3779 s1 = NULL;
3780 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003782 if (s2 != NULL && *s2 == NUL)
3783 s2 = NULL;
3784 if (s1 == NULL || s2 == NULL)
3785 {
3786 if (s1 != s2)
3787 return FALSE;
3788 }
3789 else if (STRCMP(s1, s2) != 0)
3790 return FALSE;
3791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003792 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003793 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3794 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3795 if (d1 == NULL || d2 == NULL)
3796 {
3797 if (d1 != d2)
3798 return FALSE;
3799 }
3800 else if (!dict_equal(d1, d2, ic, TRUE))
3801 return FALSE;
3802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003803 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003804 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3805 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3806 if (a1 != a2)
3807 return FALSE;
3808 for (i = 0; i < a1; ++i)
3809 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3810 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3811 return FALSE;
3812
3813 return TRUE;
3814}
3815
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003816/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003817 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003818 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003819 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003820 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822tv_equal(
3823 typval_T *tv1,
3824 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 int ic, // ignore case
3826 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003827{
3828 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003830 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003831 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003833 // Catch lists and dicts that have an endless loop by limiting
3834 // recursiveness to a limit. We guess they are equal then.
3835 // A fixed limit has the problem of still taking an awful long time.
3836 // Reduce the limit every time running into it. That should work fine for
3837 // deeply linked structures that are not recursively linked and catch
3838 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003839 if (!recursive)
3840 tv_equal_recurse_limit = 1000;
3841 if (recursive_cnt >= tv_equal_recurse_limit)
3842 {
3843 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003844 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003845 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003847 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3848 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003849 if ((tv1->v_type == VAR_FUNC
3850 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3851 && (tv2->v_type == VAR_FUNC
3852 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3853 {
3854 ++recursive_cnt;
3855 r = func_equal(tv1, tv2, ic);
3856 --recursive_cnt;
3857 return r;
3858 }
3859
3860 if (tv1->v_type != tv2->v_type)
3861 return FALSE;
3862
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003863 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003864 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003865 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003866 ++recursive_cnt;
3867 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3868 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003869 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003870
3871 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003872 ++recursive_cnt;
3873 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3874 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003875 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003876
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003877 case VAR_BLOB:
3878 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3879
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003880 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 case VAR_BOOL:
3882 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003883 return tv1->vval.v_number == tv2->vval.v_number;
3884
3885 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003886 s1 = tv_get_string_buf(tv1, buf1);
3887 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003888 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003889
Bram Moolenaar835dc632016-02-07 14:27:38 +01003890 case VAR_FLOAT:
3891#ifdef FEAT_FLOAT
3892 return tv1->vval.v_float == tv2->vval.v_float;
3893#endif
3894 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003895#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003896 return tv1->vval.v_job == tv2->vval.v_job;
3897#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003898 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003899#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003900 return tv1->vval.v_channel == tv2->vval.v_channel;
3901#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003903 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003904 return tv1->vval.v_partial == tv2->vval.v_partial;
3905
3906 case VAR_FUNC:
3907 return tv1->vval.v_string == tv2->vval.v_string;
3908
Bram Moolenaar835dc632016-02-07 14:27:38 +01003909 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003910 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003912 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003913 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003915 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3916 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003917 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003918}
3919
3920/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003921 * Return the next (unique) copy ID.
3922 * Used for serializing nested structures.
3923 */
3924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003925get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003926{
3927 current_copyID += COPYID_INC;
3928 return current_copyID;
3929}
3930
3931/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003932 * Garbage collection for lists and dictionaries.
3933 *
3934 * We use reference counts to be able to free most items right away when they
3935 * are no longer used. But for composite items it's possible that it becomes
3936 * unused while the reference count is > 0: When there is a recursive
3937 * reference. Example:
3938 * :let l = [1, 2, 3]
3939 * :let d = {9: l}
3940 * :let l[1] = d
3941 *
3942 * Since this is quite unusual we handle this with garbage collection: every
3943 * once in a while find out which lists and dicts are not referenced from any
3944 * variable.
3945 *
3946 * Here is a good reference text about garbage collection (refers to Python
3947 * but it applies to all reference-counting mechanisms):
3948 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003949 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003950
3951/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003952 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003953 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003954 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003955 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003956 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003957garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003958{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003959 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003960 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003961 buf_T *buf;
3962 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003963 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003964 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003965
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003966 if (!testing)
3967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003968 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003969 want_garbage_collect = FALSE;
3970 may_garbage_collect = FALSE;
3971 garbage_collect_at_exit = FALSE;
3972 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003973
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003974 // The execution stack can grow big, limit the size.
3975 if (exestack.ga_maxlen - exestack.ga_len > 500)
3976 {
3977 size_t new_len;
3978 char_u *pp;
3979 int n;
3980
3981 // Keep 150% of the current size, with a minimum of the growth size.
3982 n = exestack.ga_len / 2;
3983 if (n < exestack.ga_growsize)
3984 n = exestack.ga_growsize;
3985
3986 // Don't make it bigger though.
3987 if (exestack.ga_len + n < exestack.ga_maxlen)
3988 {
3989 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3990 pp = vim_realloc(exestack.ga_data, new_len);
3991 if (pp == NULL)
3992 return FAIL;
3993 exestack.ga_maxlen = exestack.ga_len + n;
3994 exestack.ga_data = pp;
3995 }
3996 }
3997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003998 // We advance by two because we add one for items referenced through
3999 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004000 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004001
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004002 /*
4003 * 1. Go through all accessible variables and mark all lists and dicts
4004 * with copyID.
4005 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 // Don't free variables in the previous_funccal list unless they are only
4008 // referenced through previous_funccal. This must be first, because if
4009 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004012 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004013 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004016 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4018 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004020 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004022 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4023 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004024 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004025 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4026 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004027#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004028 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004029 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4030 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004031 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004032 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004033 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4034 NULL, NULL);
4035#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004038 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4040 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004042 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004044 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004048 abort = abort || set_ref_in_functions(copyID);
4049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004053 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004054 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004055
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004056 // callbacks in buffers
4057 abort = abort || set_ref_in_buffers(copyID);
4058
Bram Moolenaar1dced572012-04-05 16:54:08 +02004059#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004061#endif
4062
Bram Moolenaardb913952012-06-29 12:54:53 +02004063#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004065#endif
4066
4067#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004068 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004069#endif
4070
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004071#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004072 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004073 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004074#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004075#ifdef FEAT_NETBEANS_INTG
4076 abort = abort || set_ref_in_nb_channel(copyID);
4077#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004078
Bram Moolenaare3188e22016-05-31 21:13:04 +02004079#ifdef FEAT_TIMERS
4080 abort = abort || set_ref_in_timer(copyID);
4081#endif
4082
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004083#ifdef FEAT_QUICKFIX
4084 abort = abort || set_ref_in_quickfix(copyID);
4085#endif
4086
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004087#ifdef FEAT_TERMINAL
4088 abort = abort || set_ref_in_term(copyID);
4089#endif
4090
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004091#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004092 abort = abort || set_ref_in_popups(copyID);
4093#endif
4094
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004095 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004096 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004097 /*
4098 * 2. Free lists and dictionaries that are not referenced.
4099 */
4100 did_free = free_unref_items(copyID);
4101
4102 /*
4103 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004105 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004106 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004107 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004108 else if (p_verbose > 0)
4109 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004111 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004112
4113 return did_free;
4114}
4115
4116/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004117 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004118 */
4119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004120free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004121{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004122 int did_free = FALSE;
4123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004124 // Let all "free" functions know that we are here. This means no
4125 // dictionaries, lists, channels or jobs are to be freed, because we will
4126 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004127 in_free_unref_items = TRUE;
4128
4129 /*
4130 * PASS 1: free the contents of the items. We don't free the items
4131 * themselves yet, so that it is possible to decrement refcount counters
4132 */
4133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004134 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004135 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004137 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004138 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139
4140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // Go through the list of jobs and free items without the copyID. This
4142 // must happen before doing channels, because jobs refer to channels, but
4143 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4145
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004146 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4148#endif
4149
4150 /*
4151 * PASS 2: free the items themselves.
4152 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004153 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004154 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004157 // Go through the list of jobs and free items without the copyID. This
4158 // must happen before doing channels, because jobs refer to channels, but
4159 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004160 free_unused_jobs(copyID, COPYID_MASK);
4161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004162 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 free_unused_channels(copyID, COPYID_MASK);
4164#endif
4165
4166 in_free_unref_items = FALSE;
4167
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004168 return did_free;
4169}
4170
4171/*
4172 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004173 * "list_stack" is used to add lists to be marked. Can be NULL.
4174 *
4175 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004176 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004178set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004179{
4180 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004181 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004182 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004183 hashtab_T *cur_ht;
4184 ht_stack_T *ht_stack = NULL;
4185 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004186
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004187 cur_ht = ht;
4188 for (;;)
4189 {
4190 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004192 // Mark each item in the hashtab. If the item contains a hashtab
4193 // it is added to ht_stack, if it contains a list it is added to
4194 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004195 todo = (int)cur_ht->ht_used;
4196 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4197 if (!HASHITEM_EMPTY(hi))
4198 {
4199 --todo;
4200 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4201 &ht_stack, list_stack);
4202 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004203 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004204
4205 if (ht_stack == NULL)
4206 break;
4207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004208 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004209 cur_ht = ht_stack->ht;
4210 tempitem = ht_stack;
4211 ht_stack = ht_stack->prev;
4212 free(tempitem);
4213 }
4214
4215 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004216}
4217
4218/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004219 * Mark a dict and its items with "copyID".
4220 * Returns TRUE if setting references failed somehow.
4221 */
4222 int
4223set_ref_in_dict(dict_T *d, int copyID)
4224{
4225 if (d != NULL && d->dv_copyID != copyID)
4226 {
4227 d->dv_copyID = copyID;
4228 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4229 }
4230 return FALSE;
4231}
4232
4233/*
4234 * Mark a list and its items with "copyID".
4235 * Returns TRUE if setting references failed somehow.
4236 */
4237 int
4238set_ref_in_list(list_T *ll, int copyID)
4239{
4240 if (ll != NULL && ll->lv_copyID != copyID)
4241 {
4242 ll->lv_copyID = copyID;
4243 return set_ref_in_list_items(ll, copyID, NULL);
4244 }
4245 return FALSE;
4246}
4247
4248/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004249 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004250 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4251 *
4252 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004253 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004254 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004255set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004256{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004257 listitem_T *li;
4258 int abort = FALSE;
4259 list_T *cur_l;
4260 list_stack_T *list_stack = NULL;
4261 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004262
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004263 cur_l = l;
4264 for (;;)
4265 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004266 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // Mark each item in the list. If the item contains a hashtab
4268 // it is added to ht_stack, if it contains a list it is added to
4269 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004270 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4271 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4272 ht_stack, &list_stack);
4273 if (list_stack == NULL)
4274 break;
4275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004276 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004277 cur_l = list_stack->list;
4278 tempitem = list_stack;
4279 list_stack = list_stack->prev;
4280 free(tempitem);
4281 }
4282
4283 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004284}
4285
4286/*
4287 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004288 * "list_stack" is used to add lists to be marked. Can be NULL.
4289 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4290 *
4291 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004294set_ref_in_item(
4295 typval_T *tv,
4296 int copyID,
4297 ht_stack_T **ht_stack,
4298 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004299{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004300 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004301
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004302 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004303 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304 dict_T *dd = tv->vval.v_dict;
4305
Bram Moolenaara03f2332016-02-06 18:09:59 +01004306 if (dd != NULL && dd->dv_copyID != copyID)
4307 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004308 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004309 dd->dv_copyID = copyID;
4310 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004311 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004312 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4313 }
4314 else
4315 {
4316 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4317 if (newitem == NULL)
4318 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 else
4320 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004321 newitem->ht = &dd->dv_hashtab;
4322 newitem->prev = *ht_stack;
4323 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004324 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004325 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004326 }
4327 }
4328 else if (tv->v_type == VAR_LIST)
4329 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004330 list_T *ll = tv->vval.v_list;
4331
Bram Moolenaara03f2332016-02-06 18:09:59 +01004332 if (ll != NULL && ll->lv_copyID != copyID)
4333 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004334 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004335 ll->lv_copyID = copyID;
4336 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004337 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004338 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004339 }
4340 else
4341 {
4342 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004344 if (newitem == NULL)
4345 abort = TRUE;
4346 else
4347 {
4348 newitem->list = ll;
4349 newitem->prev = *list_stack;
4350 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004351 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004352 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004353 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004354 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004355 else if (tv->v_type == VAR_FUNC)
4356 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004357 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004358 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004359 else if (tv->v_type == VAR_PARTIAL)
4360 {
4361 partial_T *pt = tv->vval.v_partial;
4362 int i;
4363
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004364 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004365 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004366 // Didn't see this partial yet.
4367 pt->pt_copyID = copyID;
4368
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004369 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004370
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004371 if (pt->pt_dict != NULL)
4372 {
4373 typval_T dtv;
4374
4375 dtv.v_type = VAR_DICT;
4376 dtv.vval.v_dict = pt->pt_dict;
4377 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4378 }
4379
4380 for (i = 0; i < pt->pt_argc; ++i)
4381 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4382 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004383 if (pt->pt_funcstack != NULL)
4384 {
4385 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4386
4387 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4388 abort = abort || set_ref_in_item(stack + i, copyID,
4389 ht_stack, list_stack);
4390 }
4391
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004392 }
4393 }
4394#ifdef FEAT_JOB_CHANNEL
4395 else if (tv->v_type == VAR_JOB)
4396 {
4397 job_T *job = tv->vval.v_job;
4398 typval_T dtv;
4399
4400 if (job != NULL && job->jv_copyID != copyID)
4401 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004402 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004403 if (job->jv_channel != NULL)
4404 {
4405 dtv.v_type = VAR_CHANNEL;
4406 dtv.vval.v_channel = job->jv_channel;
4407 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4408 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004409 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004410 {
4411 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004412 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4414 }
4415 }
4416 }
4417 else if (tv->v_type == VAR_CHANNEL)
4418 {
4419 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004420 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004421 typval_T dtv;
4422 jsonq_T *jq;
4423 cbq_T *cq;
4424
4425 if (ch != NULL && ch->ch_copyID != copyID)
4426 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004427 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004428 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004429 {
4430 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4431 jq = jq->jq_next)
4432 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4433 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4434 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004435 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004436 {
4437 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004438 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004439 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4440 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004441 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004442 {
4443 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004444 dtv.vval.v_partial =
4445 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4447 }
4448 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004449 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004450 {
4451 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004452 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004453 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4454 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004455 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004456 {
4457 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004458 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004459 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4460 }
4461 }
4462 }
4463#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004464 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004465}
4466
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 * Return a string with the string representation of a variable.
4469 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004471 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004472 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4473 * stings as "string()", otherwise does not put quotes around strings, as
4474 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004475 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4476 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004477 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004479 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004480echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004481 typval_T *tv,
4482 char_u **tofree,
4483 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004484 int copyID,
4485 int echo_style,
4486 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004487 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004489 static int recurse = 0;
4490 char_u *r = NULL;
4491
Bram Moolenaar33570922005-01-25 22:26:29 +00004492 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004493 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004494 if (!did_echo_string_emsg)
4495 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // Only give this message once for a recursive call to avoid
4497 // flooding the user with errors. And stop iterating over lists
4498 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004499 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004500 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004501 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004502 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004503 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004504 }
4505 ++recurse;
4506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 switch (tv->v_type)
4508 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004509 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004510 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004511 {
4512 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004513 r = tv->vval.v_string;
4514 if (r == NULL)
4515 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004516 }
4517 else
4518 {
4519 *tofree = string_quote(tv->vval.v_string, FALSE);
4520 r = *tofree;
4521 }
4522 break;
4523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004525 if (echo_style)
4526 {
4527 *tofree = NULL;
4528 r = tv->vval.v_string;
4529 }
4530 else
4531 {
4532 *tofree = string_quote(tv->vval.v_string, TRUE);
4533 r = *tofree;
4534 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004535 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004536
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004537 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004538 {
4539 partial_T *pt = tv->vval.v_partial;
4540 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004541 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004542 garray_T ga;
4543 int i;
4544 char_u *tf;
4545
4546 ga_init2(&ga, 1, 100);
4547 ga_concat(&ga, (char_u *)"function(");
4548 if (fname != NULL)
4549 {
4550 ga_concat(&ga, fname);
4551 vim_free(fname);
4552 }
4553 if (pt != NULL && pt->pt_argc > 0)
4554 {
4555 ga_concat(&ga, (char_u *)", [");
4556 for (i = 0; i < pt->pt_argc; ++i)
4557 {
4558 if (i > 0)
4559 ga_concat(&ga, (char_u *)", ");
4560 ga_concat(&ga,
4561 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4562 vim_free(tf);
4563 }
4564 ga_concat(&ga, (char_u *)"]");
4565 }
4566 if (pt != NULL && pt->pt_dict != NULL)
4567 {
4568 typval_T dtv;
4569
4570 ga_concat(&ga, (char_u *)", ");
4571 dtv.v_type = VAR_DICT;
4572 dtv.vval.v_dict = pt->pt_dict;
4573 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4574 vim_free(tf);
4575 }
4576 ga_concat(&ga, (char_u *)")");
4577
4578 *tofree = ga.ga_data;
4579 r = *tofree;
4580 break;
4581 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004582
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004583 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004584 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004585 break;
4586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004588 if (tv->vval.v_list == NULL)
4589 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004590 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004591 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004592 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004593 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004594 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4595 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004596 {
4597 *tofree = NULL;
4598 r = (char_u *)"[...]";
4599 }
4600 else
4601 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004602 int old_copyID = tv->vval.v_list->lv_copyID;
4603
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004604 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004605 *tofree = list2string(tv, copyID, restore_copyID);
4606 if (restore_copyID)
4607 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004608 r = *tofree;
4609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004611
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004613 if (tv->vval.v_dict == NULL)
4614 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004615 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004616 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004617 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004618 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004619 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4620 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004621 {
4622 *tofree = NULL;
4623 r = (char_u *)"{...}";
4624 }
4625 else
4626 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004627 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004628
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004629 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004630 *tofree = dict2string(tv, copyID, restore_copyID);
4631 if (restore_copyID)
4632 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004633 r = *tofree;
4634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004635 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004638 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004639 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004641 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004643 break;
4644
Bram Moolenaar835dc632016-02-07 14:27:38 +01004645 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004646 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004647 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004648 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004649 if (composite_val)
4650 {
4651 *tofree = string_quote(r, FALSE);
4652 r = *tofree;
4653 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004657#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004658 *tofree = NULL;
4659 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4660 r = numbuf;
4661 break;
4662#endif
4663
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004664 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004665 case VAR_SPECIAL:
4666 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004667 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004668 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004670
Bram Moolenaar8502c702014-06-17 12:51:16 +02004671 if (--recurse == 0)
4672 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004674}
4675
4676/*
4677 * Return a string with the string representation of a variable.
4678 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4679 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004680 * Does not put quotes around strings, as ":echo" displays values.
4681 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4682 * May return NULL.
4683 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004684 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004685echo_string(
4686 typval_T *tv,
4687 char_u **tofree,
4688 char_u *numbuf,
4689 int copyID)
4690{
4691 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4692}
4693
4694/*
4695 * Return a string with the string representation of a variable.
4696 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4697 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004698 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004699 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004700 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004702tv2string(
4703 typval_T *tv,
4704 char_u **tofree,
4705 char_u *numbuf,
4706 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004707{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004708 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709}
4710
4711/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 * Return string "str" in ' quotes, doubling ' characters.
4713 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004715 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004717string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004718{
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004720 char_u *p, *r, *s;
4721
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 len = (function ? 13 : 3);
4723 if (str != NULL)
4724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004726 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 if (*p == '\'')
4728 ++len;
4729 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004730 s = r = alloc(len);
4731 if (r != NULL)
4732 {
4733 if (function)
4734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004736 r += 10;
4737 }
4738 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004740 if (str != NULL)
4741 for (p = str; *p != NUL; )
4742 {
4743 if (*p == '\'')
4744 *r++ = '\'';
4745 MB_COPY_CHAR(p, r);
4746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004748 if (function)
4749 *r++ = ')';
4750 *r++ = NUL;
4751 }
4752 return s;
4753}
4754
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004755#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756/*
4757 * Convert the string "text" to a floating point number.
4758 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4759 * this always uses a decimal point.
4760 * Returns the length of the text that was consumed.
4761 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004763string2float(
4764 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766{
4767 char *s = (char *)text;
4768 float_T f;
4769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004770 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004771 if (STRNICMP(text, "inf", 3) == 0)
4772 {
4773 *value = INFINITY;
4774 return 3;
4775 }
4776 if (STRNICMP(text, "-inf", 3) == 0)
4777 {
4778 *value = -INFINITY;
4779 return 4;
4780 }
4781 if (STRNICMP(text, "nan", 3) == 0)
4782 {
4783 *value = NAN;
4784 return 3;
4785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f = strtod(s, &s);
4787 *value = f;
4788 return (int)((char_u *)s - text);
4789}
4790#endif
4791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * Get the value of an environment variable.
4794 * "arg" is pointing to the '$'. It is advanced to after the name.
4795 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004796 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801 char_u *string = NULL;
4802 int len;
4803 int cc;
4804 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004805 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 ++*arg;
4808 name = *arg;
4809 len = get_env_len(arg);
4810 if (evaluate)
4811 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004812 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004813 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004814
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004815 cc = name[len];
4816 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004818 string = vim_getenv(name, &mustfree);
4819 if (string != NULL && *string != NUL)
4820 {
4821 if (!mustfree)
4822 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004824 else
4825 {
4826 if (mustfree)
4827 vim_free(string);
4828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004829 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004830 string = expand_env_save(name - 1);
4831 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004832 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004833 }
4834 name[len] = cc;
4835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 rettv->v_type = VAR_STRING;
4837 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839
4840 return OK;
4841}
4842
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004845 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004848var2fpos(
4849 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 int dollar_lnum, // TRUE when $ is last line
4851 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004853 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004855 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004858 if (varp->v_type == VAR_LIST)
4859 {
4860 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004861 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004862 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004863 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004864
4865 l = varp->vval.v_list;
4866 if (l == NULL)
4867 return NULL;
4868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004870 pos.lnum = list_find_nr(l, 0L, &error);
4871 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004875 pos.col = list_find_nr(l, 1L, &error);
4876 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004877 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004878 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004880 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004881 li = list_find(l, 1L);
4882 if (li != NULL && li->li_tv.v_type == VAR_STRING
4883 && li->li_tv.vval.v_string != NULL
4884 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4885 pos.col = len + 1;
4886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004887 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004888 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004889 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004890 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004893 pos.coladd = list_find_nr(l, 2L, &error);
4894 if (error)
4895 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004896
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004897 return &pos;
4898 }
4899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004900 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 if (name == NULL)
4902 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004906 {
4907 if (VIsual_active)
4908 return &VIsual;
4909 return &curwin->w_cursor;
4910 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004913 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4915 return NULL;
4916 return pp;
4917 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004918
Bram Moolenaara5525202006-03-02 22:52:09 +00004919 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004920
Bram Moolenaar477933c2007-07-17 14:32:23 +00004921 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004922 {
4923 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004925 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004926 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // In silent Ex mode topline is zero, but that's not a valid line
4928 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004929 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004930 return &pos;
4931 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004933 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004934 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004936 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004937 return &pos;
4938 }
4939 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004940 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004942 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 pos.lnum = curbuf->b_ml.ml_line_count;
4945 pos.col = 0;
4946 }
4947 else
4948 {
4949 pos.lnum = curwin->w_cursor.lnum;
4950 pos.col = (colnr_T)STRLEN(ml_get_curline());
4951 }
4952 return &pos;
4953 }
4954 return NULL;
4955}
4956
4957/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004958 * Convert list in "arg" into a position and optional file number.
4959 * When "fnump" is NULL there is no file number, only 3 items.
4960 * Note that the column is passed on as-is, the caller may want to decrement
4961 * it to use 1 for the first column.
4962 * Return FAIL when conversion is not possible, doesn't check the position for
4963 * validity.
4964 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004966list2fpos(
4967 typval_T *arg,
4968 pos_T *posp,
4969 int *fnump,
4970 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004971{
4972 list_T *l = arg->vval.v_list;
4973 long i = 0;
4974 long n;
4975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4977 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004978 if (arg->v_type != VAR_LIST
4979 || l == NULL
4980 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004981 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004982 return FAIL;
4983
4984 if (fnump != NULL)
4985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004987 if (n < 0)
4988 return FAIL;
4989 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004991 *fnump = n;
4992 }
4993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004995 if (n < 0)
4996 return FAIL;
4997 posp->lnum = n;
4998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005000 if (n < 0)
5001 return FAIL;
5002 posp->col = n;
5003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005004 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005005 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005006 posp->coladd = 0;
5007 else
5008 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005009
Bram Moolenaar493c1782014-05-28 14:34:46 +02005010 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005012
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005013 return OK;
5014}
5015
5016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 * Get the length of an environment variable name.
5018 * Advance "arg" to the first character after the name.
5019 * Return 0 for error.
5020 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005022get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 char_u *p;
5025 int len;
5026
5027 for (p = *arg; vim_isIDc(*p); ++p)
5028 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 return 0;
5031
5032 len = (int)(p - *arg);
5033 *arg = p;
5034 return len;
5035}
5036
5037/*
5038 * Get the length of the name of a function or internal variable.
5039 * "arg" is advanced to the first non-white character after the name.
5040 * Return 0 if something is wrong.
5041 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005043get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 char_u *p;
5046 int len;
5047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005048 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005050 {
5051 if (*p == ':')
5052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 // "s:" is start of "s:var", but "n:" is not and can be used in
5054 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005055 len = (int)(p - *arg);
5056 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5057 || len > 1)
5058 break;
5059 }
5060 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 return 0;
5063
5064 len = (int)(p - *arg);
5065 *arg = skipwhite(p);
5066
5067 return len;
5068}
5069
5070/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005071 * Get the length of the name of a variable or function.
5072 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005074 * Return -1 if curly braces expansion failed.
5075 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 * If the name contains 'magic' {}'s, expand them and return the
5077 * expanded name in an allocated string via 'alias' - caller must free.
5078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005080get_name_len(
5081 char_u **arg,
5082 char_u **alias,
5083 int evaluate,
5084 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 char_u *p;
5088 char_u *expr_start;
5089 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
5093 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5094 && (*arg)[2] == (int)KE_SNR)
5095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *arg += 3;
5098 return get_id_len(arg) + 3;
5099 }
5100 len = eval_fname_script(*arg);
5101 if (len > 0)
5102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 *arg += len;
5105 }
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005110 p = find_name_end(*arg, &expr_start, &expr_end,
5111 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (expr_start != NULL)
5113 {
5114 char_u *temp_string;
5115
5116 if (!evaluate)
5117 {
5118 len += (int)(p - *arg);
5119 *arg = skipwhite(p);
5120 return len;
5121 }
5122
5123 /*
5124 * Include any <SID> etc in the expanded string:
5125 * Thus the -len here.
5126 */
5127 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5128 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005129 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 *alias = temp_string;
5131 *arg = skipwhite(p);
5132 return (int)STRLEN(temp_string);
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005136 // Only give an error when there is something, otherwise it will be
5137 // reported at a higher level.
5138 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
5141 return len;
5142}
5143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144/*
5145 * Find the end of a variable or function name, taking care of magic braces.
5146 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5147 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005148 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 * Return a pointer to just after the name. Equal to "arg" if there is no
5150 * valid name.
5151 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005153find_name_end(
5154 char_u *arg,
5155 char_u **expr_start,
5156 char_u **expr_end,
5157 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 int mb_nest = 0;
5160 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005162 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005163 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 *expr_start = NULL;
5168 *expr_end = NULL;
5169 }
5170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005172 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5173 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005174 return arg;
5175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 for (p = arg; *p != NUL
5177 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005178 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005179 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005181 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005183 if (*p == '\'')
5184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005186 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005187 ;
5188 if (*p == NUL)
5189 break;
5190 }
5191 else if (*p == '"')
5192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005194 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005195 if (*p == '\\' && p[1] != NUL)
5196 ++p;
5197 if (*p == NUL)
5198 break;
5199 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005200 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // "s:" is start of "s:var", but "n:" is not and can be used in
5203 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005204 len = (int)(p - arg);
5205 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005206 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005207 break;
5208 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 if (*p == '[')
5213 ++br_nest;
5214 else if (*p == ']')
5215 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005217
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005218 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 if (*p == '{')
5221 {
5222 mb_nest++;
5223 if (expr_start != NULL && *expr_start == NULL)
5224 *expr_start = p;
5225 }
5226 else if (*p == '}')
5227 {
5228 mb_nest--;
5229 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5230 *expr_end = p;
5231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234
5235 return p;
5236}
5237
5238/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239 * Expands out the 'magic' {}'s in a variable/function name.
5240 * Note that this can call itself recursively, to deal with
5241 * constructs like foo{bar}{baz}{bam}
5242 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5243 * "in_start" ^
5244 * "expr_start" ^
5245 * "expr_end" ^
5246 * "in_end" ^
5247 *
5248 * Returns a new allocated string, which the caller must free.
5249 * Returns NULL for failure.
5250 */
5251 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005252make_expanded_name(
5253 char_u *in_start,
5254 char_u *expr_start,
5255 char_u *expr_end,
5256 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257{
5258 char_u c1;
5259 char_u *retval = NULL;
5260 char_u *temp_result;
5261 char_u *nextcmd = NULL;
5262
5263 if (expr_end == NULL || in_end == NULL)
5264 return NULL;
5265 *expr_start = NUL;
5266 *expr_end = NUL;
5267 c1 = *in_end;
5268 *in_end = NUL;
5269
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005270 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271 if (temp_result != NULL && nextcmd == NULL)
5272 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005273 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5274 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 if (retval != NULL)
5276 {
5277 STRCPY(retval, in_start);
5278 STRCAT(retval, temp_result);
5279 STRCAT(retval, expr_end + 1);
5280 }
5281 }
5282 vim_free(temp_result);
5283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005285 *expr_start = '{';
5286 *expr_end = '}';
5287
5288 if (retval != NULL)
5289 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005290 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 if (expr_start != NULL)
5292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005294 temp_result = make_expanded_name(retval, expr_start,
5295 expr_end, temp_result);
5296 vim_free(retval);
5297 retval = temp_result;
5298 }
5299 }
5300
5301 return retval;
5302}
5303
5304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005306 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005309eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005311 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5312}
5313
5314/*
5315 * Return TRUE if character "c" can be used as the first character in a
5316 * variable or function name (excluding '{' and '}').
5317 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005319eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005320{
5321 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322}
5323
5324/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005325 * Handle:
5326 * - expr[expr], expr[expr:expr] subscript
5327 * - ".name" lookup
5328 * - function call with Funcref variable: func(expr)
5329 * - method call: var->method()
5330 *
5331 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005332 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334handle_subscript(
5335 char_u **arg,
5336 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005337 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005338 int verbose, // give error messages
5339 char_u *start_leader, // start of '!' and '-' prefixes
5340 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341{
Bram Moolenaar32e35112020-05-14 22:41:15 +02005342 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343 int ret = OK;
5344 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345
Bram Moolenaar61343f02019-07-20 21:11:13 +02005346 // "." is ".name" lookup when we found a dict or when evaluating and
5347 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005349 && (((**arg == '['
5350 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005351 || (!evaluate
5352 && (*arg)[1] != '.'
5353 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005354 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005355 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005356 && !VIM_ISWHITE(*(*arg - 1)))
5357 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005358 {
5359 if (**arg == '(')
5360 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005361 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005362
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005363 // Stop the expression evaluation when immediately aborting on
5364 // error, or when an interrupt occurred or an exception was thrown
5365 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (aborting())
5367 {
5368 if (ret == OK)
5369 clear_tv(rettv);
5370 ret = FAIL;
5371 }
5372 dict_unref(selfdict);
5373 selfdict = NULL;
5374 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005375 else if (**arg == '-')
5376 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005377 // Expression "-1.0->method()" applies the leader "-" before
5378 // applying ->.
5379 if (evaluate && *end_leaderp > start_leader)
5380 ret = eval7_leader(rettv, start_leader, end_leaderp);
5381 if (ret == OK)
5382 {
5383 if ((*arg)[2] == '{')
5384 // expr->{lambda}()
5385 ret = eval_lambda(arg, rettv, evaluate, verbose);
5386 else
5387 // expr->name()
5388 ret = eval_method(arg, rettv, evaluate, verbose);
5389 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005390 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005391 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005392 {
5393 dict_unref(selfdict);
5394 if (rettv->v_type == VAR_DICT)
5395 {
5396 selfdict = rettv->vval.v_dict;
5397 if (selfdict != NULL)
5398 ++selfdict->dv_refcount;
5399 }
5400 else
5401 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02005402 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005403 {
5404 clear_tv(rettv);
5405 ret = FAIL;
5406 }
5407 }
5408 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5411 // Don't do this when "Func" is already a partial that was bound
5412 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005413 if (selfdict != NULL
5414 && (rettv->v_type == VAR_FUNC
5415 || (rettv->v_type == VAR_PARTIAL
5416 && (rettv->vval.v_partial->pt_auto
5417 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005418 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005420 dict_unref(selfdict);
5421 return ret;
5422}
5423
5424/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005425 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 * value).
5427 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005428 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005431 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432}
5433
5434/*
5435 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 * The string "s" must have been allocated, it is consumed.
5437 * Return NULL for out of memory, the variable otherwise.
5438 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005439 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005440alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 rettv = alloc_tv();
5445 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 rettv->v_type = VAR_STRING;
5448 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450 else
5451 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454
5455/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005459free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 if (varp != NULL)
5462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 switch (varp->v_type)
5464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005466 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005468 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 vim_free(varp->vval.v_string);
5470 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005471 case VAR_PARTIAL:
5472 partial_unref(varp->vval.v_partial);
5473 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005474 case VAR_BLOB:
5475 blob_unref(varp->vval.v_blob);
5476 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 case VAR_LIST:
5478 list_unref(varp->vval.v_list);
5479 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005480 case VAR_DICT:
5481 dict_unref(varp->vval.v_dict);
5482 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005484#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 job_unref(varp->vval.v_job);
5486 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005487#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005488 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005489#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005490 channel_unref(varp->vval.v_channel);
5491 break;
5492#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493 case VAR_NUMBER:
5494 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005495 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005496 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005498 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005499 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 vim_free(varp);
5503 }
5504}
5505
5506/*
5507 * Free the memory for a variable value and set the value to NULL or 0.
5508 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 if (varp != NULL)
5513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005517 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005518 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005520 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005522 case VAR_PARTIAL:
5523 partial_unref(varp->vval.v_partial);
5524 varp->vval.v_partial = NULL;
5525 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005526 case VAR_BLOB:
5527 blob_unref(varp->vval.v_blob);
5528 varp->vval.v_blob = NULL;
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 case VAR_LIST:
5531 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005532 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005534 case VAR_DICT:
5535 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005536 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005539 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005540 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 varp->vval.v_number = 0;
5542 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005543 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005544#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005545 varp->vval.v_float = 0.0;
5546 break;
5547#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005548 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005550 job_unref(varp->vval.v_job);
5551 varp->vval.v_job = NULL;
5552#endif
5553 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005554 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005555#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005556 channel_unref(varp->vval.v_channel);
5557 varp->vval.v_channel = NULL;
5558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005560 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005564 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566}
5567
5568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 * Set the value of a variable to NULL without freeing items.
5570 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005572init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573{
5574 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005575 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576}
5577
5578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 * Get the number value of a variable.
5580 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005583 * caller of incompatible types: it sets *denote to TRUE if "denote"
5584 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005586 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005587tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005589 int error = FALSE;
5590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005591 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005592}
5593
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005594 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005595tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005596{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005597 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005602 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005603 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005605 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005606 break;
5607#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005609 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005610 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 break;
5612 case VAR_STRING:
5613 if (varp->vval.v_string != NULL)
5614 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005615 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005616 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005618 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005620 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005621 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005622 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005623 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005624 case VAR_SPECIAL:
5625 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005626 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005628 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005629 break;
5630#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005631 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005633 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005634 break;
5635#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005636 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005637 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005638 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005639 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005640 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005642 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005645 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005646 n = -1;
5647 else
5648 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005652#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005654tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005655{
5656 switch (varp->v_type)
5657 {
5658 case VAR_NUMBER:
5659 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005660 case VAR_FLOAT:
5661 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005662 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005663 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005665 break;
5666 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005667 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005668 break;
5669 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005670 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005671 break;
5672 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005673 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005674 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005675 case VAR_BOOL:
5676 emsg(_("E362: Using a boolean value as a Float"));
5677 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005678 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005679 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005680 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005681 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005682# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005683 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005684 break;
5685# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005686 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005687# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005688 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005689 break;
5690# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005691 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005692 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005693 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005694 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005695 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005696 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005697 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005698 break;
5699 }
5700 return 0;
5701}
5702#endif
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * Get the string value of a variable.
5706 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005707 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5708 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 * If the String variable has never been set, return an empty string.
5710 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005711 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005712 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005714 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 static char_u mybuf[NUMBUFLEN];
5718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005719 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005722 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005723tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005725 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005726
5727 return res != NULL ? res : (char_u *)"";
5728}
5729
Bram Moolenaar7d647822014-04-05 21:28:56 +02005730/*
5731 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5732 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005733 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005735{
5736 static char_u mybuf[NUMBUFLEN];
5737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005738 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005739}
5740
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005741 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005742tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005743{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005747 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005748 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 return buf;
5750 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005751 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005752 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 break;
5754 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005755 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 break;
5757 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005758 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005760 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005761#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005762 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005763 break;
5764#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 case VAR_STRING:
5766 if (varp->vval.v_string != NULL)
5767 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005768 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005769 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005770 case VAR_SPECIAL:
5771 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5772 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005773 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005774 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005775 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005776 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005778 {
5779 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005780 char *status;
5781
5782 if (job == NULL)
5783 return (char_u *)"no process";
5784 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005785 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005786 : "run";
5787# ifdef UNIX
5788 vim_snprintf((char *)buf, NUMBUFLEN,
5789 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005790# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005791 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005792 "process %ld %s",
5793 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005794 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005795# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005797 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5798# endif
5799 return buf;
5800 }
5801#endif
5802 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005803 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005804#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005805 {
5806 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005807 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005808
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005809 if (channel == NULL)
5810 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5811 else
5812 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005813 "channel %d %s", channel->ch_id, status);
5814 return buf;
5815 }
5816#endif
5817 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005818 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005819 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005821 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005824 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
5827/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005828 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5829 * string() on Dict, List, etc.
5830 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005831 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005832tv_stringify(typval_T *varp, char_u *buf)
5833{
5834 if (varp->v_type == VAR_LIST
5835 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005836 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005837 || varp->v_type == VAR_FUNC
5838 || varp->v_type == VAR_PARTIAL
5839 || varp->v_type == VAR_FLOAT)
5840 {
5841 typval_T tmp;
5842
5843 f_string(varp, &tmp);
5844 tv_get_string_buf(&tmp, buf);
5845 clear_tv(varp);
5846 *varp = tmp;
5847 return tmp.vval.v_string;
5848 }
5849 return tv_get_string_buf(varp, buf);
5850}
5851
5852/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005853 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5854 * Also give an error message, using "name" or _("name") when use_gettext is
5855 * TRUE.
5856 */
5857 static int
5858tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5859{
5860 int lock = 0;
5861
5862 switch (tv->v_type)
5863 {
5864 case VAR_BLOB:
5865 if (tv->vval.v_blob != NULL)
5866 lock = tv->vval.v_blob->bv_lock;
5867 break;
5868 case VAR_LIST:
5869 if (tv->vval.v_list != NULL)
5870 lock = tv->vval.v_list->lv_lock;
5871 break;
5872 case VAR_DICT:
5873 if (tv->vval.v_dict != NULL)
5874 lock = tv->vval.v_dict->dv_lock;
5875 break;
5876 default:
5877 break;
5878 }
5879 return var_check_lock(tv->v_lock, name, use_gettext)
5880 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5881}
5882
5883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005886 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005887 * It is OK for "from" and "to" to point to the same item. This is used to
5888 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005891copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005894 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 switch (from->v_type)
5896 {
5897 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005898 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005899 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 to->vval.v_number = from->vval.v_number;
5901 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005902 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005903#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005904 to->vval.v_float = from->vval.v_float;
5905 break;
5906#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005907 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005909 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005910 if (to->vval.v_job != NULL)
5911 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005912 break;
5913#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005914 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005915#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005916 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005917 if (to->vval.v_channel != NULL)
5918 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005919 break;
5920#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 case VAR_STRING:
5922 case VAR_FUNC:
5923 if (from->vval.v_string == NULL)
5924 to->vval.v_string = NULL;
5925 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005926 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005928 if (from->v_type == VAR_FUNC)
5929 func_ref(to->vval.v_string);
5930 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005932 case VAR_PARTIAL:
5933 if (from->vval.v_partial == NULL)
5934 to->vval.v_partial = NULL;
5935 else
5936 {
5937 to->vval.v_partial = from->vval.v_partial;
5938 ++to->vval.v_partial->pt_refcount;
5939 }
5940 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005941 case VAR_BLOB:
5942 if (from->vval.v_blob == NULL)
5943 to->vval.v_blob = NULL;
5944 else
5945 {
5946 to->vval.v_blob = from->vval.v_blob;
5947 ++to->vval.v_blob->bv_refcount;
5948 }
5949 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 case VAR_LIST:
5951 if (from->vval.v_list == NULL)
5952 to->vval.v_list = NULL;
5953 else
5954 {
5955 to->vval.v_list = from->vval.v_list;
5956 ++to->vval.v_list->lv_refcount;
5957 }
5958 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 case VAR_DICT:
5960 if (from->vval.v_dict == NULL)
5961 to->vval.v_dict = NULL;
5962 else
5963 {
5964 to->vval.v_dict = from->vval.v_dict;
5965 ++to->vval.v_dict->dv_refcount;
5966 }
5967 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005968 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005969 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005971 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 break;
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974}
5975
5976/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977 * Make a copy of an item.
5978 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005979 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5980 * reference to an already copied list/dict can be used.
5981 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005982 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984item_copy(
5985 typval_T *from,
5986 typval_T *to,
5987 int deep,
5988 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989{
5990 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005991 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005995 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 }
5998 ++recurse;
5999
6000 switch (from->v_type)
6001 {
6002 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006003 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004 case VAR_STRING:
6005 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006006 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006007 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006008 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006009 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006010 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 copy_tv(from, to);
6012 break;
6013 case VAR_LIST:
6014 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006015 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006016 if (from->vval.v_list == NULL)
6017 to->vval.v_list = NULL;
6018 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6019 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006020 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006021 to->vval.v_list = from->vval.v_list->lv_copylist;
6022 ++to->vval.v_list->lv_refcount;
6023 }
6024 else
6025 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6026 if (to->vval.v_list == NULL)
6027 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006029 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006031 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 case VAR_DICT:
6033 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006034 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006035 if (from->vval.v_dict == NULL)
6036 to->vval.v_dict = NULL;
6037 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006039 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006040 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6041 ++to->vval.v_dict->dv_refcount;
6042 }
6043 else
6044 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6045 if (to->vval.v_dict == NULL)
6046 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006048 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006049 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006051 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006052 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 }
6054 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056}
6057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 void
6059echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6060{
6061 char_u *tofree;
6062 char_u numbuf[NUMBUFLEN];
6063 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6064
6065 if (*atstart)
6066 {
6067 *atstart = FALSE;
6068 // Call msg_start() after eval1(), evaluating the expression
6069 // may cause a message to appear.
6070 if (with_space)
6071 {
6072 // Mark the saved text as finishing the line, so that what
6073 // follows is displayed on a new line when scrolling back
6074 // at the more prompt.
6075 msg_sb_eol();
6076 msg_start();
6077 }
6078 }
6079 else if (with_space)
6080 msg_puts_attr(" ", echo_attr);
6081
6082 if (p != NULL)
6083 for ( ; *p != NUL && !got_int; ++p)
6084 {
6085 if (*p == '\n' || *p == '\r' || *p == TAB)
6086 {
6087 if (*p != TAB && *needclr)
6088 {
6089 // remove any text still there from the command
6090 msg_clr_eos();
6091 *needclr = FALSE;
6092 }
6093 msg_putchar_attr(*p, echo_attr);
6094 }
6095 else
6096 {
6097 if (has_mbyte)
6098 {
6099 int i = (*mb_ptr2len)(p);
6100
6101 (void)msg_outtrans_len_attr(p, i, echo_attr);
6102 p += i - 1;
6103 }
6104 else
6105 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6106 }
6107 }
6108 vim_free(tofree);
6109}
6110
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 * ":echo expr1 ..." print each argument separated with a space, add a
6113 * newline at the end.
6114 * ":echon expr1 ..." print each argument plain.
6115 */
6116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006117ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 char_u *p;
6122 int needclr = TRUE;
6123 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006124 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006125 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 if (eap->skip)
6128 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006129 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006131 // If eval1() causes an error message the text from the command may
6132 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006133 need_clr_eos = needclr;
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02006136 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 /*
6139 * Report the invalid expression unless the expression evaluation
6140 * has been cancelled due to an aborting error, an interrupt, or an
6141 * exception.
6142 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006143 if (!aborting() && did_emsg == did_emsg_before
6144 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006145 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006146 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 break;
6148 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006149 need_clr_eos = FALSE;
6150
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 arg = skipwhite(arg);
6156 }
6157 eap->nextcmd = check_nextcmd(arg);
6158
6159 if (eap->skip)
6160 --emsg_skip;
6161 else
6162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006163 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (needclr)
6165 msg_clr_eos();
6166 if (eap->cmdidx == CMD_echo)
6167 msg_end();
6168 }
6169}
6170
6171/*
6172 * ":echohl {name}".
6173 */
6174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006177 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178}
6179
6180/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006181 * Returns the :echo attribute
6182 */
6183 int
6184get_echo_attr(void)
6185{
6186 return echo_attr;
6187}
6188
6189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 * ":execute expr1 ..." execute the result of an expression.
6191 * ":echomsg expr1 ..." Print a message
6192 * ":echoerr expr1 ..." Print an error
6193 * Each gets spaces around each argument and a newline at the end for
6194 * echo commands
6195 */
6196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 int ret = OK;
6202 char_u *p;
6203 garray_T ga;
6204 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206 ga_init2(&ga, 1, 80);
6207
6208 if (eap->skip)
6209 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006210 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006212 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6213 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 if (!eap->skip)
6217 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006218 char_u buf[NUMBUFLEN];
6219
6220 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006221 {
6222 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6223 {
6224 emsg(_(e_inval_string));
6225 p = NULL;
6226 }
6227 else
6228 p = tv_get_string_buf(&rettv, buf);
6229 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006230 else
6231 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006232 if (p == NULL)
6233 {
6234 clear_tv(&rettv);
6235 ret = FAIL;
6236 break;
6237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 len = (int)STRLEN(p);
6239 if (ga_grow(&ga, len + 2) == FAIL)
6240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006241 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 ret = FAIL;
6243 break;
6244 }
6245 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 ga.ga_len += len;
6249 }
6250
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 arg = skipwhite(arg);
6253 }
6254
6255 if (ret != FAIL && ga.ga_data != NULL)
6256 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006257 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006259 // Mark the already saved text as finishing the line, so that what
6260 // follows is displayed on a new line when scrolling back at the
6261 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006262 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006263 }
6264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006266 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006267 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006268 out_flush();
6269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 else if (eap->cmdidx == CMD_echoerr)
6271 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006272 int save_did_emsg = did_emsg;
6273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006274 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006275 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (!force_abort)
6277 did_emsg = save_did_emsg;
6278 }
6279 else if (eap->cmdidx == CMD_execute)
6280 do_cmdline((char_u *)ga.ga_data,
6281 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6282 }
6283
6284 ga_clear(&ga);
6285
6286 if (eap->skip)
6287 --emsg_skip;
6288
6289 eap->nextcmd = check_nextcmd(arg);
6290}
6291
6292/*
6293 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6294 * "arg" points to the "&" or '+' when called, to "option" when returning.
6295 * Returns NULL when no option name found. Otherwise pointer to the char
6296 * after the option name.
6297 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006298 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 char_u *p = *arg;
6302
6303 ++p;
6304 if (*p == 'g' && p[1] == ':')
6305 {
6306 *opt_flags = OPT_GLOBAL;
6307 p += 2;
6308 }
6309 else if (*p == 'l' && p[1] == ':')
6310 {
6311 *opt_flags = OPT_LOCAL;
6312 p += 2;
6313 }
6314 else
6315 *opt_flags = 0;
6316
6317 if (!ASCII_ISALPHA(*p))
6318 return NULL;
6319 *arg = p;
6320
6321 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006322 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 else
6324 while (ASCII_ISALPHA(*p))
6325 ++p;
6326 return p;
6327}
6328
6329/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006330 * Display script name where an item was last set.
6331 * Should only be invoked when 'verbose' is non-zero.
6332 */
6333 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006334last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006335{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006336 char_u *p;
6337
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006338 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006339 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006340 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006341 if (p != NULL)
6342 {
6343 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006344 msg_puts(_("\n\tLast set from "));
6345 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006346 if (script_ctx.sc_lnum > 0)
6347 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006348 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006349 msg_outnum((long)script_ctx.sc_lnum);
6350 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006351 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006352 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006353 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006354 }
6355}
6356
Bram Moolenaar61be3762019-03-19 23:04:17 +01006357/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006358 * Compare "typ1" and "typ2". Put the result in "typ1".
6359 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006360 int
6361typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006362 typval_T *typ1, // first operand
6363 typval_T *typ2, // second operand
6364 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006366{
6367 int i;
6368 varnumber_T n1, n2;
6369 char_u *s1, *s2;
6370 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006371 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006372
Bram Moolenaar31988702018-02-13 12:57:42 +01006373 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006375 // For "is" a different type always means FALSE, for "notis"
6376 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006377 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006378 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006379 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6380 {
6381 if (type_is)
6382 {
6383 n1 = (typ1->v_type == typ2->v_type
6384 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006385 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006386 n1 = !n1;
6387 }
6388 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006389 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006390 {
6391 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006392 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006393 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006394 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006395 clear_tv(typ1);
6396 return FAIL;
6397 }
6398 else
6399 {
6400 // Compare two Blobs for being equal or unequal.
6401 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006402 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006403 n1 = !n1;
6404 }
6405 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006406 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6407 {
6408 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006409 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006410 n1 = (typ1->v_type == typ2->v_type
6411 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006412 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006413 n1 = !n1;
6414 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006415 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006416 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006417 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006418 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006419 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006420 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006421 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006422 clear_tv(typ1);
6423 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006424 }
6425 else
6426 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006428 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6429 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006430 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006431 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006432 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006433 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006434
6435 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6436 {
6437 if (type_is)
6438 {
6439 n1 = (typ1->v_type == typ2->v_type
6440 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006441 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006442 n1 = !n1;
6443 }
6444 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006445 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006446 {
6447 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006448 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006449 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006450 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006451 clear_tv(typ1);
6452 return FAIL;
6453 }
6454 else
6455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006457 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6458 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006459 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006460 n1 = !n1;
6461 }
6462 }
6463
6464 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6465 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6466 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006467 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6468 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006470 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006471 clear_tv(typ1);
6472 return FAIL;
6473 }
6474 if ((typ1->v_type == VAR_PARTIAL
6475 && typ1->vval.v_partial == NULL)
6476 || (typ2->v_type == VAR_PARTIAL
6477 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006478 // When both partials are NULL, then they are equal.
6479 // Otherwise they are not equal.
6480 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006481 else if (type_is)
6482 {
6483 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 // strings are considered the same if their value is
6485 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006486 n1 = tv_equal(typ1, typ2, ic, FALSE);
6487 else if (typ1->v_type == VAR_PARTIAL
6488 && typ2->v_type == VAR_PARTIAL)
6489 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6490 else
6491 n1 = FALSE;
6492 }
6493 else
6494 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006495 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006496 n1 = !n1;
6497 }
6498
6499#ifdef FEAT_FLOAT
6500 /*
6501 * If one of the two variables is a float, compare as a float.
6502 * When using "=~" or "!~", always compare as string.
6503 */
6504 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006505 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006506 {
6507 float_T f1, f2;
6508
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006509 f1 = tv_get_float(typ1);
6510 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006511 n1 = FALSE;
6512 switch (type)
6513 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006514 case EXPR_IS:
6515 case EXPR_EQUAL: n1 = (f1 == f2); break;
6516 case EXPR_ISNOT:
6517 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6518 case EXPR_GREATER: n1 = (f1 > f2); break;
6519 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6520 case EXPR_SMALLER: n1 = (f1 < f2); break;
6521 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6522 case EXPR_UNKNOWN:
6523 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006525 }
6526 }
6527#endif
6528
6529 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006530 * If one of the two variables is a number, compare as a number.
6531 * When using "=~" or "!~", always compare as string.
6532 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006533 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006534 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006536 n1 = tv_get_number(typ1);
6537 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006538 switch (type)
6539 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006540 case EXPR_IS:
6541 case EXPR_EQUAL: n1 = (n1 == n2); break;
6542 case EXPR_ISNOT:
6543 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6544 case EXPR_GREATER: n1 = (n1 > n2); break;
6545 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6546 case EXPR_SMALLER: n1 = (n1 < n2); break;
6547 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6548 case EXPR_UNKNOWN:
6549 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006551 }
6552 }
6553 else
6554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006555 s1 = tv_get_string_buf(typ1, buf1);
6556 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006557 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6559 else
6560 i = 0;
6561 n1 = FALSE;
6562 switch (type)
6563 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006564 case EXPR_IS:
6565 case EXPR_EQUAL: n1 = (i == 0); break;
6566 case EXPR_ISNOT:
6567 case EXPR_NEQUAL: n1 = (i != 0); break;
6568 case EXPR_GREATER: n1 = (i > 0); break;
6569 case EXPR_GEQUAL: n1 = (i >= 0); break;
6570 case EXPR_SMALLER: n1 = (i < 0); break;
6571 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006572
Bram Moolenaar87396072019-12-31 22:36:18 +01006573 case EXPR_MATCH:
6574 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006575 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006576 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006577 n1 = !n1;
6578 break;
6579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006581 }
6582 }
6583 clear_tv(typ1);
6584 typ1->v_type = VAR_NUMBER;
6585 typ1->vval.v_number = n1;
6586
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006587 return OK;
6588}
6589
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006590 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006591typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006592{
6593 char_u *tofree;
6594 char_u numbuf[NUMBUFLEN];
6595 char_u *ret = NULL;
6596
6597 if (arg == NULL)
6598 return vim_strsave((char_u *)"(does not exist)");
6599 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006600 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006601 if (ret != NULL && tofree == NULL)
6602 ret = vim_strsave(ret);
6603 return ret;
6604}
6605
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006606#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608/*
6609 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006610 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 * "flags" can be "g" to do a global substitute.
6612 * Returns an allocated string, NULL for error.
6613 */
6614 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006615do_string_sub(
6616 char_u *str,
6617 char_u *pat,
6618 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006619 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 int sublen;
6623 regmatch_T regmatch;
6624 int i;
6625 int do_all;
6626 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006627 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 garray_T ga;
6629 char_u *ret;
6630 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006631 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006633 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006635 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637 ga_init2(&ga, 1, 200);
6638
6639 do_all = (flags[0] == 'g');
6640
6641 regmatch.rm_ic = p_ic;
6642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6643 if (regmatch.regprog != NULL)
6644 {
6645 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006646 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006650 if (regmatch.startp[0] == regmatch.endp[0])
6651 {
6652 if (zero_width == regmatch.startp[0])
6653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006655 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006656 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6657 (size_t)i);
6658 ga.ga_len += i;
6659 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006660 continue;
6661 }
6662 zero_width = regmatch.startp[0];
6663 }
6664
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 /*
6666 * Get some space for a temporary buffer to do the substitution
6667 * into. It will contain:
6668 * - The text up to where the match is.
6669 * - The substituted text.
6670 * - The text after the match.
6671 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006672 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006673 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6675 {
6676 ga_clear(&ga);
6677 break;
6678 }
6679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006680 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 i = (int)(regmatch.startp[0] - tail);
6682 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006683 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006684 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 + ga.ga_len + i, TRUE, TRUE, FALSE);
6686 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006687 tail = regmatch.endp[0];
6688 if (*tail == NUL)
6689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (!do_all)
6691 break;
6692 }
6693
6694 if (ga.ga_data != NULL)
6695 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6696
Bram Moolenaar473de612013-06-08 18:19:48 +02006697 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699
6700 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6701 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006702 if (p_cpo == empty_option)
6703 p_cpo = save_cpo;
6704 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006705 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006706 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 return ret;
6709}