blob: 72cb0897affcc0fc79b163a795da86e9ea3a8370 [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 Moolenaar48e697e2016-01-23 22:17:30 +010051static int eval2(char_u **arg, typval_T *rettv, int evaluate);
52static int eval3(char_u **arg, typval_T *rettv, int evaluate);
53static int eval4(char_u **arg, typval_T *rettv, int evaluate);
54static int eval5(char_u **arg, typval_T *rettv, int evaluate);
55static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
56static int eval7(char_u **arg, typval_T *rettv, int evaluate, 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 Moolenaarc70646c2005-01-04 21:52:38 +0000176 if (eval0(arg, &tv, nextcmd, !skip) == 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
204 ret = eval1(arg, rettv, evaluate);
205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
215 return ret;
216}
217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218/*
219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe.evaluate = TRUE;
237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0)
248 {
249 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL)
250 return FAIL;
251 }
252 else
253 {
254 s = partial_name(partial);
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 funcexe.evaluate = TRUE;
259 funcexe.partial = partial;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
261 return FAIL;
262 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 }
264 else
265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100266 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 if (s == NULL)
268 return FAIL;
269 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100270 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100272 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200274 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100275 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100276 return FAIL;
277 }
278 }
279 return OK;
280}
281
282/*
283 * Like eval_to_bool() but using a typval_T instead of a string.
284 * Works for string, funcref and partial.
285 */
286 int
287eval_expr_to_bool(typval_T *expr, int *error)
288{
289 typval_T rettv;
290 int res;
291
292 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
293 {
294 *error = TRUE;
295 return FALSE;
296 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 clear_tv(&rettv);
299 return res;
300}
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302/*
303 * Top level evaluation function, returning a string. If "skip" is TRUE,
304 * only parsing to "nextcmd" is done, without reporting errors. Return
305 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
306 */
307 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100308eval_to_string_skip(
309 char_u *arg,
310 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100311 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
Bram Moolenaar33570922005-01-25 22:26:29 +0000313 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 char_u *retval;
315
316 if (skip)
317 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000318 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 retval = NULL;
320 else
321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100322 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000323 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 if (skip)
326 --emsg_skip;
327
328 return retval;
329}
330
331/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000332 * Skip over an expression at "*pp".
333 * Return FAIL for an error, OK otherwise.
334 */
335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000337{
Bram Moolenaar33570922005-01-25 22:26:29 +0000338 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000339
340 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000341 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342}
343
344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000346 * When "convert" is TRUE convert a List into a sequence of lines and convert
347 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 * Return pointer to allocated memory, or NULL for failure.
349 */
350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100351eval_to_string(
352 char_u *arg,
353 char_u **nextcmd,
354 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
Bram Moolenaar33570922005-01-25 22:26:29 +0000356 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000358 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000359#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000363 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 retval = NULL;
365 else
366 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000367 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 {
369 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000370 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200371 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200372 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200373 if (tv.vval.v_list->lv_len > 0)
374 ga_append(&ga, NL);
375 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000376 ga_append(&ga, NUL);
377 retval = (char_u *)ga.ga_data;
378 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000379#ifdef FEAT_FLOAT
380 else if (convert && tv.v_type == VAR_FLOAT)
381 {
382 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
383 retval = vim_strsave(numbuf);
384 }
385#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000386 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100387 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000388 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390
391 return retval;
392}
393
394/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000395 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200396 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399eval_to_string_safe(
400 char_u *arg,
401 char_u **nextcmd,
402 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
404 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200405 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000408 if (use_sandbox)
409 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200410 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000411 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000412 if (use_sandbox)
413 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200414 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200415 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return retval;
417}
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419/*
420 * Top level evaluation function, returning a number.
421 * Evaluates "expr" silently.
422 * Returns -1 for an error.
423 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200424 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100425eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200428 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000429 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431 ++emsg_off;
432
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000433 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 retval = -1;
435 else
436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100437 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 }
440 --emsg_off;
441
442 return retval;
443}
444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000445/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000446 * Top level evaluation function.
447 * Returns an allocated typval_T with the result.
448 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000449 */
450 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000452{
453 typval_T *tv;
454
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200455 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000456 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000458
459 return tv;
460}
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100463 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200464 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
465 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000466 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100469call_vim_function(
470 char_u *func,
471 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200472 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200473 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000475 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200476 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100478 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200479 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200480 funcexe.firstline = curwin->w_cursor.lnum;
481 funcexe.lastline = curwin->w_cursor.lnum;
482 funcexe.evaluate = TRUE;
483 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000484 if (ret == FAIL)
485 clear_tv(rettv);
486
487 return ret;
488}
489
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100490/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100491 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200493 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
494 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100495 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200496 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100497call_func_retnr(
498 char_u *func,
499 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200500 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501{
502 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200503 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100504
Bram Moolenaarded27a12018-08-01 19:06:03 +0200505 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506 return -1;
507
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100508 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100509 clear_tv(&rettv);
510 return retval;
511}
512
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000513/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100514 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000515 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200516 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
517 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000518 */
519 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100520call_func_retstr(
521 char_u *func,
522 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200523 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524{
525 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000526 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527
Bram Moolenaarded27a12018-08-01 19:06:03 +0200528 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 return NULL;
530
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100531 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return retval;
534}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000535
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000536/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100537 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200538 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
539 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000540 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000541 */
542 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100543call_func_retlist(
544 char_u *func,
545 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200546 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000547{
548 typval_T rettv;
549
Bram Moolenaarded27a12018-08-01 19:06:03 +0200550 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000551 return NULL;
552
553 if (rettv.v_type != VAR_LIST)
554 {
555 clear_tv(&rettv);
556 return NULL;
557 }
558
559 return rettv.vval.v_list;
560}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef FEAT_FOLDING
563/*
564 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
565 * it in "*cp". Doesn't give error messages.
566 */
567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
Bram Moolenaar33570922005-01-25 22:26:29 +0000570 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200571 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000573 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
574 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200579 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000581 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = 0;
583 else
584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100585 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 if (tv.v_type == VAR_NUMBER)
587 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000588 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 retval = 0;
590 else
591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100592 // If the result is a string, check if there is a non-digit before
593 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (!VIM_ISDIGIT(*s) && *s != '-')
596 *cp = *s++;
597 retval = atol((char *)s);
598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000599 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 if (use_sandbox)
603 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200604 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200606 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607}
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000611 * Get an lval: variable, Dict item or List item that can be assigned a value
612 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
613 * "name.key", "name.key[expr]" etc.
614 * Indexing only works if "name" is an existing List or Dictionary.
615 * "name" points to the start of the name.
616 * If "rettv" is not NULL it points to the value to be assigned.
617 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
618 * wrong; must end in space or cmd separator.
619 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100620 * flags:
621 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100622 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100623 * GLV_NO_AUTOLOAD: do not use script autoloading
624 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000625 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000626 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000627 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200629 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630get_lval(
631 char_u *name,
632 typval_T *rettv,
633 lval_T *lp,
634 int unlet,
635 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100636 int flags, // GLV_ values
637 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000640 char_u *expr_start, *expr_end;
641 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 dictitem_T *v;
643 typval_T var1;
644 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000645 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000646 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000647 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000648 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000649 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100650 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000654
655 if (skip)
656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100657 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000658 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000659 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 }
661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000663 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000665 if (expr_start != NULL)
666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100667 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100668 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 && *p != '[' && *p != '.')
670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000672 return NULL;
673 }
674
675 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
676 if (lp->ll_exp_name == NULL)
677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100678 // Report an invalid expression in braces, unless the
679 // expression evaluation has been cancelled due to an
680 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000681 if (!aborting() && !quiet)
682 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000683 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100684 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000685 return NULL;
686 }
687 }
688 lp->ll_name = lp->ll_exp_name;
689 }
690 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000692 lp->ll_name = name;
693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
695 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100696 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 char_u *tp = skipwhite(p + 1);
698
699 // parse the type after the name
700 lp->ll_type = parse_type(&tp, &si->sn_type_list);
701 lp->ll_name_end = tp;
702 }
703 }
704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100705 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000706 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
707 return p;
708
709 cc = *p;
710 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100711 // Only pass &ht when we would write to the variable, it prevents autoload
712 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100713 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
714 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100716 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 if (v == NULL)
719 return NULL;
720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 /*
722 * Loop until no more [idx] or .key is following.
723 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100725 var1.v_type = VAR_UNKNOWN;
726 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000729 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
730 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100731 && lp->ll_tv->vval.v_dict != NULL)
732 && !(lp->ll_tv->v_type == VAR_BLOB
733 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000745
Bram Moolenaar8c711452005-01-14 21:53:12 +0000746 len = -1;
747 if (*p == '.')
748 {
749 key = p + 1;
750 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
751 ;
752 if (len == 0)
753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000757 }
758 p = key + len;
759 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000760 else
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000763 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000764 if (*p == ':')
765 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000766 else
767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000768 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100771 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000774 clear_tv(&var1);
775 return NULL;
776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 }
778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100779 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 if (*p == ':')
781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000782 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000788 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100789 if (rettv != NULL
790 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100791 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100792 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100793 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000799 }
800 p = skipwhite(p + 1);
801 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 else
804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100806 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000807 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100811 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100814 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000815 clear_tv(&var2);
816 return NULL;
817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000818 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100828 clear_tv(&var1);
829 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 }
832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 ++p;
835 }
836
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 {
839 if (len == -1)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // "[key]": get key from "var1"
842 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200843 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000847 }
848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000849 lp->ll_list = NULL;
850 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000851 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 // When assigning to a scope dictionary check that a function and
854 // variable name is valid (only variable name unless it is l: or
855 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200856 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200857 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 int prevval;
859 int wrong;
860
861 if (len != -1)
862 {
863 prevval = key[len];
864 key[len] = NUL;
865 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200866 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200868 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
869 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200870 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200871 || !valid_varname(key);
872 if (len != -1)
873 key[len] = prevval;
874 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200875 {
876 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200877 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200878 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200879 }
880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100883 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200884 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100885 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100887 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100888 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 return NULL;
890 }
891
Bram Moolenaar31b81602019-02-10 22:14:27 +0100892 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100896 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100897 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 }
900 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 p = NULL;
907 break;
908 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100910 else if ((flags & GLV_READ_ONLY) == 0
911 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100912 {
913 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200914 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100915 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200916
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100917 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100920 else if (lp->ll_tv->v_type == VAR_BLOB)
921 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100922 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
923
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100924 /*
925 * Get the number and item for the only or first index of the List.
926 */
927 if (empty1)
928 lp->ll_n1 = 0;
929 else
930 // is number or string
931 lp->ll_n1 = (long)tv_get_number(&var1);
932 clear_tv(&var1);
933
934 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100935 || lp->ll_n1 > bloblen
936 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100937 {
938 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100940 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100941 return NULL;
942 }
943 if (lp->ll_range && !lp->ll_empty2)
944 {
945 lp->ll_n2 = (long)tv_get_number(&var2);
946 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100947 if (lp->ll_n2 < 0
948 || lp->ll_n2 >= bloblen
949 || lp->ll_n2 < lp->ll_n1)
950 {
951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100953 return NULL;
954 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100955 }
956 lp->ll_blob = lp->ll_tv->vval.v_blob;
957 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100958 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100959 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000960 else
961 {
962 /*
963 * Get the number and item for the only or first index of the List.
964 */
965 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100969 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100970 clear_tv(&var1);
971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000972 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 lp->ll_list = lp->ll_tv->vval.v_list;
974 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
975 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000977 if (lp->ll_n1 < 0)
978 {
979 lp->ll_n1 = 0;
980 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
981 }
982 }
983 if (lp->ll_li == NULL)
984 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100985 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200986 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100987 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 }
990
991 /*
992 * May need to find the item or absolute index for the second
993 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 * When no index given: "lp->ll_empty2" is TRUE.
995 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100999 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001000 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001006 {
1007 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001008 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 }
1013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 if (lp->ll_n1 < 0)
1016 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1017 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001018 {
1019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001022 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001023 }
1024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 }
1028
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001029 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 return p;
1032}
1033
1034/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001037 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001038clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039{
1040 vim_free(lp->ll_exp_name);
1041 vim_free(lp->ll_newkey);
1042}
1043
1044/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001045 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001047 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1048 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001051set_var_lval(
1052 lval_T *lp,
1053 char_u *endp,
1054 typval_T *rettv,
1055 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058{
1059 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001060 listitem_T *ri;
1061 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062
1063 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065 cc = *endp;
1066 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 if (lp->ll_blob != NULL)
1068 {
1069 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 if (op != NULL && *op != '=')
1072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001074 return;
1075 }
1076
1077 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1078 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001079 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001080
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001081 if (lp->ll_empty2)
1082 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1083
1084 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001086 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001087 return;
1088 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 if (lp->ll_empty2)
1090 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001091
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 ir = 0;
1093 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1094 blob_set(lp->ll_blob, il,
1095 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001096 }
1097 else
1098 {
1099 val = (int)tv_get_number_chk(rettv, &error);
1100 if (!error)
1101 {
1102 garray_T *gap = &lp->ll_blob->bv_ga;
1103
1104 // Allow for appending a byte. Setting a byte beyond
1105 // the end is an error otherwise.
1106 if (lp->ll_n1 < gap->ga_len
1107 || (lp->ll_n1 == gap->ga_len
1108 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1109 {
1110 blob_set(lp->ll_blob, lp->ll_n1, val);
1111 if (lp->ll_n1 == gap->ga_len)
1112 ++gap->ga_len;
1113 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001114 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001115 }
1116 }
1117 }
1118 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001120 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001123 {
1124 emsg(_(e_cannot_mod));
1125 *endp = cc;
1126 return;
1127 }
1128
Bram Moolenaarff697e62019-02-12 22:28:33 +01001129 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001130 di = NULL;
1131 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1132 &tv, &di, TRUE, FALSE) == OK)
1133 {
1134 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001135 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1136 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001137 && tv_op(&tv, rettv, op) == OK)
1138 set_var(lp->ll_name, &tv, FALSE);
1139 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001142 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001144 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001146 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001147 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001148 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001149 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 else if (lp->ll_range)
1151 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001152 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001153 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001156 {
1157 emsg(_("E996: Cannot lock a range"));
1158 return;
1159 }
1160
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001161 /*
1162 * Check whether any of the list items is locked
1163 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001164 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001165 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001166 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001167 return;
1168 ri = ri->li_next;
1169 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1170 break;
1171 ll_li = ll_li->li_next;
1172 ++ll_n1;
1173 }
1174
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 /*
1176 * Assign the List values to the list items.
1177 */
1178 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001179 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001180 if (op != NULL && *op != '=')
1181 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1182 else
1183 {
1184 clear_tv(&lp->ll_li->li_tv);
1185 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1186 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 ri = ri->li_next;
1188 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1189 break;
1190 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001193 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 ri = NULL;
1196 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001197 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_li = lp->ll_li->li_next;
1200 ++lp->ll_n1;
1201 }
1202 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 else if (lp->ll_empty2
1205 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 }
1209 else
1210 {
1211 /*
1212 * Assign to a List or Dictionary item.
1213 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001215 {
1216 emsg(_("E996: Cannot lock a list or dict"));
1217 return;
1218 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 if (lp->ll_newkey != NULL)
1220 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001221 if (op != NULL && *op != '=')
1222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 return;
1225 }
1226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001228 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (di == NULL)
1230 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001231 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1232 {
1233 vim_free(di);
1234 return;
1235 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 else if (op != NULL && *op != '=')
1239 {
1240 tv_op(lp->ll_tv, rettv, op);
1241 return;
1242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 /*
1247 * Assign the value to the variable or list item.
1248 */
1249 if (copy)
1250 copy_tv(rettv, lp->ll_tv);
1251 else
1252 {
1253 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001254 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001256 }
1257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258}
1259
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001261 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1262 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001263 * Returns OK or FAIL.
1264 */
1265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001267{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001268 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001269 char_u numbuf[NUMBUFLEN];
1270 char_u *s;
1271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001272 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001273 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001274 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001275 {
1276 switch (tv1->v_type)
1277 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001278 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001279 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 case VAR_DICT:
1282 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001283 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001284 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001285 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001287 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 break;
1289
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 case VAR_BLOB:
1291 if (*op != '+' || tv2->v_type != VAR_BLOB)
1292 break;
1293 // BLOB += BLOB
1294 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1295 {
1296 blob_T *b1 = tv1->vval.v_blob;
1297 blob_T *b2 = tv2->vval.v_blob;
1298 int i, len = blob_len(b2);
1299 for (i = 0; i < len; i++)
1300 ga_append(&b1->bv_ga, blob_get(b2, i));
1301 }
1302 return OK;
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 case VAR_LIST:
1305 if (*op != '+' || tv2->v_type != VAR_LIST)
1306 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001307 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001308 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1309 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1310 return OK;
1311
1312 case VAR_NUMBER:
1313 case VAR_STRING:
1314 if (tv2->v_type == VAR_LIST)
1315 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001316 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001318 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001320#ifdef FEAT_FLOAT
1321 if (tv2->v_type == VAR_FLOAT)
1322 {
1323 float_T f = n;
1324
Bram Moolenaarff697e62019-02-12 22:28:33 +01001325 if (*op == '%')
1326 break;
1327 switch (*op)
1328 {
1329 case '+': f += tv2->vval.v_float; break;
1330 case '-': f -= tv2->vval.v_float; break;
1331 case '*': f *= tv2->vval.v_float; break;
1332 case '/': f /= tv2->vval.v_float; break;
1333 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001334 clear_tv(tv1);
1335 tv1->v_type = VAR_FLOAT;
1336 tv1->vval.v_float = f;
1337 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001339#endif
1340 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001341 switch (*op)
1342 {
1343 case '+': n += tv_get_number(tv2); break;
1344 case '-': n -= tv_get_number(tv2); break;
1345 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001346 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1347 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001348 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001349 clear_tv(tv1);
1350 tv1->v_type = VAR_NUMBER;
1351 tv1->vval.v_number = n;
1352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353 }
1354 else
1355 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001356 if (tv2->v_type == VAR_FLOAT)
1357 break;
1358
Bram Moolenaarff697e62019-02-12 22:28:33 +01001359 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001360 s = tv_get_string(tv1);
1361 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 clear_tv(tv1);
1363 tv1->v_type = VAR_STRING;
1364 tv1->vval.v_string = s;
1365 }
1366 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001368 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001369#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001370 {
1371 float_T f;
1372
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 if (*op == '%' || *op == '.'
1374 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001375 && tv2->v_type != VAR_NUMBER
1376 && tv2->v_type != VAR_STRING))
1377 break;
1378 if (tv2->v_type == VAR_FLOAT)
1379 f = tv2->vval.v_float;
1380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001382 switch (*op)
1383 {
1384 case '+': tv1->vval.v_float += f; break;
1385 case '-': tv1->vval.v_float -= f; break;
1386 case '*': tv1->vval.v_float *= f; break;
1387 case '/': tv1->vval.v_float /= f; break;
1388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001389 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001390#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001391 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392 }
1393 }
1394
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001395 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 return FAIL;
1397}
1398
1399/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400 * Evaluate the expression used in a ":for var in expr" command.
1401 * "arg" points to "var".
1402 * Set "*errp" to TRUE for an error, FALSE otherwise;
1403 * Return a pointer that holds the info. Null when there is an error.
1404 */
1405 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_for_line(
1407 char_u *arg,
1408 int *errp,
1409 char_u **nextcmdp,
1410 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
1415 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001418
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001419 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 if (fi == NULL)
1421 return NULL;
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424 if (expr == NULL)
1425 return fi;
1426
1427 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001428 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 return fi;
1432 }
1433
1434 if (skip)
1435 ++emsg_skip;
1436 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1437 {
1438 *errp = FALSE;
1439 if (!skip)
1440 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001442 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 l = tv.vval.v_list;
1444 if (l == NULL)
1445 {
1446 // a null list is like an empty list: do nothing
1447 clear_tv(&tv);
1448 }
1449 else
1450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 // Need a real list here.
1452 range_list_materialize(l);
1453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 // No need to increment the refcount, it's already set for
1455 // the list being used in "tv".
1456 fi->fi_list = l;
1457 list_add_watch(l, &fi->fi_lw);
1458 fi->fi_lw.lw_item = l->lv_first;
1459 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001460 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001462 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001463 fi->fi_bi = 0;
1464 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001466 typval_T btv;
1467
1468 // Make a copy, so that the iteration still works when the
1469 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001471 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001473 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001474 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 else
1476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001477 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 }
1480 }
1481 }
1482 if (skip)
1483 --emsg_skip;
1484
1485 return fi;
1486}
1487
1488/*
1489 * Use the first item in a ":for" list. Advance to the next.
1490 * Assign the values to the variable (list). "arg" points to the first one.
1491 * Return TRUE when a valid item was found, FALSE when at end of list or
1492 * something wrong.
1493 */
1494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001497 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001499 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1500 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001503 if (fi->fi_blob != NULL)
1504 {
1505 typval_T tv;
1506
1507 if (fi->fi_bi >= blob_len(fi->fi_blob))
1508 return FALSE;
1509 tv.v_type = VAR_NUMBER;
1510 tv.v_lock = VAR_FIXED;
1511 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1512 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001513 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001514 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 }
1516
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 item = fi->fi_lw.lw_item;
1518 if (item == NULL)
1519 result = FALSE;
1520 else
1521 {
1522 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001523 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001524 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 }
1526 return result;
1527}
1528
1529/*
1530 * Free the structure used to store info used by ":for".
1531 */
1532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534{
Bram Moolenaar33570922005-01-25 22:26:29 +00001535 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001537 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001538 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001540 list_unref(fi->fi_list);
1541 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001542 if (fi != NULL && fi->fi_blob != NULL)
1543 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 vim_free(fi);
1545}
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548set_context_for_expression(
1549 expand_T *xp,
1550 char_u *arg,
1551 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int got_eq = FALSE;
1554 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001557 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 {
1559 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001562 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564 {
1565 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001566 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001567 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 break;
1569 }
1570 return;
1571 }
1572 }
1573 else
1574 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1575 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 while ((xp->xp_pattern = vim_strpbrk(arg,
1577 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1578 {
1579 c = *xp->xp_pattern;
1580 if (c == '&')
1581 {
1582 c = xp->xp_pattern[1];
1583 if (c == '&')
1584 {
1585 ++xp->xp_pattern;
1586 xp->xp_context = cmdidx != CMD_let || got_eq
1587 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1588 }
1589 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001592 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1593 xp->xp_pattern += 2;
1594
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else if (c == '$')
1598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001599 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 xp->xp_context = EXPAND_ENV_VARS;
1601 }
1602 else if (c == '=')
1603 {
1604 got_eq = TRUE;
1605 xp->xp_context = EXPAND_EXPRESSION;
1606 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001607 else if (c == '#'
1608 && xp->xp_context == EXPAND_EXPRESSION)
1609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001610 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001611 break;
1612 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001613 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 && xp->xp_context == EXPAND_FUNCTIONS
1615 && vim_strchr(xp->xp_pattern, '(') == NULL)
1616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 break;
1619 }
1620 else if (cmdidx != CMD_let || got_eq)
1621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001622 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1625 if (c == '\\' && xp->xp_pattern[1] != NUL)
1626 ++xp->xp_pattern;
1627 xp->xp_context = EXPAND_NOTHING;
1628 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1633 /* skip */ ;
1634 xp->xp_context = EXPAND_NOTHING;
1635 }
1636 else if (c == '|')
1637 {
1638 if (xp->xp_pattern[1] == '|')
1639 {
1640 ++xp->xp_pattern;
1641 xp->xp_context = EXPAND_EXPRESSION;
1642 }
1643 else
1644 xp->xp_context = EXPAND_COMMANDS;
1645 }
1646 else
1647 xp->xp_context = EXPAND_EXPRESSION;
1648 }
1649 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001650 // Doesn't look like something valid, expand as an expression
1651 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 arg = xp->xp_pattern;
1654 if (*arg != NUL)
1655 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1656 /* skip */ ;
1657 }
1658 xp->xp_pattern = arg;
1659}
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001662 * Return TRUE if "pat" matches "text".
1663 * Does not use 'cpo' and always uses 'magic'.
1664 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001665 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001666pattern_match(char_u *pat, char_u *text, int ic)
1667{
1668 int matches = FALSE;
1669 char_u *save_cpo;
1670 regmatch_T regmatch;
1671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001673 save_cpo = p_cpo;
1674 p_cpo = (char_u *)"";
1675 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1676 if (regmatch.regprog != NULL)
1677 {
1678 regmatch.rm_ic = ic;
1679 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1680 vim_regfree(regmatch.regprog);
1681 }
1682 p_cpo = save_cpo;
1683 return matches;
1684}
1685
1686/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001687 * Handle a name followed by "(". Both for just "name(arg)" and for
1688 * "expr->name(arg)".
1689 * Returns OK or FAIL.
1690 */
1691 static int
1692eval_func(
1693 char_u **arg, // points to "(", will be advanced
1694 char_u *name,
1695 int name_len,
1696 typval_T *rettv,
1697 int evaluate,
1698 typval_T *basetv) // "expr" for "expr->name(arg)"
1699{
1700 char_u *s = name;
1701 int len = name_len;
1702 partial_T *partial;
1703 int ret = OK;
1704
1705 if (!evaluate)
1706 check_vars(s, len);
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // If "s" is the name of a variable of type VAR_FUNC
1709 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001710 s = deref_func_name(s, &len, &partial, !evaluate);
1711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // Need to make a copy, in case evaluating the arguments makes
1713 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001714 s = vim_strsave(s);
1715 if (s == NULL)
1716 ret = FAIL;
1717 else
1718 {
1719 funcexe_T funcexe;
1720
1721 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001722 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001723 funcexe.firstline = curwin->w_cursor.lnum;
1724 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001725 funcexe.evaluate = evaluate;
1726 funcexe.partial = partial;
1727 funcexe.basetv = basetv;
1728 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1729 }
1730 vim_free(s);
1731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001732 // If evaluate is FALSE rettv->v_type was not set in
1733 // get_func_tv, but it's needed in handle_subscript() to parse
1734 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001735 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1736 {
1737 rettv->vval.v_string = NULL;
1738 rettv->v_type = VAR_FUNC;
1739 }
1740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001741 // Stop the expression evaluation when immediately
1742 // aborting on error, or when an interrupt occurred or
1743 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001744 if (evaluate && aborting())
1745 {
1746 if (ret == OK)
1747 clear_tv(rettv);
1748 ret = FAIL;
1749 }
1750 return ret;
1751}
1752
1753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1757 */
1758
1759/*
1760 * Handle zero level expression.
1761 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001763 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 * Return OK or FAIL.
1765 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767eval0(
1768 char_u *arg,
1769 typval_T *rettv,
1770 char_u **nextcmd,
1771 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 int ret;
1774 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001775 int did_emsg_before = did_emsg;
1776 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 ret = eval1(&p, rettv, evaluate);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001780 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 /*
1785 * Report the invalid expression unless the expression evaluation has
1786 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001787 * exception, or we already gave a more specific error.
1788 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001790 if (!aborting() && did_emsg == did_emsg_before
1791 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001792 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 ret = FAIL;
1794 }
1795 if (nextcmd != NULL)
1796 *nextcmd = check_nextcmd(p);
1797
1798 return ret;
1799}
1800
1801/*
1802 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001803 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 *
1805 * "arg" must point to the first non-white of the expression.
1806 * "arg" is advanced to the next non-white after the recognized expression.
1807 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001808 * Note: "rettv.v_lock" is not set.
1809 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 * Return OK or FAIL.
1811 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 /*
1819 * Get the first variable.
1820 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 return FAIL;
1823
1824 if ((*arg)[0] == '?')
1825 {
1826 result = FALSE;
1827 if (evaluate)
1828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001829 int error = FALSE;
1830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001831 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001834 if (error)
1835 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837
1838 /*
1839 * Get the second variable.
1840 */
1841 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 return FAIL;
1844
1845 /*
1846 * Check for the ":".
1847 */
1848 if ((*arg)[0] != ':')
1849 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 return FAIL;
1854 }
1855
1856 /*
1857 * Get the third variable.
1858 */
1859 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001860 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return FAIL;
1865 }
1866 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
1870 return OK;
1871}
1872
1873/*
1874 * Handle first level expression:
1875 * expr2 || expr2 || expr2 logical OR
1876 *
1877 * "arg" must point to the first non-white of the expression.
1878 * "arg" is advanced to the next non-white after the recognized expression.
1879 *
1880 * Return OK or FAIL.
1881 */
1882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001883eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 long result;
1887 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001888 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /*
1891 * Get the first variable.
1892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return FAIL;
1895
1896 /*
1897 * Repeat until there is no following "||".
1898 */
1899 first = TRUE;
1900 result = FALSE;
1901 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1902 {
1903 if (evaluate && first)
1904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001905 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001908 if (error)
1909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 first = FALSE;
1911 }
1912
1913 /*
1914 * Get the second variable.
1915 */
1916 *arg = skipwhite(*arg + 2);
1917 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1918 return FAIL;
1919
1920 /*
1921 * Compute the result.
1922 */
1923 if (evaluate && !result)
1924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001925 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (error)
1929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 if (evaluate)
1932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 rettv->v_type = VAR_NUMBER;
1934 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936 }
1937
1938 return OK;
1939}
1940
1941/*
1942 * Handle second level expression:
1943 * expr3 && expr3 && expr3 logical AND
1944 *
1945 * "arg" must point to the first non-white of the expression.
1946 * "arg" is advanced to the next non-white after the recognized expression.
1947 *
1948 * Return OK or FAIL.
1949 */
1950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001951eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 long result;
1955 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /*
1959 * Get the first variable.
1960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return FAIL;
1963
1964 /*
1965 * Repeat until there is no following "&&".
1966 */
1967 first = TRUE;
1968 result = TRUE;
1969 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1970 {
1971 if (evaluate && first)
1972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001976 if (error)
1977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 first = FALSE;
1979 }
1980
1981 /*
1982 * Get the second variable.
1983 */
1984 *arg = skipwhite(*arg + 2);
1985 if (eval4(arg, &var2, evaluate && result) == FAIL)
1986 return FAIL;
1987
1988 /*
1989 * Compute the result.
1990 */
1991 if (evaluate && result)
1992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001993 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996 if (error)
1997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (evaluate)
2000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 rettv->v_type = VAR_NUMBER;
2002 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
2005
2006 return OK;
2007}
2008
2009/*
2010 * Handle third level expression:
2011 * var1 == var2
2012 * var1 =~ var2
2013 * var1 != var2
2014 * var1 !~ var2
2015 * var1 > var2
2016 * var1 >= var2
2017 * var1 < var2
2018 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002019 * var1 is var2
2020 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 *
2022 * "arg" must point to the first non-white of the expression.
2023 * "arg" is advanced to the next non-white after the recognized expression.
2024 *
2025 * Return OK or FAIL.
2026 */
2027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 char_u *p;
2032 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002033 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 /*
2038 * Get the first variable.
2039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return FAIL;
2042
2043 p = *arg;
2044 switch (p[0])
2045 {
2046 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002047 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002049 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
2051 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002052 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002054 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056 case '>': if (p[1] != '=')
2057 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002058 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 len = 1;
2060 }
2061 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002062 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 break;
2064 case '<': if (p[1] != '=')
2065 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002066 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 len = 1;
2068 }
2069 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002070 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002072 case 'i': if (p[1] == 's')
2073 {
2074 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2075 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002076 i = p[len];
2077 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002078 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002079 }
2080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002084 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002086 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002088 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (p[len] == '?')
2090 {
2091 ic = TRUE;
2092 ++len;
2093 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else if (p[len] == '#')
2096 {
2097 ic = FALSE;
2098 ++len;
2099 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002100 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 ic = p_ic;
2103
2104 /*
2105 * Get the second variable.
2106 */
2107 *arg = skipwhite(p + len);
2108 if (eval5(arg, &var2, evaluate) == FAIL)
2109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 return FAIL;
2112 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002113 if (evaluate)
2114 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002115 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002116
2117 clear_tv(&var2);
2118 return ret;
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
2122 return OK;
2123}
2124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 void
2126eval_addblob(typval_T *tv1, typval_T *tv2)
2127{
2128 blob_T *b1 = tv1->vval.v_blob;
2129 blob_T *b2 = tv2->vval.v_blob;
2130 blob_T *b = blob_alloc();
2131 int i;
2132
2133 if (b != NULL)
2134 {
2135 for (i = 0; i < blob_len(b1); i++)
2136 ga_append(&b->bv_ga, blob_get(b1, i));
2137 for (i = 0; i < blob_len(b2); i++)
2138 ga_append(&b->bv_ga, blob_get(b2, i));
2139
2140 clear_tv(tv1);
2141 rettv_blob_set(tv1, b);
2142 }
2143}
2144
2145 int
2146eval_addlist(typval_T *tv1, typval_T *tv2)
2147{
2148 typval_T var3;
2149
2150 // concatenate Lists
2151 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2152 {
2153 clear_tv(tv1);
2154 clear_tv(tv2);
2155 return FAIL;
2156 }
2157 clear_tv(tv1);
2158 *tv1 = var3;
2159 return OK;
2160}
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162/*
2163 * Handle fourth level expression:
2164 * + number addition
2165 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002166 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002167 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 *
2169 * "arg" must point to the first non-white of the expression.
2170 * "arg" is advanced to the next non-white after the recognized expression.
2171 *
2172 * Return OK or FAIL.
2173 */
2174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaar33570922005-01-25 22:26:29 +00002177 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002179 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002180#ifdef FEAT_FLOAT
2181 float_T f1 = 0, f2 = 0;
2182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 char_u *s1, *s2;
2184 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2185 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002186 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /*
2189 * Get the first variable.
2190 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002191 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return FAIL;
2193
2194 /*
2195 * Repeat computing, until no '+', '-' or '.' is following.
2196 */
2197 for (;;)
2198 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002199 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002201 concat = op == '.'
2202 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2203 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 break;
2205
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002206 if ((op != '+' || (rettv->v_type != VAR_LIST
2207 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002208#ifdef FEAT_FLOAT
2209 && (op == '.' || rettv->v_type != VAR_FLOAT)
2210#endif
2211 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // For "list + ...", an illegal use of the first operand as
2214 // a number cannot be determined before evaluating the 2nd
2215 // operand: if this is also a list, all is ok.
2216 // For "something . ...", "something - ..." or "non-list + ...",
2217 // we know that the first operand needs to be a string or number
2218 // without evaluating the 2nd operand. So check before to avoid
2219 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002220 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002221 {
2222 clear_tv(rettv);
2223 return FAIL;
2224 }
2225 }
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /*
2228 * Get the second variable.
2229 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002230 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2231 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002233 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FAIL;
2237 }
2238
2239 if (evaluate)
2240 {
2241 /*
2242 * Compute the result.
2243 */
2244 if (op == '.')
2245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002246 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002247 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002248 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002249 {
2250 clear_tv(rettv);
2251 clear_tv(&var2);
2252 return FAIL;
2253 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 clear_tv(rettv);
2256 rettv->v_type = VAR_STRING;
2257 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002259 else if (op == '+' && rettv->v_type == VAR_BLOB
2260 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002262 else if (op == '+' && rettv->v_type == VAR_LIST
2263 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002264 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002266 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 else
2269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 int error = FALSE;
2271
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002272#ifdef FEAT_FLOAT
2273 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002275 f1 = rettv->vval.v_float;
2276 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002278 else
2279#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002282 if (error)
2283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002284 // This can only happen for "list + non-list". For
2285 // "non-list + ..." or "something - ...", we returned
2286 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002287 clear_tv(rettv);
2288 return FAIL;
2289 }
2290#ifdef FEAT_FLOAT
2291 if (var2.v_type == VAR_FLOAT)
2292 f1 = n1;
2293#endif
2294 }
2295#ifdef FEAT_FLOAT
2296 if (var2.v_type == VAR_FLOAT)
2297 {
2298 f2 = var2.vval.v_float;
2299 n2 = 0;
2300 }
2301 else
2302#endif
2303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002305 if (error)
2306 {
2307 clear_tv(rettv);
2308 clear_tv(&var2);
2309 return FAIL;
2310 }
2311#ifdef FEAT_FLOAT
2312 if (rettv->v_type == VAR_FLOAT)
2313 f2 = n2;
2314#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002317
2318#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002319 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002320 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2321 {
2322 if (op == '+')
2323 f1 = f1 + f2;
2324 else
2325 f1 = f1 - f2;
2326 rettv->v_type = VAR_FLOAT;
2327 rettv->vval.v_float = f1;
2328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002330#endif
2331 {
2332 if (op == '+')
2333 n1 = n1 + n2;
2334 else
2335 n1 = n1 - n2;
2336 rettv->v_type = VAR_NUMBER;
2337 rettv->vval.v_number = n1;
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 }
2343 return OK;
2344}
2345
2346/*
2347 * Handle fifth level expression:
2348 * * number multiplication
2349 * / number division
2350 * % number modulo
2351 *
2352 * "arg" must point to the first non-white of the expression.
2353 * "arg" is advanced to the next non-white after the recognized expression.
2354 *
2355 * Return OK or FAIL.
2356 */
2357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358eval6(
2359 char_u **arg,
2360 typval_T *rettv,
2361 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar33570922005-01-25 22:26:29 +00002364 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002366 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002367#ifdef FEAT_FLOAT
2368 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002369 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002370#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 /*
2374 * Get the first variable.
2375 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002376 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return FAIL;
2378
2379 /*
2380 * Repeat computing, until no '*', '/' or '%' is following.
2381 */
2382 for (;;)
2383 {
2384 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002385 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 break;
2387
2388 if (evaluate)
2389 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390#ifdef FEAT_FLOAT
2391 if (rettv->v_type == VAR_FLOAT)
2392 {
2393 f1 = rettv->vval.v_float;
2394 use_float = TRUE;
2395 n1 = 0;
2396 }
2397 else
2398#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002399 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 if (error)
2402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 else
2405 n1 = 0;
2406
2407 /*
2408 * Get the second variable.
2409 */
2410 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002411 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 return FAIL;
2413
2414 if (evaluate)
2415 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002416#ifdef FEAT_FLOAT
2417 if (var2.v_type == VAR_FLOAT)
2418 {
2419 if (!use_float)
2420 {
2421 f1 = n1;
2422 use_float = TRUE;
2423 }
2424 f2 = var2.vval.v_float;
2425 n2 = 0;
2426 }
2427 else
2428#endif
2429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002430 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431 clear_tv(&var2);
2432 if (error)
2433 return FAIL;
2434#ifdef FEAT_FLOAT
2435 if (use_float)
2436 f2 = n2;
2437#endif
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /*
2441 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002442 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444#ifdef FEAT_FLOAT
2445 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002447 if (op == '*')
2448 f1 = f1 * f2;
2449 else if (op == '/')
2450 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002451# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002452 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002453 if (f2 == 0.0)
2454 {
2455 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002456 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002457 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002458 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002459 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002460 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002461 }
2462 else
2463 f1 = f1 / f2;
2464# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 // We rely on the floating point library to handle divide
2466 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002468# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002471 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002473 return FAIL;
2474 }
2475 rettv->v_type = VAR_FLOAT;
2476 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002481 if (op == '*')
2482 n1 = n1 * n2;
2483 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002484 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002486 n1 = num_modulus(n1, n2);
2487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002488 rettv->v_type = VAR_NUMBER;
2489 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 }
2493
2494 return OK;
2495}
2496
2497/*
2498 * Handle sixth level expression:
2499 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002500 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002501 * "string" string constant
2502 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * &option-name option value
2504 * @r register contents
2505 * identifier variable value
2506 * function() function call
2507 * $VAR environment variable
2508 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002509 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002511 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002512 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 *
2514 * Also handle:
2515 * ! in front logical NOT
2516 * - in front unary minus
2517 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 * trailing [] subscript in String or List
2519 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002520 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 *
2522 * "arg" must point to the first non-white of the expression.
2523 * "arg" is advanced to the next non-white after the recognized expression.
2524 *
2525 * Return OK or FAIL.
2526 */
2527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528eval7(
2529 char_u **arg,
2530 typval_T *rettv,
2531 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int len;
2535 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 char_u *start_leader, *end_leader;
2537 int ret = OK;
2538 char_u *alias;
2539
2540 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002542 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002547 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
2549 start_leader = *arg;
2550 while (**arg == '!' || **arg == '-' || **arg == '+')
2551 *arg = skipwhite(*arg + 1);
2552 end_leader = *arg;
2553
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002554 if (**arg == '.' && (!isdigit(*(*arg + 1))
2555#ifdef FEAT_FLOAT
2556 || current_sctx.sc_version < 2
2557#endif
2558 ))
2559 {
2560 semsg(_(e_invexpr2), *arg);
2561 ++*arg;
2562 return FAIL;
2563 }
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 switch (**arg)
2566 {
2567 /*
2568 * Number constant.
2569 */
2570 case '0':
2571 case '1':
2572 case '2':
2573 case '3':
2574 case '4':
2575 case '5':
2576 case '6':
2577 case '7':
2578 case '8':
2579 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 break;
2582
2583 /*
2584 * String constant: "string".
2585 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 break;
2588
2589 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002593 break;
2594
2595 /*
2596 * List: [expr, expr]
2597 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600
2601 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002602 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002603 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002604 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002605 {
2606 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002607 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002608 }
2609 else
2610 ret = NOTDONE;
2611 break;
2612
2613 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002614 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002615 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002617 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2618 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002619 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 break;
2621
2622 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002623 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002625 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 break;
2627
2628 /*
2629 * Environment variable: $VAR.
2630 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 break;
2633
2634 /*
2635 * Register contents: @r.
2636 */
2637 case '@': ++*arg;
2638 if (evaluate)
2639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002641 rettv->vval.v_string = get_reg_contents(**arg,
2642 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 if (**arg != NUL)
2645 ++*arg;
2646 break;
2647
2648 /*
2649 * nested expression: (expression).
2650 */
2651 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (**arg == ')')
2654 ++*arg;
2655 else if (ret == OK)
2656 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ret = FAIL;
2660 }
2661 break;
2662
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 break;
2665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666
2667 if (ret == NOTDONE)
2668 {
2669 /*
2670 * Must be a variable or function name.
2671 * Can also be a curly-braces kind of name: {expr}.
2672 */
2673 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002674 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (alias != NULL)
2676 s = alias;
2677
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002678 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 ret = FAIL;
2680 else
2681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002683 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002685 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002687 {
2688 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002692 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 *arg = skipwhite(*arg);
2696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2698 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002699 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002700 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2701 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 /*
2704 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2705 */
2706 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002707 ret = eval7_leader(rettv, start_leader, &end_leader);
2708 return ret;
2709}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002711/*
2712 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2713 * Adjusts "end_leaderp" until it is at "start_leader".
2714 */
2715 static int
2716eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2717{
2718 char_u *end_leader = *end_leaderp;
2719 int ret = OK;
2720 int error = FALSE;
2721 varnumber_T val = 0;
2722#ifdef FEAT_FLOAT
2723 float_T f = 0.0;
2724
2725 if (rettv->v_type == VAR_FLOAT)
2726 f = rettv->vval.v_float;
2727 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002728#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002729 val = tv_get_number_chk(rettv, &error);
2730 if (error)
2731 {
2732 clear_tv(rettv);
2733 ret = FAIL;
2734 }
2735 else
2736 {
2737 while (end_leader > start_leader)
2738 {
2739 --end_leader;
2740 if (*end_leader == '!')
2741 {
2742#ifdef FEAT_FLOAT
2743 if (rettv->v_type == VAR_FLOAT)
2744 f = !f;
2745 else
2746#endif
2747 val = !val;
2748 }
2749 else if (*end_leader == '-')
2750 {
2751#ifdef FEAT_FLOAT
2752 if (rettv->v_type == VAR_FLOAT)
2753 f = -f;
2754 else
2755#endif
2756 val = -val;
2757 }
2758 }
2759#ifdef FEAT_FLOAT
2760 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002766#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002768 clear_tv(rettv);
2769 rettv->v_type = VAR_NUMBER;
2770 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002773 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return ret;
2775}
2776
2777/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002778 * Call the function referred to in "rettv".
2779 */
2780 static int
2781call_func_rettv(
2782 char_u **arg,
2783 typval_T *rettv,
2784 int evaluate,
2785 dict_T *selfdict,
2786 typval_T *basetv)
2787{
2788 partial_T *pt = NULL;
2789 funcexe_T funcexe;
2790 typval_T functv;
2791 char_u *s;
2792 int ret;
2793
2794 // need to copy the funcref so that we can clear rettv
2795 if (evaluate)
2796 {
2797 functv = *rettv;
2798 rettv->v_type = VAR_UNKNOWN;
2799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002801 if (functv.v_type == VAR_PARTIAL)
2802 {
2803 pt = functv.vval.v_partial;
2804 s = partial_name(pt);
2805 }
2806 else
2807 s = functv.vval.v_string;
2808 }
2809 else
2810 s = (char_u *)"";
2811
Bram Moolenaara80faa82020-04-12 19:37:17 +02002812 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002813 funcexe.firstline = curwin->w_cursor.lnum;
2814 funcexe.lastline = curwin->w_cursor.lnum;
2815 funcexe.evaluate = evaluate;
2816 funcexe.partial = pt;
2817 funcexe.selfdict = selfdict;
2818 funcexe.basetv = basetv;
2819 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 // Clear the funcref afterwards, so that deleting it while
2822 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002823 if (evaluate)
2824 clear_tv(&functv);
2825
2826 return ret;
2827}
2828
2829/*
2830 * Evaluate "->method()".
2831 * "*arg" points to the '-'.
2832 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2833 */
2834 static int
2835eval_lambda(
2836 char_u **arg,
2837 typval_T *rettv,
2838 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002840{
2841 typval_T base = *rettv;
2842 int ret;
2843
2844 // Skip over the ->.
2845 *arg += 2;
2846 rettv->v_type = VAR_UNKNOWN;
2847
2848 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002849 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002850 return FAIL;
2851 else if (**arg != '(')
2852 {
2853 if (verbose)
2854 {
2855 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002856 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002857 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002859 }
2860 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002861 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002862 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002863 else
2864 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2865
2866 // Clear the funcref afterwards, so that deleting it while
2867 // evaluating the arguments is possible (see test55).
2868 if (evaluate)
2869 clear_tv(&base);
2870
2871 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002872}
2873
2874/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002875 * Evaluate "->method()".
2876 * "*arg" points to the '-'.
2877 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2878 */
2879 static int
2880eval_method(
2881 char_u **arg,
2882 typval_T *rettv,
2883 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002885{
2886 char_u *name;
2887 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002888 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002889 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002890 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002891
2892 // Skip over the ->.
2893 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002894 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002895
Bram Moolenaarac92e252019-08-03 21:58:38 +02002896 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002897 len = get_name_len(arg, &alias, evaluate, TRUE);
2898 if (alias != NULL)
2899 name = alias;
2900
2901 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002902 {
2903 if (verbose)
2904 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002906 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002907 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002908 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002909 if (**arg != '(')
2910 {
2911 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002913 ret = FAIL;
2914 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002915 else if (VIM_ISWHITE((*arg)[-1]))
2916 {
2917 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002918 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002919 ret = FAIL;
2920 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002921 else
2922 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002923 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002924
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002925 // Clear the funcref afterwards, so that deleting it while
2926 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 if (evaluate)
2928 clear_tv(&base);
2929
Bram Moolenaarac92e252019-08-03 21:58:38 +02002930 return ret;
2931}
2932
2933/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002934 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2935 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002936 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2937 */
2938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939eval_index(
2940 char_u **arg,
2941 typval_T *rettv,
2942 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002943 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002944{
2945 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002947 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002948 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002949 long len = -1;
2950 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002952 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002953
Bram Moolenaara03f2332016-02-06 18:09:59 +01002954 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002955 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002956 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002960 return FAIL;
2961 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002962#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002963 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002965 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002966#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002967 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002968 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002969 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002970 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002971 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002973 return FAIL;
2974 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002975 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002977 if (evaluate)
2978 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002979 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002980
2981 case VAR_STRING:
2982 case VAR_NUMBER:
2983 case VAR_LIST:
2984 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002985 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002986 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002989 init_tv(&var1);
2990 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002991 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 /*
2994 * dict.name
2995 */
2996 key = *arg + 1;
2997 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2998 ;
2999 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003002 }
3003 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003005 /*
3006 * something[idx]
3007 *
3008 * Get the (first) variable from inside the [].
3009 */
3010 *arg = skipwhite(*arg + 1);
3011 if (**arg == ':')
3012 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003014 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003015 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003017 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 clear_tv(&var1);
3019 return FAIL;
3020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021
3022 /*
3023 * Get the second variable from inside the [:].
3024 */
3025 if (**arg == ':')
3026 {
3027 range = TRUE;
3028 *arg = skipwhite(*arg + 1);
3029 if (**arg == ']')
3030 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003031 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 if (!empty1)
3034 clear_tv(&var1);
3035 return FAIL;
3036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003039 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003040 if (!empty1)
3041 clear_tv(&var1);
3042 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 return FAIL;
3044 }
3045 }
3046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003047 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048 if (**arg != ']')
3049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052 clear_tv(&var1);
3053 if (range)
3054 clear_tv(&var2);
3055 return FAIL;
3056 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003057 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003058 }
3059
3060 if (evaluate)
3061 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003062 n1 = 0;
3063 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003064 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003067 }
3068 if (range)
3069 {
3070 if (empty2)
3071 n2 = -1;
3072 else
3073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 }
3077 }
3078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003081 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003082 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003084 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003085 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003086 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003087 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003088 case VAR_SPECIAL:
3089 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003091 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003092
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 case VAR_NUMBER:
3094 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003095 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 len = (long)STRLEN(s);
3097 if (range)
3098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 // The resulting variable is a substring. If the indexes
3100 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 if (n1 < 0)
3102 {
3103 n1 = len + n1;
3104 if (n1 < 0)
3105 n1 = 0;
3106 }
3107 if (n2 < 0)
3108 n2 = len + n2;
3109 else if (n2 >= len)
3110 n2 = len;
3111 if (n1 >= len || n2 < 0 || n1 > n2)
3112 s = NULL;
3113 else
3114 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3115 }
3116 else
3117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 // The resulting variable is a string of a single
3119 // character. If the index is too big or negative the
3120 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003121 if (n1 >= len || n1 < 0)
3122 s = NULL;
3123 else
3124 s = vim_strnsave(s + n1, 1);
3125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003126 clear_tv(rettv);
3127 rettv->v_type = VAR_STRING;
3128 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003129 break;
3130
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003131 case VAR_BLOB:
3132 len = blob_len(rettv->vval.v_blob);
3133 if (range)
3134 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003135 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003136 // are out of range the result is empty.
3137 if (n1 < 0)
3138 {
3139 n1 = len + n1;
3140 if (n1 < 0)
3141 n1 = 0;
3142 }
3143 if (n2 < 0)
3144 n2 = len + n2;
3145 else if (n2 >= len)
3146 n2 = len - 1;
3147 if (n1 >= len || n2 < 0 || n1 > n2)
3148 {
3149 clear_tv(rettv);
3150 rettv->v_type = VAR_BLOB;
3151 rettv->vval.v_blob = NULL;
3152 }
3153 else
3154 {
3155 blob_T *blob = blob_alloc();
3156
3157 if (blob != NULL)
3158 {
3159 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3160 {
3161 blob_free(blob);
3162 return FAIL;
3163 }
3164 blob->bv_ga.ga_len = n2 - n1 + 1;
3165 for (i = n1; i <= n2; i++)
3166 blob_set(blob, i - n1,
3167 blob_get(rettv->vval.v_blob, i));
3168
3169 clear_tv(rettv);
3170 rettv_blob_set(rettv, blob);
3171 }
3172 }
3173 }
3174 else
3175 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003176 // The resulting variable is a byte value.
3177 // If the index is too big or negative that is an error.
3178 if (n1 < 0)
3179 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003180 if (n1 < len && n1 >= 0)
3181 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003182 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003183
3184 clear_tv(rettv);
3185 rettv->v_type = VAR_NUMBER;
3186 rettv->vval.v_number = v;
3187 }
3188 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003189 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003190 }
3191 break;
3192
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003193 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003195 if (n1 < 0)
3196 n1 = len + n1;
3197 if (!empty1 && (n1 < 0 || n1 >= len))
3198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003199 // For a range we allow invalid values and return an empty
3200 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003201 if (!range)
3202 {
3203 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003204 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003205 return FAIL;
3206 }
3207 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003208 }
3209 if (range)
3210 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 list_T *l;
3212 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003213
3214 if (n2 < 0)
3215 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003216 else if (n2 >= len)
3217 n2 = len - 1;
3218 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003219 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003220 l = list_alloc();
3221 if (l == NULL)
3222 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003223 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 n1 <= n2; ++n1)
3225 {
3226 if (list_append_tv(l, &item->li_tv) == FAIL)
3227 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003228 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003229 return FAIL;
3230 }
3231 item = item->li_next;
3232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003234 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 }
3236 else
3237 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003238 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 clear_tv(rettv);
3240 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003241 }
3242 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243
3244 case VAR_DICT:
3245 if (range)
3246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003247 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003248 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003249 if (len == -1)
3250 clear_tv(&var1);
3251 return FAIL;
3252 }
3253 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003255
3256 if (len == -1)
3257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003258 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003259 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261 clear_tv(&var1);
3262 return FAIL;
3263 }
3264 }
3265
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003266 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003268 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003269 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003270 if (len == -1)
3271 clear_tv(&var1);
3272 if (item == NULL)
3273 return FAIL;
3274
3275 copy_tv(&item->di_tv, &var1);
3276 clear_tv(rettv);
3277 *rettv = var1;
3278 }
3279 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003280 }
3281 }
3282
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 return OK;
3284}
3285
3286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 * Get an option value.
3288 * "arg" points to the '&' or '+' before the option name.
3289 * "arg" is advanced to character after the option name.
3290 * Return OK or FAIL.
3291 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293get_option_tv(
3294 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003295 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003296 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 char_u *option_end;
3299 long numval;
3300 char_u *stringval;
3301 int opt_type;
3302 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003303 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int ret = OK;
3305 int opt_flags;
3306
3307 /*
3308 * Isolate the option name and find its value.
3309 */
3310 option_end = find_option_end(arg, &opt_flags);
3311 if (option_end == NULL)
3312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return FAIL;
3316 }
3317
3318 if (!evaluate)
3319 {
3320 *arg = option_end;
3321 return OK;
3322 }
3323
3324 c = *option_end;
3325 *option_end = NUL;
3326 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003329 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 ret = FAIL;
3334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003339 rettv->v_type = VAR_STRING;
3340 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003342 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344 rettv->v_type = VAR_NUMBER;
3345 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 rettv->v_type = VAR_NUMBER;
3350 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 rettv->v_type = VAR_STRING;
3355 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357 }
3358 else if (working && (opt_type == -2 || opt_type == -1))
3359 ret = FAIL;
3360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 *arg = option_end;
3363
3364 return ret;
3365}
3366
3367/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3369 * Return OK or FAIL.
3370 */
3371 int
3372get_number_tv(
3373 char_u **arg,
3374 typval_T *rettv,
3375 int evaluate,
3376 int want_string UNUSED)
3377{
3378 int len;
3379#ifdef FEAT_FLOAT
3380 char_u *p;
3381 int get_float = FALSE;
3382
3383 // We accept a float when the format matches
3384 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3385 // strict to avoid backwards compatibility problems.
3386 // With script version 2 and later the leading digit can be
3387 // omitted.
3388 // Don't look for a float after the "." operator, so that
3389 // ":let vers = 1.2.3" doesn't fail.
3390 if (**arg == '.')
3391 p = *arg;
3392 else
3393 p = skipdigits(*arg + 1);
3394 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3395 {
3396 get_float = TRUE;
3397 p = skipdigits(p + 2);
3398 if (*p == 'e' || *p == 'E')
3399 {
3400 ++p;
3401 if (*p == '-' || *p == '+')
3402 ++p;
3403 if (!vim_isdigit(*p))
3404 get_float = FALSE;
3405 else
3406 p = skipdigits(p + 1);
3407 }
3408 if (ASCII_ISALPHA(*p) || *p == '.')
3409 get_float = FALSE;
3410 }
3411 if (get_float)
3412 {
3413 float_T f;
3414
3415 *arg += string2float(*arg, &f);
3416 if (evaluate)
3417 {
3418 rettv->v_type = VAR_FLOAT;
3419 rettv->vval.v_float = f;
3420 }
3421 }
3422 else
3423#endif
3424 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3425 {
3426 char_u *bp;
3427 blob_T *blob = NULL; // init for gcc
3428
3429 // Blob constant: 0z0123456789abcdef
3430 if (evaluate)
3431 blob = blob_alloc();
3432 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3433 {
3434 if (!vim_isxdigit(bp[1]))
3435 {
3436 if (blob != NULL)
3437 {
3438 emsg(_("E973: Blob literal should have an even number of hex characters"));
3439 ga_clear(&blob->bv_ga);
3440 VIM_CLEAR(blob);
3441 }
3442 return FAIL;
3443 }
3444 if (blob != NULL)
3445 ga_append(&blob->bv_ga,
3446 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3447 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3448 ++bp;
3449 }
3450 if (blob != NULL)
3451 rettv_blob_set(rettv, blob);
3452 *arg = bp;
3453 }
3454 else
3455 {
3456 varnumber_T n;
3457
3458 // decimal, hex or octal number
3459 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3460 ? STR2NR_NO_OCT + STR2NR_QUOTE
3461 : STR2NR_ALL, &n, NULL, 0, TRUE);
3462 if (len == 0)
3463 {
3464 semsg(_(e_invexpr2), *arg);
3465 return FAIL;
3466 }
3467 *arg += len;
3468 if (evaluate)
3469 {
3470 rettv->v_type = VAR_NUMBER;
3471 rettv->vval.v_number = n;
3472 }
3473 }
3474 return OK;
3475}
3476
3477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * Allocate a variable for a string constant.
3479 * Return OK or FAIL.
3480 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003482get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 char_u *p;
3485 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int extra = 0;
3487
3488 /*
3489 * Find the end of the string, skipping backslashed characters.
3490 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003491 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 if (*p == '\\' && p[1] != NUL)
3494 {
3495 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // A "\<x>" form occupies at least 4 characters, and produces up
3497 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (*p == '<')
3499 extra += 2;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
3503 if (*p != '"')
3504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003505 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return FAIL;
3507 }
3508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003509 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (!evaluate)
3511 {
3512 *arg = p + 1;
3513 return OK;
3514 }
3515
3516 /*
3517 * Copy the string into allocated memory, handling backslashed
3518 * characters.
3519 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003520 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (name == NULL)
3522 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 rettv->v_type = VAR_STRING;
3524 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 if (*p == '\\')
3529 {
3530 switch (*++p)
3531 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 case 'b': *name++ = BS; ++p; break;
3533 case 'e': *name++ = ESC; ++p; break;
3534 case 'f': *name++ = FF; ++p; break;
3535 case 'n': *name++ = NL; ++p; break;
3536 case 'r': *name++ = CAR; ++p; break;
3537 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003541 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 case 'U':
3543 if (vim_isxdigit(p[1]))
3544 {
3545 int n, nr;
3546 int c = toupper(*p);
3547
3548 if (c == 'X')
3549 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003550 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003552 else
3553 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 nr = 0;
3555 while (--n >= 0 && vim_isxdigit(p[1]))
3556 {
3557 ++p;
3558 nr = (nr << 4) + hex2nr(*p);
3559 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 // For "\u" store the number according to
3562 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003570 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 case '0':
3572 case '1':
3573 case '2':
3574 case '3':
3575 case '4':
3576 case '5':
3577 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 case '7': *name = *p++ - '0';
3579 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 *name = (*name << 3) + *p++ - '0';
3582 if (*p >= '0' && *p <= '7')
3583 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003589 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3590 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (extra != 0)
3592 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 break;
3595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003596 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 break;
3600 }
3601 }
3602 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003608 ++p;
3609 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return OK;
3612}
3613
3614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * Return OK or FAIL.
3617 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003619get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003622 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003623 int reduce = 0;
3624
3625 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003627 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003633 break;
3634 ++reduce;
3635 ++p;
3636 }
3637 }
3638
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003641 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003642 return FAIL;
3643 }
3644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003645 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003646 if (!evaluate)
3647 {
3648 *arg = p + 1;
3649 return OK;
3650 }
3651
3652 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003654 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003655 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003656 if (str == NULL)
3657 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 rettv->v_type = VAR_STRING;
3659 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003666 break;
3667 ++p;
3668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003672 *arg = p + 1;
3673
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003674 return OK;
3675}
3676
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003677/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003678 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003679 */
3680 char_u *
3681partial_name(partial_T *pt)
3682{
3683 if (pt->pt_name != NULL)
3684 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003685 if (pt->pt_func != NULL)
3686 return pt->pt_func->uf_name;
3687 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003688}
3689
Bram Moolenaarddecc252016-04-06 22:59:37 +02003690 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003691partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003692{
3693 int i;
3694
3695 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003696 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003697 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003698 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003699 if (pt->pt_name != NULL)
3700 {
3701 func_unref(pt->pt_name);
3702 vim_free(pt->pt_name);
3703 }
3704 else
3705 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003706
3707 if (pt->pt_funcstack != NULL)
3708 {
3709 // Decrease the reference count for the context of a closure. If down
3710 // to zero free it and clear the variables on the stack.
3711 if (--pt->pt_funcstack->fs_refcount == 0)
3712 {
3713 garray_T *gap = &pt->pt_funcstack->fs_ga;
3714 typval_T *stack = gap->ga_data;
3715
3716 for (i = 0; i < gap->ga_len; ++i)
3717 clear_tv(stack + i);
3718 ga_clear(gap);
3719 vim_free(pt->pt_funcstack);
3720 }
3721 pt->pt_funcstack = NULL;
3722 }
3723
Bram Moolenaarddecc252016-04-06 22:59:37 +02003724 vim_free(pt);
3725}
3726
3727/*
3728 * Unreference a closure: decrement the reference count and free it when it
3729 * becomes zero.
3730 */
3731 void
3732partial_unref(partial_T *pt)
3733{
3734 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003735 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003736}
3737
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003738static int tv_equal_recurse_limit;
3739
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003740 static int
3741func_equal(
3742 typval_T *tv1,
3743 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003745{
3746 char_u *s1, *s2;
3747 dict_T *d1, *d2;
3748 int a1, a2;
3749 int i;
3750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003751 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003752 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003754 if (s1 != NULL && *s1 == NUL)
3755 s1 = NULL;
3756 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003757 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003758 if (s2 != NULL && *s2 == NUL)
3759 s2 = NULL;
3760 if (s1 == NULL || s2 == NULL)
3761 {
3762 if (s1 != s2)
3763 return FALSE;
3764 }
3765 else if (STRCMP(s1, s2) != 0)
3766 return FALSE;
3767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003769 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3770 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3771 if (d1 == NULL || d2 == NULL)
3772 {
3773 if (d1 != d2)
3774 return FALSE;
3775 }
3776 else if (!dict_equal(d1, d2, ic, TRUE))
3777 return FALSE;
3778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003779 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003780 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3781 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3782 if (a1 != a2)
3783 return FALSE;
3784 for (i = 0; i < a1; ++i)
3785 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3786 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3787 return FALSE;
3788
3789 return TRUE;
3790}
3791
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003792/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003793 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003794 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003795 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003796 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003798tv_equal(
3799 typval_T *tv1,
3800 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003801 int ic, // ignore case
3802 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003803{
3804 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003805 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003806 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003807 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 // Catch lists and dicts that have an endless loop by limiting
3810 // recursiveness to a limit. We guess they are equal then.
3811 // A fixed limit has the problem of still taking an awful long time.
3812 // Reduce the limit every time running into it. That should work fine for
3813 // deeply linked structures that are not recursively linked and catch
3814 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003815 if (!recursive)
3816 tv_equal_recurse_limit = 1000;
3817 if (recursive_cnt >= tv_equal_recurse_limit)
3818 {
3819 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003820 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003821 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3824 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003825 if ((tv1->v_type == VAR_FUNC
3826 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3827 && (tv2->v_type == VAR_FUNC
3828 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3829 {
3830 ++recursive_cnt;
3831 r = func_equal(tv1, tv2, ic);
3832 --recursive_cnt;
3833 return r;
3834 }
3835
3836 if (tv1->v_type != tv2->v_type)
3837 return FALSE;
3838
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003839 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003840 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003841 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003842 ++recursive_cnt;
3843 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3844 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003845 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003846
3847 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003848 ++recursive_cnt;
3849 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3850 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003851 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003852
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003853 case VAR_BLOB:
3854 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3855
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003856 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 case VAR_BOOL:
3858 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003859 return tv1->vval.v_number == tv2->vval.v_number;
3860
3861 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003862 s1 = tv_get_string_buf(tv1, buf1);
3863 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003864 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003865
Bram Moolenaar835dc632016-02-07 14:27:38 +01003866 case VAR_FLOAT:
3867#ifdef FEAT_FLOAT
3868 return tv1->vval.v_float == tv2->vval.v_float;
3869#endif
3870 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003871#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003872 return tv1->vval.v_job == tv2->vval.v_job;
3873#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003874 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003875#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003876 return tv1->vval.v_channel == tv2->vval.v_channel;
3877#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003879 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003880 return tv1->vval.v_partial == tv2->vval.v_partial;
3881
3882 case VAR_FUNC:
3883 return tv1->vval.v_string == tv2->vval.v_string;
3884
Bram Moolenaar835dc632016-02-07 14:27:38 +01003885 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003886 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003888 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003889 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003891 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3892 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003893 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003894}
3895
3896/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003897 * Return the next (unique) copy ID.
3898 * Used for serializing nested structures.
3899 */
3900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003901get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003902{
3903 current_copyID += COPYID_INC;
3904 return current_copyID;
3905}
3906
3907/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 * Garbage collection for lists and dictionaries.
3909 *
3910 * We use reference counts to be able to free most items right away when they
3911 * are no longer used. But for composite items it's possible that it becomes
3912 * unused while the reference count is > 0: When there is a recursive
3913 * reference. Example:
3914 * :let l = [1, 2, 3]
3915 * :let d = {9: l}
3916 * :let l[1] = d
3917 *
3918 * Since this is quite unusual we handle this with garbage collection: every
3919 * once in a while find out which lists and dicts are not referenced from any
3920 * variable.
3921 *
3922 * Here is a good reference text about garbage collection (refers to Python
3923 * but it applies to all reference-counting mechanisms):
3924 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003925 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003926
3927/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003928 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003929 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003930 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003931 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003932 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003933garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003934{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003935 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003936 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003937 buf_T *buf;
3938 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003939 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003940 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003941
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003942 if (!testing)
3943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003945 want_garbage_collect = FALSE;
3946 may_garbage_collect = FALSE;
3947 garbage_collect_at_exit = FALSE;
3948 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003949
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003950 // The execution stack can grow big, limit the size.
3951 if (exestack.ga_maxlen - exestack.ga_len > 500)
3952 {
3953 size_t new_len;
3954 char_u *pp;
3955 int n;
3956
3957 // Keep 150% of the current size, with a minimum of the growth size.
3958 n = exestack.ga_len / 2;
3959 if (n < exestack.ga_growsize)
3960 n = exestack.ga_growsize;
3961
3962 // Don't make it bigger though.
3963 if (exestack.ga_len + n < exestack.ga_maxlen)
3964 {
3965 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3966 pp = vim_realloc(exestack.ga_data, new_len);
3967 if (pp == NULL)
3968 return FAIL;
3969 exestack.ga_maxlen = exestack.ga_len + n;
3970 exestack.ga_data = pp;
3971 }
3972 }
3973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 // We advance by two because we add one for items referenced through
3975 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003976 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003978 /*
3979 * 1. Go through all accessible variables and mark all lists and dicts
3980 * with copyID.
3981 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003983 // Don't free variables in the previous_funccal list unless they are only
3984 // referenced through previous_funccal. This must be first, because if
3985 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003986 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003988 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003989 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003991 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003992 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003993 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3994 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003997 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003998 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3999 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004000 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004001 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4002 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004003#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004004 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004005 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4006 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004007 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004008 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004009 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4010 NULL, NULL);
4011#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004013 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004014 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004015 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4016 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004018 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004020 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004024 abort = abort || set_ref_in_functions(copyID);
4025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004026 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004030 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004031
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004032 // callbacks in buffers
4033 abort = abort || set_ref_in_buffers(copyID);
4034
Bram Moolenaar1dced572012-04-05 16:54:08 +02004035#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004036 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004037#endif
4038
Bram Moolenaardb913952012-06-29 12:54:53 +02004039#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004040 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004041#endif
4042
4043#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004044 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004045#endif
4046
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004047#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004048 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004049 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004050#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004051#ifdef FEAT_NETBEANS_INTG
4052 abort = abort || set_ref_in_nb_channel(copyID);
4053#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004054
Bram Moolenaare3188e22016-05-31 21:13:04 +02004055#ifdef FEAT_TIMERS
4056 abort = abort || set_ref_in_timer(copyID);
4057#endif
4058
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004059#ifdef FEAT_QUICKFIX
4060 abort = abort || set_ref_in_quickfix(copyID);
4061#endif
4062
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004063#ifdef FEAT_TERMINAL
4064 abort = abort || set_ref_in_term(copyID);
4065#endif
4066
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004067#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004068 abort = abort || set_ref_in_popups(copyID);
4069#endif
4070
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004072 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 /*
4074 * 2. Free lists and dictionaries that are not referenced.
4075 */
4076 did_free = free_unref_items(copyID);
4077
4078 /*
4079 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004083 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004084 else if (p_verbose > 0)
4085 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004086 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004088
4089 return did_free;
4090}
4091
4092/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004093 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004094 */
4095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004096free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004097{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004098 int did_free = FALSE;
4099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004100 // Let all "free" functions know that we are here. This means no
4101 // dictionaries, lists, channels or jobs are to be freed, because we will
4102 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004103 in_free_unref_items = TRUE;
4104
4105 /*
4106 * PASS 1: free the contents of the items. We don't free the items
4107 * themselves yet, so that it is possible to decrement refcount counters
4108 */
4109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004110 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004111 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004113 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004114 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004115
4116#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004117 // Go through the list of jobs and free items without the copyID. This
4118 // must happen before doing channels, because jobs refer to channels, but
4119 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004120 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004122 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004123 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4124#endif
4125
4126 /*
4127 * PASS 2: free the items themselves.
4128 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004129 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004130 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004132#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004133 // Go through the list of jobs and free items without the copyID. This
4134 // must happen before doing channels, because jobs refer to channels, but
4135 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004136 free_unused_jobs(copyID, COPYID_MASK);
4137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 free_unused_channels(copyID, COPYID_MASK);
4140#endif
4141
4142 in_free_unref_items = FALSE;
4143
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004144 return did_free;
4145}
4146
4147/*
4148 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004149 * "list_stack" is used to add lists to be marked. Can be NULL.
4150 *
4151 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004152 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004154set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004155{
4156 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004157 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004158 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004159 hashtab_T *cur_ht;
4160 ht_stack_T *ht_stack = NULL;
4161 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004162
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004163 cur_ht = ht;
4164 for (;;)
4165 {
4166 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004168 // Mark each item in the hashtab. If the item contains a hashtab
4169 // it is added to ht_stack, if it contains a list it is added to
4170 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 todo = (int)cur_ht->ht_used;
4172 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4173 if (!HASHITEM_EMPTY(hi))
4174 {
4175 --todo;
4176 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4177 &ht_stack, list_stack);
4178 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004179 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004180
4181 if (ht_stack == NULL)
4182 break;
4183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004184 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004185 cur_ht = ht_stack->ht;
4186 tempitem = ht_stack;
4187 ht_stack = ht_stack->prev;
4188 free(tempitem);
4189 }
4190
4191 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004192}
4193
4194/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004195 * Mark a dict and its items with "copyID".
4196 * Returns TRUE if setting references failed somehow.
4197 */
4198 int
4199set_ref_in_dict(dict_T *d, int copyID)
4200{
4201 if (d != NULL && d->dv_copyID != copyID)
4202 {
4203 d->dv_copyID = copyID;
4204 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4205 }
4206 return FALSE;
4207}
4208
4209/*
4210 * Mark a list and its items with "copyID".
4211 * Returns TRUE if setting references failed somehow.
4212 */
4213 int
4214set_ref_in_list(list_T *ll, int copyID)
4215{
4216 if (ll != NULL && ll->lv_copyID != copyID)
4217 {
4218 ll->lv_copyID = copyID;
4219 return set_ref_in_list_items(ll, copyID, NULL);
4220 }
4221 return FALSE;
4222}
4223
4224/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004225 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004226 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4227 *
4228 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004229 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004230 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004231set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004232{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004233 listitem_T *li;
4234 int abort = FALSE;
4235 list_T *cur_l;
4236 list_stack_T *list_stack = NULL;
4237 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004238
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004239 cur_l = l;
4240 for (;;)
4241 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004242 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004243 // Mark each item in the list. If the item contains a hashtab
4244 // it is added to ht_stack, if it contains a list it is added to
4245 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004246 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4247 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4248 ht_stack, &list_stack);
4249 if (list_stack == NULL)
4250 break;
4251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004252 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004253 cur_l = list_stack->list;
4254 tempitem = list_stack;
4255 list_stack = list_stack->prev;
4256 free(tempitem);
4257 }
4258
4259 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004260}
4261
4262/*
4263 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004264 * "list_stack" is used to add lists to be marked. Can be NULL.
4265 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4266 *
4267 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004268 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004270set_ref_in_item(
4271 typval_T *tv,
4272 int copyID,
4273 ht_stack_T **ht_stack,
4274 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004275{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004276 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004277
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004278 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004279 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004280 dict_T *dd = tv->vval.v_dict;
4281
Bram Moolenaara03f2332016-02-06 18:09:59 +01004282 if (dd != NULL && dd->dv_copyID != copyID)
4283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004285 dd->dv_copyID = copyID;
4286 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004287 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4289 }
4290 else
4291 {
4292 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4293 if (newitem == NULL)
4294 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004295 else
4296 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004297 newitem->ht = &dd->dv_hashtab;
4298 newitem->prev = *ht_stack;
4299 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004300 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004301 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004302 }
4303 }
4304 else if (tv->v_type == VAR_LIST)
4305 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004306 list_T *ll = tv->vval.v_list;
4307
Bram Moolenaara03f2332016-02-06 18:09:59 +01004308 if (ll != NULL && ll->lv_copyID != copyID)
4309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004310 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004311 ll->lv_copyID = copyID;
4312 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004313 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004314 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004315 }
4316 else
4317 {
4318 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004320 if (newitem == NULL)
4321 abort = TRUE;
4322 else
4323 {
4324 newitem->list = ll;
4325 newitem->prev = *list_stack;
4326 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004327 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004328 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004329 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004330 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004331 else if (tv->v_type == VAR_FUNC)
4332 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004333 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004334 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004335 else if (tv->v_type == VAR_PARTIAL)
4336 {
4337 partial_T *pt = tv->vval.v_partial;
4338 int i;
4339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004341 if (pt != NULL)
4342 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004343 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004344
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004345 if (pt->pt_dict != NULL)
4346 {
4347 typval_T dtv;
4348
4349 dtv.v_type = VAR_DICT;
4350 dtv.vval.v_dict = pt->pt_dict;
4351 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4352 }
4353
4354 for (i = 0; i < pt->pt_argc; ++i)
4355 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4356 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004357 if (pt->pt_funcstack != NULL)
4358 {
4359 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4360
4361 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4362 abort = abort || set_ref_in_item(stack + i, copyID,
4363 ht_stack, list_stack);
4364 }
4365
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 }
4367 }
4368#ifdef FEAT_JOB_CHANNEL
4369 else if (tv->v_type == VAR_JOB)
4370 {
4371 job_T *job = tv->vval.v_job;
4372 typval_T dtv;
4373
4374 if (job != NULL && job->jv_copyID != copyID)
4375 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004376 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004377 if (job->jv_channel != NULL)
4378 {
4379 dtv.v_type = VAR_CHANNEL;
4380 dtv.vval.v_channel = job->jv_channel;
4381 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4382 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004383 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384 {
4385 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004386 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4388 }
4389 }
4390 }
4391 else if (tv->v_type == VAR_CHANNEL)
4392 {
4393 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004394 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 typval_T dtv;
4396 jsonq_T *jq;
4397 cbq_T *cq;
4398
4399 if (ch != NULL && ch->ch_copyID != copyID)
4400 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004401 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004402 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004403 {
4404 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4405 jq = jq->jq_next)
4406 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4407 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4408 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004409 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004410 {
4411 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004412 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4414 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004415 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004416 {
4417 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004418 dtv.vval.v_partial =
4419 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004420 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4421 }
4422 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004423 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424 {
4425 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004426 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004427 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4428 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004429 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 {
4431 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004432 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004433 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4434 }
4435 }
4436 }
4437#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004438 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004439}
4440
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 * Return a string with the string representation of a variable.
4443 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004445 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004446 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4447 * stings as "string()", otherwise does not put quotes around strings, as
4448 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004449 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4450 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004451 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004453 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004454echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455 typval_T *tv,
4456 char_u **tofree,
4457 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004458 int copyID,
4459 int echo_style,
4460 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004461 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004463 static int recurse = 0;
4464 char_u *r = NULL;
4465
Bram Moolenaar33570922005-01-25 22:26:29 +00004466 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004467 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004468 if (!did_echo_string_emsg)
4469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470 // Only give this message once for a recursive call to avoid
4471 // flooding the user with errors. And stop iterating over lists
4472 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004473 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004474 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004476 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004477 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004478 }
4479 ++recurse;
4480
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 switch (tv->v_type)
4482 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004483 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004484 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004485 {
4486 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004487 r = tv->vval.v_string;
4488 if (r == NULL)
4489 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004490 }
4491 else
4492 {
4493 *tofree = string_quote(tv->vval.v_string, FALSE);
4494 r = *tofree;
4495 }
4496 break;
4497
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004499 if (echo_style)
4500 {
4501 *tofree = NULL;
4502 r = tv->vval.v_string;
4503 }
4504 else
4505 {
4506 *tofree = string_quote(tv->vval.v_string, TRUE);
4507 r = *tofree;
4508 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004509 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004510
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004511 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004512 {
4513 partial_T *pt = tv->vval.v_partial;
4514 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004516 garray_T ga;
4517 int i;
4518 char_u *tf;
4519
4520 ga_init2(&ga, 1, 100);
4521 ga_concat(&ga, (char_u *)"function(");
4522 if (fname != NULL)
4523 {
4524 ga_concat(&ga, fname);
4525 vim_free(fname);
4526 }
4527 if (pt != NULL && pt->pt_argc > 0)
4528 {
4529 ga_concat(&ga, (char_u *)", [");
4530 for (i = 0; i < pt->pt_argc; ++i)
4531 {
4532 if (i > 0)
4533 ga_concat(&ga, (char_u *)", ");
4534 ga_concat(&ga,
4535 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4536 vim_free(tf);
4537 }
4538 ga_concat(&ga, (char_u *)"]");
4539 }
4540 if (pt != NULL && pt->pt_dict != NULL)
4541 {
4542 typval_T dtv;
4543
4544 ga_concat(&ga, (char_u *)", ");
4545 dtv.v_type = VAR_DICT;
4546 dtv.vval.v_dict = pt->pt_dict;
4547 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4548 vim_free(tf);
4549 }
4550 ga_concat(&ga, (char_u *)")");
4551
4552 *tofree = ga.ga_data;
4553 r = *tofree;
4554 break;
4555 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004556
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004557 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004558 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004559 break;
4560
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004562 if (tv->vval.v_list == NULL)
4563 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004564 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004565 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004566 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004567 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004568 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4569 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004570 {
4571 *tofree = NULL;
4572 r = (char_u *)"[...]";
4573 }
4574 else
4575 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004576 int old_copyID = tv->vval.v_list->lv_copyID;
4577
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004578 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004579 *tofree = list2string(tv, copyID, restore_copyID);
4580 if (restore_copyID)
4581 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004582 r = *tofree;
4583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004584 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004585
Bram Moolenaar8c711452005-01-14 21:53:12 +00004586 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004587 if (tv->vval.v_dict == NULL)
4588 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004589 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004590 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004591 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004592 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004593 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4594 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004595 {
4596 *tofree = NULL;
4597 r = (char_u *)"{...}";
4598 }
4599 else
4600 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004601 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004602
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004603 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004604 *tofree = dict2string(tv, copyID, restore_copyID);
4605 if (restore_copyID)
4606 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004607 r = *tofree;
4608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004609 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004610
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004612 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004613 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004615 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004616 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004617 break;
4618
Bram Moolenaar835dc632016-02-07 14:27:38 +01004619 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004620 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004621 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004622 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004623 if (composite_val)
4624 {
4625 *tofree = string_quote(r, FALSE);
4626 r = *tofree;
4627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004631#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 *tofree = NULL;
4633 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4634 r = numbuf;
4635 break;
4636#endif
4637
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004638 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004639 case VAR_SPECIAL:
4640 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004641 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004642 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004644
Bram Moolenaar8502c702014-06-17 12:51:16 +02004645 if (--recurse == 0)
4646 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004647 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004648}
4649
4650/*
4651 * Return a string with the string representation of a variable.
4652 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4653 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004654 * Does not put quotes around strings, as ":echo" displays values.
4655 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4656 * May return NULL.
4657 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004658 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004659echo_string(
4660 typval_T *tv,
4661 char_u **tofree,
4662 char_u *numbuf,
4663 int copyID)
4664{
4665 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4666}
4667
4668/*
4669 * Return a string with the string representation of a variable.
4670 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4671 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004672 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004673 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004674 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004675 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004676tv2string(
4677 typval_T *tv,
4678 char_u **tofree,
4679 char_u *numbuf,
4680 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004681{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004682 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683}
4684
4685/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 * Return string "str" in ' quotes, doubling ' characters.
4687 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004689 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004690 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004692{
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004694 char_u *p, *r, *s;
4695
Bram Moolenaar33570922005-01-25 22:26:29 +00004696 len = (function ? 13 : 3);
4697 if (str != NULL)
4698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004699 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004700 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 if (*p == '\'')
4702 ++len;
4703 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004704 s = r = alloc(len);
4705 if (r != NULL)
4706 {
4707 if (function)
4708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710 r += 10;
4711 }
4712 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 if (str != NULL)
4715 for (p = str; *p != NUL; )
4716 {
4717 if (*p == '\'')
4718 *r++ = '\'';
4719 MB_COPY_CHAR(p, r);
4720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004721 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004722 if (function)
4723 *r++ = ')';
4724 *r++ = NUL;
4725 }
4726 return s;
4727}
4728
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004729#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730/*
4731 * Convert the string "text" to a floating point number.
4732 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4733 * this always uses a decimal point.
4734 * Returns the length of the text that was consumed.
4735 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004737string2float(
4738 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004740{
4741 char *s = (char *)text;
4742 float_T f;
4743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004744 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004745 if (STRNICMP(text, "inf", 3) == 0)
4746 {
4747 *value = INFINITY;
4748 return 3;
4749 }
4750 if (STRNICMP(text, "-inf", 3) == 0)
4751 {
4752 *value = -INFINITY;
4753 return 4;
4754 }
4755 if (STRNICMP(text, "nan", 3) == 0)
4756 {
4757 *value = NAN;
4758 return 3;
4759 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760 f = strtod(s, &s);
4761 *value = f;
4762 return (int)((char_u *)s - text);
4763}
4764#endif
4765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 * Get the value of an environment variable.
4768 * "arg" is pointing to the '$'. It is advanced to after the name.
4769 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004770 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004773get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 char_u *string = NULL;
4776 int len;
4777 int cc;
4778 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004779 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 ++*arg;
4782 name = *arg;
4783 len = get_env_len(arg);
4784 if (evaluate)
4785 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004786 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004788
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004789 cc = name[len];
4790 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004792 string = vim_getenv(name, &mustfree);
4793 if (string != NULL && *string != NUL)
4794 {
4795 if (!mustfree)
4796 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004798 else
4799 {
4800 if (mustfree)
4801 vim_free(string);
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004804 string = expand_env_save(name - 1);
4805 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004806 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004807 }
4808 name[len] = cc;
4809
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 rettv->v_type = VAR_STRING;
4811 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 }
4813
4814 return OK;
4815}
4816
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004819 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004821 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004822var2fpos(
4823 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 int dollar_lnum, // TRUE when $ is last line
4825 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004827 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004829 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004832 if (varp->v_type == VAR_LIST)
4833 {
4834 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004835 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004836 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004837 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004838
4839 l = varp->vval.v_list;
4840 if (l == NULL)
4841 return NULL;
4842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004844 pos.lnum = list_find_nr(l, 0L, &error);
4845 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004848 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004849 pos.col = list_find_nr(l, 1L, &error);
4850 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004851 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004852 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004854 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004855 li = list_find(l, 1L);
4856 if (li != NULL && li->li_tv.v_type == VAR_STRING
4857 && li->li_tv.vval.v_string != NULL
4858 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4859 pos.col = len + 1;
4860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004861 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004862 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004864 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004867 pos.coladd = list_find_nr(l, 2L, &error);
4868 if (error)
4869 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004870
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004871 return &pos;
4872 }
4873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004874 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 if (name == NULL)
4876 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004880 {
4881 if (VIsual_active)
4882 return &VIsual;
4883 return &curwin->w_cursor;
4884 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004887 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4889 return NULL;
4890 return pp;
4891 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004892
Bram Moolenaara5525202006-03-02 22:52:09 +00004893 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004894
Bram Moolenaar477933c2007-07-17 14:32:23 +00004895 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004896 {
4897 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004899 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004900 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 // In silent Ex mode topline is zero, but that's not a valid line
4902 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004903 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004904 return &pos;
4905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004907 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004908 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004910 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004911 return &pos;
4912 }
4913 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004916 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 pos.lnum = curbuf->b_ml.ml_line_count;
4919 pos.col = 0;
4920 }
4921 else
4922 {
4923 pos.lnum = curwin->w_cursor.lnum;
4924 pos.col = (colnr_T)STRLEN(ml_get_curline());
4925 }
4926 return &pos;
4927 }
4928 return NULL;
4929}
4930
4931/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004932 * Convert list in "arg" into a position and optional file number.
4933 * When "fnump" is NULL there is no file number, only 3 items.
4934 * Note that the column is passed on as-is, the caller may want to decrement
4935 * it to use 1 for the first column.
4936 * Return FAIL when conversion is not possible, doesn't check the position for
4937 * validity.
4938 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004940list2fpos(
4941 typval_T *arg,
4942 pos_T *posp,
4943 int *fnump,
4944 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004945{
4946 list_T *l = arg->vval.v_list;
4947 long i = 0;
4948 long n;
4949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4951 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004952 if (arg->v_type != VAR_LIST
4953 || l == NULL
4954 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004955 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004956 return FAIL;
4957
4958 if (fnump != NULL)
4959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004961 if (n < 0)
4962 return FAIL;
4963 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004965 *fnump = n;
4966 }
4967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004969 if (n < 0)
4970 return FAIL;
4971 posp->lnum = n;
4972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004974 if (n < 0)
4975 return FAIL;
4976 posp->col = n;
4977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004979 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004980 posp->coladd = 0;
4981 else
4982 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004983
Bram Moolenaar493c1782014-05-28 14:34:46 +02004984 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004986
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004987 return OK;
4988}
4989
4990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 * Get the length of an environment variable name.
4992 * Advance "arg" to the first character after the name.
4993 * Return 0 for error.
4994 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004996get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 char_u *p;
4999 int len;
5000
5001 for (p = *arg; vim_isIDc(*p); ++p)
5002 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return 0;
5005
5006 len = (int)(p - *arg);
5007 *arg = p;
5008 return len;
5009}
5010
5011/*
5012 * Get the length of the name of a function or internal variable.
5013 * "arg" is advanced to the first non-white character after the name.
5014 * Return 0 if something is wrong.
5015 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005017get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019 char_u *p;
5020 int len;
5021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005022 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005024 {
5025 if (*p == ':')
5026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // "s:" is start of "s:var", but "n:" is not and can be used in
5028 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005029 len = (int)(p - *arg);
5030 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5031 || len > 1)
5032 break;
5033 }
5034 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 return 0;
5037
5038 len = (int)(p - *arg);
5039 *arg = skipwhite(p);
5040
5041 return len;
5042}
5043
5044/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005045 * Get the length of the name of a variable or function.
5046 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005048 * Return -1 if curly braces expansion failed.
5049 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * If the name contains 'magic' {}'s, expand them and return the
5051 * expanded name in an allocated string via 'alias' - caller must free.
5052 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054get_name_len(
5055 char_u **arg,
5056 char_u **alias,
5057 int evaluate,
5058 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *p;
5062 char_u *expr_start;
5063 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5068 && (*arg)[2] == (int)KE_SNR)
5069 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 *arg += 3;
5072 return get_id_len(arg) + 3;
5073 }
5074 len = eval_fname_script(*arg);
5075 if (len > 0)
5076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005077 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 *arg += len;
5079 }
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005084 p = find_name_end(*arg, &expr_start, &expr_end,
5085 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (expr_start != NULL)
5087 {
5088 char_u *temp_string;
5089
5090 if (!evaluate)
5091 {
5092 len += (int)(p - *arg);
5093 *arg = skipwhite(p);
5094 return len;
5095 }
5096
5097 /*
5098 * Include any <SID> etc in the expanded string:
5099 * Thus the -len here.
5100 */
5101 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5102 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005103 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 *alias = temp_string;
5105 *arg = skipwhite(p);
5106 return (int)STRLEN(temp_string);
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005110 // Only give an error when there is something, otherwise it will be
5111 // reported at a higher level.
5112 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005113 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 return len;
5116}
5117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118/*
5119 * Find the end of a variable or function name, taking care of magic braces.
5120 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5121 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005122 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 * Return a pointer to just after the name. Equal to "arg" if there is no
5124 * valid name.
5125 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005126 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005127find_name_end(
5128 char_u *arg,
5129 char_u **expr_start,
5130 char_u **expr_end,
5131 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 int mb_nest = 0;
5134 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005136 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005137 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 *expr_start = NULL;
5142 *expr_end = NULL;
5143 }
5144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005146 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5147 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005148 return arg;
5149
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 for (p = arg; *p != NUL
5151 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005152 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005153 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005155 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005157 if (*p == '\'')
5158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005159 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005160 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005161 ;
5162 if (*p == NUL)
5163 break;
5164 }
5165 else if (*p == '"')
5166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005168 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005169 if (*p == '\\' && p[1] != NUL)
5170 ++p;
5171 if (*p == NUL)
5172 break;
5173 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005174 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005176 // "s:" is start of "s:var", but "n:" is not and can be used in
5177 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005178 len = (int)(p - arg);
5179 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005180 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005181 break;
5182 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 if (*p == '[')
5187 ++br_nest;
5188 else if (*p == ']')
5189 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005191
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005192 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 if (*p == '{')
5195 {
5196 mb_nest++;
5197 if (expr_start != NULL && *expr_start == NULL)
5198 *expr_start = p;
5199 }
5200 else if (*p == '}')
5201 {
5202 mb_nest--;
5203 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5204 *expr_end = p;
5205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208
5209 return p;
5210}
5211
5212/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213 * Expands out the 'magic' {}'s in a variable/function name.
5214 * Note that this can call itself recursively, to deal with
5215 * constructs like foo{bar}{baz}{bam}
5216 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5217 * "in_start" ^
5218 * "expr_start" ^
5219 * "expr_end" ^
5220 * "in_end" ^
5221 *
5222 * Returns a new allocated string, which the caller must free.
5223 * Returns NULL for failure.
5224 */
5225 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005226make_expanded_name(
5227 char_u *in_start,
5228 char_u *expr_start,
5229 char_u *expr_end,
5230 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005231{
5232 char_u c1;
5233 char_u *retval = NULL;
5234 char_u *temp_result;
5235 char_u *nextcmd = NULL;
5236
5237 if (expr_end == NULL || in_end == NULL)
5238 return NULL;
5239 *expr_start = NUL;
5240 *expr_end = NUL;
5241 c1 = *in_end;
5242 *in_end = NUL;
5243
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005244 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 if (temp_result != NULL && nextcmd == NULL)
5246 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005247 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5248 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 if (retval != NULL)
5250 {
5251 STRCPY(retval, in_start);
5252 STRCAT(retval, temp_result);
5253 STRCAT(retval, expr_end + 1);
5254 }
5255 }
5256 vim_free(temp_result);
5257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 *expr_start = '{';
5260 *expr_end = '}';
5261
5262 if (retval != NULL)
5263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005264 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 if (expr_start != NULL)
5266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268 temp_result = make_expanded_name(retval, expr_start,
5269 expr_end, temp_result);
5270 vim_free(retval);
5271 retval = temp_result;
5272 }
5273 }
5274
5275 return retval;
5276}
5277
5278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005280 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005283eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005285 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5286}
5287
5288/*
5289 * Return TRUE if character "c" can be used as the first character in a
5290 * variable or function name (excluding '{' and '}').
5291 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005293eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005294{
5295 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296}
5297
5298/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005299 * Handle:
5300 * - expr[expr], expr[expr:expr] subscript
5301 * - ".name" lookup
5302 * - function call with Funcref variable: func(expr)
5303 * - method call: var->method()
5304 *
5305 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005308handle_subscript(
5309 char_u **arg,
5310 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005311 int evaluate, // do more than finding the end
5312 int verbose, // give error messages
5313 char_u *start_leader, // start of '!' and '-' prefixes
5314 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005315{
5316 int ret = OK;
5317 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005318
Bram Moolenaar61343f02019-07-20 21:11:13 +02005319 // "." is ".name" lookup when we found a dict or when evaluating and
5320 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005322 && (((**arg == '['
5323 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005324 || (!evaluate
5325 && (*arg)[1] != '.'
5326 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005327 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005328 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005329 && !VIM_ISWHITE(*(*arg - 1)))
5330 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005331 {
5332 if (**arg == '(')
5333 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005334 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005335
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005336 // Stop the expression evaluation when immediately aborting on
5337 // error, or when an interrupt occurred or an exception was thrown
5338 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (aborting())
5340 {
5341 if (ret == OK)
5342 clear_tv(rettv);
5343 ret = FAIL;
5344 }
5345 dict_unref(selfdict);
5346 selfdict = NULL;
5347 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005348 else if (**arg == '-')
5349 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005350 // Expression "-1.0->method()" applies the leader "-" before
5351 // applying ->.
5352 if (evaluate && *end_leaderp > start_leader)
5353 ret = eval7_leader(rettv, start_leader, end_leaderp);
5354 if (ret == OK)
5355 {
5356 if ((*arg)[2] == '{')
5357 // expr->{lambda}()
5358 ret = eval_lambda(arg, rettv, evaluate, verbose);
5359 else
5360 // expr->name()
5361 ret = eval_method(arg, rettv, evaluate, verbose);
5362 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005365 {
5366 dict_unref(selfdict);
5367 if (rettv->v_type == VAR_DICT)
5368 {
5369 selfdict = rettv->vval.v_dict;
5370 if (selfdict != NULL)
5371 ++selfdict->dv_refcount;
5372 }
5373 else
5374 selfdict = NULL;
5375 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5376 {
5377 clear_tv(rettv);
5378 ret = FAIL;
5379 }
5380 }
5381 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5384 // Don't do this when "Func" is already a partial that was bound
5385 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005386 if (selfdict != NULL
5387 && (rettv->v_type == VAR_FUNC
5388 || (rettv->v_type == VAR_PARTIAL
5389 && (rettv->vval.v_partial->pt_auto
5390 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005393 dict_unref(selfdict);
5394 return ret;
5395}
5396
5397/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005398 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 * value).
5400 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005401 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005402alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005404 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405}
5406
5407/*
5408 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * The string "s" must have been allocated, it is consumed.
5410 * Return NULL for out of memory, the variable otherwise.
5411 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005412 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 rettv = alloc_tv();
5418 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 rettv->v_type = VAR_STRING;
5421 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423 else
5424 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426}
5427
5428/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 if (varp != NULL)
5435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 switch (varp->v_type)
5437 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005439 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005441 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 vim_free(varp->vval.v_string);
5443 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005444 case VAR_PARTIAL:
5445 partial_unref(varp->vval.v_partial);
5446 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005447 case VAR_BLOB:
5448 blob_unref(varp->vval.v_blob);
5449 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 case VAR_LIST:
5451 list_unref(varp->vval.v_list);
5452 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005453 case VAR_DICT:
5454 dict_unref(varp->vval.v_dict);
5455 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005456 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005458 job_unref(varp->vval.v_job);
5459 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005460#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005461 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005462#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005463 channel_unref(varp->vval.v_channel);
5464 break;
5465#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005466 case VAR_NUMBER:
5467 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005468 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005469 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005471 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005472 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 vim_free(varp);
5476 }
5477}
5478
5479/*
5480 * Free the memory for a variable value and set the value to NULL or 0.
5481 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005483clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 if (varp != NULL)
5486 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005493 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005495 case VAR_PARTIAL:
5496 partial_unref(varp->vval.v_partial);
5497 varp->vval.v_partial = NULL;
5498 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005499 case VAR_BLOB:
5500 blob_unref(varp->vval.v_blob);
5501 varp->vval.v_blob = NULL;
5502 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 case VAR_LIST:
5504 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005505 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 case VAR_DICT:
5508 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005509 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005512 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005513 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 varp->vval.v_number = 0;
5515 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005516 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005517#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005518 varp->vval.v_float = 0.0;
5519 break;
5520#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005523 job_unref(varp->vval.v_job);
5524 varp->vval.v_job = NULL;
5525#endif
5526 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005527 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005529 channel_unref(varp->vval.v_channel);
5530 varp->vval.v_channel = NULL;
5531#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005533 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005537 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539}
5540
5541/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 * Set the value of a variable to NULL without freeing items.
5543 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005545init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546{
5547 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005548 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549}
5550
5551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * Get the number value of a variable.
5553 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005554 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005555 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005556 * caller of incompatible types: it sets *denote to TRUE if "denote"
5557 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005559 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005560tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005562 int error = FALSE;
5563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005565}
5566
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005567 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005568tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005569{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005570 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005575 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005576 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005577#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005578 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005579 break;
5580#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005582 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005583 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 break;
5585 case VAR_STRING:
5586 if (varp->vval.v_string != NULL)
5587 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005588 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005589 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005590 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005591 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005593 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005594 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005595 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005596 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005597 case VAR_SPECIAL:
5598 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005599 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005600#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005602 break;
5603#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005604 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005605#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005606 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005607 break;
5608#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005609 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005610 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005611 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005612 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005613 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005615 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005618 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005619 n = -1;
5620 else
5621 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005625#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005627tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005628{
5629 switch (varp->v_type)
5630 {
5631 case VAR_NUMBER:
5632 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005633 case VAR_FLOAT:
5634 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005635 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005636 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005637 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005638 break;
5639 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005640 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005641 break;
5642 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005643 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005644 break;
5645 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005646 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005647 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005648 case VAR_BOOL:
5649 emsg(_("E362: Using a boolean value as a Float"));
5650 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005652 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005653 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005654 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005655# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005656 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005657 break;
5658# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005659 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005660# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005661 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005662 break;
5663# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005664 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005666 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005667 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005668 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005670 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005671 break;
5672 }
5673 return 0;
5674}
5675#endif
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get the string value of a variable.
5679 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5681 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 * If the String variable has never been set, return an empty string.
5683 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005684 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005685 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005687 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005688tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 static char_u mybuf[NUMBUFLEN];
5691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005692 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693}
5694
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005695 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005696tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005698 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005699
5700 return res != NULL ? res : (char_u *)"";
5701}
5702
Bram Moolenaar7d647822014-04-05 21:28:56 +02005703/*
5704 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5705 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005706 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005707tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005708{
5709 static char_u mybuf[NUMBUFLEN];
5710
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005711 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005712}
5713
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005714 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005716{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005720 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005721 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 return buf;
5723 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005724 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005725 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 break;
5727 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005728 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 break;
5730 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005731 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005733 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005734#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005735 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005736 break;
5737#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 case VAR_STRING:
5739 if (varp->vval.v_string != NULL)
5740 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005741 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005742 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005743 case VAR_SPECIAL:
5744 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5745 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005746 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005747 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005748 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005749 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005751 {
5752 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005753 char *status;
5754
5755 if (job == NULL)
5756 return (char_u *)"no process";
5757 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005758 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005759 : "run";
5760# ifdef UNIX
5761 vim_snprintf((char *)buf, NUMBUFLEN,
5762 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005763# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005764 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005765 "process %ld %s",
5766 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005767 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005770 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5771# endif
5772 return buf;
5773 }
5774#endif
5775 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005776 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005778 {
5779 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005780 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005781
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005782 if (channel == NULL)
5783 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5784 else
5785 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005786 "channel %d %s", channel->ch_id, status);
5787 return buf;
5788 }
5789#endif
5790 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005791 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005792 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005794 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798}
5799
5800/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005801 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5802 * string() on Dict, List, etc.
5803 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005804 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005805tv_stringify(typval_T *varp, char_u *buf)
5806{
5807 if (varp->v_type == VAR_LIST
5808 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005809 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005810 || varp->v_type == VAR_FUNC
5811 || varp->v_type == VAR_PARTIAL
5812 || varp->v_type == VAR_FLOAT)
5813 {
5814 typval_T tmp;
5815
5816 f_string(varp, &tmp);
5817 tv_get_string_buf(&tmp, buf);
5818 clear_tv(varp);
5819 *varp = tmp;
5820 return tmp.vval.v_string;
5821 }
5822 return tv_get_string_buf(varp, buf);
5823}
5824
5825/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005826 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5827 * Also give an error message, using "name" or _("name") when use_gettext is
5828 * TRUE.
5829 */
5830 static int
5831tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5832{
5833 int lock = 0;
5834
5835 switch (tv->v_type)
5836 {
5837 case VAR_BLOB:
5838 if (tv->vval.v_blob != NULL)
5839 lock = tv->vval.v_blob->bv_lock;
5840 break;
5841 case VAR_LIST:
5842 if (tv->vval.v_list != NULL)
5843 lock = tv->vval.v_list->lv_lock;
5844 break;
5845 case VAR_DICT:
5846 if (tv->vval.v_dict != NULL)
5847 lock = tv->vval.v_dict->dv_lock;
5848 break;
5849 default:
5850 break;
5851 }
5852 return var_check_lock(tv->v_lock, name, use_gettext)
5853 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5854}
5855
5856/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005857 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005859 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005860 * It is OK for "from" and "to" to point to the same item. This is used to
5861 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005863 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005867 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 switch (from->v_type)
5869 {
5870 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005871 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005872 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 to->vval.v_number = from->vval.v_number;
5874 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005875 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005876#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005877 to->vval.v_float = from->vval.v_float;
5878 break;
5879#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005880 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005881#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005882 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005883 if (to->vval.v_job != NULL)
5884 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005885 break;
5886#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005887 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005888#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005889 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005890 if (to->vval.v_channel != NULL)
5891 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005892 break;
5893#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 case VAR_STRING:
5895 case VAR_FUNC:
5896 if (from->vval.v_string == NULL)
5897 to->vval.v_string = NULL;
5898 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005901 if (from->v_type == VAR_FUNC)
5902 func_ref(to->vval.v_string);
5903 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005905 case VAR_PARTIAL:
5906 if (from->vval.v_partial == NULL)
5907 to->vval.v_partial = NULL;
5908 else
5909 {
5910 to->vval.v_partial = from->vval.v_partial;
5911 ++to->vval.v_partial->pt_refcount;
5912 }
5913 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005914 case VAR_BLOB:
5915 if (from->vval.v_blob == NULL)
5916 to->vval.v_blob = NULL;
5917 else
5918 {
5919 to->vval.v_blob = from->vval.v_blob;
5920 ++to->vval.v_blob->bv_refcount;
5921 }
5922 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 case VAR_LIST:
5924 if (from->vval.v_list == NULL)
5925 to->vval.v_list = NULL;
5926 else
5927 {
5928 to->vval.v_list = from->vval.v_list;
5929 ++to->vval.v_list->lv_refcount;
5930 }
5931 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 case VAR_DICT:
5933 if (from->vval.v_dict == NULL)
5934 to->vval.v_dict = NULL;
5935 else
5936 {
5937 to->vval.v_dict = from->vval.v_dict;
5938 ++to->vval.v_dict->dv_refcount;
5939 }
5940 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005941 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005942 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005944 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 break;
5946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947}
5948
5949/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005950 * Make a copy of an item.
5951 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005952 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5953 * reference to an already copied list/dict can be used.
5954 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957item_copy(
5958 typval_T *from,
5959 typval_T *to,
5960 int deep,
5961 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005962{
5963 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005964 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005968 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005969 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 }
5971 ++recurse;
5972
5973 switch (from->v_type)
5974 {
5975 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977 case VAR_STRING:
5978 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005979 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005980 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005981 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005982 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005983 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 copy_tv(from, to);
5985 break;
5986 case VAR_LIST:
5987 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005988 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 if (from->vval.v_list == NULL)
5990 to->vval.v_list = NULL;
5991 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005994 to->vval.v_list = from->vval.v_list->lv_copylist;
5995 ++to->vval.v_list->lv_refcount;
5996 }
5997 else
5998 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5999 if (to->vval.v_list == NULL)
6000 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006002 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006004 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 case VAR_DICT:
6006 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 if (from->vval.v_dict == NULL)
6009 to->vval.v_dict = NULL;
6010 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6014 ++to->vval.v_dict->dv_refcount;
6015 }
6016 else
6017 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6018 if (to->vval.v_dict == NULL)
6019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006021 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006022 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006024 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 }
6027 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006028 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029}
6030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 void
6032echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6033{
6034 char_u *tofree;
6035 char_u numbuf[NUMBUFLEN];
6036 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6037
6038 if (*atstart)
6039 {
6040 *atstart = FALSE;
6041 // Call msg_start() after eval1(), evaluating the expression
6042 // may cause a message to appear.
6043 if (with_space)
6044 {
6045 // Mark the saved text as finishing the line, so that what
6046 // follows is displayed on a new line when scrolling back
6047 // at the more prompt.
6048 msg_sb_eol();
6049 msg_start();
6050 }
6051 }
6052 else if (with_space)
6053 msg_puts_attr(" ", echo_attr);
6054
6055 if (p != NULL)
6056 for ( ; *p != NUL && !got_int; ++p)
6057 {
6058 if (*p == '\n' || *p == '\r' || *p == TAB)
6059 {
6060 if (*p != TAB && *needclr)
6061 {
6062 // remove any text still there from the command
6063 msg_clr_eos();
6064 *needclr = FALSE;
6065 }
6066 msg_putchar_attr(*p, echo_attr);
6067 }
6068 else
6069 {
6070 if (has_mbyte)
6071 {
6072 int i = (*mb_ptr2len)(p);
6073
6074 (void)msg_outtrans_len_attr(p, i, echo_attr);
6075 p += i - 1;
6076 }
6077 else
6078 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6079 }
6080 }
6081 vim_free(tofree);
6082}
6083
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 * ":echo expr1 ..." print each argument separated with a space, add a
6086 * newline at the end.
6087 * ":echon expr1 ..." print each argument plain.
6088 */
6089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 char_u *p;
6095 int needclr = TRUE;
6096 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006097 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006098 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 if (eap->skip)
6101 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006102 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006104 // If eval1() causes an error message the text from the command may
6105 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006106 need_clr_eos = needclr;
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
6111 /*
6112 * Report the invalid expression unless the expression evaluation
6113 * has been cancelled due to an aborting error, an interrupt, or an
6114 * exception.
6115 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006116 if (!aborting() && did_emsg == did_emsg_before
6117 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006118 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006119 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
6121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006122 need_clr_eos = FALSE;
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 arg = skipwhite(arg);
6129 }
6130 eap->nextcmd = check_nextcmd(arg);
6131
6132 if (eap->skip)
6133 --emsg_skip;
6134 else
6135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if (needclr)
6138 msg_clr_eos();
6139 if (eap->cmdidx == CMD_echo)
6140 msg_end();
6141 }
6142}
6143
6144/*
6145 * ":echohl {name}".
6146 */
6147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006150 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151}
6152
6153/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006154 * Returns the :echo attribute
6155 */
6156 int
6157get_echo_attr(void)
6158{
6159 return echo_attr;
6160}
6161
6162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 * ":execute expr1 ..." execute the result of an expression.
6164 * ":echomsg expr1 ..." Print a message
6165 * ":echoerr expr1 ..." Print an error
6166 * Each gets spaces around each argument and a newline at the end for
6167 * echo commands
6168 */
6169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 int ret = OK;
6175 char_u *p;
6176 garray_T ga;
6177 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 ga_init2(&ga, 1, 80);
6180
6181 if (eap->skip)
6182 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006183 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006185 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6186 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
6189 if (!eap->skip)
6190 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006191 char_u buf[NUMBUFLEN];
6192
6193 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006194 {
6195 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6196 {
6197 emsg(_(e_inval_string));
6198 p = NULL;
6199 }
6200 else
6201 p = tv_get_string_buf(&rettv, buf);
6202 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006203 else
6204 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006205 if (p == NULL)
6206 {
6207 clear_tv(&rettv);
6208 ret = FAIL;
6209 break;
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 len = (int)STRLEN(p);
6212 if (ga_grow(&ga, len + 2) == FAIL)
6213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006214 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ret = FAIL;
6216 break;
6217 }
6218 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 ga.ga_len += len;
6222 }
6223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006224 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 arg = skipwhite(arg);
6226 }
6227
6228 if (ret != FAIL && ga.ga_data != NULL)
6229 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006230 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006232 // Mark the already saved text as finishing the line, so that what
6233 // follows is displayed on a new line when scrolling back at the
6234 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006235 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006236 }
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006239 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006240 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006241 out_flush();
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else if (eap->cmdidx == CMD_echoerr)
6244 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006245 int save_did_emsg = did_emsg;
6246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006247 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006248 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (!force_abort)
6250 did_emsg = save_did_emsg;
6251 }
6252 else if (eap->cmdidx == CMD_execute)
6253 do_cmdline((char_u *)ga.ga_data,
6254 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6255 }
6256
6257 ga_clear(&ga);
6258
6259 if (eap->skip)
6260 --emsg_skip;
6261
6262 eap->nextcmd = check_nextcmd(arg);
6263}
6264
6265/*
6266 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6267 * "arg" points to the "&" or '+' when called, to "option" when returning.
6268 * Returns NULL when no option name found. Otherwise pointer to the char
6269 * after the option name.
6270 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006271 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 char_u *p = *arg;
6275
6276 ++p;
6277 if (*p == 'g' && p[1] == ':')
6278 {
6279 *opt_flags = OPT_GLOBAL;
6280 p += 2;
6281 }
6282 else if (*p == 'l' && p[1] == ':')
6283 {
6284 *opt_flags = OPT_LOCAL;
6285 p += 2;
6286 }
6287 else
6288 *opt_flags = 0;
6289
6290 if (!ASCII_ISALPHA(*p))
6291 return NULL;
6292 *arg = p;
6293
6294 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006295 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 else
6297 while (ASCII_ISALPHA(*p))
6298 ++p;
6299 return p;
6300}
6301
6302/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006303 * Display script name where an item was last set.
6304 * Should only be invoked when 'verbose' is non-zero.
6305 */
6306 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006307last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006308{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006309 char_u *p;
6310
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006311 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006312 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006313 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006314 if (p != NULL)
6315 {
6316 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006317 msg_puts(_("\n\tLast set from "));
6318 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006319 if (script_ctx.sc_lnum > 0)
6320 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006321 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006322 msg_outnum((long)script_ctx.sc_lnum);
6323 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006324 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006325 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006326 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006327 }
6328}
6329
Bram Moolenaar61be3762019-03-19 23:04:17 +01006330/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006331 * Compare "typ1" and "typ2". Put the result in "typ1".
6332 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006333 int
6334typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 typval_T *typ1, // first operand
6336 typval_T *typ2, // second operand
6337 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006339{
6340 int i;
6341 varnumber_T n1, n2;
6342 char_u *s1, *s2;
6343 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006344 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006345
Bram Moolenaar31988702018-02-13 12:57:42 +01006346 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006348 // For "is" a different type always means FALSE, for "notis"
6349 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006350 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006351 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006352 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6353 {
6354 if (type_is)
6355 {
6356 n1 = (typ1->v_type == typ2->v_type
6357 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006358 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006359 n1 = !n1;
6360 }
6361 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006362 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006363 {
6364 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006365 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006366 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006367 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006368 clear_tv(typ1);
6369 return FAIL;
6370 }
6371 else
6372 {
6373 // Compare two Blobs for being equal or unequal.
6374 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006375 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006376 n1 = !n1;
6377 }
6378 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006379 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6380 {
6381 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006382 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006383 n1 = (typ1->v_type == typ2->v_type
6384 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006385 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006386 n1 = !n1;
6387 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006388 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006389 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006390 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006391 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006392 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006393 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006394 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006395 clear_tv(typ1);
6396 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006397 }
6398 else
6399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006401 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6402 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006403 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006404 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006405 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006406 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006407
6408 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6409 {
6410 if (type_is)
6411 {
6412 n1 = (typ1->v_type == typ2->v_type
6413 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006414 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006415 n1 = !n1;
6416 }
6417 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006418 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006419 {
6420 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006421 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006422 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006423 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006424 clear_tv(typ1);
6425 return FAIL;
6426 }
6427 else
6428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006429 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006430 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6431 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006432 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006433 n1 = !n1;
6434 }
6435 }
6436
6437 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6438 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6439 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006440 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6441 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006443 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006444 clear_tv(typ1);
6445 return FAIL;
6446 }
6447 if ((typ1->v_type == VAR_PARTIAL
6448 && typ1->vval.v_partial == NULL)
6449 || (typ2->v_type == VAR_PARTIAL
6450 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006451 // When both partials are NULL, then they are equal.
6452 // Otherwise they are not equal.
6453 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006454 else if (type_is)
6455 {
6456 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 // strings are considered the same if their value is
6458 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006459 n1 = tv_equal(typ1, typ2, ic, FALSE);
6460 else if (typ1->v_type == VAR_PARTIAL
6461 && typ2->v_type == VAR_PARTIAL)
6462 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6463 else
6464 n1 = FALSE;
6465 }
6466 else
6467 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006468 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006469 n1 = !n1;
6470 }
6471
6472#ifdef FEAT_FLOAT
6473 /*
6474 * If one of the two variables is a float, compare as a float.
6475 * When using "=~" or "!~", always compare as string.
6476 */
6477 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006478 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006479 {
6480 float_T f1, f2;
6481
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006482 f1 = tv_get_float(typ1);
6483 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006484 n1 = FALSE;
6485 switch (type)
6486 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006487 case EXPR_IS:
6488 case EXPR_EQUAL: n1 = (f1 == f2); break;
6489 case EXPR_ISNOT:
6490 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6491 case EXPR_GREATER: n1 = (f1 > f2); break;
6492 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6493 case EXPR_SMALLER: n1 = (f1 < f2); break;
6494 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6495 case EXPR_UNKNOWN:
6496 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006498 }
6499 }
6500#endif
6501
6502 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006503 * If one of the two variables is a number, compare as a number.
6504 * When using "=~" or "!~", always compare as string.
6505 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006506 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006507 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006509 n1 = tv_get_number(typ1);
6510 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006511 switch (type)
6512 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006513 case EXPR_IS:
6514 case EXPR_EQUAL: n1 = (n1 == n2); break;
6515 case EXPR_ISNOT:
6516 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6517 case EXPR_GREATER: n1 = (n1 > n2); break;
6518 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6519 case EXPR_SMALLER: n1 = (n1 < n2); break;
6520 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6521 case EXPR_UNKNOWN:
6522 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006524 }
6525 }
6526 else
6527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006528 s1 = tv_get_string_buf(typ1, buf1);
6529 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006530 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006531 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6532 else
6533 i = 0;
6534 n1 = FALSE;
6535 switch (type)
6536 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006537 case EXPR_IS:
6538 case EXPR_EQUAL: n1 = (i == 0); break;
6539 case EXPR_ISNOT:
6540 case EXPR_NEQUAL: n1 = (i != 0); break;
6541 case EXPR_GREATER: n1 = (i > 0); break;
6542 case EXPR_GEQUAL: n1 = (i >= 0); break;
6543 case EXPR_SMALLER: n1 = (i < 0); break;
6544 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006545
Bram Moolenaar87396072019-12-31 22:36:18 +01006546 case EXPR_MATCH:
6547 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006548 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006549 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006550 n1 = !n1;
6551 break;
6552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006554 }
6555 }
6556 clear_tv(typ1);
6557 typ1->v_type = VAR_NUMBER;
6558 typ1->vval.v_number = n1;
6559
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006560 return OK;
6561}
6562
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006563 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006564typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006565{
6566 char_u *tofree;
6567 char_u numbuf[NUMBUFLEN];
6568 char_u *ret = NULL;
6569
6570 if (arg == NULL)
6571 return vim_strsave((char_u *)"(does not exist)");
6572 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006573 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006574 if (ret != NULL && tofree == NULL)
6575 ret = vim_strsave(ret);
6576 return ret;
6577}
6578
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006579#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581/*
6582 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006583 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 * "flags" can be "g" to do a global substitute.
6585 * Returns an allocated string, NULL for error.
6586 */
6587 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006588do_string_sub(
6589 char_u *str,
6590 char_u *pat,
6591 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006592 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int sublen;
6596 regmatch_T regmatch;
6597 int i;
6598 int do_all;
6599 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006600 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 garray_T ga;
6602 char_u *ret;
6603 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006604 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006608 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
6610 ga_init2(&ga, 1, 200);
6611
6612 do_all = (flags[0] == 'g');
6613
6614 regmatch.rm_ic = p_ic;
6615 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6616 if (regmatch.regprog != NULL)
6617 {
6618 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006619 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006623 if (regmatch.startp[0] == regmatch.endp[0])
6624 {
6625 if (zero_width == regmatch.startp[0])
6626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006627 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006628 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006629 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6630 (size_t)i);
6631 ga.ga_len += i;
6632 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006633 continue;
6634 }
6635 zero_width = regmatch.startp[0];
6636 }
6637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 /*
6639 * Get some space for a temporary buffer to do the substitution
6640 * into. It will contain:
6641 * - The text up to where the match is.
6642 * - The substituted text.
6643 * - The text after the match.
6644 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006645 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006646 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6648 {
6649 ga_clear(&ga);
6650 break;
6651 }
6652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006653 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 i = (int)(regmatch.startp[0] - tail);
6655 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006656 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006657 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 + ga.ga_len + i, TRUE, TRUE, FALSE);
6659 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006660 tail = regmatch.endp[0];
6661 if (*tail == NUL)
6662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (!do_all)
6664 break;
6665 }
6666
6667 if (ga.ga_data != NULL)
6668 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6669
Bram Moolenaar473de612013-06-08 18:19:48 +02006670 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
6672
6673 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6674 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006675 if (p_cpo == empty_option)
6676 p_cpo = save_cpo;
6677 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006679 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681 return ret;
6682}