blob: d76ec94102a703b7d472d699644ba5731fa9cfd6 [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 Moolenaar822ba242020-05-24 23:00:18 +0200247 if (partial->pt_func != NULL
248 && partial->pt_func->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200250 if (call_def_function(partial->pt_func, argc, argv,
251 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 return FAIL;
253 }
254 else
255 {
256 s = partial_name(partial);
257 if (s == NULL || *s == NUL)
258 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200259 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 funcexe.evaluate = TRUE;
261 funcexe.partial = partial;
262 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
263 return FAIL;
264 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100265 }
266 else
267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100268 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 if (s == NULL)
270 return FAIL;
271 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100272 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100274 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100275 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200276 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100277 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100278 return FAIL;
279 }
280 }
281 return OK;
282}
283
284/*
285 * Like eval_to_bool() but using a typval_T instead of a string.
286 * Works for string, funcref and partial.
287 */
288 int
289eval_expr_to_bool(typval_T *expr, int *error)
290{
291 typval_T rettv;
292 int res;
293
294 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
295 {
296 *error = TRUE;
297 return FALSE;
298 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100299 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 clear_tv(&rettv);
301 return res;
302}
303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304/*
305 * Top level evaluation function, returning a string. If "skip" is TRUE,
306 * only parsing to "nextcmd" is done, without reporting errors. Return
307 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
308 */
309 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100310eval_to_string_skip(
311 char_u *arg,
312 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100313 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314{
Bram Moolenaar33570922005-01-25 22:26:29 +0000315 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 char_u *retval;
317
318 if (skip)
319 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200320 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 retval = NULL;
322 else
323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100324 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000325 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 }
327 if (skip)
328 --emsg_skip;
329
330 return retval;
331}
332
333/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000334 * Skip over an expression at "*pp".
335 * Return FAIL for an error, OK otherwise.
336 */
337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100338skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000339{
Bram Moolenaar33570922005-01-25 22:26:29 +0000340 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000341
342 *pp = skipwhite(*pp);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200343 return eval1(pp, &rettv, 0);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000344}
345
346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000348 * When "convert" is TRUE convert a List into a sequence of lines and convert
349 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 * Return pointer to allocated memory, or NULL for failure.
351 */
352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_to_string(
354 char_u *arg,
355 char_u **nextcmd,
356 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000360 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000361#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000362 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
Bram Moolenaar32e35112020-05-14 22:41:15 +0200365 if (eval0(arg, &tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 retval = NULL;
367 else
368 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000369 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000370 {
371 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000372 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200373 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200374 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200375 if (tv.vval.v_list->lv_len > 0)
376 ga_append(&ga, NL);
377 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000378 ga_append(&ga, NUL);
379 retval = (char_u *)ga.ga_data;
380 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000381#ifdef FEAT_FLOAT
382 else if (convert && tv.v_type == VAR_FLOAT)
383 {
384 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
385 retval = vim_strsave(numbuf);
386 }
387#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000388 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100389 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000390 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392
393 return retval;
394}
395
396/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000397 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200398 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100401eval_to_string_safe(
402 char_u *arg,
403 char_u **nextcmd,
404 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
406 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200409 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000410 if (use_sandbox)
411 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200412 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000413 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000414 if (use_sandbox)
415 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200416 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200417 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 return retval;
419}
420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421/*
422 * Top level evaluation function, returning a number.
423 * Evaluates "expr" silently.
424 * Returns -1 for an error.
425 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200426 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100427eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
Bram Moolenaar33570922005-01-25 22:26:29 +0000429 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200430 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000431 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433 ++emsg_off;
434
Bram Moolenaar32e35112020-05-14 22:41:15 +0200435 if (eval1(&p, &rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 retval = -1;
437 else
438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100439 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000440 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
442 --emsg_off;
443
444 return retval;
445}
446
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000447/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000448 * Top level evaluation function.
449 * Returns an allocated typval_T with the result.
450 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000451 */
452 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100453eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000454{
455 typval_T *tv;
456
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200457 tv = ALLOC_ONE(typval_T);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200458 if (tv != NULL && eval0(arg, tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100459 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000460
461 return tv;
462}
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100465 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200466 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
467 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000468 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100471call_vim_function(
472 char_u *func,
473 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200474 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200475 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000477 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200478 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100480 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200481 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200482 funcexe.firstline = curwin->w_cursor.lnum;
483 funcexe.lastline = curwin->w_cursor.lnum;
484 funcexe.evaluate = TRUE;
485 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000486 if (ret == FAIL)
487 clear_tv(rettv);
488
489 return ret;
490}
491
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100493 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100494 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200495 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
496 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100497 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200498 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100499call_func_retnr(
500 char_u *func,
501 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200502 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100503{
504 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200505 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506
Bram Moolenaarded27a12018-08-01 19:06:03 +0200507 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100508 return -1;
509
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100510 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100511 clear_tv(&rettv);
512 return retval;
513}
514
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000515/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100516 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000517 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200518 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
519 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000520 */
521 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522call_func_retstr(
523 char_u *func,
524 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200525 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000526{
527 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000528 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529
Bram Moolenaarded27a12018-08-01 19:06:03 +0200530 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000531 return NULL;
532
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100533 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000534 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 return retval;
536}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000537
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000538/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100539 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200540 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
541 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000542 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000543 */
544 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545call_func_retlist(
546 char_u *func,
547 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200548 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000549{
550 typval_T rettv;
551
Bram Moolenaarded27a12018-08-01 19:06:03 +0200552 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000553 return NULL;
554
555 if (rettv.v_type != VAR_LIST)
556 {
557 clear_tv(&rettv);
558 return NULL;
559 }
560
561 return rettv.vval.v_list;
562}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#ifdef FEAT_FOLDING
565/*
566 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
567 * it in "*cp". Doesn't give error messages.
568 */
569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100570eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
Bram Moolenaar33570922005-01-25 22:26:29 +0000572 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200573 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000575 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
576 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000579 if (use_sandbox)
580 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200581 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 *cp = NUL;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200583 if (eval0(arg, &tv, NULL, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 retval = 0;
585 else
586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100587 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000588 if (tv.v_type == VAR_NUMBER)
589 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000590 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = 0;
592 else
593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100594 // If the result is a string, check if there is a non-digit before
595 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (!VIM_ISDIGIT(*s) && *s != '-')
598 *cp = *s++;
599 retval = atol((char *)s);
600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000601 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000604 if (use_sandbox)
605 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200606 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200608 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609}
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000613 * Get an lval: variable, Dict item or List item that can be assigned a value
614 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
615 * "name.key", "name.key[expr]" etc.
616 * Indexing only works if "name" is an existing List or Dictionary.
617 * "name" points to the start of the name.
618 * If "rettv" is not NULL it points to the value to be assigned.
619 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
620 * wrong; must end in space or cmd separator.
621 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100622 * flags:
623 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100624 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100625 * GLV_NO_AUTOLOAD: do not use script autoloading
626 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000627 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000628 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000629 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200631 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100632get_lval(
633 char_u *name,
634 typval_T *rettv,
635 lval_T *lp,
636 int unlet,
637 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100638 int flags, // GLV_ values
639 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000641 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000642 char_u *expr_start, *expr_end;
643 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000644 dictitem_T *v;
645 typval_T var1;
646 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000647 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000648 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000649 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000650 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000651 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100652 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100654 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200655 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000656
657 if (skip)
658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100659 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000661 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000662 }
663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100664 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000665 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000667 if (expr_start != NULL)
668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100669 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100670 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000671 && *p != '[' && *p != '.')
672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100673 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000674 return NULL;
675 }
676
677 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
678 if (lp->ll_exp_name == NULL)
679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100680 // Report an invalid expression in braces, unless the
681 // expression evaluation has been cancelled due to an
682 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000683 if (!aborting() && !quiet)
684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000685 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100686 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000687 return NULL;
688 }
689 }
690 lp->ll_name = lp->ll_exp_name;
691 }
692 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000694 lp->ll_name = name;
695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
697 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100698 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 char_u *tp = skipwhite(p + 1);
700
701 // parse the type after the name
702 lp->ll_type = parse_type(&tp, &si->sn_type_list);
703 lp->ll_name_end = tp;
704 }
705 }
706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100707 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000708 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
709 return p;
710
711 cc = *p;
712 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100713 // Only pass &ht when we would write to the variable, it prevents autoload
714 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100715 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
716 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100718 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000719 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000720 if (v == NULL)
721 return NULL;
722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000723 /*
724 * Loop until no more [idx] or .key is following.
725 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100727 var1.v_type = VAR_UNKNOWN;
728 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000729 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000731 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
732 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100733 && lp->ll_tv->vval.v_dict != NULL)
734 && !(lp->ll_tv->v_type == VAR_BLOB
735 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100738 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000746 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000747
Bram Moolenaar8c711452005-01-14 21:53:12 +0000748 len = -1;
749 if (*p == '.')
750 {
751 key = p + 1;
752 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
753 ;
754 if (len == 0)
755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100757 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000759 }
760 p = key + len;
761 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000762 else
763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100764 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000765 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000766 if (*p == ':')
767 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000768 else
769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000770 empty1 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200771 if (eval1(&p, &var1, EVAL_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100773 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000776 clear_tv(&var1);
777 return NULL;
778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000779 }
780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 if (*p == ':')
783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000789 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000790 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100791 if (rettv != NULL
792 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100793 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100794 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100795 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000796 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000801 }
802 p = skipwhite(p + 1);
803 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000805 else
806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200808 // recursive!
809 if (eval1(&p, &var2, EVAL_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000813 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100814 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100817 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000818 clear_tv(&var2);
819 return NULL;
820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000821 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000826
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100831 clear_tv(&var1);
832 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 }
835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100836 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 ++p;
838 }
839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 {
842 if (len == -1)
843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // "[key]": get key from "var1"
845 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200846 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000848 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000850 }
851 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000852 lp->ll_list = NULL;
853 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000854 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // When assigning to a scope dictionary check that a function and
857 // variable name is valid (only variable name unless it is l: or
858 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200859 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200860 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200861 int prevval;
862 int wrong;
863
864 if (len != -1)
865 {
866 prevval = key[len];
867 key[len] = NUL;
868 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200869 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100870 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200871 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
872 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200873 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200874 || !valid_varname(key);
875 if (len != -1)
876 key[len] = prevval;
877 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200878 {
879 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200880 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200881 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200882 }
883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000885 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100886 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200887 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100888 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100890 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100891 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200892 return NULL;
893 }
894
Bram Moolenaar31b81602019-02-10 22:14:27 +0100895 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000897 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100900 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 }
903 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000906 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100907 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 p = NULL;
910 break;
911 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100913 else if ((flags & GLV_READ_ONLY) == 0
914 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100915 {
916 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200917 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100918 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200919
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100920 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000922 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100923 else if (lp->ll_tv->v_type == VAR_BLOB)
924 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100925 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
926
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100927 /*
928 * Get the number and item for the only or first index of the List.
929 */
930 if (empty1)
931 lp->ll_n1 = 0;
932 else
933 // is number or string
934 lp->ll_n1 = (long)tv_get_number(&var1);
935 clear_tv(&var1);
936
937 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100938 || lp->ll_n1 > bloblen
939 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100940 {
941 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100942 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100943 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100944 return NULL;
945 }
946 if (lp->ll_range && !lp->ll_empty2)
947 {
948 lp->ll_n2 = (long)tv_get_number(&var2);
949 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100950 if (lp->ll_n2 < 0
951 || lp->ll_n2 >= bloblen
952 || lp->ll_n2 < lp->ll_n1)
953 {
954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100956 return NULL;
957 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100958 }
959 lp->ll_blob = lp->ll_tv->vval.v_blob;
960 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100961 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 else
964 {
965 /*
966 * Get the number and item for the only or first index of the List.
967 */
968 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100972 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100973 clear_tv(&var1);
974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000975 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 lp->ll_list = lp->ll_tv->vval.v_list;
977 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
978 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000980 if (lp->ll_n1 < 0)
981 {
982 lp->ll_n1 = 0;
983 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
984 }
985 }
986 if (lp->ll_li == NULL)
987 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100988 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200989 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100990 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 }
993
994 /*
995 * May need to find the item or absolute index for the second
996 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 * When no index given: "lp->ll_empty2" is TRUE.
998 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001002 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001009 {
1010 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001011 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 }
1016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 if (lp->ll_n1 < 0)
1019 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1020 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001021 {
1022 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001023 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001025 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001029 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 }
1031
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 return p;
1035}
1036
1037/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001041clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042{
1043 vim_free(lp->ll_exp_name);
1044 vim_free(lp->ll_newkey);
1045}
1046
1047/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001048 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001050 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1051 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001054set_var_lval(
1055 lval_T *lp,
1056 char_u *endp,
1057 typval_T *rettv,
1058 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001060 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061{
1062 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001063 listitem_T *ri;
1064 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065
1066 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001068 cc = *endp;
1069 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001070 if (lp->ll_blob != NULL)
1071 {
1072 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001073
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001074 if (op != NULL && *op != '=')
1075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001076 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001077 return;
1078 }
1079
1080 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1081 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001083
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001084 if (lp->ll_empty2)
1085 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1086
1087 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001089 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090 return;
1091 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 if (lp->ll_empty2)
1093 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001094
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001095 ir = 0;
1096 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1097 blob_set(lp->ll_blob, il,
1098 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 }
1100 else
1101 {
1102 val = (int)tv_get_number_chk(rettv, &error);
1103 if (!error)
1104 {
1105 garray_T *gap = &lp->ll_blob->bv_ga;
1106
1107 // Allow for appending a byte. Setting a byte beyond
1108 // the end is an error otherwise.
1109 if (lp->ll_n1 < gap->ga_len
1110 || (lp->ll_n1 == gap->ga_len
1111 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1112 {
1113 blob_set(lp->ll_blob, lp->ll_n1, val);
1114 if (lp->ll_n1 == gap->ga_len)
1115 ++gap->ga_len;
1116 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001117 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001118 }
1119 }
1120 }
1121 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001123 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001126 {
1127 emsg(_(e_cannot_mod));
1128 *endp = cc;
1129 return;
1130 }
1131
Bram Moolenaarff697e62019-02-12 22:28:33 +01001132 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001133 di = NULL;
1134 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1135 &tv, &di, TRUE, FALSE) == OK)
1136 {
1137 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001138 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1139 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001140 && tv_op(&tv, rettv, op) == OK)
1141 set_var(lp->ll_name, &tv, FALSE);
1142 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001145 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001147 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001149 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001150 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001151 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001152 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 else if (lp->ll_range)
1154 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001155 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001156 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001159 {
1160 emsg(_("E996: Cannot lock a range"));
1161 return;
1162 }
1163
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164 /*
1165 * Check whether any of the list items is locked
1166 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001167 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001168 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001169 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001170 return;
1171 ri = ri->li_next;
1172 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1173 break;
1174 ll_li = ll_li->li_next;
1175 ++ll_n1;
1176 }
1177
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 /*
1179 * Assign the List values to the list items.
1180 */
1181 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001182 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001183 if (op != NULL && *op != '=')
1184 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1185 else
1186 {
1187 clear_tv(&lp->ll_li->li_tv);
1188 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001190 ri = ri->li_next;
1191 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1192 break;
1193 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001195 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001196 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001197 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 ri = NULL;
1199 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001200 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001201 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 lp->ll_li = lp->ll_li->li_next;
1203 ++lp->ll_n1;
1204 }
1205 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 else if (lp->ll_empty2
1208 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 }
1212 else
1213 {
1214 /*
1215 * Assign to a List or Dictionary item.
1216 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001218 {
1219 emsg(_("E996: Cannot lock a list or dict"));
1220 return;
1221 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 if (lp->ll_newkey != NULL)
1223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 if (op != NULL && *op != '=')
1225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 return;
1228 }
1229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001230 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001231 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 if (di == NULL)
1233 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001234 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1235 {
1236 vim_free(di);
1237 return;
1238 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001240 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001241 else if (op != NULL && *op != '=')
1242 {
1243 tv_op(lp->ll_tv, rettv, op);
1244 return;
1245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 /*
1250 * Assign the value to the variable or list item.
1251 */
1252 if (copy)
1253 copy_tv(rettv, lp->ll_tv);
1254 else
1255 {
1256 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001257 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 }
1260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261}
1262
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001263/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001264 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1265 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001266 * Returns OK or FAIL.
1267 */
1268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001271 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 char_u numbuf[NUMBUFLEN];
1273 char_u *s;
1274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001275 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001276 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001277 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 {
1279 switch (tv1->v_type)
1280 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001281 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001282 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001284 case VAR_DICT:
1285 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001286 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001287 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001288 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001289 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001290 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001291 break;
1292
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 case VAR_BLOB:
1294 if (*op != '+' || tv2->v_type != VAR_BLOB)
1295 break;
1296 // BLOB += BLOB
1297 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1298 {
1299 blob_T *b1 = tv1->vval.v_blob;
1300 blob_T *b2 = tv2->vval.v_blob;
1301 int i, len = blob_len(b2);
1302 for (i = 0; i < len; i++)
1303 ga_append(&b1->bv_ga, blob_get(b2, i));
1304 }
1305 return OK;
1306
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 case VAR_LIST:
1308 if (*op != '+' || tv2->v_type != VAR_LIST)
1309 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001310 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1312 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1313 return OK;
1314
1315 case VAR_NUMBER:
1316 case VAR_STRING:
1317 if (tv2->v_type == VAR_LIST)
1318 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001319 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001321 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001322 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001323#ifdef FEAT_FLOAT
1324 if (tv2->v_type == VAR_FLOAT)
1325 {
1326 float_T f = n;
1327
Bram Moolenaarff697e62019-02-12 22:28:33 +01001328 if (*op == '%')
1329 break;
1330 switch (*op)
1331 {
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 case '/': f /= tv2->vval.v_float; break;
1336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001337 clear_tv(tv1);
1338 tv1->v_type = VAR_FLOAT;
1339 tv1->vval.v_float = f;
1340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001341 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001342#endif
1343 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001344 switch (*op)
1345 {
1346 case '+': n += tv_get_number(tv2); break;
1347 case '-': n -= tv_get_number(tv2); break;
1348 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001349 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1350 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001351 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001352 clear_tv(tv1);
1353 tv1->v_type = VAR_NUMBER;
1354 tv1->vval.v_number = n;
1355 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356 }
1357 else
1358 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001359 if (tv2->v_type == VAR_FLOAT)
1360 break;
1361
Bram Moolenaarff697e62019-02-12 22:28:33 +01001362 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001363 s = tv_get_string(tv1);
1364 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 clear_tv(tv1);
1366 tv1->v_type = VAR_STRING;
1367 tv1->vval.v_string = s;
1368 }
1369 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001370
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001371 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001372#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001373 {
1374 float_T f;
1375
Bram Moolenaarff697e62019-02-12 22:28:33 +01001376 if (*op == '%' || *op == '.'
1377 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001378 && tv2->v_type != VAR_NUMBER
1379 && tv2->v_type != VAR_STRING))
1380 break;
1381 if (tv2->v_type == VAR_FLOAT)
1382 f = tv2->vval.v_float;
1383 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001384 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001385 switch (*op)
1386 {
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 case '/': tv1->vval.v_float /= f; break;
1391 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001392 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001393#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001394 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 }
1396 }
1397
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001398 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 return FAIL;
1400}
1401
1402/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 * Evaluate the expression used in a ":for var in expr" command.
1404 * "arg" points to "var".
1405 * Set "*errp" to TRUE for an error, FALSE otherwise;
1406 * Return a pointer that holds the info. Null when there is an error.
1407 */
1408 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_for_line(
1410 char_u *arg,
1411 int *errp,
1412 char_u **nextcmdp,
1413 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414{
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T tv;
1418 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001420 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001422 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 if (fi == NULL)
1424 return NULL;
1425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427 if (expr == NULL)
1428 return fi;
1429
1430 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001431 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001432 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001434 return fi;
1435 }
1436
1437 if (skip)
1438 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001439 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, skip ? 0 : EVAL_EVALUATE)
1440 == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001441 {
1442 *errp = FALSE;
1443 if (!skip)
1444 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001445 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001446 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001447 l = tv.vval.v_list;
1448 if (l == NULL)
1449 {
1450 // a null list is like an empty list: do nothing
1451 clear_tv(&tv);
1452 }
1453 else
1454 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001456 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 // No need to increment the refcount, it's already set for
1459 // the list being used in "tv".
1460 fi->fi_list = l;
1461 list_add_watch(l, &fi->fi_lw);
1462 fi->fi_lw.lw_item = l->lv_first;
1463 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001464 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001466 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001467 fi->fi_bi = 0;
1468 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001470 typval_T btv;
1471
1472 // Make a copy, so that the iteration still works when the
1473 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001475 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001477 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001478 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 else
1480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001481 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483 }
1484 }
1485 }
1486 if (skip)
1487 --emsg_skip;
1488
1489 return fi;
1490}
1491
1492/*
1493 * Use the first item in a ":for" list. Advance to the next.
1494 * Assign the values to the variable (list). "arg" points to the first one.
1495 * Return TRUE when a valid item was found, FALSE when at end of list or
1496 * something wrong.
1497 */
1498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001501 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001503 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1504 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001507 if (fi->fi_blob != NULL)
1508 {
1509 typval_T tv;
1510
1511 if (fi->fi_bi >= blob_len(fi->fi_blob))
1512 return FALSE;
1513 tv.v_type = VAR_NUMBER;
1514 tv.v_lock = VAR_FIXED;
1515 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1516 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001517 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001518 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001519 }
1520
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 item = fi->fi_lw.lw_item;
1522 if (item == NULL)
1523 result = FALSE;
1524 else
1525 {
1526 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001527 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001528 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 }
1530 return result;
1531}
1532
1533/*
1534 * Free the structure used to store info used by ":for".
1535 */
1536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538{
Bram Moolenaar33570922005-01-25 22:26:29 +00001539 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001540
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001541 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001542 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001543 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001544 list_unref(fi->fi_list);
1545 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001546 if (fi != NULL && fi->fi_blob != NULL)
1547 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548 vim_free(fi);
1549}
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552set_context_for_expression(
1553 expand_T *xp,
1554 char_u *arg,
1555 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
1557 int got_eq = FALSE;
1558 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001561 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001562 {
1563 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001564 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001567 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 {
1569 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001570 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001571 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572 break;
1573 }
1574 return;
1575 }
1576 }
1577 else
1578 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1579 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 while ((xp->xp_pattern = vim_strpbrk(arg,
1581 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1582 {
1583 c = *xp->xp_pattern;
1584 if (c == '&')
1585 {
1586 c = xp->xp_pattern[1];
1587 if (c == '&')
1588 {
1589 ++xp->xp_pattern;
1590 xp->xp_context = cmdidx != CMD_let || got_eq
1591 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1592 }
1593 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001596 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1597 xp->xp_pattern += 2;
1598
1599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else if (c == '$')
1602 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001603 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 xp->xp_context = EXPAND_ENV_VARS;
1605 }
1606 else if (c == '=')
1607 {
1608 got_eq = TRUE;
1609 xp->xp_context = EXPAND_EXPRESSION;
1610 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001611 else if (c == '#'
1612 && xp->xp_context == EXPAND_EXPRESSION)
1613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001614 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001615 break;
1616 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001617 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 && xp->xp_context == EXPAND_FUNCTIONS
1619 && vim_strchr(xp->xp_pattern, '(') == NULL)
1620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001621 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 break;
1623 }
1624 else if (cmdidx != CMD_let || got_eq)
1625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1629 if (c == '\\' && xp->xp_pattern[1] != NUL)
1630 ++xp->xp_pattern;
1631 xp->xp_context = EXPAND_NOTHING;
1632 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001635 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1637 /* skip */ ;
1638 xp->xp_context = EXPAND_NOTHING;
1639 }
1640 else if (c == '|')
1641 {
1642 if (xp->xp_pattern[1] == '|')
1643 {
1644 ++xp->xp_pattern;
1645 xp->xp_context = EXPAND_EXPRESSION;
1646 }
1647 else
1648 xp->xp_context = EXPAND_COMMANDS;
1649 }
1650 else
1651 xp->xp_context = EXPAND_EXPRESSION;
1652 }
1653 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001654 // Doesn't look like something valid, expand as an expression
1655 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 arg = xp->xp_pattern;
1658 if (*arg != NUL)
1659 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1660 /* skip */ ;
1661 }
1662 xp->xp_pattern = arg;
1663}
1664
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001666 * Return TRUE if "pat" matches "text".
1667 * Does not use 'cpo' and always uses 'magic'.
1668 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001669 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001670pattern_match(char_u *pat, char_u *text, int ic)
1671{
1672 int matches = FALSE;
1673 char_u *save_cpo;
1674 regmatch_T regmatch;
1675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001676 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001677 save_cpo = p_cpo;
1678 p_cpo = (char_u *)"";
1679 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1680 if (regmatch.regprog != NULL)
1681 {
1682 regmatch.rm_ic = ic;
1683 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1684 vim_regfree(regmatch.regprog);
1685 }
1686 p_cpo = save_cpo;
1687 return matches;
1688}
1689
1690/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001691 * Handle a name followed by "(". Both for just "name(arg)" and for
1692 * "expr->name(arg)".
1693 * Returns OK or FAIL.
1694 */
1695 static int
1696eval_func(
1697 char_u **arg, // points to "(", will be advanced
1698 char_u *name,
1699 int name_len,
1700 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001701 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001702 typval_T *basetv) // "expr" for "expr->name(arg)"
1703{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001704 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001705 char_u *s = name;
1706 int len = name_len;
1707 partial_T *partial;
1708 int ret = OK;
1709
1710 if (!evaluate)
1711 check_vars(s, len);
1712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001713 // If "s" is the name of a variable of type VAR_FUNC
1714 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001715 s = deref_func_name(s, &len, &partial, !evaluate);
1716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001717 // Need to make a copy, in case evaluating the arguments makes
1718 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001719 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001720 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001721 ret = FAIL;
1722 else
1723 {
1724 funcexe_T funcexe;
1725
1726 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001727 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001728 funcexe.firstline = curwin->w_cursor.lnum;
1729 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001730 funcexe.evaluate = evaluate;
1731 funcexe.partial = partial;
1732 funcexe.basetv = basetv;
1733 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1734 }
1735 vim_free(s);
1736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001737 // If evaluate is FALSE rettv->v_type was not set in
1738 // get_func_tv, but it's needed in handle_subscript() to parse
1739 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001740 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1741 {
1742 rettv->vval.v_string = NULL;
1743 rettv->v_type = VAR_FUNC;
1744 }
1745
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001746 // Stop the expression evaluation when immediately
1747 // aborting on error, or when an interrupt occurred or
1748 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001749 if (evaluate && aborting())
1750 {
1751 if (ret == OK)
1752 clear_tv(rettv);
1753 ret = FAIL;
1754 }
1755 return ret;
1756}
1757
1758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1762 */
1763
1764/*
1765 * Handle zero level expression.
1766 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001768 * Note: "rettv.v_lock" is not set.
Bram Moolenaar32e35112020-05-14 22:41:15 +02001769 * "flags" has EVAL_EVALUATE and similar flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 * Return OK or FAIL.
1771 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001773eval0(
1774 char_u *arg,
1775 typval_T *rettv,
1776 char_u **nextcmd,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001777 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 int ret;
1780 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001781 int did_emsg_before = did_emsg;
1782 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 p = skipwhite(arg);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001785 ret = eval1(&p, rettv, flags);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001786 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
1788 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 /*
1791 * Report the invalid expression unless the expression evaluation has
1792 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001793 * exception, or we already gave a more specific error.
1794 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001796 if (!aborting()
1797 && did_emsg == did_emsg_before
1798 && called_emsg == called_emsg_before
1799 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001800 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 ret = FAIL;
1802 }
1803 if (nextcmd != NULL)
1804 *nextcmd = check_nextcmd(p);
1805
1806 return ret;
1807}
1808
1809/*
1810 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001811 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 *
1813 * "arg" must point to the first non-white of the expression.
1814 * "arg" is advanced to the next non-white after the recognized expression.
1815 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001816 * Note: "rettv.v_lock" is not set.
1817 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 * Return OK or FAIL.
1819 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001820 int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001821eval1(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 /*
1827 * Get the first variable.
1828 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001829 if (eval2(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 return FAIL;
1831
1832 if ((*arg)[0] == '?')
1833 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001834 int evaluate = flags & EVAL_EVALUATE;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 result = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001837 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001839 int error = FALSE;
1840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001841 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001844 if (error)
1845 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847
1848 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001849 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001852 if (eval1(arg, rettv, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 return FAIL;
1854
1855 /*
1856 * Check for the ":".
1857 */
1858 if ((*arg)[0] != ':')
1859 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 return FAIL;
1864 }
1865
1866 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001867 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001870 if (eval1(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
1872 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 return FAIL;
1875 }
1876 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879
1880 return OK;
1881}
1882
1883/*
1884 * Handle first level expression:
1885 * expr2 || expr2 || expr2 logical OR
1886 *
1887 * "arg" must point to the first non-white of the expression.
1888 * "arg" is advanced to the next non-white after the recognized expression.
1889 *
1890 * Return OK or FAIL.
1891 */
1892 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001893eval2(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 long result;
1897 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001898 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 /*
1901 * Get the first variable.
1902 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001903 if (eval3(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return FAIL;
1905
1906 /*
1907 * Repeat until there is no following "||".
1908 */
1909 first = TRUE;
1910 result = FALSE;
1911 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1912 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001913 int evaluate = flags & EVAL_EVALUATE;
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (evaluate && first)
1916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001917 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001920 if (error)
1921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 first = FALSE;
1923 }
1924
1925 /*
1926 * Get the second variable.
1927 */
1928 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001929 if (eval3(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE)
1930 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 return FAIL;
1932
1933 /*
1934 * Compute the result.
1935 */
1936 if (evaluate && !result)
1937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001938 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001941 if (error)
1942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 if (evaluate)
1945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 rettv->v_type = VAR_NUMBER;
1947 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 }
1950
1951 return OK;
1952}
1953
1954/*
1955 * Handle second level expression:
1956 * expr3 && expr3 && expr3 logical AND
1957 *
1958 * "arg" must point to the first non-white of the expression.
1959 * "arg" is advanced to the next non-white after the recognized expression.
1960 *
1961 * Return OK or FAIL.
1962 */
1963 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001964eval3(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 long result;
1968 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971 /*
1972 * Get the first variable.
1973 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001974 if (eval4(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 return FAIL;
1976
1977 /*
1978 * Repeat until there is no following "&&".
1979 */
1980 first = TRUE;
1981 result = TRUE;
1982 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1983 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001984 int evaluate = flags & EVAL_EVALUATE;
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (evaluate && first)
1987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001988 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001991 if (error)
1992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 first = FALSE;
1994 }
1995
1996 /*
1997 * Get the second variable.
1998 */
1999 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002000 if (eval4(arg, &var2, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 return FAIL;
2002
2003 /*
2004 * Compute the result.
2005 */
2006 if (evaluate && result)
2007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002008 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002011 if (error)
2012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014 if (evaluate)
2015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 rettv->v_type = VAR_NUMBER;
2017 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019 }
2020
2021 return OK;
2022}
2023
2024/*
2025 * Handle third level expression:
2026 * var1 == var2
2027 * var1 =~ var2
2028 * var1 != var2
2029 * var1 !~ var2
2030 * var1 > var2
2031 * var1 >= var2
2032 * var1 < var2
2033 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002034 * var1 is var2
2035 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 *
2037 * "arg" must point to the first non-white of the expression.
2038 * "arg" is advanced to the next non-white after the recognized expression.
2039 *
2040 * Return OK or FAIL.
2041 */
2042 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002043eval4(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
Bram Moolenaar33570922005-01-25 22:26:29 +00002045 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 char_u *p;
2047 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002048 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 /*
2053 * Get the first variable.
2054 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002055 if (eval5(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 return FAIL;
2057
2058 p = *arg;
2059 switch (p[0])
2060 {
2061 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002062 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002064 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 break;
2066 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002067 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002069 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 break;
2071 case '>': if (p[1] != '=')
2072 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002073 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 len = 1;
2075 }
2076 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002077 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 break;
2079 case '<': if (p[1] != '=')
2080 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002081 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 len = 1;
2083 }
2084 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002085 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002087 case 'i': if (p[1] == 's')
2088 {
2089 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2090 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002091 i = p[len];
2092 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002093 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002094 }
2095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097
2098 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002099 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002101 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002103 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (p[len] == '?')
2105 {
2106 ic = TRUE;
2107 ++len;
2108 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002109 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 else if (p[len] == '#')
2111 {
2112 ic = FALSE;
2113 ++len;
2114 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002115 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 else
2117 ic = p_ic;
2118
2119 /*
2120 * Get the second variable.
2121 */
2122 *arg = skipwhite(p + len);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002123 if (eval5(arg, &var2, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 return FAIL;
2127 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002128 if (flags & EVAL_EVALUATE)
Bram Moolenaar31988702018-02-13 12:57:42 +01002129 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002130 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002131
2132 clear_tv(&var2);
2133 return ret;
2134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136
2137 return OK;
2138}
2139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 void
2141eval_addblob(typval_T *tv1, typval_T *tv2)
2142{
2143 blob_T *b1 = tv1->vval.v_blob;
2144 blob_T *b2 = tv2->vval.v_blob;
2145 blob_T *b = blob_alloc();
2146 int i;
2147
2148 if (b != NULL)
2149 {
2150 for (i = 0; i < blob_len(b1); i++)
2151 ga_append(&b->bv_ga, blob_get(b1, i));
2152 for (i = 0; i < blob_len(b2); i++)
2153 ga_append(&b->bv_ga, blob_get(b2, i));
2154
2155 clear_tv(tv1);
2156 rettv_blob_set(tv1, b);
2157 }
2158}
2159
2160 int
2161eval_addlist(typval_T *tv1, typval_T *tv2)
2162{
2163 typval_T var3;
2164
2165 // concatenate Lists
2166 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2167 {
2168 clear_tv(tv1);
2169 clear_tv(tv2);
2170 return FAIL;
2171 }
2172 clear_tv(tv1);
2173 *tv1 = var3;
2174 return OK;
2175}
2176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177/*
2178 * Handle fourth level expression:
2179 * + number addition
2180 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002181 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002182 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 *
2184 * "arg" must point to the first non-white of the expression.
2185 * "arg" is advanced to the next non-white after the recognized expression.
2186 *
2187 * Return OK or FAIL.
2188 */
2189 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002190eval5(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002194 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002195#ifdef FEAT_FLOAT
2196 float_T f1 = 0, f2 = 0;
2197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 char_u *s1, *s2;
2199 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2200 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002201 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 /*
2204 * Get the first variable.
2205 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002206 if (eval6(arg, rettv, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return FAIL;
2208
2209 /*
2210 * Repeat computing, until no '+', '-' or '.' is following.
2211 */
2212 for (;;)
2213 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002214 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002216 concat = op == '.'
2217 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2218 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 break;
2220
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002221 if ((op != '+' || (rettv->v_type != VAR_LIST
2222 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002223#ifdef FEAT_FLOAT
2224 && (op == '.' || rettv->v_type != VAR_FLOAT)
2225#endif
2226 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002228 // For "list + ...", an illegal use of the first operand as
2229 // a number cannot be determined before evaluating the 2nd
2230 // operand: if this is also a list, all is ok.
2231 // For "something . ...", "something - ..." or "non-list + ...",
2232 // we know that the first operand needs to be a string or number
2233 // without evaluating the 2nd operand. So check before to avoid
2234 // side effects after an error.
Bram Moolenaar32e35112020-05-14 22:41:15 +02002235 if ((flags & EVAL_EVALUATE) && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 {
2237 clear_tv(rettv);
2238 return FAIL;
2239 }
2240 }
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 /*
2243 * Get the second variable.
2244 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002245 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2246 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002248 if (eval6(arg, &var2, flags, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return FAIL;
2252 }
2253
Bram Moolenaar32e35112020-05-14 22:41:15 +02002254 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
2256 /*
2257 * Compute the result.
2258 */
2259 if (op == '.')
2260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002261 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002262 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002263 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002264 {
2265 clear_tv(rettv);
2266 clear_tv(&var2);
2267 return FAIL;
2268 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 clear_tv(rettv);
2271 rettv->v_type = VAR_STRING;
2272 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002274 else if (op == '+' && rettv->v_type == VAR_BLOB
2275 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002277 else if (op == '+' && rettv->v_type == VAR_LIST
2278 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002279 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002281 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 else
2284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002285 int error = FALSE;
2286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002287#ifdef FEAT_FLOAT
2288 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002289 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002290 f1 = rettv->vval.v_float;
2291 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002292 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002293 else
2294#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002296 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002297 if (error)
2298 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002299 // This can only happen for "list + non-list". For
2300 // "non-list + ..." or "something - ...", we returned
2301 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002302 clear_tv(rettv);
2303 return FAIL;
2304 }
2305#ifdef FEAT_FLOAT
2306 if (var2.v_type == VAR_FLOAT)
2307 f1 = n1;
2308#endif
2309 }
2310#ifdef FEAT_FLOAT
2311 if (var2.v_type == VAR_FLOAT)
2312 {
2313 f2 = var2.vval.v_float;
2314 n2 = 0;
2315 }
2316 else
2317#endif
2318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002319 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002320 if (error)
2321 {
2322 clear_tv(rettv);
2323 clear_tv(&var2);
2324 return FAIL;
2325 }
2326#ifdef FEAT_FLOAT
2327 if (rettv->v_type == VAR_FLOAT)
2328 f2 = n2;
2329#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002332
2333#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002334 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002335 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2336 {
2337 if (op == '+')
2338 f1 = f1 + f2;
2339 else
2340 f1 = f1 - f2;
2341 rettv->v_type = VAR_FLOAT;
2342 rettv->vval.v_float = f1;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002345#endif
2346 {
2347 if (op == '+')
2348 n1 = n1 + n2;
2349 else
2350 n1 = n1 - n2;
2351 rettv->v_type = VAR_NUMBER;
2352 rettv->vval.v_number = n1;
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 }
2358 return OK;
2359}
2360
2361/*
2362 * Handle fifth level expression:
2363 * * number multiplication
2364 * / number division
2365 * % number modulo
2366 *
2367 * "arg" must point to the first non-white of the expression.
2368 * "arg" is advanced to the next non-white after the recognized expression.
2369 *
2370 * Return OK or FAIL.
2371 */
2372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002373eval6(
2374 char_u **arg,
2375 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002376 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
Bram Moolenaar33570922005-01-25 22:26:29 +00002379 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002381 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002382#ifdef FEAT_FLOAT
2383 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002384 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002385#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * Get the first variable.
2390 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002391 if (eval7(arg, rettv, flags, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 return FAIL;
2393
2394 /*
2395 * Repeat computing, until no '*', '/' or '%' is following.
2396 */
2397 for (;;)
2398 {
2399 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002400 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 break;
2402
Bram Moolenaar32e35112020-05-14 22:41:15 +02002403 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002405#ifdef FEAT_FLOAT
2406 if (rettv->v_type == VAR_FLOAT)
2407 {
2408 f1 = rettv->vval.v_float;
2409 use_float = TRUE;
2410 n1 = 0;
2411 }
2412 else
2413#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002414 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 if (error)
2417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419 else
2420 n1 = 0;
2421
2422 /*
2423 * Get the second variable.
2424 */
2425 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002426 if (eval7(arg, &var2, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 return FAIL;
2428
Bram Moolenaar32e35112020-05-14 22:41:15 +02002429 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431#ifdef FEAT_FLOAT
2432 if (var2.v_type == VAR_FLOAT)
2433 {
2434 if (!use_float)
2435 {
2436 f1 = n1;
2437 use_float = TRUE;
2438 }
2439 f2 = var2.vval.v_float;
2440 n2 = 0;
2441 }
2442 else
2443#endif
2444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002445 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002446 clear_tv(&var2);
2447 if (error)
2448 return FAIL;
2449#ifdef FEAT_FLOAT
2450 if (use_float)
2451 f2 = n2;
2452#endif
2453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 /*
2456 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002457 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002459#ifdef FEAT_FLOAT
2460 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002462 if (op == '*')
2463 f1 = f1 * f2;
2464 else if (op == '/')
2465 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002466# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002467 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002468 if (f2 == 0.0)
2469 {
2470 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002471 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002472 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002473 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002474 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002475 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002476 }
2477 else
2478 f1 = f1 / f2;
2479# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002480 // We rely on the floating point library to handle divide
2481 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002482 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002483# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002486 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002488 return FAIL;
2489 }
2490 rettv->v_type = VAR_FLOAT;
2491 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002496 if (op == '*')
2497 n1 = n1 * n2;
2498 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002499 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002501 n1 = num_modulus(n1, n2);
2502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002503 rettv->v_type = VAR_NUMBER;
2504 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 }
2508
2509 return OK;
2510}
2511
2512/*
2513 * Handle sixth level expression:
2514 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002515 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002516 * "string" string constant
2517 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * &option-name option value
2519 * @r register contents
2520 * identifier variable value
2521 * function() function call
2522 * $VAR environment variable
2523 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002524 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002526 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002527 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 *
2529 * Also handle:
2530 * ! in front logical NOT
2531 * - in front unary minus
2532 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 * trailing [] subscript in String or List
2534 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002535 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
2537 * "arg" must point to the first non-white of the expression.
2538 * "arg" is advanced to the next non-white after the recognized expression.
2539 *
2540 * Return OK or FAIL.
2541 */
2542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543eval7(
2544 char_u **arg,
2545 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002546 int flags,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002549 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 int len;
2551 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 char_u *start_leader, *end_leader;
2553 int ret = OK;
2554 char_u *alias;
2555
2556 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002558 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002563 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
2565 start_leader = *arg;
2566 while (**arg == '!' || **arg == '-' || **arg == '+')
2567 *arg = skipwhite(*arg + 1);
2568 end_leader = *arg;
2569
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002570 if (**arg == '.' && (!isdigit(*(*arg + 1))
2571#ifdef FEAT_FLOAT
2572 || current_sctx.sc_version < 2
2573#endif
2574 ))
2575 {
2576 semsg(_(e_invexpr2), *arg);
2577 ++*arg;
2578 return FAIL;
2579 }
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 switch (**arg)
2582 {
2583 /*
2584 * Number constant.
2585 */
2586 case '0':
2587 case '1':
2588 case '2':
2589 case '3':
2590 case '4':
2591 case '5':
2592 case '6':
2593 case '7':
2594 case '8':
2595 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 break;
2598
2599 /*
2600 * String constant: "string".
2601 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 break;
2604
2605 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002609 break;
2610
2611 /*
2612 * List: [expr, expr]
2613 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002614 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 break;
2616
2617 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002618 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002619 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002620 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002621 {
2622 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002623 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002624 }
2625 else
2626 ret = NOTDONE;
2627 break;
2628
2629 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002630 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002631 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002633 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2634 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002635 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 break;
2637
2638 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002639 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002641 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 break;
2643
2644 /*
2645 * Environment variable: $VAR.
2646 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 break;
2649
2650 /*
2651 * Register contents: @r.
2652 */
2653 case '@': ++*arg;
2654 if (evaluate)
2655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002657 rettv->vval.v_string = get_reg_contents(**arg,
2658 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660 if (**arg != NUL)
2661 ++*arg;
2662 break;
2663
2664 /*
2665 * nested expression: (expression).
2666 */
2667 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002668 ret = eval1(arg, rettv, flags); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (**arg == ')')
2670 ++*arg;
2671 else if (ret == OK)
2672 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ret = FAIL;
2676 }
2677 break;
2678
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 break;
2681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682
2683 if (ret == NOTDONE)
2684 {
2685 /*
2686 * Must be a variable or function name.
2687 * Can also be a curly-braces kind of name: {expr}.
2688 */
2689 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002690 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (alias != NULL)
2692 s = alias;
2693
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002694 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 ret = FAIL;
2696 else
2697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002699 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002700 else if (flags & EVAL_CONSTANT)
2701 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002703 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002704 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002705 {
2706 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002710 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 *arg = skipwhite(*arg);
2714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002715 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2716 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002717 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002718 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002719 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 /*
2722 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2723 */
2724 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002725 ret = eval7_leader(rettv, start_leader, &end_leader);
2726 return ret;
2727}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002729/*
2730 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2731 * Adjusts "end_leaderp" until it is at "start_leader".
2732 */
2733 static int
2734eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2735{
2736 char_u *end_leader = *end_leaderp;
2737 int ret = OK;
2738 int error = FALSE;
2739 varnumber_T val = 0;
2740#ifdef FEAT_FLOAT
2741 float_T f = 0.0;
2742
2743 if (rettv->v_type == VAR_FLOAT)
2744 f = rettv->vval.v_float;
2745 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002746#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002747 val = tv_get_number_chk(rettv, &error);
2748 if (error)
2749 {
2750 clear_tv(rettv);
2751 ret = FAIL;
2752 }
2753 else
2754 {
2755 while (end_leader > start_leader)
2756 {
2757 --end_leader;
2758 if (*end_leader == '!')
2759 {
2760#ifdef FEAT_FLOAT
2761 if (rettv->v_type == VAR_FLOAT)
2762 f = !f;
2763 else
2764#endif
2765 val = !val;
2766 }
2767 else if (*end_leader == '-')
2768 {
2769#ifdef FEAT_FLOAT
2770 if (rettv->v_type == VAR_FLOAT)
2771 f = -f;
2772 else
2773#endif
2774 val = -val;
2775 }
2776 }
2777#ifdef FEAT_FLOAT
2778 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002781 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002783 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002786 clear_tv(rettv);
2787 rettv->v_type = VAR_NUMBER;
2788 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002791 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 return ret;
2793}
2794
2795/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002796 * Call the function referred to in "rettv".
2797 */
2798 static int
2799call_func_rettv(
2800 char_u **arg,
2801 typval_T *rettv,
2802 int evaluate,
2803 dict_T *selfdict,
2804 typval_T *basetv)
2805{
2806 partial_T *pt = NULL;
2807 funcexe_T funcexe;
2808 typval_T functv;
2809 char_u *s;
2810 int ret;
2811
2812 // need to copy the funcref so that we can clear rettv
2813 if (evaluate)
2814 {
2815 functv = *rettv;
2816 rettv->v_type = VAR_UNKNOWN;
2817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002819 if (functv.v_type == VAR_PARTIAL)
2820 {
2821 pt = functv.vval.v_partial;
2822 s = partial_name(pt);
2823 }
2824 else
2825 s = functv.vval.v_string;
2826 }
2827 else
2828 s = (char_u *)"";
2829
Bram Moolenaara80faa82020-04-12 19:37:17 +02002830 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002831 funcexe.firstline = curwin->w_cursor.lnum;
2832 funcexe.lastline = curwin->w_cursor.lnum;
2833 funcexe.evaluate = evaluate;
2834 funcexe.partial = pt;
2835 funcexe.selfdict = selfdict;
2836 funcexe.basetv = basetv;
2837 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 // Clear the funcref afterwards, so that deleting it while
2840 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002841 if (evaluate)
2842 clear_tv(&functv);
2843
2844 return ret;
2845}
2846
2847/*
2848 * Evaluate "->method()".
2849 * "*arg" points to the '-'.
2850 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2851 */
2852 static int
2853eval_lambda(
2854 char_u **arg,
2855 typval_T *rettv,
2856 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002857 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002858{
2859 typval_T base = *rettv;
2860 int ret;
2861
2862 // Skip over the ->.
2863 *arg += 2;
2864 rettv->v_type = VAR_UNKNOWN;
2865
2866 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002867 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002868 return FAIL;
2869 else if (**arg != '(')
2870 {
2871 if (verbose)
2872 {
2873 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002874 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002875 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002877 }
2878 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002879 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002880 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002881 else
2882 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2883
2884 // Clear the funcref afterwards, so that deleting it while
2885 // evaluating the arguments is possible (see test55).
2886 if (evaluate)
2887 clear_tv(&base);
2888
2889 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002890}
2891
2892/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002893 * Evaluate "->method()".
2894 * "*arg" points to the '-'.
2895 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2896 */
2897 static int
2898eval_method(
2899 char_u **arg,
2900 typval_T *rettv,
2901 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002902 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002903{
2904 char_u *name;
2905 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002906 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002907 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002909
2910 // Skip over the ->.
2911 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002912 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002913
Bram Moolenaarac92e252019-08-03 21:58:38 +02002914 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002915 len = get_name_len(arg, &alias, evaluate, TRUE);
2916 if (alias != NULL)
2917 name = alias;
2918
2919 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002920 {
2921 if (verbose)
2922 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002923 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002924 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002925 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002926 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002927 if (**arg != '(')
2928 {
2929 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002931 ret = FAIL;
2932 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002933 else if (VIM_ISWHITE((*arg)[-1]))
2934 {
2935 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002936 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002937 ret = FAIL;
2938 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002939 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002940 ret = eval_func(arg, name, len, rettv,
2941 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002942 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002943
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002944 // Clear the funcref afterwards, so that deleting it while
2945 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002946 if (evaluate)
2947 clear_tv(&base);
2948
Bram Moolenaarac92e252019-08-03 21:58:38 +02002949 return ret;
2950}
2951
2952/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002953 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2954 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002955 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2956 */
2957 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958eval_index(
2959 char_u **arg,
2960 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002961 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002962 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002963{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002964 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002965 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002967 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002968 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002969 long len = -1;
2970 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002971 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002973
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002975 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002977 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002978 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002979 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002980 return FAIL;
2981 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002982#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002983 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002985 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002986#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002987 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002988 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002989 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002990 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002991 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002992 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002993 return FAIL;
2994 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002995 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002997 if (evaluate)
2998 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002999 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003000
3001 case VAR_STRING:
3002 case VAR_NUMBER:
3003 case VAR_LIST:
3004 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003005 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003006 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003007 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003008
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003009 init_tv(&var1);
3010 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003011 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013 /*
3014 * dict.name
3015 */
3016 key = *arg + 1;
3017 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3018 ;
3019 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003020 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003022 }
3023 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003024 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003025 /*
3026 * something[idx]
3027 *
3028 * Get the (first) variable from inside the [].
3029 */
3030 *arg = skipwhite(*arg + 1);
3031 if (**arg == ':')
3032 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003033 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003034 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003035 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003037 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 clear_tv(&var1);
3039 return FAIL;
3040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041
3042 /*
3043 * Get the second variable from inside the [:].
3044 */
3045 if (**arg == ':')
3046 {
3047 range = TRUE;
3048 *arg = skipwhite(*arg + 1);
3049 if (**arg == ']')
3050 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003051 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003053 if (!empty1)
3054 clear_tv(&var1);
3055 return FAIL;
3056 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003057 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003059 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003060 if (!empty1)
3061 clear_tv(&var1);
3062 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003063 return FAIL;
3064 }
3065 }
3066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 if (**arg != ']')
3069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003070 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003071 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003072 clear_tv(&var1);
3073 if (range)
3074 clear_tv(&var2);
3075 return FAIL;
3076 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003077 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003078 }
3079
3080 if (evaluate)
3081 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003082 n1 = 0;
3083 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003087 }
3088 if (range)
3089 {
3090 if (empty2)
3091 n2 = -1;
3092 else
3093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003094 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 }
3097 }
3098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003099 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003100 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003101 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003102 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003104 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003105 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003106 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003107 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003108 case VAR_SPECIAL:
3109 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003110 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003112
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003113 case VAR_NUMBER:
3114 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003116 len = (long)STRLEN(s);
3117 if (range)
3118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003119 // The resulting variable is a substring. If the indexes
3120 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003121 if (n1 < 0)
3122 {
3123 n1 = len + n1;
3124 if (n1 < 0)
3125 n1 = 0;
3126 }
3127 if (n2 < 0)
3128 n2 = len + n2;
3129 else if (n2 >= len)
3130 n2 = len;
3131 if (n1 >= len || n2 < 0 || n1 > n2)
3132 s = NULL;
3133 else
3134 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3135 }
3136 else
3137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003138 // The resulting variable is a string of a single
3139 // character. If the index is too big or negative the
3140 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003141 if (n1 >= len || n1 < 0)
3142 s = NULL;
3143 else
3144 s = vim_strnsave(s + n1, 1);
3145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 clear_tv(rettv);
3147 rettv->v_type = VAR_STRING;
3148 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 break;
3150
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003151 case VAR_BLOB:
3152 len = blob_len(rettv->vval.v_blob);
3153 if (range)
3154 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003155 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003156 // are out of range the result is empty.
3157 if (n1 < 0)
3158 {
3159 n1 = len + n1;
3160 if (n1 < 0)
3161 n1 = 0;
3162 }
3163 if (n2 < 0)
3164 n2 = len + n2;
3165 else if (n2 >= len)
3166 n2 = len - 1;
3167 if (n1 >= len || n2 < 0 || n1 > n2)
3168 {
3169 clear_tv(rettv);
3170 rettv->v_type = VAR_BLOB;
3171 rettv->vval.v_blob = NULL;
3172 }
3173 else
3174 {
3175 blob_T *blob = blob_alloc();
3176
3177 if (blob != NULL)
3178 {
3179 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3180 {
3181 blob_free(blob);
3182 return FAIL;
3183 }
3184 blob->bv_ga.ga_len = n2 - n1 + 1;
3185 for (i = n1; i <= n2; i++)
3186 blob_set(blob, i - n1,
3187 blob_get(rettv->vval.v_blob, i));
3188
3189 clear_tv(rettv);
3190 rettv_blob_set(rettv, blob);
3191 }
3192 }
3193 }
3194 else
3195 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003196 // The resulting variable is a byte value.
3197 // If the index is too big or negative that is an error.
3198 if (n1 < 0)
3199 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003200 if (n1 < len && n1 >= 0)
3201 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003202 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003203
3204 clear_tv(rettv);
3205 rettv->v_type = VAR_NUMBER;
3206 rettv->vval.v_number = v;
3207 }
3208 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003209 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003210 }
3211 break;
3212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003213 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003215 if (n1 < 0)
3216 n1 = len + n1;
3217 if (!empty1 && (n1 < 0 || n1 >= len))
3218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003219 // For a range we allow invalid values and return an empty
3220 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003221 if (!range)
3222 {
3223 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003225 return FAIL;
3226 }
3227 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003228 }
3229 if (range)
3230 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 list_T *l;
3232 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003233
3234 if (n2 < 0)
3235 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003236 else if (n2 >= len)
3237 n2 = len - 1;
3238 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003239 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 l = list_alloc();
3241 if (l == NULL)
3242 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003243 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003244 n1 <= n2; ++n1)
3245 {
3246 if (list_append_tv(l, &item->li_tv) == FAIL)
3247 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003248 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003249 return FAIL;
3250 }
3251 item = item->li_next;
3252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003253 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003254 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003255 }
3256 else
3257 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003258 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003259 clear_tv(rettv);
3260 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 }
3262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263
3264 case VAR_DICT:
3265 if (range)
3266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003268 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269 if (len == -1)
3270 clear_tv(&var1);
3271 return FAIL;
3272 }
3273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275
3276 if (len == -1)
3277 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003278 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003279 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003281 clear_tv(&var1);
3282 return FAIL;
3283 }
3284 }
3285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003286 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003288 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003289 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 if (len == -1)
3291 clear_tv(&var1);
3292 if (item == NULL)
3293 return FAIL;
3294
3295 copy_tv(&item->di_tv, &var1);
3296 clear_tv(rettv);
3297 *rettv = var1;
3298 }
3299 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003300 }
3301 }
3302
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 return OK;
3304}
3305
3306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 * Get an option value.
3308 * "arg" points to the '&' or '+' before the option name.
3309 * "arg" is advanced to character after the option name.
3310 * Return OK or FAIL.
3311 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313get_option_tv(
3314 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003315 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003316 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 char_u *option_end;
3319 long numval;
3320 char_u *stringval;
3321 int opt_type;
3322 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003323 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int ret = OK;
3325 int opt_flags;
3326
3327 /*
3328 * Isolate the option name and find its value.
3329 */
3330 option_end = find_option_end(arg, &opt_flags);
3331 if (option_end == NULL)
3332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003334 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return FAIL;
3336 }
3337
3338 if (!evaluate)
3339 {
3340 *arg = option_end;
3341 return OK;
3342 }
3343
3344 c = *option_end;
3345 *option_end = NUL;
3346 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003349 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 ret = FAIL;
3354 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003357 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 rettv->v_type = VAR_STRING;
3360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003362 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003364 rettv->v_type = VAR_NUMBER;
3365 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003367 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 rettv->v_type = VAR_NUMBER;
3370 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003372 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003374 rettv->v_type = VAR_STRING;
3375 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 }
3378 else if (working && (opt_type == -2 || opt_type == -1))
3379 ret = FAIL;
3380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003381 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 *arg = option_end;
3383
3384 return ret;
3385}
3386
3387/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3389 * Return OK or FAIL.
3390 */
3391 int
3392get_number_tv(
3393 char_u **arg,
3394 typval_T *rettv,
3395 int evaluate,
3396 int want_string UNUSED)
3397{
3398 int len;
3399#ifdef FEAT_FLOAT
3400 char_u *p;
3401 int get_float = FALSE;
3402
3403 // We accept a float when the format matches
3404 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3405 // strict to avoid backwards compatibility problems.
3406 // With script version 2 and later the leading digit can be
3407 // omitted.
3408 // Don't look for a float after the "." operator, so that
3409 // ":let vers = 1.2.3" doesn't fail.
3410 if (**arg == '.')
3411 p = *arg;
3412 else
3413 p = skipdigits(*arg + 1);
3414 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3415 {
3416 get_float = TRUE;
3417 p = skipdigits(p + 2);
3418 if (*p == 'e' || *p == 'E')
3419 {
3420 ++p;
3421 if (*p == '-' || *p == '+')
3422 ++p;
3423 if (!vim_isdigit(*p))
3424 get_float = FALSE;
3425 else
3426 p = skipdigits(p + 1);
3427 }
3428 if (ASCII_ISALPHA(*p) || *p == '.')
3429 get_float = FALSE;
3430 }
3431 if (get_float)
3432 {
3433 float_T f;
3434
3435 *arg += string2float(*arg, &f);
3436 if (evaluate)
3437 {
3438 rettv->v_type = VAR_FLOAT;
3439 rettv->vval.v_float = f;
3440 }
3441 }
3442 else
3443#endif
3444 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3445 {
3446 char_u *bp;
3447 blob_T *blob = NULL; // init for gcc
3448
3449 // Blob constant: 0z0123456789abcdef
3450 if (evaluate)
3451 blob = blob_alloc();
3452 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3453 {
3454 if (!vim_isxdigit(bp[1]))
3455 {
3456 if (blob != NULL)
3457 {
3458 emsg(_("E973: Blob literal should have an even number of hex characters"));
3459 ga_clear(&blob->bv_ga);
3460 VIM_CLEAR(blob);
3461 }
3462 return FAIL;
3463 }
3464 if (blob != NULL)
3465 ga_append(&blob->bv_ga,
3466 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3467 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3468 ++bp;
3469 }
3470 if (blob != NULL)
3471 rettv_blob_set(rettv, blob);
3472 *arg = bp;
3473 }
3474 else
3475 {
3476 varnumber_T n;
3477
3478 // decimal, hex or octal number
3479 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3480 ? STR2NR_NO_OCT + STR2NR_QUOTE
3481 : STR2NR_ALL, &n, NULL, 0, TRUE);
3482 if (len == 0)
3483 {
3484 semsg(_(e_invexpr2), *arg);
3485 return FAIL;
3486 }
3487 *arg += len;
3488 if (evaluate)
3489 {
3490 rettv->v_type = VAR_NUMBER;
3491 rettv->vval.v_number = n;
3492 }
3493 }
3494 return OK;
3495}
3496
3497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * Allocate a variable for a string constant.
3499 * Return OK or FAIL.
3500 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003502get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 char_u *p;
3505 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 int extra = 0;
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003507 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509 /*
3510 * Find the end of the string, skipping backslashed characters.
3511 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003512 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 if (*p == '\\' && p[1] != NUL)
3515 {
3516 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003517 // A "\<x>" form occupies at least 4 characters, and produces up
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003518 // to 9 characters (6 for the char and 3 for a modifier): reserve
3519 // space for 5 extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (*p == '<')
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003521 extra += 5;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
3525 if (*p != '"')
3526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003527 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 return FAIL;
3529 }
3530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003531 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (!evaluate)
3533 {
3534 *arg = p + 1;
3535 return OK;
3536 }
3537
3538 /*
3539 * Copy the string into allocated memory, handling backslashed
3540 * characters.
3541 */
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003542 len = (int)(p - *arg + extra);
3543 name = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (name == NULL)
3545 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003546 rettv->v_type = VAR_STRING;
3547 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar8c711452005-01-14 21:53:12 +00003549 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 if (*p == '\\')
3552 {
3553 switch (*++p)
3554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003555 case 'b': *name++ = BS; ++p; break;
3556 case 'e': *name++ = ESC; ++p; break;
3557 case 'f': *name++ = FF; ++p; break;
3558 case 'n': *name++ = NL; ++p; break;
3559 case 'r': *name++ = CAR; ++p; break;
3560 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003562 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003564 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 case 'U':
3566 if (vim_isxdigit(p[1]))
3567 {
3568 int n, nr;
3569 int c = toupper(*p);
3570
3571 if (c == 'X')
3572 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003573 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003575 else
3576 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 nr = 0;
3578 while (--n >= 0 && vim_isxdigit(p[1]))
3579 {
3580 ++p;
3581 nr = (nr << 4) + hex2nr(*p);
3582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // For "\u" store the number according to
3585 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 break;
3592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003593 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 case '0':
3595 case '1':
3596 case '2':
3597 case '3':
3598 case '4':
3599 case '5':
3600 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 case '7': *name = *p++ - '0';
3602 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 *name = (*name << 3) + *p++ - '0';
3605 if (*p >= '0' && *p <= '7')
3606 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 break;
3610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003611 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003612 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3613 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (extra != 0)
3615 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 name += extra;
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003617 if (name >= rettv->vval.v_string + len)
3618 iemsg("get_string_tv() used more space than allocated");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 break;
3620 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003621 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 break;
3625 }
3626 }
3627 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003632 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003633 ++p;
3634 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 return OK;
3637}
3638
3639/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 * Return OK or FAIL.
3642 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003647 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003648 int reduce = 0;
3649
3650 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003652 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003653 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003658 break;
3659 ++reduce;
3660 ++p;
3661 }
3662 }
3663
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003666 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003667 return FAIL;
3668 }
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003671 if (!evaluate)
3672 {
3673 *arg = p + 1;
3674 return OK;
3675 }
3676
3677 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003679 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003680 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003681 if (str == NULL)
3682 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003683 rettv->v_type = VAR_STRING;
3684 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003685
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003691 break;
3692 ++p;
3693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003696 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003697 *arg = p + 1;
3698
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003699 return OK;
3700}
3701
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003702/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003703 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003704 */
3705 char_u *
3706partial_name(partial_T *pt)
3707{
3708 if (pt->pt_name != NULL)
3709 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003710 if (pt->pt_func != NULL)
3711 return pt->pt_func->uf_name;
3712 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003713}
3714
Bram Moolenaarddecc252016-04-06 22:59:37 +02003715 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003716partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003717{
3718 int i;
3719
3720 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003721 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003722 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003723 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003724 if (pt->pt_name != NULL)
3725 {
3726 func_unref(pt->pt_name);
3727 vim_free(pt->pt_name);
3728 }
3729 else
3730 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003731
3732 if (pt->pt_funcstack != NULL)
3733 {
3734 // Decrease the reference count for the context of a closure. If down
3735 // to zero free it and clear the variables on the stack.
3736 if (--pt->pt_funcstack->fs_refcount == 0)
3737 {
3738 garray_T *gap = &pt->pt_funcstack->fs_ga;
3739 typval_T *stack = gap->ga_data;
3740
3741 for (i = 0; i < gap->ga_len; ++i)
3742 clear_tv(stack + i);
3743 ga_clear(gap);
3744 vim_free(pt->pt_funcstack);
3745 }
3746 pt->pt_funcstack = NULL;
3747 }
3748
Bram Moolenaarddecc252016-04-06 22:59:37 +02003749 vim_free(pt);
3750}
3751
3752/*
3753 * Unreference a closure: decrement the reference count and free it when it
3754 * becomes zero.
3755 */
3756 void
3757partial_unref(partial_T *pt)
3758{
3759 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003760 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003761}
3762
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003763static int tv_equal_recurse_limit;
3764
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003765 static int
3766func_equal(
3767 typval_T *tv1,
3768 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003770{
3771 char_u *s1, *s2;
3772 dict_T *d1, *d2;
3773 int a1, a2;
3774 int i;
3775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003776 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003777 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003778 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003779 if (s1 != NULL && *s1 == NUL)
3780 s1 = NULL;
3781 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003782 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003783 if (s2 != NULL && *s2 == NUL)
3784 s2 = NULL;
3785 if (s1 == NULL || s2 == NULL)
3786 {
3787 if (s1 != s2)
3788 return FALSE;
3789 }
3790 else if (STRCMP(s1, s2) != 0)
3791 return FALSE;
3792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003794 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3795 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3796 if (d1 == NULL || d2 == NULL)
3797 {
3798 if (d1 != d2)
3799 return FALSE;
3800 }
3801 else if (!dict_equal(d1, d2, ic, TRUE))
3802 return FALSE;
3803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003804 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003805 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3806 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3807 if (a1 != a2)
3808 return FALSE;
3809 for (i = 0; i < a1; ++i)
3810 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3811 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3812 return FALSE;
3813
3814 return TRUE;
3815}
3816
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003817/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003818 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003819 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003820 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003821 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003823tv_equal(
3824 typval_T *tv1,
3825 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003826 int ic, // ignore case
3827 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003828{
3829 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003832 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 // Catch lists and dicts that have an endless loop by limiting
3835 // recursiveness to a limit. We guess they are equal then.
3836 // A fixed limit has the problem of still taking an awful long time.
3837 // Reduce the limit every time running into it. That should work fine for
3838 // deeply linked structures that are not recursively linked and catch
3839 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003840 if (!recursive)
3841 tv_equal_recurse_limit = 1000;
3842 if (recursive_cnt >= tv_equal_recurse_limit)
3843 {
3844 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003845 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003846 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003848 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3849 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003850 if ((tv1->v_type == VAR_FUNC
3851 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3852 && (tv2->v_type == VAR_FUNC
3853 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3854 {
3855 ++recursive_cnt;
3856 r = func_equal(tv1, tv2, ic);
3857 --recursive_cnt;
3858 return r;
3859 }
3860
3861 if (tv1->v_type != tv2->v_type)
3862 return FALSE;
3863
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003864 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003865 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003866 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003867 ++recursive_cnt;
3868 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3869 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003870 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003871
3872 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003873 ++recursive_cnt;
3874 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3875 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003876 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003877
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003878 case VAR_BLOB:
3879 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3880
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003881 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 case VAR_BOOL:
3883 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003884 return tv1->vval.v_number == tv2->vval.v_number;
3885
3886 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 s1 = tv_get_string_buf(tv1, buf1);
3888 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003889 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003890
Bram Moolenaar835dc632016-02-07 14:27:38 +01003891 case VAR_FLOAT:
3892#ifdef FEAT_FLOAT
3893 return tv1->vval.v_float == tv2->vval.v_float;
3894#endif
3895 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003897 return tv1->vval.v_job == tv2->vval.v_job;
3898#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003899 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003901 return tv1->vval.v_channel == tv2->vval.v_channel;
3902#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003904 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003905 return tv1->vval.v_partial == tv2->vval.v_partial;
3906
3907 case VAR_FUNC:
3908 return tv1->vval.v_string == tv2->vval.v_string;
3909
Bram Moolenaar835dc632016-02-07 14:27:38 +01003910 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003911 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003913 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003914 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003916 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3917 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003918 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003919}
3920
3921/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003922 * Return the next (unique) copy ID.
3923 * Used for serializing nested structures.
3924 */
3925 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003926get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003927{
3928 current_copyID += COPYID_INC;
3929 return current_copyID;
3930}
3931
3932/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003933 * Garbage collection for lists and dictionaries.
3934 *
3935 * We use reference counts to be able to free most items right away when they
3936 * are no longer used. But for composite items it's possible that it becomes
3937 * unused while the reference count is > 0: When there is a recursive
3938 * reference. Example:
3939 * :let l = [1, 2, 3]
3940 * :let d = {9: l}
3941 * :let l[1] = d
3942 *
3943 * Since this is quite unusual we handle this with garbage collection: every
3944 * once in a while find out which lists and dicts are not referenced from any
3945 * variable.
3946 *
3947 * Here is a good reference text about garbage collection (refers to Python
3948 * but it applies to all reference-counting mechanisms):
3949 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003950 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003951
3952/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003953 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003954 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003956 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003958garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003959{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003960 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003961 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003962 buf_T *buf;
3963 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003964 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003965 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003966
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003967 if (!testing)
3968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003969 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003970 want_garbage_collect = FALSE;
3971 may_garbage_collect = FALSE;
3972 garbage_collect_at_exit = FALSE;
3973 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003974
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003975 // The execution stack can grow big, limit the size.
3976 if (exestack.ga_maxlen - exestack.ga_len > 500)
3977 {
3978 size_t new_len;
3979 char_u *pp;
3980 int n;
3981
3982 // Keep 150% of the current size, with a minimum of the growth size.
3983 n = exestack.ga_len / 2;
3984 if (n < exestack.ga_growsize)
3985 n = exestack.ga_growsize;
3986
3987 // Don't make it bigger though.
3988 if (exestack.ga_len + n < exestack.ga_maxlen)
3989 {
3990 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3991 pp = vim_realloc(exestack.ga_data, new_len);
3992 if (pp == NULL)
3993 return FAIL;
3994 exestack.ga_maxlen = exestack.ga_len + n;
3995 exestack.ga_data = pp;
3996 }
3997 }
3998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // We advance by two because we add one for items referenced through
4000 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004001 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004003 /*
4004 * 1. Go through all accessible variables and mark all lists and dicts
4005 * with copyID.
4006 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 // Don't free variables in the previous_funccal list unless they are only
4009 // referenced through previous_funccal. This must be first, because if
4010 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004013 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004014 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004016 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004017 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4019 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004021 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004022 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004023 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4024 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004025 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004026 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4027 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004028#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004029 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004030 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4031 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004032 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004033 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004034 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4035 NULL, NULL);
4036#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004038 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004039 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004040 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4041 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004042 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004043 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004049 abort = abort || set_ref_in_functions(copyID);
4050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004051 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004055 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004056
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004057 // callbacks in buffers
4058 abort = abort || set_ref_in_buffers(copyID);
4059
Bram Moolenaar1dced572012-04-05 16:54:08 +02004060#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004062#endif
4063
Bram Moolenaardb913952012-06-29 12:54:53 +02004064#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004065 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004066#endif
4067
4068#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004069 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004070#endif
4071
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004073 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004074 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004075#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004076#ifdef FEAT_NETBEANS_INTG
4077 abort = abort || set_ref_in_nb_channel(copyID);
4078#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004079
Bram Moolenaare3188e22016-05-31 21:13:04 +02004080#ifdef FEAT_TIMERS
4081 abort = abort || set_ref_in_timer(copyID);
4082#endif
4083
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004084#ifdef FEAT_QUICKFIX
4085 abort = abort || set_ref_in_quickfix(copyID);
4086#endif
4087
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004088#ifdef FEAT_TERMINAL
4089 abort = abort || set_ref_in_term(copyID);
4090#endif
4091
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004092#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004093 abort = abort || set_ref_in_popups(copyID);
4094#endif
4095
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004096 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004097 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004098 /*
4099 * 2. Free lists and dictionaries that are not referenced.
4100 */
4101 did_free = free_unref_items(copyID);
4102
4103 /*
4104 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004106 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004108 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004109 else if (p_verbose > 0)
4110 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004111 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004112 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004113
4114 return did_free;
4115}
4116
4117/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004119 */
4120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004122{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004123 int did_free = FALSE;
4124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // Let all "free" functions know that we are here. This means no
4126 // dictionaries, lists, channels or jobs are to be freed, because we will
4127 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004128 in_free_unref_items = TRUE;
4129
4130 /*
4131 * PASS 1: free the contents of the items. We don't free the items
4132 * themselves yet, so that it is possible to decrement refcount counters
4133 */
4134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004135 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004136 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004139 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004140
4141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004142 // Go through the list of jobs and free items without the copyID. This
4143 // must happen before doing channels, because jobs refer to channels, but
4144 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004145 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004148 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4149#endif
4150
4151 /*
4152 * PASS 2: free the items themselves.
4153 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004154 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004155 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004156
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // Go through the list of jobs and free items without the copyID. This
4159 // must happen before doing channels, because jobs refer to channels, but
4160 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 free_unused_jobs(copyID, COPYID_MASK);
4162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004163 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 free_unused_channels(copyID, COPYID_MASK);
4165#endif
4166
4167 in_free_unref_items = FALSE;
4168
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004169 return did_free;
4170}
4171
4172/*
4173 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004174 * "list_stack" is used to add lists to be marked. Can be NULL.
4175 *
4176 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004177 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004178 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004179set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004180{
4181 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004182 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004183 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 hashtab_T *cur_ht;
4185 ht_stack_T *ht_stack = NULL;
4186 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004187
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004188 cur_ht = ht;
4189 for (;;)
4190 {
4191 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004193 // Mark each item in the hashtab. If the item contains a hashtab
4194 // it is added to ht_stack, if it contains a list it is added to
4195 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004196 todo = (int)cur_ht->ht_used;
4197 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4198 if (!HASHITEM_EMPTY(hi))
4199 {
4200 --todo;
4201 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4202 &ht_stack, list_stack);
4203 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004204 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205
4206 if (ht_stack == NULL)
4207 break;
4208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004209 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004210 cur_ht = ht_stack->ht;
4211 tempitem = ht_stack;
4212 ht_stack = ht_stack->prev;
4213 free(tempitem);
4214 }
4215
4216 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004217}
4218
4219/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004220 * Mark a dict and its items with "copyID".
4221 * Returns TRUE if setting references failed somehow.
4222 */
4223 int
4224set_ref_in_dict(dict_T *d, int copyID)
4225{
4226 if (d != NULL && d->dv_copyID != copyID)
4227 {
4228 d->dv_copyID = copyID;
4229 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4230 }
4231 return FALSE;
4232}
4233
4234/*
4235 * Mark a list and its items with "copyID".
4236 * Returns TRUE if setting references failed somehow.
4237 */
4238 int
4239set_ref_in_list(list_T *ll, int copyID)
4240{
4241 if (ll != NULL && ll->lv_copyID != copyID)
4242 {
4243 ll->lv_copyID = copyID;
4244 return set_ref_in_list_items(ll, copyID, NULL);
4245 }
4246 return FALSE;
4247}
4248
4249/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004250 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4252 *
4253 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004254 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004255 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004256set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004257{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004258 listitem_T *li;
4259 int abort = FALSE;
4260 list_T *cur_l;
4261 list_stack_T *list_stack = NULL;
4262 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004263
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004264 cur_l = l;
4265 for (;;)
4266 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004267 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004268 // Mark each item in the list. If the item contains a hashtab
4269 // it is added to ht_stack, if it contains a list it is added to
4270 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4272 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4273 ht_stack, &list_stack);
4274 if (list_stack == NULL)
4275 break;
4276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004277 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278 cur_l = list_stack->list;
4279 tempitem = list_stack;
4280 list_stack = list_stack->prev;
4281 free(tempitem);
4282 }
4283
4284 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004285}
4286
4287/*
4288 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004289 * "list_stack" is used to add lists to be marked. Can be NULL.
4290 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4291 *
4292 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004293 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004295set_ref_in_item(
4296 typval_T *tv,
4297 int copyID,
4298 ht_stack_T **ht_stack,
4299 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004300{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004301 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004302
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004303 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004304 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 dict_T *dd = tv->vval.v_dict;
4306
Bram Moolenaara03f2332016-02-06 18:09:59 +01004307 if (dd != NULL && dd->dv_copyID != copyID)
4308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004309 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004310 dd->dv_copyID = copyID;
4311 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004312 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004313 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4314 }
4315 else
4316 {
4317 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4318 if (newitem == NULL)
4319 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 else
4321 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004322 newitem->ht = &dd->dv_hashtab;
4323 newitem->prev = *ht_stack;
4324 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004325 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004326 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004327 }
4328 }
4329 else if (tv->v_type == VAR_LIST)
4330 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004331 list_T *ll = tv->vval.v_list;
4332
Bram Moolenaara03f2332016-02-06 18:09:59 +01004333 if (ll != NULL && ll->lv_copyID != copyID)
4334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004335 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004336 ll->lv_copyID = copyID;
4337 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004338 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004339 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004340 }
4341 else
4342 {
4343 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004344 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004345 if (newitem == NULL)
4346 abort = TRUE;
4347 else
4348 {
4349 newitem->list = ll;
4350 newitem->prev = *list_stack;
4351 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004352 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004353 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004354 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004355 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004356 else if (tv->v_type == VAR_FUNC)
4357 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004358 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004359 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004360 else if (tv->v_type == VAR_PARTIAL)
4361 {
4362 partial_T *pt = tv->vval.v_partial;
4363 int i;
4364
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004365 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004367 // Didn't see this partial yet.
4368 pt->pt_copyID = copyID;
4369
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004370 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004371
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004372 if (pt->pt_dict != NULL)
4373 {
4374 typval_T dtv;
4375
4376 dtv.v_type = VAR_DICT;
4377 dtv.vval.v_dict = pt->pt_dict;
4378 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4379 }
4380
4381 for (i = 0; i < pt->pt_argc; ++i)
4382 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4383 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004384 if (pt->pt_funcstack != NULL)
4385 {
4386 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4387
4388 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4389 abort = abort || set_ref_in_item(stack + i, copyID,
4390 ht_stack, list_stack);
4391 }
4392
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004393 }
4394 }
4395#ifdef FEAT_JOB_CHANNEL
4396 else if (tv->v_type == VAR_JOB)
4397 {
4398 job_T *job = tv->vval.v_job;
4399 typval_T dtv;
4400
4401 if (job != NULL && job->jv_copyID != copyID)
4402 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004403 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004404 if (job->jv_channel != NULL)
4405 {
4406 dtv.v_type = VAR_CHANNEL;
4407 dtv.vval.v_channel = job->jv_channel;
4408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4409 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004410 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004411 {
4412 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004413 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004414 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4415 }
4416 }
4417 }
4418 else if (tv->v_type == VAR_CHANNEL)
4419 {
4420 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004421 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004422 typval_T dtv;
4423 jsonq_T *jq;
4424 cbq_T *cq;
4425
4426 if (ch != NULL && ch->ch_copyID != copyID)
4427 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004428 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004429 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 {
4431 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4432 jq = jq->jq_next)
4433 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4434 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4435 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004436 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 {
4438 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004439 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4441 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004442 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004443 {
4444 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004445 dtv.vval.v_partial =
4446 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4448 }
4449 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004450 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004451 {
4452 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004453 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004454 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4455 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004456 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004457 {
4458 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004459 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004460 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4461 }
4462 }
4463 }
4464#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004466}
4467
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469 * Return a string with the string representation of a variable.
4470 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004472 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004473 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4474 * stings as "string()", otherwise does not put quotes around strings, as
4475 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004476 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4477 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004478 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004479 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004480 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004481echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004482 typval_T *tv,
4483 char_u **tofree,
4484 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004485 int copyID,
4486 int echo_style,
4487 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004488 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004489{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004490 static int recurse = 0;
4491 char_u *r = NULL;
4492
Bram Moolenaar33570922005-01-25 22:26:29 +00004493 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004494 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004495 if (!did_echo_string_emsg)
4496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Only give this message once for a recursive call to avoid
4498 // flooding the user with errors. And stop iterating over lists
4499 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004500 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004501 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004502 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004503 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004504 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004505 }
4506 ++recurse;
4507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 switch (tv->v_type)
4509 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004510 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004511 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004512 {
4513 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004514 r = tv->vval.v_string;
4515 if (r == NULL)
4516 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004517 }
4518 else
4519 {
4520 *tofree = string_quote(tv->vval.v_string, FALSE);
4521 r = *tofree;
4522 }
4523 break;
4524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004526 if (echo_style)
4527 {
4528 *tofree = NULL;
4529 r = tv->vval.v_string;
4530 }
4531 else
4532 {
4533 *tofree = string_quote(tv->vval.v_string, TRUE);
4534 r = *tofree;
4535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004536 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004537
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004538 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004539 {
4540 partial_T *pt = tv->vval.v_partial;
4541 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004542 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004543 garray_T ga;
4544 int i;
4545 char_u *tf;
4546
4547 ga_init2(&ga, 1, 100);
4548 ga_concat(&ga, (char_u *)"function(");
4549 if (fname != NULL)
4550 {
4551 ga_concat(&ga, fname);
4552 vim_free(fname);
4553 }
4554 if (pt != NULL && pt->pt_argc > 0)
4555 {
4556 ga_concat(&ga, (char_u *)", [");
4557 for (i = 0; i < pt->pt_argc; ++i)
4558 {
4559 if (i > 0)
4560 ga_concat(&ga, (char_u *)", ");
4561 ga_concat(&ga,
4562 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4563 vim_free(tf);
4564 }
4565 ga_concat(&ga, (char_u *)"]");
4566 }
4567 if (pt != NULL && pt->pt_dict != NULL)
4568 {
4569 typval_T dtv;
4570
4571 ga_concat(&ga, (char_u *)", ");
4572 dtv.v_type = VAR_DICT;
4573 dtv.vval.v_dict = pt->pt_dict;
4574 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4575 vim_free(tf);
4576 }
4577 ga_concat(&ga, (char_u *)")");
4578
4579 *tofree = ga.ga_data;
4580 r = *tofree;
4581 break;
4582 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004583
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004584 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004585 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004586 break;
4587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004589 if (tv->vval.v_list == NULL)
4590 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004591 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004592 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004593 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004594 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004595 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4596 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004597 {
4598 *tofree = NULL;
4599 r = (char_u *)"[...]";
4600 }
4601 else
4602 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004603 int old_copyID = tv->vval.v_list->lv_copyID;
4604
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004605 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004606 *tofree = list2string(tv, copyID, restore_copyID);
4607 if (restore_copyID)
4608 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004609 r = *tofree;
4610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004611 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004612
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004614 if (tv->vval.v_dict == NULL)
4615 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004616 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004617 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004618 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004619 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004620 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4621 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004622 {
4623 *tofree = NULL;
4624 r = (char_u *)"{...}";
4625 }
4626 else
4627 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004628 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004629
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004630 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004631 *tofree = dict2string(tv, copyID, restore_copyID);
4632 if (restore_copyID)
4633 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004634 r = *tofree;
4635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004639 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004640 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004642 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004644 break;
4645
Bram Moolenaar835dc632016-02-07 14:27:38 +01004646 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004647 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004648 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004649 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004650 if (composite_val)
4651 {
4652 *tofree = string_quote(r, FALSE);
4653 r = *tofree;
4654 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004656
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004657 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004658#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659 *tofree = NULL;
4660 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4661 r = numbuf;
4662 break;
4663#endif
4664
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004665 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004666 case VAR_SPECIAL:
4667 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004668 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004669 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004671
Bram Moolenaar8502c702014-06-17 12:51:16 +02004672 if (--recurse == 0)
4673 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004674 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004675}
4676
4677/*
4678 * Return a string with the string representation of a variable.
4679 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4680 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004681 * Does not put quotes around strings, as ":echo" displays values.
4682 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4683 * May return NULL.
4684 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004686echo_string(
4687 typval_T *tv,
4688 char_u **tofree,
4689 char_u *numbuf,
4690 int copyID)
4691{
4692 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4693}
4694
4695/*
4696 * Return a string with the string representation of a variable.
4697 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4698 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004699 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004700 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004701 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004703tv2string(
4704 typval_T *tv,
4705 char_u **tofree,
4706 char_u *numbuf,
4707 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004708{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004709 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710}
4711
4712/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 * Return string "str" in ' quotes, doubling ' characters.
4714 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004715 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004716 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004717 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004718string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004719{
Bram Moolenaar33570922005-01-25 22:26:29 +00004720 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004721 char_u *p, *r, *s;
4722
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 len = (function ? 13 : 3);
4724 if (str != NULL)
4725 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004726 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004727 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004728 if (*p == '\'')
4729 ++len;
4730 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004731 s = r = alloc(len);
4732 if (r != NULL)
4733 {
4734 if (function)
4735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004737 r += 10;
4738 }
4739 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004741 if (str != NULL)
4742 for (p = str; *p != NUL; )
4743 {
4744 if (*p == '\'')
4745 *r++ = '\'';
4746 MB_COPY_CHAR(p, r);
4747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004749 if (function)
4750 *r++ = ')';
4751 *r++ = NUL;
4752 }
4753 return s;
4754}
4755
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004756#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757/*
4758 * Convert the string "text" to a floating point number.
4759 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4760 * this always uses a decimal point.
4761 * Returns the length of the text that was consumed.
4762 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004764string2float(
4765 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767{
4768 char *s = (char *)text;
4769 float_T f;
4770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004772 if (STRNICMP(text, "inf", 3) == 0)
4773 {
4774 *value = INFINITY;
4775 return 3;
4776 }
4777 if (STRNICMP(text, "-inf", 3) == 0)
4778 {
4779 *value = -INFINITY;
4780 return 4;
4781 }
4782 if (STRNICMP(text, "nan", 3) == 0)
4783 {
4784 *value = NAN;
4785 return 3;
4786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 f = strtod(s, &s);
4788 *value = f;
4789 return (int)((char_u *)s - text);
4790}
4791#endif
4792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 * Get the value of an environment variable.
4795 * "arg" is pointing to the '$'. It is advanced to after the name.
4796 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004797 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004800get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 char_u *string = NULL;
4803 int len;
4804 int cc;
4805 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004806 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 ++*arg;
4809 name = *arg;
4810 len = get_env_len(arg);
4811 if (evaluate)
4812 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004813 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004815
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004816 cc = name[len];
4817 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004819 string = vim_getenv(name, &mustfree);
4820 if (string != NULL && *string != NUL)
4821 {
4822 if (!mustfree)
4823 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004825 else
4826 {
4827 if (mustfree)
4828 vim_free(string);
4829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004830 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004831 string = expand_env_save(name - 1);
4832 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004833 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004834 }
4835 name[len] = cc;
4836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 rettv->v_type = VAR_STRING;
4838 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840
4841 return OK;
4842}
4843
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004846 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004849var2fpos(
4850 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004851 int dollar_lnum, // TRUE when $ is last line
4852 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004854 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004856 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004858 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004859 if (varp->v_type == VAR_LIST)
4860 {
4861 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004862 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004863 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004864 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004865
4866 l = varp->vval.v_list;
4867 if (l == NULL)
4868 return NULL;
4869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004870 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004871 pos.lnum = list_find_nr(l, 0L, &error);
4872 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004875 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004876 pos.col = list_find_nr(l, 1L, &error);
4877 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004878 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004879 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004882 li = list_find(l, 1L);
4883 if (li != NULL && li->li_tv.v_type == VAR_STRING
4884 && li->li_tv.vval.v_string != NULL
4885 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4886 pos.col = len + 1;
4887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004888 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004889 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004890 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004891 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004893 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004894 pos.coladd = list_find_nr(l, 2L, &error);
4895 if (error)
4896 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004897
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004898 return &pos;
4899 }
4900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004902 if (name == NULL)
4903 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004907 {
4908 if (VIsual_active)
4909 return &VIsual;
4910 return &curwin->w_cursor;
4911 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004914 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4916 return NULL;
4917 return pp;
4918 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004919
Bram Moolenaara5525202006-03-02 22:52:09 +00004920 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004921
Bram Moolenaar477933c2007-07-17 14:32:23 +00004922 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004923 {
4924 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004926 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004927 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 // In silent Ex mode topline is zero, but that's not a valid line
4929 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004930 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004931 return &pos;
4932 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004934 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004935 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004937 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004938 return &pos;
4939 }
4940 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004943 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
4945 pos.lnum = curbuf->b_ml.ml_line_count;
4946 pos.col = 0;
4947 }
4948 else
4949 {
4950 pos.lnum = curwin->w_cursor.lnum;
4951 pos.col = (colnr_T)STRLEN(ml_get_curline());
4952 }
4953 return &pos;
4954 }
4955 return NULL;
4956}
4957
4958/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004959 * Convert list in "arg" into a position and optional file number.
4960 * When "fnump" is NULL there is no file number, only 3 items.
4961 * Note that the column is passed on as-is, the caller may want to decrement
4962 * it to use 1 for the first column.
4963 * Return FAIL when conversion is not possible, doesn't check the position for
4964 * validity.
4965 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004967list2fpos(
4968 typval_T *arg,
4969 pos_T *posp,
4970 int *fnump,
4971 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004972{
4973 list_T *l = arg->vval.v_list;
4974 long i = 0;
4975 long n;
4976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4978 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004979 if (arg->v_type != VAR_LIST
4980 || l == NULL
4981 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004982 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004983 return FAIL;
4984
4985 if (fnump != NULL)
4986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004987 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004988 if (n < 0)
4989 return FAIL;
4990 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004992 *fnump = n;
4993 }
4994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004996 if (n < 0)
4997 return FAIL;
4998 posp->lnum = n;
4999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005001 if (n < 0)
5002 return FAIL;
5003 posp->col = n;
5004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005006 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005007 posp->coladd = 0;
5008 else
5009 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005010
Bram Moolenaar493c1782014-05-28 14:34:46 +02005011 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005013
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005014 return OK;
5015}
5016
5017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * Get the length of an environment variable name.
5019 * Advance "arg" to the first character after the name.
5020 * Return 0 for error.
5021 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 char_u *p;
5026 int len;
5027
5028 for (p = *arg; vim_isIDc(*p); ++p)
5029 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005030 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 return 0;
5032
5033 len = (int)(p - *arg);
5034 *arg = p;
5035 return len;
5036}
5037
5038/*
5039 * Get the length of the name of a function or internal variable.
5040 * "arg" is advanced to the first non-white character after the name.
5041 * Return 0 if something is wrong.
5042 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005044get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 char_u *p;
5047 int len;
5048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005051 {
5052 if (*p == ':')
5053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // "s:" is start of "s:var", but "n:" is not and can be used in
5055 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005056 len = (int)(p - *arg);
5057 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5058 || len > 1)
5059 break;
5060 }
5061 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005062 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 return 0;
5064
5065 len = (int)(p - *arg);
5066 *arg = skipwhite(p);
5067
5068 return len;
5069}
5070
5071/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005072 * Get the length of the name of a variable or function.
5073 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005075 * Return -1 if curly braces expansion failed.
5076 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 * If the name contains 'magic' {}'s, expand them and return the
5078 * expanded name in an allocated string via 'alias' - caller must free.
5079 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081get_name_len(
5082 char_u **arg,
5083 char_u **alias,
5084 int evaluate,
5085 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 char_u *p;
5089 char_u *expr_start;
5090 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005092 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
5094 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5095 && (*arg)[2] == (int)KE_SNR)
5096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *arg += 3;
5099 return get_id_len(arg) + 3;
5100 }
5101 len = eval_fname_script(*arg);
5102 if (len > 0)
5103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 *arg += len;
5106 }
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005111 p = find_name_end(*arg, &expr_start, &expr_end,
5112 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (expr_start != NULL)
5114 {
5115 char_u *temp_string;
5116
5117 if (!evaluate)
5118 {
5119 len += (int)(p - *arg);
5120 *arg = skipwhite(p);
5121 return len;
5122 }
5123
5124 /*
5125 * Include any <SID> etc in the expanded string:
5126 * Thus the -len here.
5127 */
5128 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5129 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005130 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 *alias = temp_string;
5132 *arg = skipwhite(p);
5133 return (int)STRLEN(temp_string);
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005137 // Only give an error when there is something, otherwise it will be
5138 // reported at a higher level.
5139 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005140 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 return len;
5143}
5144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145/*
5146 * Find the end of a variable or function name, taking care of magic braces.
5147 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5148 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005149 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 * Return a pointer to just after the name. Equal to "arg" if there is no
5151 * valid name.
5152 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005153 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005154find_name_end(
5155 char_u *arg,
5156 char_u **expr_start,
5157 char_u **expr_end,
5158 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 int mb_nest = 0;
5161 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005163 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005164 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 *expr_start = NULL;
5169 *expr_end = NULL;
5170 }
5171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005173 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5174 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005175 return arg;
5176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 for (p = arg; *p != NUL
5178 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005179 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005180 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005182 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005184 if (*p == '\'')
5185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005186 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005187 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005188 ;
5189 if (*p == NUL)
5190 break;
5191 }
5192 else if (*p == '"')
5193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005194 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005195 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005196 if (*p == '\\' && p[1] != NUL)
5197 ++p;
5198 if (*p == NUL)
5199 break;
5200 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005201 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005203 // "s:" is start of "s:var", but "n:" is not and can be used in
5204 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005205 len = (int)(p - arg);
5206 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005207 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005208 break;
5209 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 if (*p == '[')
5214 ++br_nest;
5215 else if (*p == ']')
5216 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005218
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005219 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 if (*p == '{')
5222 {
5223 mb_nest++;
5224 if (expr_start != NULL && *expr_start == NULL)
5225 *expr_start = p;
5226 }
5227 else if (*p == '}')
5228 {
5229 mb_nest--;
5230 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5231 *expr_end = p;
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235
5236 return p;
5237}
5238
5239/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 * Expands out the 'magic' {}'s in a variable/function name.
5241 * Note that this can call itself recursively, to deal with
5242 * constructs like foo{bar}{baz}{bam}
5243 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5244 * "in_start" ^
5245 * "expr_start" ^
5246 * "expr_end" ^
5247 * "in_end" ^
5248 *
5249 * Returns a new allocated string, which the caller must free.
5250 * Returns NULL for failure.
5251 */
5252 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253make_expanded_name(
5254 char_u *in_start,
5255 char_u *expr_start,
5256 char_u *expr_end,
5257 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258{
5259 char_u c1;
5260 char_u *retval = NULL;
5261 char_u *temp_result;
5262 char_u *nextcmd = NULL;
5263
5264 if (expr_end == NULL || in_end == NULL)
5265 return NULL;
5266 *expr_start = NUL;
5267 *expr_end = NUL;
5268 c1 = *in_end;
5269 *in_end = NUL;
5270
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005271 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 if (temp_result != NULL && nextcmd == NULL)
5273 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005274 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5275 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (retval != NULL)
5277 {
5278 STRCPY(retval, in_start);
5279 STRCAT(retval, temp_result);
5280 STRCAT(retval, expr_end + 1);
5281 }
5282 }
5283 vim_free(temp_result);
5284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 *expr_start = '{';
5287 *expr_end = '}';
5288
5289 if (retval != NULL)
5290 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005291 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292 if (expr_start != NULL)
5293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005295 temp_result = make_expanded_name(retval, expr_start,
5296 expr_end, temp_result);
5297 vim_free(retval);
5298 retval = temp_result;
5299 }
5300 }
5301
5302 return retval;
5303}
5304
5305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005307 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005312 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5313}
5314
5315/*
5316 * Return TRUE if character "c" can be used as the first character in a
5317 * variable or function name (excluding '{' and '}').
5318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005320eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005321{
5322 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323}
5324
5325/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005326 * Handle:
5327 * - expr[expr], expr[expr:expr] subscript
5328 * - ".name" lookup
5329 * - function call with Funcref variable: func(expr)
5330 * - method call: var->method()
5331 *
5332 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005335handle_subscript(
5336 char_u **arg,
5337 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02005338 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005339 int verbose, // give error messages
5340 char_u *start_leader, // start of '!' and '-' prefixes
5341 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005342{
Bram Moolenaar32e35112020-05-14 22:41:15 +02005343 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 int ret = OK;
5345 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346
Bram Moolenaar61343f02019-07-20 21:11:13 +02005347 // "." is ".name" lookup when we found a dict or when evaluating and
5348 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005349 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005350 && (((**arg == '['
5351 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005352 || (!evaluate
5353 && (*arg)[1] != '.'
5354 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005355 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005356 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005357 && !VIM_ISWHITE(*(*arg - 1)))
5358 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 {
5360 if (**arg == '(')
5361 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005362 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005363
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005364 // Stop the expression evaluation when immediately aborting on
5365 // error, or when an interrupt occurred or an exception was thrown
5366 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 if (aborting())
5368 {
5369 if (ret == OK)
5370 clear_tv(rettv);
5371 ret = FAIL;
5372 }
5373 dict_unref(selfdict);
5374 selfdict = NULL;
5375 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005376 else if (**arg == '-')
5377 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005378 // Expression "-1.0->method()" applies the leader "-" before
5379 // applying ->.
5380 if (evaluate && *end_leaderp > start_leader)
5381 ret = eval7_leader(rettv, start_leader, end_leaderp);
5382 if (ret == OK)
5383 {
5384 if ((*arg)[2] == '{')
5385 // expr->{lambda}()
5386 ret = eval_lambda(arg, rettv, evaluate, verbose);
5387 else
5388 // expr->name()
5389 ret = eval_method(arg, rettv, evaluate, verbose);
5390 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005393 {
5394 dict_unref(selfdict);
5395 if (rettv->v_type == VAR_DICT)
5396 {
5397 selfdict = rettv->vval.v_dict;
5398 if (selfdict != NULL)
5399 ++selfdict->dv_refcount;
5400 }
5401 else
5402 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02005403 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005404 {
5405 clear_tv(rettv);
5406 ret = FAIL;
5407 }
5408 }
5409 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005411 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5412 // Don't do this when "Func" is already a partial that was bound
5413 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005414 if (selfdict != NULL
5415 && (rettv->v_type == VAR_FUNC
5416 || (rettv->v_type == VAR_PARTIAL
5417 && (rettv->vval.v_partial->pt_auto
5418 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005419 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005421 dict_unref(selfdict);
5422 return ret;
5423}
5424
5425/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005426 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 * value).
5428 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005429 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005430alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005432 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433}
5434
5435/*
5436 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 * The string "s" must have been allocated, it is consumed.
5438 * Return NULL for out of memory, the variable otherwise.
5439 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005440 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005441alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445 rettv = alloc_tv();
5446 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 rettv->v_type = VAR_STRING;
5449 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451 else
5452 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454}
5455
5456/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005460free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 if (varp != NULL)
5463 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 switch (varp->v_type)
5465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005467 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005468 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005469 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 vim_free(varp->vval.v_string);
5471 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005472 case VAR_PARTIAL:
5473 partial_unref(varp->vval.v_partial);
5474 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005475 case VAR_BLOB:
5476 blob_unref(varp->vval.v_blob);
5477 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 case VAR_LIST:
5479 list_unref(varp->vval.v_list);
5480 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005481 case VAR_DICT:
5482 dict_unref(varp->vval.v_dict);
5483 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005485#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 job_unref(varp->vval.v_job);
5487 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005488#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005489 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005491 channel_unref(varp->vval.v_channel);
5492 break;
5493#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 case VAR_NUMBER:
5495 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005496 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005497 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005499 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005500 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005501 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 vim_free(varp);
5504 }
5505}
5506
5507/*
5508 * Free the memory for a variable value and set the value to NULL or 0.
5509 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 if (varp != NULL)
5514 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005518 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005520 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005521 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005523 case VAR_PARTIAL:
5524 partial_unref(varp->vval.v_partial);
5525 varp->vval.v_partial = NULL;
5526 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005527 case VAR_BLOB:
5528 blob_unref(varp->vval.v_blob);
5529 varp->vval.v_blob = NULL;
5530 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 case VAR_LIST:
5532 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005533 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 case VAR_DICT:
5536 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005537 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005538 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005540 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005541 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 varp->vval.v_number = 0;
5543 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005544 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005546 varp->vval.v_float = 0.0;
5547 break;
5548#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005549 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005551 job_unref(varp->vval.v_job);
5552 varp->vval.v_job = NULL;
5553#endif
5554 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005555 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005557 channel_unref(varp->vval.v_channel);
5558 varp->vval.v_channel = NULL;
5559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005561 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005565 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567}
5568
5569/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 * Set the value of a variable to NULL without freeing items.
5571 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005573init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574{
5575 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005576 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577}
5578
5579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 * Get the number value of a variable.
5581 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005582 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005583 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005584 * caller of incompatible types: it sets *denote to TRUE if "denote"
5585 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005587 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005588tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590 int error = FALSE;
5591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005592 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005593}
5594
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005595 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005596tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005597{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005598 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005603 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005604 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005605#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005606 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005607 break;
5608#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005610 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005611 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 break;
5613 case VAR_STRING:
5614 if (varp->vval.v_string != NULL)
5615 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005616 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005617 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005619 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005620 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005621 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005622 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005623 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005624 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005625 case VAR_SPECIAL:
5626 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005627 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005628#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005629 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005630 break;
5631#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005632 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005634 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005635 break;
5636#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005637 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005638 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005639 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005640 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005641 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005643 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005646 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005647 n = -1;
5648 else
5649 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651}
5652
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005653#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005656{
5657 switch (varp->v_type)
5658 {
5659 case VAR_NUMBER:
5660 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005661 case VAR_FLOAT:
5662 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005663 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005664 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005666 break;
5667 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005668 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005669 break;
5670 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005671 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005672 break;
5673 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005674 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005675 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005676 case VAR_BOOL:
5677 emsg(_("E362: Using a boolean value as a Float"));
5678 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005679 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005680 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005681 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005682 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005683# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005684 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005685 break;
5686# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005687 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005688# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005689 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005690 break;
5691# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005692 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005693 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005694 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005695 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005696 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005698 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005699 break;
5700 }
5701 return 0;
5702}
5703#endif
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * Get the string value of a variable.
5707 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005708 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5709 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 * If the String variable has never been set, return an empty string.
5711 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005712 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005713 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005715 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005716tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 static char_u mybuf[NUMBUFLEN];
5719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005720 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721}
5722
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005723 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005724tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005726 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005727
5728 return res != NULL ? res : (char_u *)"";
5729}
5730
Bram Moolenaar7d647822014-04-05 21:28:56 +02005731/*
5732 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5733 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005734 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005735tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005736{
5737 static char_u mybuf[NUMBUFLEN];
5738
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005739 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005740}
5741
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005742 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005743tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005744{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005748 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005749 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 return buf;
5751 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005752 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005753 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 break;
5755 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005756 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 break;
5758 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005759 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005761 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005762#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005763 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005764 break;
5765#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 case VAR_STRING:
5767 if (varp->vval.v_string != NULL)
5768 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005769 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005770 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005771 case VAR_SPECIAL:
5772 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5773 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005774 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005775 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005776 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005777 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005778#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005779 {
5780 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005781 char *status;
5782
5783 if (job == NULL)
5784 return (char_u *)"no process";
5785 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005786 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005787 : "run";
5788# ifdef UNIX
5789 vim_snprintf((char *)buf, NUMBUFLEN,
5790 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005791# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005792 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005793 "process %ld %s",
5794 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005795 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005796# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005798 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5799# endif
5800 return buf;
5801 }
5802#endif
5803 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005804 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005805#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005806 {
5807 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005808 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005809
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005810 if (channel == NULL)
5811 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5812 else
5813 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005814 "channel %d %s", channel->ch_id, status);
5815 return buf;
5816 }
5817#endif
5818 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005819 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005820 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005822 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005825 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827
5828/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005829 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5830 * string() on Dict, List, etc.
5831 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005832 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005833tv_stringify(typval_T *varp, char_u *buf)
5834{
5835 if (varp->v_type == VAR_LIST
5836 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005837 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005838 || varp->v_type == VAR_FUNC
5839 || varp->v_type == VAR_PARTIAL
5840 || varp->v_type == VAR_FLOAT)
5841 {
5842 typval_T tmp;
5843
5844 f_string(varp, &tmp);
5845 tv_get_string_buf(&tmp, buf);
5846 clear_tv(varp);
5847 *varp = tmp;
5848 return tmp.vval.v_string;
5849 }
5850 return tv_get_string_buf(varp, buf);
5851}
5852
5853/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005854 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5855 * Also give an error message, using "name" or _("name") when use_gettext is
5856 * TRUE.
5857 */
5858 static int
5859tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5860{
5861 int lock = 0;
5862
5863 switch (tv->v_type)
5864 {
5865 case VAR_BLOB:
5866 if (tv->vval.v_blob != NULL)
5867 lock = tv->vval.v_blob->bv_lock;
5868 break;
5869 case VAR_LIST:
5870 if (tv->vval.v_list != NULL)
5871 lock = tv->vval.v_list->lv_lock;
5872 break;
5873 case VAR_DICT:
5874 if (tv->vval.v_dict != NULL)
5875 lock = tv->vval.v_dict->dv_lock;
5876 break;
5877 default:
5878 break;
5879 }
5880 return var_check_lock(tv->v_lock, name, use_gettext)
5881 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5882}
5883
5884/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005887 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005888 * It is OK for "from" and "to" to point to the same item. This is used to
5889 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005895 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 switch (from->v_type)
5897 {
5898 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005899 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005900 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 to->vval.v_number = from->vval.v_number;
5902 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005903 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005904#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005905 to->vval.v_float = from->vval.v_float;
5906 break;
5907#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005908 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005910 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005911 if (to->vval.v_job != NULL)
5912 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005913 break;
5914#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005915 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005917 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005918 if (to->vval.v_channel != NULL)
5919 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005920 break;
5921#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 case VAR_STRING:
5923 case VAR_FUNC:
5924 if (from->vval.v_string == NULL)
5925 to->vval.v_string = NULL;
5926 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005927 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005929 if (from->v_type == VAR_FUNC)
5930 func_ref(to->vval.v_string);
5931 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005933 case VAR_PARTIAL:
5934 if (from->vval.v_partial == NULL)
5935 to->vval.v_partial = NULL;
5936 else
5937 {
5938 to->vval.v_partial = from->vval.v_partial;
5939 ++to->vval.v_partial->pt_refcount;
5940 }
5941 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005942 case VAR_BLOB:
5943 if (from->vval.v_blob == NULL)
5944 to->vval.v_blob = NULL;
5945 else
5946 {
5947 to->vval.v_blob = from->vval.v_blob;
5948 ++to->vval.v_blob->bv_refcount;
5949 }
5950 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 case VAR_LIST:
5952 if (from->vval.v_list == NULL)
5953 to->vval.v_list = NULL;
5954 else
5955 {
5956 to->vval.v_list = from->vval.v_list;
5957 ++to->vval.v_list->lv_refcount;
5958 }
5959 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 case VAR_DICT:
5961 if (from->vval.v_dict == NULL)
5962 to->vval.v_dict = NULL;
5963 else
5964 {
5965 to->vval.v_dict = from->vval.v_dict;
5966 ++to->vval.v_dict->dv_refcount;
5967 }
5968 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005969 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005970 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005972 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 break;
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975}
5976
5977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005978 * Make a copy of an item.
5979 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5981 * reference to an already copied list/dict can be used.
5982 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005985item_copy(
5986 typval_T *from,
5987 typval_T *to,
5988 int deep,
5989 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990{
5991 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005996 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005998 }
5999 ++recurse;
6000
6001 switch (from->v_type)
6002 {
6003 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006004 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 case VAR_STRING:
6006 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006007 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006008 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006009 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006010 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006011 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012 copy_tv(from, to);
6013 break;
6014 case VAR_LIST:
6015 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006016 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006017 if (from->vval.v_list == NULL)
6018 to->vval.v_list = NULL;
6019 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006021 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022 to->vval.v_list = from->vval.v_list->lv_copylist;
6023 ++to->vval.v_list->lv_refcount;
6024 }
6025 else
6026 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6027 if (to->vval.v_list == NULL)
6028 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006030 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006032 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006033 case VAR_DICT:
6034 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006035 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006036 if (from->vval.v_dict == NULL)
6037 to->vval.v_dict = NULL;
6038 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006040 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6042 ++to->vval.v_dict->dv_refcount;
6043 }
6044 else
6045 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6046 if (to->vval.v_dict == NULL)
6047 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006049 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006050 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006052 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 }
6055 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057}
6058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 void
6060echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6061{
6062 char_u *tofree;
6063 char_u numbuf[NUMBUFLEN];
6064 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6065
6066 if (*atstart)
6067 {
6068 *atstart = FALSE;
6069 // Call msg_start() after eval1(), evaluating the expression
6070 // may cause a message to appear.
6071 if (with_space)
6072 {
6073 // Mark the saved text as finishing the line, so that what
6074 // follows is displayed on a new line when scrolling back
6075 // at the more prompt.
6076 msg_sb_eol();
6077 msg_start();
6078 }
6079 }
6080 else if (with_space)
6081 msg_puts_attr(" ", echo_attr);
6082
6083 if (p != NULL)
6084 for ( ; *p != NUL && !got_int; ++p)
6085 {
6086 if (*p == '\n' || *p == '\r' || *p == TAB)
6087 {
6088 if (*p != TAB && *needclr)
6089 {
6090 // remove any text still there from the command
6091 msg_clr_eos();
6092 *needclr = FALSE;
6093 }
6094 msg_putchar_attr(*p, echo_attr);
6095 }
6096 else
6097 {
6098 if (has_mbyte)
6099 {
6100 int i = (*mb_ptr2len)(p);
6101
6102 (void)msg_outtrans_len_attr(p, i, echo_attr);
6103 p += i - 1;
6104 }
6105 else
6106 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6107 }
6108 }
6109 vim_free(tofree);
6110}
6111
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 * ":echo expr1 ..." print each argument separated with a space, add a
6114 * newline at the end.
6115 * ":echon expr1 ..." print each argument plain.
6116 */
6117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 char_u *p;
6123 int needclr = TRUE;
6124 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006125 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006126 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 if (eap->skip)
6129 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006130 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006132 // If eval1() causes an error message the text from the command may
6133 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006134 need_clr_eos = needclr;
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02006137 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 /*
6140 * Report the invalid expression unless the expression evaluation
6141 * has been cancelled due to an aborting error, an interrupt, or an
6142 * exception.
6143 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006144 if (!aborting() && did_emsg == did_emsg_before
6145 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006146 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006147 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 break;
6149 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006150 need_clr_eos = FALSE;
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 arg = skipwhite(arg);
6157 }
6158 eap->nextcmd = check_nextcmd(arg);
6159
6160 if (eap->skip)
6161 --emsg_skip;
6162 else
6163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006164 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (needclr)
6166 msg_clr_eos();
6167 if (eap->cmdidx == CMD_echo)
6168 msg_end();
6169 }
6170}
6171
6172/*
6173 * ":echohl {name}".
6174 */
6175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006178 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179}
6180
6181/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006182 * Returns the :echo attribute
6183 */
6184 int
6185get_echo_attr(void)
6186{
6187 return echo_attr;
6188}
6189
6190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 * ":execute expr1 ..." execute the result of an expression.
6192 * ":echomsg expr1 ..." Print a message
6193 * ":echoerr expr1 ..." Print an error
6194 * Each gets spaces around each argument and a newline at the end for
6195 * echo commands
6196 */
6197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 int ret = OK;
6203 char_u *p;
6204 garray_T ga;
6205 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 ga_init2(&ga, 1, 80);
6208
6209 if (eap->skip)
6210 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006211 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006213 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6214 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 if (!eap->skip)
6218 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006219 char_u buf[NUMBUFLEN];
6220
6221 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006222 {
6223 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6224 {
6225 emsg(_(e_inval_string));
6226 p = NULL;
6227 }
6228 else
6229 p = tv_get_string_buf(&rettv, buf);
6230 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006231 else
6232 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006233 if (p == NULL)
6234 {
6235 clear_tv(&rettv);
6236 ret = FAIL;
6237 break;
6238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 len = (int)STRLEN(p);
6240 if (ga_grow(&ga, len + 2) == FAIL)
6241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006242 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 ret = FAIL;
6244 break;
6245 }
6246 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 ga.ga_len += len;
6250 }
6251
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 arg = skipwhite(arg);
6254 }
6255
6256 if (ret != FAIL && ga.ga_data != NULL)
6257 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006258 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006260 // Mark the already saved text as finishing the line, so that what
6261 // follows is displayed on a new line when scrolling back at the
6262 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006263 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006264 }
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006267 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006268 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006269 out_flush();
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 else if (eap->cmdidx == CMD_echoerr)
6272 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006273 int save_did_emsg = did_emsg;
6274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006276 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (!force_abort)
6278 did_emsg = save_did_emsg;
6279 }
6280 else if (eap->cmdidx == CMD_execute)
6281 do_cmdline((char_u *)ga.ga_data,
6282 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6283 }
6284
6285 ga_clear(&ga);
6286
6287 if (eap->skip)
6288 --emsg_skip;
6289
6290 eap->nextcmd = check_nextcmd(arg);
6291}
6292
6293/*
6294 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6295 * "arg" points to the "&" or '+' when called, to "option" when returning.
6296 * Returns NULL when no option name found. Otherwise pointer to the char
6297 * after the option name.
6298 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 char_u *p = *arg;
6303
6304 ++p;
6305 if (*p == 'g' && p[1] == ':')
6306 {
6307 *opt_flags = OPT_GLOBAL;
6308 p += 2;
6309 }
6310 else if (*p == 'l' && p[1] == ':')
6311 {
6312 *opt_flags = OPT_LOCAL;
6313 p += 2;
6314 }
6315 else
6316 *opt_flags = 0;
6317
6318 if (!ASCII_ISALPHA(*p))
6319 return NULL;
6320 *arg = p;
6321
6322 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006323 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 else
6325 while (ASCII_ISALPHA(*p))
6326 ++p;
6327 return p;
6328}
6329
6330/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006331 * Display script name where an item was last set.
6332 * Should only be invoked when 'verbose' is non-zero.
6333 */
6334 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006335last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006336{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006337 char_u *p;
6338
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006339 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006340 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006341 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006342 if (p != NULL)
6343 {
6344 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006345 msg_puts(_("\n\tLast set from "));
6346 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006347 if (script_ctx.sc_lnum > 0)
6348 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006349 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006350 msg_outnum((long)script_ctx.sc_lnum);
6351 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006352 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006353 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006354 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006355 }
6356}
6357
Bram Moolenaar61be3762019-03-19 23:04:17 +01006358/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006359 * Compare "typ1" and "typ2". Put the result in "typ1".
6360 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006361 int
6362typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006363 typval_T *typ1, // first operand
6364 typval_T *typ2, // second operand
6365 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006366 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006367{
6368 int i;
6369 varnumber_T n1, n2;
6370 char_u *s1, *s2;
6371 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006372 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006373
Bram Moolenaar31988702018-02-13 12:57:42 +01006374 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006375 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006376 // For "is" a different type always means FALSE, for "notis"
6377 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006378 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006379 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006380 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6381 {
6382 if (type_is)
6383 {
6384 n1 = (typ1->v_type == typ2->v_type
6385 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006386 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006387 n1 = !n1;
6388 }
6389 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006390 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006391 {
6392 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006393 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006394 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006395 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006396 clear_tv(typ1);
6397 return FAIL;
6398 }
6399 else
6400 {
6401 // Compare two Blobs for being equal or unequal.
6402 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006403 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006404 n1 = !n1;
6405 }
6406 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006407 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6408 {
6409 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006410 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006411 n1 = (typ1->v_type == typ2->v_type
6412 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006413 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006414 n1 = !n1;
6415 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006416 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006417 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006418 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006419 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006420 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006421 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006422 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006423 clear_tv(typ1);
6424 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006425 }
6426 else
6427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006428 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006429 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6430 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006431 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006432 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006433 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006434 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006435
6436 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6437 {
6438 if (type_is)
6439 {
6440 n1 = (typ1->v_type == typ2->v_type
6441 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006442 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006443 n1 = !n1;
6444 }
6445 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006446 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006447 {
6448 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006449 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006450 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006451 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006452 clear_tv(typ1);
6453 return FAIL;
6454 }
6455 else
6456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006458 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6459 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006460 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006461 n1 = !n1;
6462 }
6463 }
6464
6465 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6466 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6467 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006468 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6469 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006471 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006472 clear_tv(typ1);
6473 return FAIL;
6474 }
6475 if ((typ1->v_type == VAR_PARTIAL
6476 && typ1->vval.v_partial == NULL)
6477 || (typ2->v_type == VAR_PARTIAL
6478 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006479 // When both partials are NULL, then they are equal.
6480 // Otherwise they are not equal.
6481 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006482 else if (type_is)
6483 {
6484 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006485 // strings are considered the same if their value is
6486 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006487 n1 = tv_equal(typ1, typ2, ic, FALSE);
6488 else if (typ1->v_type == VAR_PARTIAL
6489 && typ2->v_type == VAR_PARTIAL)
6490 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6491 else
6492 n1 = FALSE;
6493 }
6494 else
6495 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006496 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006497 n1 = !n1;
6498 }
6499
6500#ifdef FEAT_FLOAT
6501 /*
6502 * If one of the two variables is a float, compare as a float.
6503 * When using "=~" or "!~", always compare as string.
6504 */
6505 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006506 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006507 {
6508 float_T f1, f2;
6509
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006510 f1 = tv_get_float(typ1);
6511 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006512 n1 = FALSE;
6513 switch (type)
6514 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006515 case EXPR_IS:
6516 case EXPR_EQUAL: n1 = (f1 == f2); break;
6517 case EXPR_ISNOT:
6518 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6519 case EXPR_GREATER: n1 = (f1 > f2); break;
6520 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6521 case EXPR_SMALLER: n1 = (f1 < f2); break;
6522 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6523 case EXPR_UNKNOWN:
6524 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006526 }
6527 }
6528#endif
6529
6530 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006531 * If one of the two variables is a number, compare as a number.
6532 * When using "=~" or "!~", always compare as string.
6533 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006534 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006535 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006537 n1 = tv_get_number(typ1);
6538 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006539 switch (type)
6540 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006541 case EXPR_IS:
6542 case EXPR_EQUAL: n1 = (n1 == n2); break;
6543 case EXPR_ISNOT:
6544 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6545 case EXPR_GREATER: n1 = (n1 > n2); break;
6546 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6547 case EXPR_SMALLER: n1 = (n1 < n2); break;
6548 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6549 case EXPR_UNKNOWN:
6550 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006552 }
6553 }
6554 else
6555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006556 s1 = tv_get_string_buf(typ1, buf1);
6557 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006558 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006559 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6560 else
6561 i = 0;
6562 n1 = FALSE;
6563 switch (type)
6564 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006565 case EXPR_IS:
6566 case EXPR_EQUAL: n1 = (i == 0); break;
6567 case EXPR_ISNOT:
6568 case EXPR_NEQUAL: n1 = (i != 0); break;
6569 case EXPR_GREATER: n1 = (i > 0); break;
6570 case EXPR_GEQUAL: n1 = (i >= 0); break;
6571 case EXPR_SMALLER: n1 = (i < 0); break;
6572 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006573
Bram Moolenaar87396072019-12-31 22:36:18 +01006574 case EXPR_MATCH:
6575 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006576 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006577 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006578 n1 = !n1;
6579 break;
6580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006582 }
6583 }
6584 clear_tv(typ1);
6585 typ1->v_type = VAR_NUMBER;
6586 typ1->vval.v_number = n1;
6587
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006588 return OK;
6589}
6590
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006591 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006592typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006593{
6594 char_u *tofree;
6595 char_u numbuf[NUMBUFLEN];
6596 char_u *ret = NULL;
6597
6598 if (arg == NULL)
6599 return vim_strsave((char_u *)"(does not exist)");
6600 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006601 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006602 if (ret != NULL && tofree == NULL)
6603 ret = vim_strsave(ret);
6604 return ret;
6605}
6606
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006607#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
6609/*
6610 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006611 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 * "flags" can be "g" to do a global substitute.
6613 * Returns an allocated string, NULL for error.
6614 */
6615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616do_string_sub(
6617 char_u *str,
6618 char_u *pat,
6619 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006620 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 int sublen;
6624 regmatch_T regmatch;
6625 int i;
6626 int do_all;
6627 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006628 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 garray_T ga;
6630 char_u *ret;
6631 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006632 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006634 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006636 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 ga_init2(&ga, 1, 200);
6639
6640 do_all = (flags[0] == 'g');
6641
6642 regmatch.rm_ic = p_ic;
6643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6644 if (regmatch.regprog != NULL)
6645 {
6646 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006647 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006651 if (regmatch.startp[0] == regmatch.endp[0])
6652 {
6653 if (zero_width == regmatch.startp[0])
6654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006656 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006657 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6658 (size_t)i);
6659 ga.ga_len += i;
6660 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006661 continue;
6662 }
6663 zero_width = regmatch.startp[0];
6664 }
6665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 /*
6667 * Get some space for a temporary buffer to do the substitution
6668 * into. It will contain:
6669 * - The text up to where the match is.
6670 * - The substituted text.
6671 * - The text after the match.
6672 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006673 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006674 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6676 {
6677 ga_clear(&ga);
6678 break;
6679 }
6680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006681 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 i = (int)(regmatch.startp[0] - tail);
6683 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006684 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006685 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 + ga.ga_len + i, TRUE, TRUE, FALSE);
6687 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006688 tail = regmatch.endp[0];
6689 if (*tail == NUL)
6690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (!do_all)
6692 break;
6693 }
6694
6695 if (ga.ga_data != NULL)
6696 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6697
Bram Moolenaar473de612013-06-08 18:19:48 +02006698 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 }
6700
6701 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6702 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006703 if (p_cpo == empty_option)
6704 p_cpo = save_cpo;
6705 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006706 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006707 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709 return ret;
6710}