blob: c3f0de8e32be19a35f00b2cbe72be109dee9b8db [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 Moolenaar5d18efe2019-12-01 21:11:22 +010037static int echo_attr = 0; // attributes used for ":echo"
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000040 * Info used by a ":for" loop.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000043{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010044 int fi_semicolon; // TRUE if ending in '; var]'
45 int fi_varcount; // nr of variables in the list
46 listwatch_T fi_lw; // keep an eye on the item used.
47 list_T *fi_list; // list being used
48 int fi_bi; // index of blob
49 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000050} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000051
Bram Moolenaar48e697e2016-01-23 22:17:30 +010052static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010053static int eval2(char_u **arg, typval_T *rettv, int evaluate);
54static int eval3(char_u **arg, typval_T *rettv, int evaluate);
55static int eval4(char_u **arg, typval_T *rettv, int evaluate);
56static int eval5(char_u **arg, typval_T *rettv, int evaluate);
57static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
58static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020059static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000060
Bram Moolenaar48e697e2016-01-23 22:17:30 +010061static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020062static 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 +010063static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020064
Bram Moolenaare21c1582019-03-02 11:57:09 +010065/*
66 * Return "n1" divided by "n2", taking care of dividing by zero.
67 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020068 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010069num_divide(varnumber_T n1, varnumber_T n2)
70{
71 varnumber_T result;
72
73 if (n2 == 0) // give an error message?
74 {
75 if (n1 == 0)
76 result = VARNUM_MIN; // similar to NaN
77 else if (n1 < 0)
78 result = -VARNUM_MAX;
79 else
80 result = VARNUM_MAX;
81 }
82 else
83 result = n1 / n2;
84
85 return result;
86}
87
88/*
89 * Return "n1" modulus "n2", taking care of dividing by zero.
90 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020091 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010092num_modulus(varnumber_T n1, varnumber_T n2)
93{
94 // Give an error when n2 is 0?
95 return (n2 == 0) ? 0 : (n1 % n2);
96}
97
Bram Moolenaara1fa8922017-01-12 20:06:33 +010098#if defined(EBCDIC) || defined(PROTO)
99/*
100 * Compare struct fst by function name.
101 */
102 static int
103compare_func_name(const void *s1, const void *s2)
104{
105 struct fst *p1 = (struct fst *)s1;
106 struct fst *p2 = (struct fst *)s2;
107
108 return STRCMP(p1->f_name, p2->f_name);
109}
110
111/*
112 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100113 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100114 * On machines using EBCDIC we have to sort it.
115 */
116 static void
117sortFunctions(void)
118{
119 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
120
121 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
122}
123#endif
124
Bram Moolenaar33570922005-01-25 22:26:29 +0000125/*
126 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000127 */
128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100129eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000130{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200131 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200132 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000133
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200134#ifdef EBCDIC
135 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100136 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200137 */
138 sortFunctions();
139#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000140}
141
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142#if defined(EXITFREE) || defined(PROTO)
143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100144eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000145{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200146 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100147 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200148 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000149
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200150 // autoloaded script names
151 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000152
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200153 // unreferenced lists and dicts
154 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200155
156 // functions not garbage collected
157 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000158}
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Top level evaluation function, returning a boolean.
163 * Sets "error" to TRUE if there was an error.
164 * Return TRUE or FALSE.
165 */
166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100167eval_to_bool(
168 char_u *arg,
169 int *error,
170 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100171 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
Bram Moolenaar33570922005-01-25 22:26:29 +0000173 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200174 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000178 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100185 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000186 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188 }
189 if (skip)
190 --emsg_skip;
191
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200192 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193}
194
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195/*
196 * Call eval1() and give an error message if not done at a lower level.
197 */
198 static int
199eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
200{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100201 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100202 int ret;
203 int did_emsg_before = did_emsg;
204 int called_emsg_before = called_emsg;
205
206 ret = eval1(arg, rettv, evaluate);
207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100215 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
217 return ret;
218}
219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220/*
221 * Evaluate an expression, which can be a function, partial or string.
222 * Pass arguments "argv[argc]".
223 * Return the result in "rettv" and OK or FAIL.
224 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200225 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100226eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
227{
228 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200230 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100231
232 if (expr->v_type == VAR_FUNC)
233 {
234 s = expr->vval.v_string;
235 if (s == NULL || *s == NUL)
236 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200237 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200238 funcexe.evaluate = TRUE;
239 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100240 return FAIL;
241 }
242 else if (expr->v_type == VAR_PARTIAL)
243 {
244 partial_T *partial = expr->vval.v_partial;
245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0)
247 {
248 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL)
249 return FAIL;
250 }
251 else
252 {
253 s = partial_name(partial);
254 if (s == NULL || *s == NUL)
255 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200256 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 funcexe.evaluate = TRUE;
258 funcexe.partial = partial;
259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
260 return FAIL;
261 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100262 }
263 else
264 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100265 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100266 if (s == NULL)
267 return FAIL;
268 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100269 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100271 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100272 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200273 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100274 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100275 return FAIL;
276 }
277 }
278 return OK;
279}
280
281/*
282 * Like eval_to_bool() but using a typval_T instead of a string.
283 * Works for string, funcref and partial.
284 */
285 int
286eval_expr_to_bool(typval_T *expr, int *error)
287{
288 typval_T rettv;
289 int res;
290
291 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
292 {
293 *error = TRUE;
294 return FALSE;
295 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100296 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 clear_tv(&rettv);
298 return res;
299}
300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301/*
302 * Top level evaluation function, returning a string. If "skip" is TRUE,
303 * only parsing to "nextcmd" is done, without reporting errors. Return
304 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
305 */
306 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100307eval_to_string_skip(
308 char_u *arg,
309 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100310 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
Bram Moolenaar33570922005-01-25 22:26:29 +0000312 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 char_u *retval;
314
315 if (skip)
316 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000317 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 retval = NULL;
319 else
320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100321 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000322 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 }
324 if (skip)
325 --emsg_skip;
326
327 return retval;
328}
329
330/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000331 * Skip over an expression at "*pp".
332 * Return FAIL for an error, OK otherwise.
333 */
334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000336{
Bram Moolenaar33570922005-01-25 22:26:29 +0000337 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000338
339 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000341}
342
343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000345 * When "convert" is TRUE convert a List into a sequence of lines and convert
346 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 * Return pointer to allocated memory, or NULL for failure.
348 */
349 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100350eval_to_string(
351 char_u *arg,
352 char_u **nextcmd,
353 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
Bram Moolenaar33570922005-01-25 22:26:29 +0000355 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000357 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000358#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000359 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000362 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 retval = NULL;
364 else
365 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000366 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000367 {
368 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000369 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200370 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200371 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200372 if (tv.vval.v_list->lv_len > 0)
373 ga_append(&ga, NL);
374 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000375 ga_append(&ga, NUL);
376 retval = (char_u *)ga.ga_data;
377 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000378#ifdef FEAT_FLOAT
379 else if (convert && tv.v_type == VAR_FLOAT)
380 {
381 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
382 retval = vim_strsave(numbuf);
383 }
384#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000385 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100386 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389
390 return retval;
391}
392
393/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000394 * Call eval_to_string() without using current local variables and using
395 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
397 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100398eval_to_string_safe(
399 char_u *arg,
400 char_u **nextcmd,
401 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
403 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200404 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200406 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000407 if (use_sandbox)
408 ++sandbox;
409 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000410 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000411 if (use_sandbox)
412 --sandbox;
413 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200414 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 return retval;
416}
417
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418/*
419 * Top level evaluation function, returning a number.
420 * Evaluates "expr" silently.
421 * Returns -1 for an error.
422 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200423 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100424eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
Bram Moolenaar33570922005-01-25 22:26:29 +0000426 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200427 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
430 ++emsg_off;
431
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 retval = -1;
434 else
435 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100436 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
439 --emsg_off;
440
441 return retval;
442}
443
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000444/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000445 * Top level evaluation function.
446 * Returns an allocated typval_T with the result.
447 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000448 */
449 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100450eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000451{
452 typval_T *tv;
453
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200454 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000455 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100456 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000457
458 return tv;
459}
460
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100462 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200463 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
464 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000465 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100468call_vim_function(
469 char_u *func,
470 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200471 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200472 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000474 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200475 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100477 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200478 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200479 funcexe.firstline = curwin->w_cursor.lnum;
480 funcexe.lastline = curwin->w_cursor.lnum;
481 funcexe.evaluate = TRUE;
482 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000483 if (ret == FAIL)
484 clear_tv(rettv);
485
486 return ret;
487}
488
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100489/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100490 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100491 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200492 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
493 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100494 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200495 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100496call_func_retnr(
497 char_u *func,
498 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200499 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100500{
501 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200502 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100503
Bram Moolenaarded27a12018-08-01 19:06:03 +0200504 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100505 return -1;
506
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100507 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100508 clear_tv(&rettv);
509 return retval;
510}
511
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000512/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100513 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000514 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200515 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
516 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000517 */
518 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100519call_func_retstr(
520 char_u *func,
521 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200522 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000523{
524 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000525 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000526
Bram Moolenaarded27a12018-08-01 19:06:03 +0200527 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000528 return NULL;
529
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 return retval;
533}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000534
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000535/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100536 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200537 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
538 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000539 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000540 */
541 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100542call_func_retlist(
543 char_u *func,
544 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200545 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000546{
547 typval_T rettv;
548
Bram Moolenaarded27a12018-08-01 19:06:03 +0200549 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000550 return NULL;
551
552 if (rettv.v_type != VAR_LIST)
553 {
554 clear_tv(&rettv);
555 return NULL;
556 }
557
558 return rettv.vval.v_list;
559}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561#ifdef FEAT_FOLDING
562/*
563 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
564 * it in "*cp". Doesn't give error messages.
565 */
566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
Bram Moolenaar33570922005-01-25 22:26:29 +0000569 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200570 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000572 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
573 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000576 if (use_sandbox)
577 ++sandbox;
578 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000580 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 retval = 0;
582 else
583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100584 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000585 if (tv.v_type == VAR_NUMBER)
586 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000587 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 retval = 0;
589 else
590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100591 // If the result is a string, check if there is a non-digit before
592 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000593 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (!VIM_ISDIGIT(*s) && *s != '-')
595 *cp = *s++;
596 retval = atol((char *)s);
597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000601 if (use_sandbox)
602 --sandbox;
603 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200605 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606}
607#endif
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000610 * Get an lval: variable, Dict item or List item that can be assigned a value
611 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
612 * "name.key", "name.key[expr]" etc.
613 * Indexing only works if "name" is an existing List or Dictionary.
614 * "name" points to the start of the name.
615 * If "rettv" is not NULL it points to the value to be assigned.
616 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
617 * wrong; must end in space or cmd separator.
618 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100619 * flags:
620 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100621 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100622 * GLV_NO_AUTOLOAD: do not use script autoloading
623 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000624 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000625 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000626 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000627 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629get_lval(
630 char_u *name,
631 typval_T *rettv,
632 lval_T *lp,
633 int unlet,
634 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100635 int flags, // GLV_ values
636 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000638 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000639 char_u *expr_start, *expr_end;
640 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000641 dictitem_T *v;
642 typval_T var1;
643 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000644 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000645 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000646 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000647 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000648 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100649 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100651 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200652 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000653
654 if (skip)
655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100656 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000657 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000658 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000659 }
660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100661 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000662 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000664 if (expr_start != NULL)
665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100666 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100667 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000668 && *p != '[' && *p != '.')
669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100670 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000671 return NULL;
672 }
673
674 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
675 if (lp->ll_exp_name == NULL)
676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // Report an invalid expression in braces, unless the
678 // expression evaluation has been cancelled due to an
679 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000680 if (!aborting() && !quiet)
681 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000682 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100683 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000684 return NULL;
685 }
686 }
687 lp->ll_name = lp->ll_exp_name;
688 }
689 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000691 lp->ll_name = name;
692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
694 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100695 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 char_u *tp = skipwhite(p + 1);
697
698 // parse the type after the name
699 lp->ll_type = parse_type(&tp, &si->sn_type_list);
700 lp->ll_name_end = tp;
701 }
702 }
703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100704 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000705 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
706 return p;
707
708 cc = *p;
709 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100710 // Only pass &ht when we would write to the variable, it prevents autoload
711 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100712 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
713 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000714 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 if (v == NULL)
718 return NULL;
719
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000720 /*
721 * Loop until no more [idx] or .key is following.
722 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000723 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100724 var1.v_type = VAR_UNKNOWN;
725 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
729 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100730 && lp->ll_tv->vval.v_dict != NULL)
731 && !(lp->ll_tv->v_type == VAR_BLOB
732 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000734 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100735 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000742 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000744
Bram Moolenaar8c711452005-01-14 21:53:12 +0000745 len = -1;
746 if (*p == '.')
747 {
748 key = p + 1;
749 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
750 ;
751 if (len == 0)
752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000753 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000756 }
757 p = key + len;
758 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000759 else
760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100761 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000762 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000763 if (*p == ':')
764 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000765 else
766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000767 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000769 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100770 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100772 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000773 clear_tv(&var1);
774 return NULL;
775 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000776 }
777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100778 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000779 if (*p == ':')
780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000783 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100785 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000787 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100788 if (rettv != NULL
789 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100790 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100791 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100792 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100795 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000798 }
799 p = skipwhite(p + 1);
800 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000802 else
803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100805 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000806 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100810 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100813 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000814 clear_tv(&var2);
815 return NULL;
816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000817 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000822
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100827 clear_tv(&var1);
828 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 }
831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000833 ++p;
834 }
835
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 {
838 if (len == -1)
839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // "[key]": get key from "var1"
841 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200842 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 }
847 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000848 lp->ll_list = NULL;
849 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000850 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // When assigning to a scope dictionary check that a function and
853 // variable name is valid (only variable name unless it is l: or
854 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200856 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200857 int prevval;
858 int wrong;
859
860 if (len != -1)
861 {
862 prevval = key[len];
863 key[len] = NUL;
864 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200865 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100866 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200867 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
868 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200869 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 || !valid_varname(key);
871 if (len != -1)
872 key[len] = prevval;
873 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200874 {
875 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200876 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200877 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200878 }
879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100882 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200883 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100884 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100887 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200888 return NULL;
889 }
890
Bram Moolenaar31b81602019-02-10 22:14:27 +0100891 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100895 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100896 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 }
899 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100903 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 p = NULL;
906 break;
907 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100908 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100909 else if ((flags & GLV_READ_ONLY) == 0
910 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100911 {
912 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200913 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100914 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200915
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000918 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100919 else if (lp->ll_tv->v_type == VAR_BLOB)
920 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100921 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
922
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100923 /*
924 * Get the number and item for the only or first index of the List.
925 */
926 if (empty1)
927 lp->ll_n1 = 0;
928 else
929 // is number or string
930 lp->ll_n1 = (long)tv_get_number(&var1);
931 clear_tv(&var1);
932
933 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100934 || lp->ll_n1 > bloblen
935 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100936 {
937 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100938 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100939 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100940 return NULL;
941 }
942 if (lp->ll_range && !lp->ll_empty2)
943 {
944 lp->ll_n2 = (long)tv_get_number(&var2);
945 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100946 if (lp->ll_n2 < 0
947 || lp->ll_n2 >= bloblen
948 || lp->ll_n2 < lp->ll_n1)
949 {
950 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100952 return NULL;
953 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100954 }
955 lp->ll_blob = lp->ll_tv->vval.v_blob;
956 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100957 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100958 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 else
960 {
961 /*
962 * Get the number and item for the only or first index of the List.
963 */
964 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100967 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100968 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100969 clear_tv(&var1);
970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000971 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 lp->ll_list = lp->ll_tv->vval.v_list;
973 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
974 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000976 if (lp->ll_n1 < 0)
977 {
978 lp->ll_n1 = 0;
979 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
980 }
981 }
982 if (lp->ll_li == NULL)
983 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100984 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200985 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
990 /*
991 * May need to find the item or absolute index for the second
992 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 * When no index given: "lp->ll_empty2" is TRUE.
994 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100998 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001005 {
1006 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001007 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 }
1012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001013 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 if (lp->ll_n1 < 0)
1015 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1016 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001017 {
1018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001021 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001022 }
1023
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 }
1027
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001028 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 return p;
1031}
1032
1033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038{
1039 vim_free(lp->ll_exp_name);
1040 vim_free(lp->ll_newkey);
1041}
1042
1043/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001044 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001046 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1047 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050set_var_lval(
1051 lval_T *lp,
1052 char_u *endp,
1053 typval_T *rettv,
1054 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001056 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057{
1058 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 listitem_T *ri;
1060 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061
1062 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001064 cc = *endp;
1065 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001066 if (lp->ll_blob != NULL)
1067 {
1068 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001069
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001070 if (op != NULL && *op != '=')
1071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001073 return;
1074 }
1075
1076 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1077 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001078 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001079
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 if (lp->ll_empty2)
1081 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1082
1083 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001085 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 return;
1087 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001088 if (lp->ll_empty2)
1089 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001091 ir = 0;
1092 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1093 blob_set(lp->ll_blob, il,
1094 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 }
1096 else
1097 {
1098 val = (int)tv_get_number_chk(rettv, &error);
1099 if (!error)
1100 {
1101 garray_T *gap = &lp->ll_blob->bv_ga;
1102
1103 // Allow for appending a byte. Setting a byte beyond
1104 // the end is an error otherwise.
1105 if (lp->ll_n1 < gap->ga_len
1106 || (lp->ll_n1 == gap->ga_len
1107 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1108 {
1109 blob_set(lp->ll_blob, lp->ll_n1, val);
1110 if (lp->ll_n1 == gap->ga_len)
1111 ++gap->ga_len;
1112 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001114 }
1115 }
1116 }
1117 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001119 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001122 {
1123 emsg(_(e_cannot_mod));
1124 *endp = cc;
1125 return;
1126 }
1127
Bram Moolenaarff697e62019-02-12 22:28:33 +01001128 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001129 di = NULL;
1130 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1131 &tv, &di, TRUE, FALSE) == OK)
1132 {
1133 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001134 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1135 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001136 && tv_op(&tv, rettv, op) == OK)
1137 set_var(lp->ll_name, &tv, FALSE);
1138 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001141 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001143 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001145 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001146 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001147 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001148 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 else if (lp->ll_range)
1150 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001151 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001152 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001155 {
1156 emsg(_("E996: Cannot lock a range"));
1157 return;
1158 }
1159
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001160 /*
1161 * Check whether any of the list items is locked
1162 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001163 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001165 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001166 return;
1167 ri = ri->li_next;
1168 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1169 break;
1170 ll_li = ll_li->li_next;
1171 ++ll_n1;
1172 }
1173
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 /*
1175 * Assign the List values to the list items.
1176 */
1177 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001178 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 if (op != NULL && *op != '=')
1180 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1181 else
1182 {
1183 clear_tv(&lp->ll_li->li_tv);
1184 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1185 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 ri = ri->li_next;
1187 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1188 break;
1189 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001191 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001192 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 ri = NULL;
1195 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001196 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_li = lp->ll_li->li_next;
1199 ++lp->ll_n1;
1200 }
1201 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001203 else if (lp->ll_empty2
1204 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 }
1208 else
1209 {
1210 /*
1211 * Assign to a List or Dictionary item.
1212 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001214 {
1215 emsg(_("E996: Cannot lock a list or dict"));
1216 return;
1217 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 if (lp->ll_newkey != NULL)
1219 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001220 if (op != NULL && *op != '=')
1221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 return;
1224 }
1225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001226 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001227 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (di == NULL)
1229 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001230 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1231 {
1232 vim_free(di);
1233 return;
1234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 else if (op != NULL && *op != '=')
1238 {
1239 tv_op(lp->ll_tv, rettv, op);
1240 return;
1241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 /*
1246 * Assign the value to the variable or list item.
1247 */
1248 if (copy)
1249 copy_tv(rettv, lp->ll_tv);
1250 else
1251 {
1252 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001253 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255 }
1256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257}
1258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001260 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1261 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001262 * Returns OK or FAIL.
1263 */
1264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001266{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001267 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 char_u numbuf[NUMBUFLEN];
1269 char_u *s;
1270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001272 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001273 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 {
1275 switch (tv1->v_type)
1276 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001277 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001278 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 case VAR_DICT:
1281 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001282 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001283 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001284 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001285 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001286 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 break;
1288
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 case VAR_BLOB:
1290 if (*op != '+' || tv2->v_type != VAR_BLOB)
1291 break;
1292 // BLOB += BLOB
1293 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1294 {
1295 blob_T *b1 = tv1->vval.v_blob;
1296 blob_T *b2 = tv2->vval.v_blob;
1297 int i, len = blob_len(b2);
1298 for (i = 0; i < len; i++)
1299 ga_append(&b1->bv_ga, blob_get(b2, i));
1300 }
1301 return OK;
1302
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 case VAR_LIST:
1304 if (*op != '+' || tv2->v_type != VAR_LIST)
1305 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001306 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1308 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1309 return OK;
1310
1311 case VAR_NUMBER:
1312 case VAR_STRING:
1313 if (tv2->v_type == VAR_LIST)
1314 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001315 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001317 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001319#ifdef FEAT_FLOAT
1320 if (tv2->v_type == VAR_FLOAT)
1321 {
1322 float_T f = n;
1323
Bram Moolenaarff697e62019-02-12 22:28:33 +01001324 if (*op == '%')
1325 break;
1326 switch (*op)
1327 {
1328 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001333 clear_tv(tv1);
1334 tv1->v_type = VAR_FLOAT;
1335 tv1->vval.v_float = f;
1336 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001338#endif
1339 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001340 switch (*op)
1341 {
1342 case '+': n += tv_get_number(tv2); break;
1343 case '-': n -= tv_get_number(tv2); break;
1344 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001345 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1346 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001348 clear_tv(tv1);
1349 tv1->v_type = VAR_NUMBER;
1350 tv1->vval.v_number = n;
1351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 }
1353 else
1354 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001355 if (tv2->v_type == VAR_FLOAT)
1356 break;
1357
Bram Moolenaarff697e62019-02-12 22:28:33 +01001358 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001359 s = tv_get_string(tv1);
1360 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001361 clear_tv(tv1);
1362 tv1->v_type = VAR_STRING;
1363 tv1->vval.v_string = s;
1364 }
1365 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001366
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001368#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001369 {
1370 float_T f;
1371
Bram Moolenaarff697e62019-02-12 22:28:33 +01001372 if (*op == '%' || *op == '.'
1373 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001374 && tv2->v_type != VAR_NUMBER
1375 && tv2->v_type != VAR_STRING))
1376 break;
1377 if (tv2->v_type == VAR_FLOAT)
1378 f = tv2->vval.v_float;
1379 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001380 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001381 switch (*op)
1382 {
1383 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001389#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001390 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 }
1392 }
1393
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001394 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 return FAIL;
1396}
1397
1398/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001399 * Evaluate the expression used in a ":for var in expr" command.
1400 * "arg" points to "var".
1401 * Set "*errp" to TRUE for an error, FALSE otherwise;
1402 * Return a pointer that holds the info. Null when there is an error.
1403 */
1404 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001405eval_for_line(
1406 char_u *arg,
1407 int *errp,
1408 char_u **nextcmdp,
1409 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T tv;
1414 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001416 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001418 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 if (fi == NULL)
1420 return NULL;
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 if (expr == NULL)
1424 return fi;
1425
1426 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001427 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 return fi;
1431 }
1432
1433 if (skip)
1434 ++emsg_skip;
1435 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1436 {
1437 *errp = FALSE;
1438 if (!skip)
1439 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001440 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001441 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 l = tv.vval.v_list;
1443 if (l == NULL)
1444 {
1445 // a null list is like an empty list: do nothing
1446 clear_tv(&tv);
1447 }
1448 else
1449 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 // Need a real list here.
1451 range_list_materialize(l);
1452
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 // No need to increment the refcount, it's already set for
1454 // the list being used in "tv".
1455 fi->fi_list = l;
1456 list_add_watch(l, &fi->fi_lw);
1457 fi->fi_lw.lw_item = l->lv_first;
1458 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001459 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001460 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001461 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001462 fi->fi_bi = 0;
1463 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001464 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001465 typval_T btv;
1466
1467 // Make a copy, so that the iteration still works when the
1468 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001470 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001471 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001472 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001473 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001474 else
1475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001476 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001477 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478 }
1479 }
1480 }
1481 if (skip)
1482 --emsg_skip;
1483
1484 return fi;
1485}
1486
1487/*
1488 * Use the first item in a ":for" list. Advance to the next.
1489 * Assign the values to the variable (list). "arg" points to the first one.
1490 * Return TRUE when a valid item was found, FALSE when at end of list or
1491 * something wrong.
1492 */
1493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001495{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001496 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001498 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1499 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001500 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001502 if (fi->fi_blob != NULL)
1503 {
1504 typval_T tv;
1505
1506 if (fi->fi_bi >= blob_len(fi->fi_blob))
1507 return FALSE;
1508 tv.v_type = VAR_NUMBER;
1509 tv.v_lock = VAR_FIXED;
1510 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1511 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001512 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001513 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001514 }
1515
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516 item = fi->fi_lw.lw_item;
1517 if (item == NULL)
1518 result = FALSE;
1519 else
1520 {
1521 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001522 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001523 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001524 }
1525 return result;
1526}
1527
1528/*
1529 * Free the structure used to store info used by ":for".
1530 */
1531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533{
Bram Moolenaar33570922005-01-25 22:26:29 +00001534 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001536 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001537 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001539 list_unref(fi->fi_list);
1540 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001541 if (fi != NULL && fi->fi_blob != NULL)
1542 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001543 vim_free(fi);
1544}
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547set_context_for_expression(
1548 expand_T *xp,
1549 char_u *arg,
1550 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 int got_eq = FALSE;
1553 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001556 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 {
1558 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001561 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 {
1564 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001565 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001566 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 break;
1568 }
1569 return;
1570 }
1571 }
1572 else
1573 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1574 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 while ((xp->xp_pattern = vim_strpbrk(arg,
1576 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1577 {
1578 c = *xp->xp_pattern;
1579 if (c == '&')
1580 {
1581 c = xp->xp_pattern[1];
1582 if (c == '&')
1583 {
1584 ++xp->xp_pattern;
1585 xp->xp_context = cmdidx != CMD_let || got_eq
1586 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1587 }
1588 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001591 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1592 xp->xp_pattern += 2;
1593
1594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else if (c == '$')
1597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001598 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 xp->xp_context = EXPAND_ENV_VARS;
1600 }
1601 else if (c == '=')
1602 {
1603 got_eq = TRUE;
1604 xp->xp_context = EXPAND_EXPRESSION;
1605 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001606 else if (c == '#'
1607 && xp->xp_context == EXPAND_EXPRESSION)
1608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001609 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001610 break;
1611 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001612 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 && xp->xp_context == EXPAND_FUNCTIONS
1614 && vim_strchr(xp->xp_pattern, '(') == NULL)
1615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001616 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 break;
1618 }
1619 else if (cmdidx != CMD_let || got_eq)
1620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001621 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1624 if (c == '\\' && xp->xp_pattern[1] != NUL)
1625 ++xp->xp_pattern;
1626 xp->xp_context = EXPAND_NOTHING;
1627 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001628 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001630 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1632 /* skip */ ;
1633 xp->xp_context = EXPAND_NOTHING;
1634 }
1635 else if (c == '|')
1636 {
1637 if (xp->xp_pattern[1] == '|')
1638 {
1639 ++xp->xp_pattern;
1640 xp->xp_context = EXPAND_EXPRESSION;
1641 }
1642 else
1643 xp->xp_context = EXPAND_COMMANDS;
1644 }
1645 else
1646 xp->xp_context = EXPAND_EXPRESSION;
1647 }
1648 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001649 // Doesn't look like something valid, expand as an expression
1650 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 arg = xp->xp_pattern;
1653 if (*arg != NUL)
1654 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1655 /* skip */ ;
1656 }
1657 xp->xp_pattern = arg;
1658}
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001661 * Return TRUE if "pat" matches "text".
1662 * Does not use 'cpo' and always uses 'magic'.
1663 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001664 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001665pattern_match(char_u *pat, char_u *text, int ic)
1666{
1667 int matches = FALSE;
1668 char_u *save_cpo;
1669 regmatch_T regmatch;
1670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001672 save_cpo = p_cpo;
1673 p_cpo = (char_u *)"";
1674 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1675 if (regmatch.regprog != NULL)
1676 {
1677 regmatch.rm_ic = ic;
1678 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1679 vim_regfree(regmatch.regprog);
1680 }
1681 p_cpo = save_cpo;
1682 return matches;
1683}
1684
1685/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001686 * Handle a name followed by "(". Both for just "name(arg)" and for
1687 * "expr->name(arg)".
1688 * Returns OK or FAIL.
1689 */
1690 static int
1691eval_func(
1692 char_u **arg, // points to "(", will be advanced
1693 char_u *name,
1694 int name_len,
1695 typval_T *rettv,
1696 int evaluate,
1697 typval_T *basetv) // "expr" for "expr->name(arg)"
1698{
1699 char_u *s = name;
1700 int len = name_len;
1701 partial_T *partial;
1702 int ret = OK;
1703
1704 if (!evaluate)
1705 check_vars(s, len);
1706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001707 // If "s" is the name of a variable of type VAR_FUNC
1708 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001709 s = deref_func_name(s, &len, &partial, !evaluate);
1710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 // Need to make a copy, in case evaluating the arguments makes
1712 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001713 s = vim_strsave(s);
1714 if (s == NULL)
1715 ret = FAIL;
1716 else
1717 {
1718 funcexe_T funcexe;
1719
1720 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001721 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001722 funcexe.firstline = curwin->w_cursor.lnum;
1723 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001724 funcexe.evaluate = evaluate;
1725 funcexe.partial = partial;
1726 funcexe.basetv = basetv;
1727 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1728 }
1729 vim_free(s);
1730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001731 // If evaluate is FALSE rettv->v_type was not set in
1732 // get_func_tv, but it's needed in handle_subscript() to parse
1733 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001734 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1735 {
1736 rettv->vval.v_string = NULL;
1737 rettv->v_type = VAR_FUNC;
1738 }
1739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001740 // Stop the expression evaluation when immediately
1741 // aborting on error, or when an interrupt occurred or
1742 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001743 if (evaluate && aborting())
1744 {
1745 if (ret == OK)
1746 clear_tv(rettv);
1747 ret = FAIL;
1748 }
1749 return ret;
1750}
1751
1752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1756 */
1757
1758/*
1759 * Handle zero level expression.
1760 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001762 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 * Return OK or FAIL.
1764 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766eval0(
1767 char_u *arg,
1768 typval_T *rettv,
1769 char_u **nextcmd,
1770 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int ret;
1773 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001774 int did_emsg_before = did_emsg;
1775 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 ret = eval1(&p, rettv, evaluate);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001779 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
1781 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 /*
1784 * Report the invalid expression unless the expression evaluation has
1785 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001786 * exception, or we already gave a more specific error.
1787 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001789 if (!aborting() && did_emsg == did_emsg_before
1790 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001791 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ret = FAIL;
1793 }
1794 if (nextcmd != NULL)
1795 *nextcmd = check_nextcmd(p);
1796
1797 return ret;
1798}
1799
1800/*
1801 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001802 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 *
1804 * "arg" must point to the first non-white of the expression.
1805 * "arg" is advanced to the next non-white after the recognized expression.
1806 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001807 * Note: "rettv.v_lock" is not set.
1808 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * Return OK or FAIL.
1810 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 /*
1818 * Get the first variable.
1819 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return FAIL;
1822
1823 if ((*arg)[0] == '?')
1824 {
1825 result = FALSE;
1826 if (evaluate)
1827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001828 int error = FALSE;
1829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001830 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001833 if (error)
1834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836
1837 /*
1838 * Get the second variable.
1839 */
1840 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001841 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 return FAIL;
1843
1844 /*
1845 * Check for the ":".
1846 */
1847 if ((*arg)[0] != ':')
1848 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 return FAIL;
1853 }
1854
1855 /*
1856 * Get the third variable.
1857 */
1858 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001859 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 return FAIL;
1864 }
1865 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868
1869 return OK;
1870}
1871
1872/*
1873 * Handle first level expression:
1874 * expr2 || expr2 || expr2 logical OR
1875 *
1876 * "arg" must point to the first non-white of the expression.
1877 * "arg" is advanced to the next non-white after the recognized expression.
1878 *
1879 * Return OK or FAIL.
1880 */
1881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001882eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883{
Bram Moolenaar33570922005-01-25 22:26:29 +00001884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 long result;
1886 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001887 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 /*
1890 * Get the first variable.
1891 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 return FAIL;
1894
1895 /*
1896 * Repeat until there is no following "||".
1897 */
1898 first = TRUE;
1899 result = FALSE;
1900 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1901 {
1902 if (evaluate && first)
1903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001904 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001907 if (error)
1908 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 first = FALSE;
1910 }
1911
1912 /*
1913 * Get the second variable.
1914 */
1915 *arg = skipwhite(*arg + 2);
1916 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1917 return FAIL;
1918
1919 /*
1920 * Compute the result.
1921 */
1922 if (evaluate && !result)
1923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001924 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001927 if (error)
1928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 if (evaluate)
1931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 rettv->v_type = VAR_NUMBER;
1933 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935 }
1936
1937 return OK;
1938}
1939
1940/*
1941 * Handle second level expression:
1942 * expr3 && expr3 && expr3 logical AND
1943 *
1944 * "arg" must point to the first non-white of the expression.
1945 * "arg" is advanced to the next non-white after the recognized expression.
1946 *
1947 * Return OK or FAIL.
1948 */
1949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001950eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 long result;
1954 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 /*
1958 * Get the first variable.
1959 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 return FAIL;
1962
1963 /*
1964 * Repeat until there is no following "&&".
1965 */
1966 first = TRUE;
1967 result = TRUE;
1968 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1969 {
1970 if (evaluate && first)
1971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001972 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001975 if (error)
1976 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 first = FALSE;
1978 }
1979
1980 /*
1981 * Get the second variable.
1982 */
1983 *arg = skipwhite(*arg + 2);
1984 if (eval4(arg, &var2, evaluate && result) == FAIL)
1985 return FAIL;
1986
1987 /*
1988 * Compute the result.
1989 */
1990 if (evaluate && result)
1991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001992 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 if (error)
1996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998 if (evaluate)
1999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 rettv->v_type = VAR_NUMBER;
2001 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 }
2004
2005 return OK;
2006}
2007
2008/*
2009 * Handle third level expression:
2010 * var1 == var2
2011 * var1 =~ var2
2012 * var1 != var2
2013 * var1 !~ var2
2014 * var1 > var2
2015 * var1 >= var2
2016 * var1 < var2
2017 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002018 * var1 is var2
2019 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 *
2021 * "arg" must point to the first non-white of the expression.
2022 * "arg" is advanced to the next non-white after the recognized expression.
2023 *
2024 * Return OK or FAIL.
2025 */
2026 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002027eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaar33570922005-01-25 22:26:29 +00002029 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 char_u *p;
2031 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002032 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 /*
2037 * Get the first variable.
2038 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 return FAIL;
2041
2042 p = *arg;
2043 switch (p[0])
2044 {
2045 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002046 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002048 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 break;
2050 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002051 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002053 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 break;
2055 case '>': if (p[1] != '=')
2056 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002057 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 len = 1;
2059 }
2060 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002061 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 break;
2063 case '<': if (p[1] != '=')
2064 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002065 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 len = 1;
2067 }
2068 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002069 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002071 case 'i': if (p[1] == 's')
2072 {
2073 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2074 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002075 i = p[len];
2076 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002077 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002078 }
2079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081
2082 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002083 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002085 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002087 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (p[len] == '?')
2089 {
2090 ic = TRUE;
2091 ++len;
2092 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002093 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else if (p[len] == '#')
2095 {
2096 ic = FALSE;
2097 ++len;
2098 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002099 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 else
2101 ic = p_ic;
2102
2103 /*
2104 * Get the second variable.
2105 */
2106 *arg = skipwhite(p + len);
2107 if (eval5(arg, &var2, evaluate) == FAIL)
2108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 return FAIL;
2111 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002112 if (evaluate)
2113 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002114 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002115
2116 clear_tv(&var2);
2117 return ret;
2118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120
2121 return OK;
2122}
2123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 void
2125eval_addblob(typval_T *tv1, typval_T *tv2)
2126{
2127 blob_T *b1 = tv1->vval.v_blob;
2128 blob_T *b2 = tv2->vval.v_blob;
2129 blob_T *b = blob_alloc();
2130 int i;
2131
2132 if (b != NULL)
2133 {
2134 for (i = 0; i < blob_len(b1); i++)
2135 ga_append(&b->bv_ga, blob_get(b1, i));
2136 for (i = 0; i < blob_len(b2); i++)
2137 ga_append(&b->bv_ga, blob_get(b2, i));
2138
2139 clear_tv(tv1);
2140 rettv_blob_set(tv1, b);
2141 }
2142}
2143
2144 int
2145eval_addlist(typval_T *tv1, typval_T *tv2)
2146{
2147 typval_T var3;
2148
2149 // concatenate Lists
2150 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2151 {
2152 clear_tv(tv1);
2153 clear_tv(tv2);
2154 return FAIL;
2155 }
2156 clear_tv(tv1);
2157 *tv1 = var3;
2158 return OK;
2159}
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161/*
2162 * Handle fourth level expression:
2163 * + number addition
2164 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002165 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002166 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 *
2168 * "arg" must point to the first non-white of the expression.
2169 * "arg" is advanced to the next non-white after the recognized expression.
2170 *
2171 * Return OK or FAIL.
2172 */
2173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
Bram Moolenaar33570922005-01-25 22:26:29 +00002176 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002178 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002179#ifdef FEAT_FLOAT
2180 float_T f1 = 0, f2 = 0;
2181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 char_u *s1, *s2;
2183 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2184 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002185 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
2188 * Get the first variable.
2189 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002190 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 return FAIL;
2192
2193 /*
2194 * Repeat computing, until no '+', '-' or '.' is following.
2195 */
2196 for (;;)
2197 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002198 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002200 concat = op == '.'
2201 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2202 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 break;
2204
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002205 if ((op != '+' || (rettv->v_type != VAR_LIST
2206 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002207#ifdef FEAT_FLOAT
2208 && (op == '.' || rettv->v_type != VAR_FLOAT)
2209#endif
2210 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // For "list + ...", an illegal use of the first operand as
2213 // a number cannot be determined before evaluating the 2nd
2214 // operand: if this is also a list, all is ok.
2215 // For "something . ...", "something - ..." or "non-list + ...",
2216 // we know that the first operand needs to be a string or number
2217 // without evaluating the 2nd operand. So check before to avoid
2218 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002219 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002220 {
2221 clear_tv(rettv);
2222 return FAIL;
2223 }
2224 }
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /*
2227 * Get the second variable.
2228 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002229 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2230 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002232 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return FAIL;
2236 }
2237
2238 if (evaluate)
2239 {
2240 /*
2241 * Compute the result.
2242 */
2243 if (op == '.')
2244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002246 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002247 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002248 {
2249 clear_tv(rettv);
2250 clear_tv(&var2);
2251 return FAIL;
2252 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002253 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 clear_tv(rettv);
2255 rettv->v_type = VAR_STRING;
2256 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002258 else if (op == '+' && rettv->v_type == VAR_BLOB
2259 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002261 else if (op == '+' && rettv->v_type == VAR_LIST
2262 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002263 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002265 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 else
2268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 int error = FALSE;
2270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002271#ifdef FEAT_FLOAT
2272 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002274 f1 = rettv->vval.v_float;
2275 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002277 else
2278#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002280 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002281 if (error)
2282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002283 // This can only happen for "list + non-list". For
2284 // "non-list + ..." or "something - ...", we returned
2285 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002286 clear_tv(rettv);
2287 return FAIL;
2288 }
2289#ifdef FEAT_FLOAT
2290 if (var2.v_type == VAR_FLOAT)
2291 f1 = n1;
2292#endif
2293 }
2294#ifdef FEAT_FLOAT
2295 if (var2.v_type == VAR_FLOAT)
2296 {
2297 f2 = var2.vval.v_float;
2298 n2 = 0;
2299 }
2300 else
2301#endif
2302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002303 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002304 if (error)
2305 {
2306 clear_tv(rettv);
2307 clear_tv(&var2);
2308 return FAIL;
2309 }
2310#ifdef FEAT_FLOAT
2311 if (rettv->v_type == VAR_FLOAT)
2312 f2 = n2;
2313#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002316
2317#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002319 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2320 {
2321 if (op == '+')
2322 f1 = f1 + f2;
2323 else
2324 f1 = f1 - f2;
2325 rettv->v_type = VAR_FLOAT;
2326 rettv->vval.v_float = f1;
2327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002329#endif
2330 {
2331 if (op == '+')
2332 n1 = n1 + n2;
2333 else
2334 n1 = n1 - n2;
2335 rettv->v_type = VAR_NUMBER;
2336 rettv->vval.v_number = n1;
2337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 }
2342 return OK;
2343}
2344
2345/*
2346 * Handle fifth level expression:
2347 * * number multiplication
2348 * / number division
2349 * % number modulo
2350 *
2351 * "arg" must point to the first non-white of the expression.
2352 * "arg" is advanced to the next non-white after the recognized expression.
2353 *
2354 * Return OK or FAIL.
2355 */
2356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002357eval6(
2358 char_u **arg,
2359 typval_T *rettv,
2360 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002361 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
Bram Moolenaar33570922005-01-25 22:26:29 +00002363 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002365 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002366#ifdef FEAT_FLOAT
2367 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002368 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002369#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 /*
2373 * Get the first variable.
2374 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002375 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 return FAIL;
2377
2378 /*
2379 * Repeat computing, until no '*', '/' or '%' is following.
2380 */
2381 for (;;)
2382 {
2383 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002384 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 break;
2386
2387 if (evaluate)
2388 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002389#ifdef FEAT_FLOAT
2390 if (rettv->v_type == VAR_FLOAT)
2391 {
2392 f1 = rettv->vval.v_float;
2393 use_float = TRUE;
2394 n1 = 0;
2395 }
2396 else
2397#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002398 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 if (error)
2401 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 else
2404 n1 = 0;
2405
2406 /*
2407 * Get the second variable.
2408 */
2409 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002410 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 return FAIL;
2412
2413 if (evaluate)
2414 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002415#ifdef FEAT_FLOAT
2416 if (var2.v_type == VAR_FLOAT)
2417 {
2418 if (!use_float)
2419 {
2420 f1 = n1;
2421 use_float = TRUE;
2422 }
2423 f2 = var2.vval.v_float;
2424 n2 = 0;
2425 }
2426 else
2427#endif
2428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002429 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002430 clear_tv(&var2);
2431 if (error)
2432 return FAIL;
2433#ifdef FEAT_FLOAT
2434 if (use_float)
2435 f2 = n2;
2436#endif
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 /*
2440 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002441 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443#ifdef FEAT_FLOAT
2444 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002446 if (op == '*')
2447 f1 = f1 * f2;
2448 else if (op == '/')
2449 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002450# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002451 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002452 if (f2 == 0.0)
2453 {
2454 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002455 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002456 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002457 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002458 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002459 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002460 }
2461 else
2462 f1 = f1 / f2;
2463# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002464 // We rely on the floating point library to handle divide
2465 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002466 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002467# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002470 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002472 return FAIL;
2473 }
2474 rettv->v_type = VAR_FLOAT;
2475 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002480 if (op == '*')
2481 n1 = n1 * n2;
2482 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002483 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002485 n1 = num_modulus(n1, n2);
2486
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002487 rettv->v_type = VAR_NUMBER;
2488 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 }
2492
2493 return OK;
2494}
2495
2496/*
2497 * Handle sixth level expression:
2498 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002499 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002500 * "string" string constant
2501 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 * &option-name option value
2503 * @r register contents
2504 * identifier variable value
2505 * function() function call
2506 * $VAR environment variable
2507 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002508 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002510 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002511 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 *
2513 * Also handle:
2514 * ! in front logical NOT
2515 * - in front unary minus
2516 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 * trailing [] subscript in String or List
2518 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002519 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 *
2521 * "arg" must point to the first non-white of the expression.
2522 * "arg" is advanced to the next non-white after the recognized expression.
2523 *
2524 * Return OK or FAIL.
2525 */
2526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002527eval7(
2528 char_u **arg,
2529 typval_T *rettv,
2530 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int len;
2534 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 char_u *start_leader, *end_leader;
2536 int ret = OK;
2537 char_u *alias;
2538
2539 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002541 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002546 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
2548 start_leader = *arg;
2549 while (**arg == '!' || **arg == '-' || **arg == '+')
2550 *arg = skipwhite(*arg + 1);
2551 end_leader = *arg;
2552
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002553 if (**arg == '.' && (!isdigit(*(*arg + 1))
2554#ifdef FEAT_FLOAT
2555 || current_sctx.sc_version < 2
2556#endif
2557 ))
2558 {
2559 semsg(_(e_invexpr2), *arg);
2560 ++*arg;
2561 return FAIL;
2562 }
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 switch (**arg)
2565 {
2566 /*
2567 * Number constant.
2568 */
2569 case '0':
2570 case '1':
2571 case '2':
2572 case '3':
2573 case '4':
2574 case '5':
2575 case '6':
2576 case '7':
2577 case '8':
2578 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 break;
2581
2582 /*
2583 * String constant: "string".
2584 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 break;
2587
2588 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002592 break;
2593
2594 /*
2595 * List: [expr, expr]
2596 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 break;
2599
2600 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002601 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002602 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002603 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002604 {
2605 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002606 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002607 }
2608 else
2609 ret = NOTDONE;
2610 break;
2611
2612 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002613 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002614 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002616 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2617 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002618 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 break;
2620
2621 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002622 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002624 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 break;
2626
2627 /*
2628 * Environment variable: $VAR.
2629 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 break;
2632
2633 /*
2634 * Register contents: @r.
2635 */
2636 case '@': ++*arg;
2637 if (evaluate)
2638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002640 rettv->vval.v_string = get_reg_contents(**arg,
2641 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 if (**arg != NUL)
2644 ++*arg;
2645 break;
2646
2647 /*
2648 * nested expression: (expression).
2649 */
2650 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002651 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (**arg == ')')
2653 ++*arg;
2654 else if (ret == OK)
2655 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ret = FAIL;
2659 }
2660 break;
2661
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 break;
2664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665
2666 if (ret == NOTDONE)
2667 {
2668 /*
2669 * Must be a variable or function name.
2670 * Can also be a curly-braces kind of name: {expr}.
2671 */
2672 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002673 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 if (alias != NULL)
2675 s = alias;
2676
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002677 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 ret = FAIL;
2679 else
2680 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002681 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002682 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002684 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002686 {
2687 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002691 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 *arg = skipwhite(*arg);
2695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002696 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2697 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002698 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002699 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2700 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 /*
2703 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2704 */
2705 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002706 ret = eval7_leader(rettv, start_leader, &end_leader);
2707 return ret;
2708}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002710/*
2711 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2712 * Adjusts "end_leaderp" until it is at "start_leader".
2713 */
2714 static int
2715eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2716{
2717 char_u *end_leader = *end_leaderp;
2718 int ret = OK;
2719 int error = FALSE;
2720 varnumber_T val = 0;
2721#ifdef FEAT_FLOAT
2722 float_T f = 0.0;
2723
2724 if (rettv->v_type == VAR_FLOAT)
2725 f = rettv->vval.v_float;
2726 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002727#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002728 val = tv_get_number_chk(rettv, &error);
2729 if (error)
2730 {
2731 clear_tv(rettv);
2732 ret = FAIL;
2733 }
2734 else
2735 {
2736 while (end_leader > start_leader)
2737 {
2738 --end_leader;
2739 if (*end_leader == '!')
2740 {
2741#ifdef FEAT_FLOAT
2742 if (rettv->v_type == VAR_FLOAT)
2743 f = !f;
2744 else
2745#endif
2746 val = !val;
2747 }
2748 else if (*end_leader == '-')
2749 {
2750#ifdef FEAT_FLOAT
2751 if (rettv->v_type == VAR_FLOAT)
2752 f = -f;
2753 else
2754#endif
2755 val = -val;
2756 }
2757 }
2758#ifdef FEAT_FLOAT
2759 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002762 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002765#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002767 clear_tv(rettv);
2768 rettv->v_type = VAR_NUMBER;
2769 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002772 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return ret;
2774}
2775
2776/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002777 * Call the function referred to in "rettv".
2778 */
2779 static int
2780call_func_rettv(
2781 char_u **arg,
2782 typval_T *rettv,
2783 int evaluate,
2784 dict_T *selfdict,
2785 typval_T *basetv)
2786{
2787 partial_T *pt = NULL;
2788 funcexe_T funcexe;
2789 typval_T functv;
2790 char_u *s;
2791 int ret;
2792
2793 // need to copy the funcref so that we can clear rettv
2794 if (evaluate)
2795 {
2796 functv = *rettv;
2797 rettv->v_type = VAR_UNKNOWN;
2798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002799 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002800 if (functv.v_type == VAR_PARTIAL)
2801 {
2802 pt = functv.vval.v_partial;
2803 s = partial_name(pt);
2804 }
2805 else
2806 s = functv.vval.v_string;
2807 }
2808 else
2809 s = (char_u *)"";
2810
Bram Moolenaara80faa82020-04-12 19:37:17 +02002811 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002812 funcexe.firstline = curwin->w_cursor.lnum;
2813 funcexe.lastline = curwin->w_cursor.lnum;
2814 funcexe.evaluate = evaluate;
2815 funcexe.partial = pt;
2816 funcexe.selfdict = selfdict;
2817 funcexe.basetv = basetv;
2818 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002820 // Clear the funcref afterwards, so that deleting it while
2821 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002822 if (evaluate)
2823 clear_tv(&functv);
2824
2825 return ret;
2826}
2827
2828/*
2829 * Evaluate "->method()".
2830 * "*arg" points to the '-'.
2831 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2832 */
2833 static int
2834eval_lambda(
2835 char_u **arg,
2836 typval_T *rettv,
2837 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002838 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002839{
2840 typval_T base = *rettv;
2841 int ret;
2842
2843 // Skip over the ->.
2844 *arg += 2;
2845 rettv->v_type = VAR_UNKNOWN;
2846
2847 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002848 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002849 return FAIL;
2850 else if (**arg != '(')
2851 {
2852 if (verbose)
2853 {
2854 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002855 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002856 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002858 }
2859 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002860 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002861 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002862 else
2863 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2864
2865 // Clear the funcref afterwards, so that deleting it while
2866 // evaluating the arguments is possible (see test55).
2867 if (evaluate)
2868 clear_tv(&base);
2869
2870 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002871}
2872
2873/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002874 * Evaluate "->method()".
2875 * "*arg" points to the '-'.
2876 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2877 */
2878 static int
2879eval_method(
2880 char_u **arg,
2881 typval_T *rettv,
2882 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002883 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002884{
2885 char_u *name;
2886 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002887 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002888 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002889 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002890
2891 // Skip over the ->.
2892 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002893 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002894
Bram Moolenaarac92e252019-08-03 21:58:38 +02002895 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002896 len = get_name_len(arg, &alias, evaluate, TRUE);
2897 if (alias != NULL)
2898 name = alias;
2899
2900 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002901 {
2902 if (verbose)
2903 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002905 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002906 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002907 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 if (**arg != '(')
2909 {
2910 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002912 ret = FAIL;
2913 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002914 else if (VIM_ISWHITE((*arg)[-1]))
2915 {
2916 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002917 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002918 ret = FAIL;
2919 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002920 else
2921 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002923
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002924 // Clear the funcref afterwards, so that deleting it while
2925 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002926 if (evaluate)
2927 clear_tv(&base);
2928
Bram Moolenaarac92e252019-08-03 21:58:38 +02002929 return ret;
2930}
2931
2932/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002933 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2934 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002935 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2936 */
2937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938eval_index(
2939 char_u **arg,
2940 typval_T *rettv,
2941 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002942 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002943{
2944 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002947 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 long len = -1;
2949 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002950 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002951 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002952
Bram Moolenaara03f2332016-02-06 18:09:59 +01002953 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002954 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002956 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002957 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002958 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002959 return FAIL;
2960 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002961#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002962 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002963 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002964 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002965#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002966 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002967 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002969 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002970 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002971 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002972 return FAIL;
2973 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002974 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 if (evaluate)
2977 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002978 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002979
2980 case VAR_STRING:
2981 case VAR_NUMBER:
2982 case VAR_LIST:
2983 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002984 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002985 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002986 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002987
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002988 init_tv(&var1);
2989 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002992 /*
2993 * dict.name
2994 */
2995 key = *arg + 1;
2996 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2997 ;
2998 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002999 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003000 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003001 }
3002 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004 /*
3005 * something[idx]
3006 *
3007 * Get the (first) variable from inside the [].
3008 */
3009 *arg = skipwhite(*arg + 1);
3010 if (**arg == ':')
3011 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003012 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003014 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003016 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003017 clear_tv(&var1);
3018 return FAIL;
3019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003020
3021 /*
3022 * Get the second variable from inside the [:].
3023 */
3024 if (**arg == ':')
3025 {
3026 range = TRUE;
3027 *arg = skipwhite(*arg + 1);
3028 if (**arg == ']')
3029 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 if (!empty1)
3033 clear_tv(&var1);
3034 return FAIL;
3035 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003038 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003039 if (!empty1)
3040 clear_tv(&var1);
3041 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 return FAIL;
3043 }
3044 }
3045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003046 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003047 if (**arg != ']')
3048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003049 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051 clear_tv(&var1);
3052 if (range)
3053 clear_tv(&var2);
3054 return FAIL;
3055 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003056 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003057 }
3058
3059 if (evaluate)
3060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061 n1 = 0;
3062 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003063 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003064 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003066 }
3067 if (range)
3068 {
3069 if (empty2)
3070 n2 = -1;
3071 else
3072 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003075 }
3076 }
3077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003079 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003080 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003081 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003083 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003084 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003085 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003086 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003087 case VAR_SPECIAL:
3088 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003089 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003091
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003092 case VAR_NUMBER:
3093 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003094 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003095 len = (long)STRLEN(s);
3096 if (range)
3097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 // The resulting variable is a substring. If the indexes
3099 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003100 if (n1 < 0)
3101 {
3102 n1 = len + n1;
3103 if (n1 < 0)
3104 n1 = 0;
3105 }
3106 if (n2 < 0)
3107 n2 = len + n2;
3108 else if (n2 >= len)
3109 n2 = len;
3110 if (n1 >= len || n2 < 0 || n1 > n2)
3111 s = NULL;
3112 else
3113 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3114 }
3115 else
3116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003117 // The resulting variable is a string of a single
3118 // character. If the index is too big or negative the
3119 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 if (n1 >= len || n1 < 0)
3121 s = NULL;
3122 else
3123 s = vim_strnsave(s + n1, 1);
3124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 clear_tv(rettv);
3126 rettv->v_type = VAR_STRING;
3127 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003128 break;
3129
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003130 case VAR_BLOB:
3131 len = blob_len(rettv->vval.v_blob);
3132 if (range)
3133 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003134 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003135 // are out of range the result is empty.
3136 if (n1 < 0)
3137 {
3138 n1 = len + n1;
3139 if (n1 < 0)
3140 n1 = 0;
3141 }
3142 if (n2 < 0)
3143 n2 = len + n2;
3144 else if (n2 >= len)
3145 n2 = len - 1;
3146 if (n1 >= len || n2 < 0 || n1 > n2)
3147 {
3148 clear_tv(rettv);
3149 rettv->v_type = VAR_BLOB;
3150 rettv->vval.v_blob = NULL;
3151 }
3152 else
3153 {
3154 blob_T *blob = blob_alloc();
3155
3156 if (blob != NULL)
3157 {
3158 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3159 {
3160 blob_free(blob);
3161 return FAIL;
3162 }
3163 blob->bv_ga.ga_len = n2 - n1 + 1;
3164 for (i = n1; i <= n2; i++)
3165 blob_set(blob, i - n1,
3166 blob_get(rettv->vval.v_blob, i));
3167
3168 clear_tv(rettv);
3169 rettv_blob_set(rettv, blob);
3170 }
3171 }
3172 }
3173 else
3174 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003175 // The resulting variable is a byte value.
3176 // If the index is too big or negative that is an error.
3177 if (n1 < 0)
3178 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003179 if (n1 < len && n1 >= 0)
3180 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003181 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003182
3183 clear_tv(rettv);
3184 rettv->v_type = VAR_NUMBER;
3185 rettv->vval.v_number = v;
3186 }
3187 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003188 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003189 }
3190 break;
3191
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003192 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003194 if (n1 < 0)
3195 n1 = len + n1;
3196 if (!empty1 && (n1 < 0 || n1 >= len))
3197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // For a range we allow invalid values and return an empty
3199 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003200 if (!range)
3201 {
3202 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003204 return FAIL;
3205 }
3206 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003207 }
3208 if (range)
3209 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 list_T *l;
3211 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003212
3213 if (n2 < 0)
3214 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003215 else if (n2 >= len)
3216 n2 = len - 1;
3217 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003218 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003219 l = list_alloc();
3220 if (l == NULL)
3221 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003223 n1 <= n2; ++n1)
3224 {
3225 if (list_append_tv(l, &item->li_tv) == FAIL)
3226 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003227 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003228 return FAIL;
3229 }
3230 item = item->li_next;
3231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003232 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003233 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003234 }
3235 else
3236 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003237 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003238 clear_tv(rettv);
3239 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 }
3241 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242
3243 case VAR_DICT:
3244 if (range)
3245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003246 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003247 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003248 if (len == -1)
3249 clear_tv(&var1);
3250 return FAIL;
3251 }
3252 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003254
3255 if (len == -1)
3256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003257 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003258 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260 clear_tv(&var1);
3261 return FAIL;
3262 }
3263 }
3264
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003265 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003268 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269 if (len == -1)
3270 clear_tv(&var1);
3271 if (item == NULL)
3272 return FAIL;
3273
3274 copy_tv(&item->di_tv, &var1);
3275 clear_tv(rettv);
3276 *rettv = var1;
3277 }
3278 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 }
3280 }
3281
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003282 return OK;
3283}
3284
3285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 * Get an option value.
3287 * "arg" points to the '&' or '+' before the option name.
3288 * "arg" is advanced to character after the option name.
3289 * Return OK or FAIL.
3290 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292get_option_tv(
3293 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 char_u *option_end;
3298 long numval;
3299 char_u *stringval;
3300 int opt_type;
3301 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003302 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int ret = OK;
3304 int opt_flags;
3305
3306 /*
3307 * Isolate the option name and find its value.
3308 */
3309 option_end = find_option_end(arg, &opt_flags);
3310 if (option_end == NULL)
3311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003312 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return FAIL;
3315 }
3316
3317 if (!evaluate)
3318 {
3319 *arg = option_end;
3320 return OK;
3321 }
3322
3323 c = *option_end;
3324 *option_end = NUL;
3325 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003330 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 ret = FAIL;
3333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003336 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003338 rettv->v_type = VAR_STRING;
3339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 rettv->v_type = VAR_NUMBER;
3344 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 rettv->v_type = VAR_NUMBER;
3349 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003351 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003353 rettv->v_type = VAR_STRING;
3354 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
3356 }
3357 else if (working && (opt_type == -2 || opt_type == -1))
3358 ret = FAIL;
3359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 *arg = option_end;
3362
3363 return ret;
3364}
3365
3366/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3368 * Return OK or FAIL.
3369 */
3370 int
3371get_number_tv(
3372 char_u **arg,
3373 typval_T *rettv,
3374 int evaluate,
3375 int want_string UNUSED)
3376{
3377 int len;
3378#ifdef FEAT_FLOAT
3379 char_u *p;
3380 int get_float = FALSE;
3381
3382 // We accept a float when the format matches
3383 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3384 // strict to avoid backwards compatibility problems.
3385 // With script version 2 and later the leading digit can be
3386 // omitted.
3387 // Don't look for a float after the "." operator, so that
3388 // ":let vers = 1.2.3" doesn't fail.
3389 if (**arg == '.')
3390 p = *arg;
3391 else
3392 p = skipdigits(*arg + 1);
3393 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3394 {
3395 get_float = TRUE;
3396 p = skipdigits(p + 2);
3397 if (*p == 'e' || *p == 'E')
3398 {
3399 ++p;
3400 if (*p == '-' || *p == '+')
3401 ++p;
3402 if (!vim_isdigit(*p))
3403 get_float = FALSE;
3404 else
3405 p = skipdigits(p + 1);
3406 }
3407 if (ASCII_ISALPHA(*p) || *p == '.')
3408 get_float = FALSE;
3409 }
3410 if (get_float)
3411 {
3412 float_T f;
3413
3414 *arg += string2float(*arg, &f);
3415 if (evaluate)
3416 {
3417 rettv->v_type = VAR_FLOAT;
3418 rettv->vval.v_float = f;
3419 }
3420 }
3421 else
3422#endif
3423 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3424 {
3425 char_u *bp;
3426 blob_T *blob = NULL; // init for gcc
3427
3428 // Blob constant: 0z0123456789abcdef
3429 if (evaluate)
3430 blob = blob_alloc();
3431 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3432 {
3433 if (!vim_isxdigit(bp[1]))
3434 {
3435 if (blob != NULL)
3436 {
3437 emsg(_("E973: Blob literal should have an even number of hex characters"));
3438 ga_clear(&blob->bv_ga);
3439 VIM_CLEAR(blob);
3440 }
3441 return FAIL;
3442 }
3443 if (blob != NULL)
3444 ga_append(&blob->bv_ga,
3445 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3446 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3447 ++bp;
3448 }
3449 if (blob != NULL)
3450 rettv_blob_set(rettv, blob);
3451 *arg = bp;
3452 }
3453 else
3454 {
3455 varnumber_T n;
3456
3457 // decimal, hex or octal number
3458 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3459 ? STR2NR_NO_OCT + STR2NR_QUOTE
3460 : STR2NR_ALL, &n, NULL, 0, TRUE);
3461 if (len == 0)
3462 {
3463 semsg(_(e_invexpr2), *arg);
3464 return FAIL;
3465 }
3466 *arg += len;
3467 if (evaluate)
3468 {
3469 rettv->v_type = VAR_NUMBER;
3470 rettv->vval.v_number = n;
3471 }
3472 }
3473 return OK;
3474}
3475
3476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 * Allocate a variable for a string constant.
3478 * Return OK or FAIL.
3479 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003481get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 char_u *p;
3484 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int extra = 0;
3486
3487 /*
3488 * Find the end of the string, skipping backslashed characters.
3489 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003490 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 if (*p == '\\' && p[1] != NUL)
3493 {
3494 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003495 // A "\<x>" form occupies at least 4 characters, and produces up
3496 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (*p == '<')
3498 extra += 2;
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
3502 if (*p != '"')
3503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003504 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 return FAIL;
3506 }
3507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (!evaluate)
3510 {
3511 *arg = p + 1;
3512 return OK;
3513 }
3514
3515 /*
3516 * Copy the string into allocated memory, handling backslashed
3517 * characters.
3518 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003519 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (name == NULL)
3521 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 rettv->v_type = VAR_STRING;
3523 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (*p == '\\')
3528 {
3529 switch (*++p)
3530 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 case 'b': *name++ = BS; ++p; break;
3532 case 'e': *name++ = ESC; ++p; break;
3533 case 'f': *name++ = FF; ++p; break;
3534 case 'n': *name++ = NL; ++p; break;
3535 case 'r': *name++ = CAR; ++p; break;
3536 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003538 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003540 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 case 'U':
3542 if (vim_isxdigit(p[1]))
3543 {
3544 int n, nr;
3545 int c = toupper(*p);
3546
3547 if (c == 'X')
3548 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003549 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003551 else
3552 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 nr = 0;
3554 while (--n >= 0 && vim_isxdigit(p[1]))
3555 {
3556 ++p;
3557 nr = (nr << 4) + hex2nr(*p);
3558 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003560 // For "\u" store the number according to
3561 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003565 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003569 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 case '0':
3571 case '1':
3572 case '2':
3573 case '3':
3574 case '4':
3575 case '5':
3576 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 case '7': *name = *p++ - '0';
3578 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 *name = (*name << 3) + *p++ - '0';
3581 if (*p >= '0' && *p <= '7')
3582 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 break;
3586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003587 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003588 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3589 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (extra != 0)
3591 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 break;
3594 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003595 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 break;
3599 }
3600 }
3601 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003607 ++p;
3608 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 return OK;
3611}
3612
3613/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * Return OK or FAIL.
3616 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003621 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003622 int reduce = 0;
3623
3624 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003626 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003627 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003629 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003632 break;
3633 ++reduce;
3634 ++p;
3635 }
3636 }
3637
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003640 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003641 return FAIL;
3642 }
3643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003644 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003645 if (!evaluate)
3646 {
3647 *arg = p + 1;
3648 return OK;
3649 }
3650
3651 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003653 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003654 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003655 if (str == NULL)
3656 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 rettv->v_type = VAR_STRING;
3658 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003665 break;
3666 ++p;
3667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003671 *arg = p + 1;
3672
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003673 return OK;
3674}
3675
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003676/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003677 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 */
3679 char_u *
3680partial_name(partial_T *pt)
3681{
3682 if (pt->pt_name != NULL)
3683 return pt->pt_name;
3684 return pt->pt_func->uf_name;
3685}
3686
Bram Moolenaarddecc252016-04-06 22:59:37 +02003687 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003688partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003689{
3690 int i;
3691
3692 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003693 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003694 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003695 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696 if (pt->pt_name != NULL)
3697 {
3698 func_unref(pt->pt_name);
3699 vim_free(pt->pt_name);
3700 }
3701 else
3702 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003703 vim_free(pt);
3704}
3705
3706/*
3707 * Unreference a closure: decrement the reference count and free it when it
3708 * becomes zero.
3709 */
3710 void
3711partial_unref(partial_T *pt)
3712{
3713 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003714 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003715}
3716
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003717static int tv_equal_recurse_limit;
3718
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003719 static int
3720func_equal(
3721 typval_T *tv1,
3722 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003723 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003724{
3725 char_u *s1, *s2;
3726 dict_T *d1, *d2;
3727 int a1, a2;
3728 int i;
3729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003730 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003731 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003732 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003733 if (s1 != NULL && *s1 == NUL)
3734 s1 = NULL;
3735 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003736 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003737 if (s2 != NULL && *s2 == NUL)
3738 s2 = NULL;
3739 if (s1 == NULL || s2 == NULL)
3740 {
3741 if (s1 != s2)
3742 return FALSE;
3743 }
3744 else if (STRCMP(s1, s2) != 0)
3745 return FALSE;
3746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003747 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003748 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3749 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3750 if (d1 == NULL || d2 == NULL)
3751 {
3752 if (d1 != d2)
3753 return FALSE;
3754 }
3755 else if (!dict_equal(d1, d2, ic, TRUE))
3756 return FALSE;
3757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003758 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003759 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3760 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3761 if (a1 != a2)
3762 return FALSE;
3763 for (i = 0; i < a1; ++i)
3764 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3765 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3766 return FALSE;
3767
3768 return TRUE;
3769}
3770
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003771/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003772 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003773 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003774 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003775 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777tv_equal(
3778 typval_T *tv1,
3779 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 int ic, // ignore case
3781 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003782{
3783 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003786 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003787
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003788 // Catch lists and dicts that have an endless loop by limiting
3789 // recursiveness to a limit. We guess they are equal then.
3790 // A fixed limit has the problem of still taking an awful long time.
3791 // Reduce the limit every time running into it. That should work fine for
3792 // deeply linked structures that are not recursively linked and catch
3793 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003794 if (!recursive)
3795 tv_equal_recurse_limit = 1000;
3796 if (recursive_cnt >= tv_equal_recurse_limit)
3797 {
3798 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003799 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003800 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003802 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3803 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003804 if ((tv1->v_type == VAR_FUNC
3805 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3806 && (tv2->v_type == VAR_FUNC
3807 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3808 {
3809 ++recursive_cnt;
3810 r = func_equal(tv1, tv2, ic);
3811 --recursive_cnt;
3812 return r;
3813 }
3814
3815 if (tv1->v_type != tv2->v_type)
3816 return FALSE;
3817
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003818 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003819 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003820 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003821 ++recursive_cnt;
3822 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3823 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003824 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003825
3826 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003827 ++recursive_cnt;
3828 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3829 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003830 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003831
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003832 case VAR_BLOB:
3833 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3834
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003835 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 case VAR_BOOL:
3837 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003838 return tv1->vval.v_number == tv2->vval.v_number;
3839
3840 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003841 s1 = tv_get_string_buf(tv1, buf1);
3842 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003843 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003844
Bram Moolenaar835dc632016-02-07 14:27:38 +01003845 case VAR_FLOAT:
3846#ifdef FEAT_FLOAT
3847 return tv1->vval.v_float == tv2->vval.v_float;
3848#endif
3849 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003850#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003851 return tv1->vval.v_job == tv2->vval.v_job;
3852#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003853 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003854#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003855 return tv1->vval.v_channel == tv2->vval.v_channel;
3856#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003858 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003859 return tv1->vval.v_partial == tv2->vval.v_partial;
3860
3861 case VAR_FUNC:
3862 return tv1->vval.v_string == tv2->vval.v_string;
3863
Bram Moolenaar835dc632016-02-07 14:27:38 +01003864 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003865 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003867 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003868 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3871 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003872 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003873}
3874
3875/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003876 * Return the next (unique) copy ID.
3877 * Used for serializing nested structures.
3878 */
3879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003880get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003881{
3882 current_copyID += COPYID_INC;
3883 return current_copyID;
3884}
3885
3886/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003887 * Garbage collection for lists and dictionaries.
3888 *
3889 * We use reference counts to be able to free most items right away when they
3890 * are no longer used. But for composite items it's possible that it becomes
3891 * unused while the reference count is > 0: When there is a recursive
3892 * reference. Example:
3893 * :let l = [1, 2, 3]
3894 * :let d = {9: l}
3895 * :let l[1] = d
3896 *
3897 * Since this is quite unusual we handle this with garbage collection: every
3898 * once in a while find out which lists and dicts are not referenced from any
3899 * variable.
3900 *
3901 * Here is a good reference text about garbage collection (refers to Python
3902 * but it applies to all reference-counting mechanisms):
3903 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003904 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003905
3906/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003907 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003908 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003910 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003911 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003912garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003913{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003914 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003915 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003916 buf_T *buf;
3917 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003918 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003919 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003920
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003921 if (!testing)
3922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003923 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003924 want_garbage_collect = FALSE;
3925 may_garbage_collect = FALSE;
3926 garbage_collect_at_exit = FALSE;
3927 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003928
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003929 // The execution stack can grow big, limit the size.
3930 if (exestack.ga_maxlen - exestack.ga_len > 500)
3931 {
3932 size_t new_len;
3933 char_u *pp;
3934 int n;
3935
3936 // Keep 150% of the current size, with a minimum of the growth size.
3937 n = exestack.ga_len / 2;
3938 if (n < exestack.ga_growsize)
3939 n = exestack.ga_growsize;
3940
3941 // Don't make it bigger though.
3942 if (exestack.ga_len + n < exestack.ga_maxlen)
3943 {
3944 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3945 pp = vim_realloc(exestack.ga_data, new_len);
3946 if (pp == NULL)
3947 return FAIL;
3948 exestack.ga_maxlen = exestack.ga_len + n;
3949 exestack.ga_data = pp;
3950 }
3951 }
3952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // We advance by two because we add one for items referenced through
3954 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003955 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003956
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957 /*
3958 * 1. Go through all accessible variables and mark all lists and dicts
3959 * with copyID.
3960 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003962 // Don't free variables in the previous_funccal list unless they are only
3963 // referenced through previous_funccal. This must be first, because if
3964 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003967 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003968 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003970 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003971 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003972 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3973 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003975 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003976 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3978 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003979 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003980 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3981 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003982#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003983 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003984 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3985 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003986 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003987 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003988 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3989 NULL, NULL);
3990#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003992 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003993 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3995 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003997 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004002 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004003 abort = abort || set_ref_in_functions(copyID);
4004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004005 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004009 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004010
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004011 // callbacks in buffers
4012 abort = abort || set_ref_in_buffers(copyID);
4013
Bram Moolenaar1dced572012-04-05 16:54:08 +02004014#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004015 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004016#endif
4017
Bram Moolenaardb913952012-06-29 12:54:53 +02004018#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004019 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004020#endif
4021
4022#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004023 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004024#endif
4025
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004026#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004027 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004028 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004029#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004030#ifdef FEAT_NETBEANS_INTG
4031 abort = abort || set_ref_in_nb_channel(copyID);
4032#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004033
Bram Moolenaare3188e22016-05-31 21:13:04 +02004034#ifdef FEAT_TIMERS
4035 abort = abort || set_ref_in_timer(copyID);
4036#endif
4037
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004038#ifdef FEAT_QUICKFIX
4039 abort = abort || set_ref_in_quickfix(copyID);
4040#endif
4041
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004042#ifdef FEAT_TERMINAL
4043 abort = abort || set_ref_in_term(copyID);
4044#endif
4045
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004046#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004047 abort = abort || set_ref_in_popups(copyID);
4048#endif
4049
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004050 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004051 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004052 /*
4053 * 2. Free lists and dictionaries that are not referenced.
4054 */
4055 did_free = free_unref_items(copyID);
4056
4057 /*
4058 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004062 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004063 else if (p_verbose > 0)
4064 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004065 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004067
4068 return did_free;
4069}
4070
4071/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004073 */
4074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004075free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004076{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004077 int did_free = FALSE;
4078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004079 // Let all "free" functions know that we are here. This means no
4080 // dictionaries, lists, channels or jobs are to be freed, because we will
4081 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004082 in_free_unref_items = TRUE;
4083
4084 /*
4085 * PASS 1: free the contents of the items. We don't free the items
4086 * themselves yet, so that it is possible to decrement refcount counters
4087 */
4088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004090 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004092 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004093 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004094
4095#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004096 // Go through the list of jobs and free items without the copyID. This
4097 // must happen before doing channels, because jobs refer to channels, but
4098 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004099 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004102 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4103#endif
4104
4105 /*
4106 * PASS 2: free the items themselves.
4107 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004108 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004109 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004110
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004111#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 // Go through the list of jobs and free items without the copyID. This
4113 // must happen before doing channels, because jobs refer to channels, but
4114 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004115 free_unused_jobs(copyID, COPYID_MASK);
4116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004117 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 free_unused_channels(copyID, COPYID_MASK);
4119#endif
4120
4121 in_free_unref_items = FALSE;
4122
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004123 return did_free;
4124}
4125
4126/*
4127 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004128 * "list_stack" is used to add lists to be marked. Can be NULL.
4129 *
4130 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004131 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004133set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004134{
4135 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004136 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004137 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004138 hashtab_T *cur_ht;
4139 ht_stack_T *ht_stack = NULL;
4140 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004141
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004142 cur_ht = ht;
4143 for (;;)
4144 {
4145 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 // Mark each item in the hashtab. If the item contains a hashtab
4148 // it is added to ht_stack, if it contains a list it is added to
4149 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004150 todo = (int)cur_ht->ht_used;
4151 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4152 if (!HASHITEM_EMPTY(hi))
4153 {
4154 --todo;
4155 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4156 &ht_stack, list_stack);
4157 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004158 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004159
4160 if (ht_stack == NULL)
4161 break;
4162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004163 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004164 cur_ht = ht_stack->ht;
4165 tempitem = ht_stack;
4166 ht_stack = ht_stack->prev;
4167 free(tempitem);
4168 }
4169
4170 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004171}
4172
4173/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004174 * Mark a dict and its items with "copyID".
4175 * Returns TRUE if setting references failed somehow.
4176 */
4177 int
4178set_ref_in_dict(dict_T *d, int copyID)
4179{
4180 if (d != NULL && d->dv_copyID != copyID)
4181 {
4182 d->dv_copyID = copyID;
4183 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4184 }
4185 return FALSE;
4186}
4187
4188/*
4189 * Mark a list and its items with "copyID".
4190 * Returns TRUE if setting references failed somehow.
4191 */
4192 int
4193set_ref_in_list(list_T *ll, int copyID)
4194{
4195 if (ll != NULL && ll->lv_copyID != copyID)
4196 {
4197 ll->lv_copyID = copyID;
4198 return set_ref_in_list_items(ll, copyID, NULL);
4199 }
4200 return FALSE;
4201}
4202
4203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004204 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4206 *
4207 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004208 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004209 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004210set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004211{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004212 listitem_T *li;
4213 int abort = FALSE;
4214 list_T *cur_l;
4215 list_stack_T *list_stack = NULL;
4216 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004217
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004218 cur_l = l;
4219 for (;;)
4220 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004221 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 // Mark each item in the list. If the item contains a hashtab
4223 // it is added to ht_stack, if it contains a list it is added to
4224 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004225 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4226 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4227 ht_stack, &list_stack);
4228 if (list_stack == NULL)
4229 break;
4230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 cur_l = list_stack->list;
4233 tempitem = list_stack;
4234 list_stack = list_stack->prev;
4235 free(tempitem);
4236 }
4237
4238 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004239}
4240
4241/*
4242 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004243 * "list_stack" is used to add lists to be marked. Can be NULL.
4244 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4245 *
4246 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004247 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249set_ref_in_item(
4250 typval_T *tv,
4251 int copyID,
4252 ht_stack_T **ht_stack,
4253 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004254{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004255 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004256
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004257 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004258 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004259 dict_T *dd = tv->vval.v_dict;
4260
Bram Moolenaara03f2332016-02-06 18:09:59 +01004261 if (dd != NULL && dd->dv_copyID != copyID)
4262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004263 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004264 dd->dv_copyID = copyID;
4265 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004266 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004267 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4268 }
4269 else
4270 {
4271 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4272 if (newitem == NULL)
4273 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004274 else
4275 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004276 newitem->ht = &dd->dv_hashtab;
4277 newitem->prev = *ht_stack;
4278 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004279 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004280 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004281 }
4282 }
4283 else if (tv->v_type == VAR_LIST)
4284 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004285 list_T *ll = tv->vval.v_list;
4286
Bram Moolenaara03f2332016-02-06 18:09:59 +01004287 if (ll != NULL && ll->lv_copyID != copyID)
4288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004289 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004290 ll->lv_copyID = copyID;
4291 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004292 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004293 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004294 }
4295 else
4296 {
4297 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004298 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004299 if (newitem == NULL)
4300 abort = TRUE;
4301 else
4302 {
4303 newitem->list = ll;
4304 newitem->prev = *list_stack;
4305 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004306 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004307 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004308 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004309 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004310 else if (tv->v_type == VAR_FUNC)
4311 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004312 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004313 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 else if (tv->v_type == VAR_PARTIAL)
4315 {
4316 partial_T *pt = tv->vval.v_partial;
4317 int i;
4318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004319 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 if (pt != NULL)
4321 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004323
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004324 if (pt->pt_dict != NULL)
4325 {
4326 typval_T dtv;
4327
4328 dtv.v_type = VAR_DICT;
4329 dtv.vval.v_dict = pt->pt_dict;
4330 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4331 }
4332
4333 for (i = 0; i < pt->pt_argc; ++i)
4334 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4335 ht_stack, list_stack);
4336 }
4337 }
4338#ifdef FEAT_JOB_CHANNEL
4339 else if (tv->v_type == VAR_JOB)
4340 {
4341 job_T *job = tv->vval.v_job;
4342 typval_T dtv;
4343
4344 if (job != NULL && job->jv_copyID != copyID)
4345 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004346 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004347 if (job->jv_channel != NULL)
4348 {
4349 dtv.v_type = VAR_CHANNEL;
4350 dtv.vval.v_channel = job->jv_channel;
4351 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4352 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004353 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004354 {
4355 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004356 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004357 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4358 }
4359 }
4360 }
4361 else if (tv->v_type == VAR_CHANNEL)
4362 {
4363 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004364 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004365 typval_T dtv;
4366 jsonq_T *jq;
4367 cbq_T *cq;
4368
4369 if (ch != NULL && ch->ch_copyID != copyID)
4370 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004371 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004372 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004373 {
4374 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4375 jq = jq->jq_next)
4376 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4377 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4378 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004379 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004380 {
4381 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004382 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004383 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4384 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004385 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004386 {
4387 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004388 dtv.vval.v_partial =
4389 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4391 }
4392 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004393 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004394 {
4395 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004396 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4398 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004399 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004400 {
4401 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004402 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004403 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4404 }
4405 }
4406 }
4407#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004408 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004409}
4410
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412 * Return a string with the string representation of a variable.
4413 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004415 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004416 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4417 * stings as "string()", otherwise does not put quotes around strings, as
4418 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004419 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4420 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004421 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004422 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004423 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004424echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004425 typval_T *tv,
4426 char_u **tofree,
4427 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004428 int copyID,
4429 int echo_style,
4430 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004431 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004433 static int recurse = 0;
4434 char_u *r = NULL;
4435
Bram Moolenaar33570922005-01-25 22:26:29 +00004436 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004437 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004438 if (!did_echo_string_emsg)
4439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004440 // Only give this message once for a recursive call to avoid
4441 // flooding the user with errors. And stop iterating over lists
4442 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004443 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004444 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004445 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004446 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004447 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004448 }
4449 ++recurse;
4450
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 switch (tv->v_type)
4452 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004453 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004454 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004455 {
4456 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004457 r = tv->vval.v_string;
4458 if (r == NULL)
4459 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004460 }
4461 else
4462 {
4463 *tofree = string_quote(tv->vval.v_string, FALSE);
4464 r = *tofree;
4465 }
4466 break;
4467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004469 if (echo_style)
4470 {
4471 *tofree = NULL;
4472 r = tv->vval.v_string;
4473 }
4474 else
4475 {
4476 *tofree = string_quote(tv->vval.v_string, TRUE);
4477 r = *tofree;
4478 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004479 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004480
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004481 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004482 {
4483 partial_T *pt = tv->vval.v_partial;
4484 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004485 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004486 garray_T ga;
4487 int i;
4488 char_u *tf;
4489
4490 ga_init2(&ga, 1, 100);
4491 ga_concat(&ga, (char_u *)"function(");
4492 if (fname != NULL)
4493 {
4494 ga_concat(&ga, fname);
4495 vim_free(fname);
4496 }
4497 if (pt != NULL && pt->pt_argc > 0)
4498 {
4499 ga_concat(&ga, (char_u *)", [");
4500 for (i = 0; i < pt->pt_argc; ++i)
4501 {
4502 if (i > 0)
4503 ga_concat(&ga, (char_u *)", ");
4504 ga_concat(&ga,
4505 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4506 vim_free(tf);
4507 }
4508 ga_concat(&ga, (char_u *)"]");
4509 }
4510 if (pt != NULL && pt->pt_dict != NULL)
4511 {
4512 typval_T dtv;
4513
4514 ga_concat(&ga, (char_u *)", ");
4515 dtv.v_type = VAR_DICT;
4516 dtv.vval.v_dict = pt->pt_dict;
4517 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4518 vim_free(tf);
4519 }
4520 ga_concat(&ga, (char_u *)")");
4521
4522 *tofree = ga.ga_data;
4523 r = *tofree;
4524 break;
4525 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004526
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004527 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004528 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004529 break;
4530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004532 if (tv->vval.v_list == NULL)
4533 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004534 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004535 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004536 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004537 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004538 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4539 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004540 {
4541 *tofree = NULL;
4542 r = (char_u *)"[...]";
4543 }
4544 else
4545 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004546 int old_copyID = tv->vval.v_list->lv_copyID;
4547
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004548 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004549 *tofree = list2string(tv, copyID, restore_copyID);
4550 if (restore_copyID)
4551 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004552 r = *tofree;
4553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004554 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004555
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004557 if (tv->vval.v_dict == NULL)
4558 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004559 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004560 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004561 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004562 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004563 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4564 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004565 {
4566 *tofree = NULL;
4567 r = (char_u *)"{...}";
4568 }
4569 else
4570 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004571 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004572
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004573 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004574 *tofree = dict2string(tv, copyID, restore_copyID);
4575 if (restore_copyID)
4576 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004577 r = *tofree;
4578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004579 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004582 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004583 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004585 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004586 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004587 break;
4588
Bram Moolenaar835dc632016-02-07 14:27:38 +01004589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004590 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004591 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004592 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004593 if (composite_val)
4594 {
4595 *tofree = string_quote(r, FALSE);
4596 r = *tofree;
4597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004599
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004600 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004601#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004602 *tofree = NULL;
4603 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4604 r = numbuf;
4605 break;
4606#endif
4607
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004608 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004609 case VAR_SPECIAL:
4610 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004611 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004612 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004613 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004614
Bram Moolenaar8502c702014-06-17 12:51:16 +02004615 if (--recurse == 0)
4616 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004617 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004618}
4619
4620/*
4621 * Return a string with the string representation of a variable.
4622 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4623 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004624 * Does not put quotes around strings, as ":echo" displays values.
4625 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4626 * May return NULL.
4627 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004629echo_string(
4630 typval_T *tv,
4631 char_u **tofree,
4632 char_u *numbuf,
4633 int copyID)
4634{
4635 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4636}
4637
4638/*
4639 * Return a string with the string representation of a variable.
4640 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4641 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004642 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004643 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004644 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004646tv2string(
4647 typval_T *tv,
4648 char_u **tofree,
4649 char_u *numbuf,
4650 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004651{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004652 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004653}
4654
4655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 * Return string "str" in ' quotes, doubling ' characters.
4657 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004660 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004662{
Bram Moolenaar33570922005-01-25 22:26:29 +00004663 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004664 char_u *p, *r, *s;
4665
Bram Moolenaar33570922005-01-25 22:26:29 +00004666 len = (function ? 13 : 3);
4667 if (str != NULL)
4668 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004669 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004670 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004671 if (*p == '\'')
4672 ++len;
4673 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004674 s = r = alloc(len);
4675 if (r != NULL)
4676 {
4677 if (function)
4678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004680 r += 10;
4681 }
4682 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004684 if (str != NULL)
4685 for (p = str; *p != NUL; )
4686 {
4687 if (*p == '\'')
4688 *r++ = '\'';
4689 MB_COPY_CHAR(p, r);
4690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004692 if (function)
4693 *r++ = ')';
4694 *r++ = NUL;
4695 }
4696 return s;
4697}
4698
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004699#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700/*
4701 * Convert the string "text" to a floating point number.
4702 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4703 * this always uses a decimal point.
4704 * Returns the length of the text that was consumed.
4705 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707string2float(
4708 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710{
4711 char *s = (char *)text;
4712 float_T f;
4713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004715 if (STRNICMP(text, "inf", 3) == 0)
4716 {
4717 *value = INFINITY;
4718 return 3;
4719 }
4720 if (STRNICMP(text, "-inf", 3) == 0)
4721 {
4722 *value = -INFINITY;
4723 return 4;
4724 }
4725 if (STRNICMP(text, "nan", 3) == 0)
4726 {
4727 *value = NAN;
4728 return 3;
4729 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730 f = strtod(s, &s);
4731 *value = f;
4732 return (int)((char_u *)s - text);
4733}
4734#endif
4735
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 * Get the value of an environment variable.
4738 * "arg" is pointing to the '$'. It is advanced to after the name.
4739 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004740 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004743get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 char_u *string = NULL;
4746 int len;
4747 int cc;
4748 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004749 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 ++*arg;
4752 name = *arg;
4753 len = get_env_len(arg);
4754 if (evaluate)
4755 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004756 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004757 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004758
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004759 cc = name[len];
4760 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004761 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004762 string = vim_getenv(name, &mustfree);
4763 if (string != NULL && *string != NUL)
4764 {
4765 if (!mustfree)
4766 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004768 else
4769 {
4770 if (mustfree)
4771 vim_free(string);
4772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004774 string = expand_env_save(name - 1);
4775 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004776 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004777 }
4778 name[len] = cc;
4779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 rettv->v_type = VAR_STRING;
4781 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783
4784 return OK;
4785}
4786
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004789 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792var2fpos(
4793 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 int dollar_lnum, // TRUE when $ is last line
4795 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004797 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004799 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004802 if (varp->v_type == VAR_LIST)
4803 {
4804 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004805 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004806 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004807 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004808
4809 l = varp->vval.v_list;
4810 if (l == NULL)
4811 return NULL;
4812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004813 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004814 pos.lnum = list_find_nr(l, 0L, &error);
4815 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004819 pos.col = list_find_nr(l, 1L, &error);
4820 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004821 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004822 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004825 li = list_find(l, 1L);
4826 if (li != NULL && li->li_tv.v_type == VAR_STRING
4827 && li->li_tv.vval.v_string != NULL
4828 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4829 pos.col = len + 1;
4830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004832 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004834 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004836 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004837 pos.coladd = list_find_nr(l, 2L, &error);
4838 if (error)
4839 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004840
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004841 return &pos;
4842 }
4843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 if (name == NULL)
4846 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004847 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004850 {
4851 if (VIsual_active)
4852 return &VIsual;
4853 return &curwin->w_cursor;
4854 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004857 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4859 return NULL;
4860 return pp;
4861 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004862
Bram Moolenaara5525202006-03-02 22:52:09 +00004863 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004864
Bram Moolenaar477933c2007-07-17 14:32:23 +00004865 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004866 {
4867 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004869 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004870 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004871 // In silent Ex mode topline is zero, but that's not a valid line
4872 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004873 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004874 return &pos;
4875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004877 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004878 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004880 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004881 return &pos;
4882 }
4883 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004886 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
4888 pos.lnum = curbuf->b_ml.ml_line_count;
4889 pos.col = 0;
4890 }
4891 else
4892 {
4893 pos.lnum = curwin->w_cursor.lnum;
4894 pos.col = (colnr_T)STRLEN(ml_get_curline());
4895 }
4896 return &pos;
4897 }
4898 return NULL;
4899}
4900
4901/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004902 * Convert list in "arg" into a position and optional file number.
4903 * When "fnump" is NULL there is no file number, only 3 items.
4904 * Note that the column is passed on as-is, the caller may want to decrement
4905 * it to use 1 for the first column.
4906 * Return FAIL when conversion is not possible, doesn't check the position for
4907 * validity.
4908 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004910list2fpos(
4911 typval_T *arg,
4912 pos_T *posp,
4913 int *fnump,
4914 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004915{
4916 list_T *l = arg->vval.v_list;
4917 long i = 0;
4918 long n;
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4921 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004922 if (arg->v_type != VAR_LIST
4923 || l == NULL
4924 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004925 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004926 return FAIL;
4927
4928 if (fnump != NULL)
4929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004931 if (n < 0)
4932 return FAIL;
4933 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004935 *fnump = n;
4936 }
4937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004939 if (n < 0)
4940 return FAIL;
4941 posp->lnum = n;
4942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004944 if (n < 0)
4945 return FAIL;
4946 posp->col = n;
4947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004949 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004950 posp->coladd = 0;
4951 else
4952 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004953
Bram Moolenaar493c1782014-05-28 14:34:46 +02004954 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004956
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004957 return OK;
4958}
4959
4960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * Get the length of an environment variable name.
4962 * Advance "arg" to the first character after the name.
4963 * Return 0 for error.
4964 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004966get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 char_u *p;
4969 int len;
4970
4971 for (p = *arg; vim_isIDc(*p); ++p)
4972 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 return 0;
4975
4976 len = (int)(p - *arg);
4977 *arg = p;
4978 return len;
4979}
4980
4981/*
4982 * Get the length of the name of a function or internal variable.
4983 * "arg" is advanced to the first non-white character after the name.
4984 * Return 0 if something is wrong.
4985 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004987get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989 char_u *p;
4990 int len;
4991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004994 {
4995 if (*p == ':')
4996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // "s:" is start of "s:var", but "n:" is not and can be used in
4998 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004999 len = (int)(p - *arg);
5000 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5001 || len > 1)
5002 break;
5003 }
5004 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 return 0;
5007
5008 len = (int)(p - *arg);
5009 *arg = skipwhite(p);
5010
5011 return len;
5012}
5013
5014/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005015 * Get the length of the name of a variable or function.
5016 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005018 * Return -1 if curly braces expansion failed.
5019 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * If the name contains 'magic' {}'s, expand them and return the
5021 * expanded name in an allocated string via 'alias' - caller must free.
5022 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005024get_name_len(
5025 char_u **arg,
5026 char_u **alias,
5027 int evaluate,
5028 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 char_u *p;
5032 char_u *expr_start;
5033 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5038 && (*arg)[2] == (int)KE_SNR)
5039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *arg += 3;
5042 return get_id_len(arg) + 3;
5043 }
5044 len = eval_fname_script(*arg);
5045 if (len > 0)
5046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *arg += len;
5049 }
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005054 p = find_name_end(*arg, &expr_start, &expr_end,
5055 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (expr_start != NULL)
5057 {
5058 char_u *temp_string;
5059
5060 if (!evaluate)
5061 {
5062 len += (int)(p - *arg);
5063 *arg = skipwhite(p);
5064 return len;
5065 }
5066
5067 /*
5068 * Include any <SID> etc in the expanded string:
5069 * Thus the -len here.
5070 */
5071 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5072 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005073 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 *alias = temp_string;
5075 *arg = skipwhite(p);
5076 return (int)STRLEN(temp_string);
5077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005080 // Only give an error when there is something, otherwise it will be
5081 // reported at a higher level.
5082 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005083 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 return len;
5086}
5087
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088/*
5089 * Find the end of a variable or function name, taking care of magic braces.
5090 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5091 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005092 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 * Return a pointer to just after the name. Equal to "arg" if there is no
5094 * valid name.
5095 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005096 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097find_name_end(
5098 char_u *arg,
5099 char_u **expr_start,
5100 char_u **expr_end,
5101 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 int mb_nest = 0;
5104 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005106 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005107 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 *expr_start = NULL;
5112 *expr_end = NULL;
5113 }
5114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005116 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5117 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005118 return arg;
5119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 for (p = arg; *p != NUL
5121 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005122 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005123 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005124 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005125 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005127 if (*p == '\'')
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005130 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005131 ;
5132 if (*p == NUL)
5133 break;
5134 }
5135 else if (*p == '"')
5136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005138 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005139 if (*p == '\\' && p[1] != NUL)
5140 ++p;
5141 if (*p == NUL)
5142 break;
5143 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005144 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // "s:" is start of "s:var", but "n:" is not and can be used in
5147 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005148 len = (int)(p - arg);
5149 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005150 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005151 break;
5152 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 if (*p == '[')
5157 ++br_nest;
5158 else if (*p == ']')
5159 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005161
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005162 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 if (*p == '{')
5165 {
5166 mb_nest++;
5167 if (expr_start != NULL && *expr_start == NULL)
5168 *expr_start = p;
5169 }
5170 else if (*p == '}')
5171 {
5172 mb_nest--;
5173 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5174 *expr_end = p;
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
5179 return p;
5180}
5181
5182/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005183 * Expands out the 'magic' {}'s in a variable/function name.
5184 * Note that this can call itself recursively, to deal with
5185 * constructs like foo{bar}{baz}{bam}
5186 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5187 * "in_start" ^
5188 * "expr_start" ^
5189 * "expr_end" ^
5190 * "in_end" ^
5191 *
5192 * Returns a new allocated string, which the caller must free.
5193 * Returns NULL for failure.
5194 */
5195 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196make_expanded_name(
5197 char_u *in_start,
5198 char_u *expr_start,
5199 char_u *expr_end,
5200 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005201{
5202 char_u c1;
5203 char_u *retval = NULL;
5204 char_u *temp_result;
5205 char_u *nextcmd = NULL;
5206
5207 if (expr_end == NULL || in_end == NULL)
5208 return NULL;
5209 *expr_start = NUL;
5210 *expr_end = NUL;
5211 c1 = *in_end;
5212 *in_end = NUL;
5213
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005214 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 if (temp_result != NULL && nextcmd == NULL)
5216 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005217 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5218 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 if (retval != NULL)
5220 {
5221 STRCPY(retval, in_start);
5222 STRCAT(retval, temp_result);
5223 STRCAT(retval, expr_end + 1);
5224 }
5225 }
5226 vim_free(temp_result);
5227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 *expr_start = '{';
5230 *expr_end = '}';
5231
5232 if (retval != NULL)
5233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005234 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005235 if (expr_start != NULL)
5236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 temp_result = make_expanded_name(retval, expr_start,
5239 expr_end, temp_result);
5240 vim_free(retval);
5241 retval = temp_result;
5242 }
5243 }
5244
5245 return retval;
5246}
5247
5248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005250 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005255 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5256}
5257
5258/*
5259 * Return TRUE if character "c" can be used as the first character in a
5260 * variable or function name (excluding '{' and '}').
5261 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005263eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005264{
5265 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266}
5267
5268/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005269 * Handle:
5270 * - expr[expr], expr[expr:expr] subscript
5271 * - ".name" lookup
5272 * - function call with Funcref variable: func(expr)
5273 * - method call: var->method()
5274 *
5275 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005278handle_subscript(
5279 char_u **arg,
5280 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005281 int evaluate, // do more than finding the end
5282 int verbose, // give error messages
5283 char_u *start_leader, // start of '!' and '-' prefixes
5284 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005285{
5286 int ret = OK;
5287 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288
Bram Moolenaar61343f02019-07-20 21:11:13 +02005289 // "." is ".name" lookup when we found a dict or when evaluating and
5290 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005292 && (((**arg == '['
5293 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005294 || (!evaluate
5295 && (*arg)[1] != '.'
5296 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005297 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005298 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005299 && !VIM_ISWHITE(*(*arg - 1)))
5300 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 {
5302 if (**arg == '(')
5303 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005304 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005305
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005306 // Stop the expression evaluation when immediately aborting on
5307 // error, or when an interrupt occurred or an exception was thrown
5308 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005309 if (aborting())
5310 {
5311 if (ret == OK)
5312 clear_tv(rettv);
5313 ret = FAIL;
5314 }
5315 dict_unref(selfdict);
5316 selfdict = NULL;
5317 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005318 else if (**arg == '-')
5319 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005320 // Expression "-1.0->method()" applies the leader "-" before
5321 // applying ->.
5322 if (evaluate && *end_leaderp > start_leader)
5323 ret = eval7_leader(rettv, start_leader, end_leaderp);
5324 if (ret == OK)
5325 {
5326 if ((*arg)[2] == '{')
5327 // expr->{lambda}()
5328 ret = eval_lambda(arg, rettv, evaluate, verbose);
5329 else
5330 // expr->name()
5331 ret = eval_method(arg, rettv, evaluate, verbose);
5332 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005333 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005335 {
5336 dict_unref(selfdict);
5337 if (rettv->v_type == VAR_DICT)
5338 {
5339 selfdict = rettv->vval.v_dict;
5340 if (selfdict != NULL)
5341 ++selfdict->dv_refcount;
5342 }
5343 else
5344 selfdict = NULL;
5345 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5346 {
5347 clear_tv(rettv);
5348 ret = FAIL;
5349 }
5350 }
5351 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5354 // Don't do this when "Func" is already a partial that was bound
5355 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005356 if (selfdict != NULL
5357 && (rettv->v_type == VAR_FUNC
5358 || (rettv->v_type == VAR_PARTIAL
5359 && (rettv->vval.v_partial->pt_auto
5360 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005361 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005362
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005363 dict_unref(selfdict);
5364 return ret;
5365}
5366
5367/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005368 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 * value).
5370 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005371 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005372alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005374 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375}
5376
5377/*
5378 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 * The string "s" must have been allocated, it is consumed.
5380 * Return NULL for out of memory, the variable otherwise.
5381 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005382 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005383alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 rettv = alloc_tv();
5388 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 rettv->v_type = VAR_STRING;
5391 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 else
5394 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
5397
5398/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005402free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 if (varp != NULL)
5405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 switch (varp->v_type)
5407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005409 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005411 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 vim_free(varp->vval.v_string);
5413 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005414 case VAR_PARTIAL:
5415 partial_unref(varp->vval.v_partial);
5416 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005417 case VAR_BLOB:
5418 blob_unref(varp->vval.v_blob);
5419 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 case VAR_LIST:
5421 list_unref(varp->vval.v_list);
5422 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423 case VAR_DICT:
5424 dict_unref(varp->vval.v_dict);
5425 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005427#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005428 job_unref(varp->vval.v_job);
5429 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005430#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005431 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005432#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005433 channel_unref(varp->vval.v_channel);
5434 break;
5435#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005436 case VAR_NUMBER:
5437 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005438 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005439 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005441 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005442 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005443 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 vim_free(varp);
5446 }
5447}
5448
5449/*
5450 * Free the memory for a variable value and set the value to NULL or 0.
5451 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005453clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 if (varp != NULL)
5456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005460 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005462 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005463 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005465 case VAR_PARTIAL:
5466 partial_unref(varp->vval.v_partial);
5467 varp->vval.v_partial = NULL;
5468 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005469 case VAR_BLOB:
5470 blob_unref(varp->vval.v_blob);
5471 varp->vval.v_blob = NULL;
5472 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 case VAR_LIST:
5474 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005475 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477 case VAR_DICT:
5478 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005479 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005482 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005483 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 varp->vval.v_number = 0;
5485 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005486 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005488 varp->vval.v_float = 0.0;
5489 break;
5490#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005491 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493 job_unref(varp->vval.v_job);
5494 varp->vval.v_job = NULL;
5495#endif
5496 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005497 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005499 channel_unref(varp->vval.v_channel);
5500 varp->vval.v_channel = NULL;
5501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005503 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005507 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509}
5510
5511/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 * Set the value of a variable to NULL without freeing items.
5513 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516{
5517 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005518 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519}
5520
5521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Get the number value of a variable.
5523 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005525 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005526 * caller of incompatible types: it sets *denote to TRUE if "denote"
5527 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005529 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005530tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005532 int error = FALSE;
5533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005535}
5536
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005537 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005538tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005539{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005540 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005545 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005546 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005547#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005548 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005549 break;
5550#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005552 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005553 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 break;
5555 case VAR_STRING:
5556 if (varp->vval.v_string != NULL)
5557 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005558 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005559 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005560 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005561 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005562 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005563 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005564 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005565 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005566 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005567 case VAR_SPECIAL:
5568 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005569 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005571 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005572 break;
5573#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005574 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005576 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005577 break;
5578#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005579 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005580 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005581 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005582 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005583 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005585 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005589 n = -1;
5590 else
5591 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593}
5594
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005595#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005597tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005598{
5599 switch (varp->v_type)
5600 {
5601 case VAR_NUMBER:
5602 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005603 case VAR_FLOAT:
5604 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005605 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005606 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005607 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005608 break;
5609 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005610 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005611 break;
5612 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005614 break;
5615 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005616 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005617 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005618 case VAR_BOOL:
5619 emsg(_("E362: Using a boolean value as a Float"));
5620 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005621 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005622 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005623 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005625# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005626 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005627 break;
5628# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005629 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005630# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005631 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005632 break;
5633# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005634 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005635 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005636 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005637 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005638 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005640 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005641 break;
5642 }
5643 return 0;
5644}
5645#endif
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 * Get the string value of a variable.
5649 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005650 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5651 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 * If the String variable has never been set, return an empty string.
5653 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005654 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005655 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005658tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 static char_u mybuf[NUMBUFLEN];
5661
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005662 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663}
5664
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005665 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005666tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005668 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005669
5670 return res != NULL ? res : (char_u *)"";
5671}
5672
Bram Moolenaar7d647822014-04-05 21:28:56 +02005673/*
5674 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5675 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005676 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005677tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005678{
5679 static char_u mybuf[NUMBUFLEN];
5680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005681 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005682}
5683
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005684 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005685tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005686{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005690 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005691 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 return buf;
5693 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005694 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005695 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 break;
5697 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005698 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 break;
5700 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005701 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005703 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005704#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005705 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005706 break;
5707#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708 case VAR_STRING:
5709 if (varp->vval.v_string != NULL)
5710 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005711 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005712 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005713 case VAR_SPECIAL:
5714 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5715 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005716 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005717 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005718 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005719 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005720#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005721 {
5722 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005723 char *status;
5724
5725 if (job == NULL)
5726 return (char_u *)"no process";
5727 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005728 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005729 : "run";
5730# ifdef UNIX
5731 vim_snprintf((char *)buf, NUMBUFLEN,
5732 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005733# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005734 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005735 "process %ld %s",
5736 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005737 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005738# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005740 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5741# endif
5742 return buf;
5743 }
5744#endif
5745 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005746 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005747#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005748 {
5749 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005750 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005751
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005752 if (channel == NULL)
5753 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5754 else
5755 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005756 "channel %d %s", channel->ch_id, status);
5757 return buf;
5758 }
5759#endif
5760 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005761 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005762 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005764 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005767 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
5770/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005771 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5772 * string() on Dict, List, etc.
5773 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005774 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005775tv_stringify(typval_T *varp, char_u *buf)
5776{
5777 if (varp->v_type == VAR_LIST
5778 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005779 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005780 || varp->v_type == VAR_FUNC
5781 || varp->v_type == VAR_PARTIAL
5782 || varp->v_type == VAR_FLOAT)
5783 {
5784 typval_T tmp;
5785
5786 f_string(varp, &tmp);
5787 tv_get_string_buf(&tmp, buf);
5788 clear_tv(varp);
5789 *varp = tmp;
5790 return tmp.vval.v_string;
5791 }
5792 return tv_get_string_buf(varp, buf);
5793}
5794
5795/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005796 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5797 * Also give an error message, using "name" or _("name") when use_gettext is
5798 * TRUE.
5799 */
5800 static int
5801tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5802{
5803 int lock = 0;
5804
5805 switch (tv->v_type)
5806 {
5807 case VAR_BLOB:
5808 if (tv->vval.v_blob != NULL)
5809 lock = tv->vval.v_blob->bv_lock;
5810 break;
5811 case VAR_LIST:
5812 if (tv->vval.v_list != NULL)
5813 lock = tv->vval.v_list->lv_lock;
5814 break;
5815 case VAR_DICT:
5816 if (tv->vval.v_dict != NULL)
5817 lock = tv->vval.v_dict->dv_lock;
5818 break;
5819 default:
5820 break;
5821 }
5822 return var_check_lock(tv->v_lock, name, use_gettext)
5823 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5824}
5825
5826/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005829 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005830 * It is OK for "from" and "to" to point to the same item. This is used to
5831 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005834copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005837 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 switch (from->v_type)
5839 {
5840 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005841 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005842 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 to->vval.v_number = from->vval.v_number;
5844 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005845 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005846#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005847 to->vval.v_float = from->vval.v_float;
5848 break;
5849#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005852 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005853 if (to->vval.v_job != NULL)
5854 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 break;
5856#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005857 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005858#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005859 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005860 if (to->vval.v_channel != NULL)
5861 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005862 break;
5863#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 case VAR_STRING:
5865 case VAR_FUNC:
5866 if (from->vval.v_string == NULL)
5867 to->vval.v_string = NULL;
5868 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005871 if (from->v_type == VAR_FUNC)
5872 func_ref(to->vval.v_string);
5873 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005875 case VAR_PARTIAL:
5876 if (from->vval.v_partial == NULL)
5877 to->vval.v_partial = NULL;
5878 else
5879 {
5880 to->vval.v_partial = from->vval.v_partial;
5881 ++to->vval.v_partial->pt_refcount;
5882 }
5883 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005884 case VAR_BLOB:
5885 if (from->vval.v_blob == NULL)
5886 to->vval.v_blob = NULL;
5887 else
5888 {
5889 to->vval.v_blob = from->vval.v_blob;
5890 ++to->vval.v_blob->bv_refcount;
5891 }
5892 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 case VAR_LIST:
5894 if (from->vval.v_list == NULL)
5895 to->vval.v_list = NULL;
5896 else
5897 {
5898 to->vval.v_list = from->vval.v_list;
5899 ++to->vval.v_list->lv_refcount;
5900 }
5901 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 case VAR_DICT:
5903 if (from->vval.v_dict == NULL)
5904 to->vval.v_dict = NULL;
5905 else
5906 {
5907 to->vval.v_dict = from->vval.v_dict;
5908 ++to->vval.v_dict->dv_refcount;
5909 }
5910 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005911 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005912 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005914 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 break;
5916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917}
5918
5919/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005920 * Make a copy of an item.
5921 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5923 * reference to an already copied list/dict can be used.
5924 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005926 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005927item_copy(
5928 typval_T *from,
5929 typval_T *to,
5930 int deep,
5931 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932{
5933 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005934 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005938 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005939 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 }
5941 ++recurse;
5942
5943 switch (from->v_type)
5944 {
5945 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005946 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005947 case VAR_STRING:
5948 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005949 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005950 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005951 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005952 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005953 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 copy_tv(from, to);
5955 break;
5956 case VAR_LIST:
5957 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005958 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005959 if (from->vval.v_list == NULL)
5960 to->vval.v_list = NULL;
5961 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005964 to->vval.v_list = from->vval.v_list->lv_copylist;
5965 ++to->vval.v_list->lv_refcount;
5966 }
5967 else
5968 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5969 if (to->vval.v_list == NULL)
5970 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005972 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005974 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005975 case VAR_DICT:
5976 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005977 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 if (from->vval.v_dict == NULL)
5979 to->vval.v_dict = NULL;
5980 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5984 ++to->vval.v_dict->dv_refcount;
5985 }
5986 else
5987 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5988 if (to->vval.v_dict == NULL)
5989 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005991 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005992 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005994 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005995 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996 }
5997 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999}
6000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 void
6002echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6003{
6004 char_u *tofree;
6005 char_u numbuf[NUMBUFLEN];
6006 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6007
6008 if (*atstart)
6009 {
6010 *atstart = FALSE;
6011 // Call msg_start() after eval1(), evaluating the expression
6012 // may cause a message to appear.
6013 if (with_space)
6014 {
6015 // Mark the saved text as finishing the line, so that what
6016 // follows is displayed on a new line when scrolling back
6017 // at the more prompt.
6018 msg_sb_eol();
6019 msg_start();
6020 }
6021 }
6022 else if (with_space)
6023 msg_puts_attr(" ", echo_attr);
6024
6025 if (p != NULL)
6026 for ( ; *p != NUL && !got_int; ++p)
6027 {
6028 if (*p == '\n' || *p == '\r' || *p == TAB)
6029 {
6030 if (*p != TAB && *needclr)
6031 {
6032 // remove any text still there from the command
6033 msg_clr_eos();
6034 *needclr = FALSE;
6035 }
6036 msg_putchar_attr(*p, echo_attr);
6037 }
6038 else
6039 {
6040 if (has_mbyte)
6041 {
6042 int i = (*mb_ptr2len)(p);
6043
6044 (void)msg_outtrans_len_attr(p, i, echo_attr);
6045 p += i - 1;
6046 }
6047 else
6048 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6049 }
6050 }
6051 vim_free(tofree);
6052}
6053
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 * ":echo expr1 ..." print each argument separated with a space, add a
6056 * newline at the end.
6057 * ":echon expr1 ..." print each argument plain.
6058 */
6059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 char_u *p;
6065 int needclr = TRUE;
6066 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006067 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006068 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 if (eap->skip)
6071 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006072 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // If eval1() causes an error message the text from the command may
6075 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006076 need_clr_eos = needclr;
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081 /*
6082 * Report the invalid expression unless the expression evaluation
6083 * has been cancelled due to an aborting error, an interrupt, or an
6084 * exception.
6085 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006086 if (!aborting() && did_emsg == did_emsg_before
6087 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006088 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 break;
6091 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006092 need_clr_eos = FALSE;
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 arg = skipwhite(arg);
6099 }
6100 eap->nextcmd = check_nextcmd(arg);
6101
6102 if (eap->skip)
6103 --emsg_skip;
6104 else
6105 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006106 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (needclr)
6108 msg_clr_eos();
6109 if (eap->cmdidx == CMD_echo)
6110 msg_end();
6111 }
6112}
6113
6114/*
6115 * ":echohl {name}".
6116 */
6117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006120 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121}
6122
6123/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006124 * Returns the :echo attribute
6125 */
6126 int
6127get_echo_attr(void)
6128{
6129 return echo_attr;
6130}
6131
6132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 * ":execute expr1 ..." execute the result of an expression.
6134 * ":echomsg expr1 ..." Print a message
6135 * ":echoerr expr1 ..." Print an error
6136 * Each gets spaces around each argument and a newline at the end for
6137 * echo commands
6138 */
6139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 int ret = OK;
6145 char_u *p;
6146 garray_T ga;
6147 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006148 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 ga_init2(&ga, 1, 80);
6151
6152 if (eap->skip)
6153 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006154 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006156 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6157 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 if (!eap->skip)
6161 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006162 char_u buf[NUMBUFLEN];
6163
6164 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006165 {
6166 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6167 {
6168 emsg(_(e_inval_string));
6169 p = NULL;
6170 }
6171 else
6172 p = tv_get_string_buf(&rettv, buf);
6173 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006174 else
6175 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006176 if (p == NULL)
6177 {
6178 clear_tv(&rettv);
6179 ret = FAIL;
6180 break;
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 len = (int)STRLEN(p);
6183 if (ga_grow(&ga, len + 2) == FAIL)
6184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006185 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 ret = FAIL;
6187 break;
6188 }
6189 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 ga.ga_len += len;
6193 }
6194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 arg = skipwhite(arg);
6197 }
6198
6199 if (ret != FAIL && ga.ga_data != NULL)
6200 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006201 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // Mark the already saved text as finishing the line, so that what
6204 // follows is displayed on a new line when scrolling back at the
6205 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006206 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006207 }
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006210 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006211 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006212 out_flush();
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else if (eap->cmdidx == CMD_echoerr)
6215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006216 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006218 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (!force_abort)
6220 did_emsg = save_did_emsg;
6221 }
6222 else if (eap->cmdidx == CMD_execute)
6223 do_cmdline((char_u *)ga.ga_data,
6224 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6225 }
6226
6227 ga_clear(&ga);
6228
6229 if (eap->skip)
6230 --emsg_skip;
6231
6232 eap->nextcmd = check_nextcmd(arg);
6233}
6234
6235/*
6236 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6237 * "arg" points to the "&" or '+' when called, to "option" when returning.
6238 * Returns NULL when no option name found. Otherwise pointer to the char
6239 * after the option name.
6240 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006241 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006242find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 char_u *p = *arg;
6245
6246 ++p;
6247 if (*p == 'g' && p[1] == ':')
6248 {
6249 *opt_flags = OPT_GLOBAL;
6250 p += 2;
6251 }
6252 else if (*p == 'l' && p[1] == ':')
6253 {
6254 *opt_flags = OPT_LOCAL;
6255 p += 2;
6256 }
6257 else
6258 *opt_flags = 0;
6259
6260 if (!ASCII_ISALPHA(*p))
6261 return NULL;
6262 *arg = p;
6263
6264 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 else
6267 while (ASCII_ISALPHA(*p))
6268 ++p;
6269 return p;
6270}
6271
6272/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006273 * Display script name where an item was last set.
6274 * Should only be invoked when 'verbose' is non-zero.
6275 */
6276 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006277last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006278{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006279 char_u *p;
6280
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006281 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006282 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006283 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006284 if (p != NULL)
6285 {
6286 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006287 msg_puts(_("\n\tLast set from "));
6288 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006289 if (script_ctx.sc_lnum > 0)
6290 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006291 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006292 msg_outnum((long)script_ctx.sc_lnum);
6293 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006294 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006295 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006296 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006297 }
6298}
6299
Bram Moolenaar61be3762019-03-19 23:04:17 +01006300/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006301 * Compare "typ1" and "typ2". Put the result in "typ1".
6302 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006303 int
6304typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 typval_T *typ1, // first operand
6306 typval_T *typ2, // second operand
6307 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006308 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006309{
6310 int i;
6311 varnumber_T n1, n2;
6312 char_u *s1, *s2;
6313 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006314 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006315
Bram Moolenaar31988702018-02-13 12:57:42 +01006316 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006318 // For "is" a different type always means FALSE, for "notis"
6319 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006320 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006321 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006322 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6323 {
6324 if (type_is)
6325 {
6326 n1 = (typ1->v_type == typ2->v_type
6327 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006328 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006329 n1 = !n1;
6330 }
6331 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006332 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006333 {
6334 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006335 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006336 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006337 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006338 clear_tv(typ1);
6339 return FAIL;
6340 }
6341 else
6342 {
6343 // Compare two Blobs for being equal or unequal.
6344 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006345 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006346 n1 = !n1;
6347 }
6348 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006349 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6350 {
6351 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006352 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006353 n1 = (typ1->v_type == typ2->v_type
6354 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006355 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006356 n1 = !n1;
6357 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006358 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006359 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006360 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006361 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006362 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006363 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006364 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006365 clear_tv(typ1);
6366 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006367 }
6368 else
6369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006370 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006371 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6372 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006373 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006374 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006375 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006376 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006377
6378 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6379 {
6380 if (type_is)
6381 {
6382 n1 = (typ1->v_type == typ2->v_type
6383 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006384 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006385 n1 = !n1;
6386 }
6387 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006388 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006389 {
6390 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006391 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006392 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006393 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006394 clear_tv(typ1);
6395 return FAIL;
6396 }
6397 else
6398 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006399 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006400 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6401 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006402 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006403 n1 = !n1;
6404 }
6405 }
6406
6407 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6408 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6409 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006410 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6411 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006413 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006414 clear_tv(typ1);
6415 return FAIL;
6416 }
6417 if ((typ1->v_type == VAR_PARTIAL
6418 && typ1->vval.v_partial == NULL)
6419 || (typ2->v_type == VAR_PARTIAL
6420 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006421 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006422 n1 = FALSE;
6423 else if (type_is)
6424 {
6425 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006426 // strings are considered the same if their value is
6427 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006428 n1 = tv_equal(typ1, typ2, ic, FALSE);
6429 else if (typ1->v_type == VAR_PARTIAL
6430 && typ2->v_type == VAR_PARTIAL)
6431 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6432 else
6433 n1 = FALSE;
6434 }
6435 else
6436 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006437 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006438 n1 = !n1;
6439 }
6440
6441#ifdef FEAT_FLOAT
6442 /*
6443 * If one of the two variables is a float, compare as a float.
6444 * When using "=~" or "!~", always compare as string.
6445 */
6446 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006447 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006448 {
6449 float_T f1, f2;
6450
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006451 f1 = tv_get_float(typ1);
6452 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006453 n1 = FALSE;
6454 switch (type)
6455 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006456 case EXPR_IS:
6457 case EXPR_EQUAL: n1 = (f1 == f2); break;
6458 case EXPR_ISNOT:
6459 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6460 case EXPR_GREATER: n1 = (f1 > f2); break;
6461 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6462 case EXPR_SMALLER: n1 = (f1 < f2); break;
6463 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6464 case EXPR_UNKNOWN:
6465 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006467 }
6468 }
6469#endif
6470
6471 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006472 * If one of the two variables is a number, compare as a number.
6473 * When using "=~" or "!~", always compare as string.
6474 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006475 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006476 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006477 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006478 n1 = tv_get_number(typ1);
6479 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006480 switch (type)
6481 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006482 case EXPR_IS:
6483 case EXPR_EQUAL: n1 = (n1 == n2); break;
6484 case EXPR_ISNOT:
6485 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6486 case EXPR_GREATER: n1 = (n1 > n2); break;
6487 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6488 case EXPR_SMALLER: n1 = (n1 < n2); break;
6489 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6490 case EXPR_UNKNOWN:
6491 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006493 }
6494 }
6495 else
6496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006497 s1 = tv_get_string_buf(typ1, buf1);
6498 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006499 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006500 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6501 else
6502 i = 0;
6503 n1 = FALSE;
6504 switch (type)
6505 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006506 case EXPR_IS:
6507 case EXPR_EQUAL: n1 = (i == 0); break;
6508 case EXPR_ISNOT:
6509 case EXPR_NEQUAL: n1 = (i != 0); break;
6510 case EXPR_GREATER: n1 = (i > 0); break;
6511 case EXPR_GEQUAL: n1 = (i >= 0); break;
6512 case EXPR_SMALLER: n1 = (i < 0); break;
6513 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006514
Bram Moolenaar87396072019-12-31 22:36:18 +01006515 case EXPR_MATCH:
6516 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006517 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006518 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006519 n1 = !n1;
6520 break;
6521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006523 }
6524 }
6525 clear_tv(typ1);
6526 typ1->v_type = VAR_NUMBER;
6527 typ1->vval.v_number = n1;
6528
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006529 return OK;
6530}
6531
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006532 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006533typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006534{
6535 char_u *tofree;
6536 char_u numbuf[NUMBUFLEN];
6537 char_u *ret = NULL;
6538
6539 if (arg == NULL)
6540 return vim_strsave((char_u *)"(does not exist)");
6541 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006542 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006543 if (ret != NULL && tofree == NULL)
6544 ret = vim_strsave(ret);
6545 return ret;
6546}
6547
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006548#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
6550/*
6551 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006552 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 * "flags" can be "g" to do a global substitute.
6554 * Returns an allocated string, NULL for error.
6555 */
6556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557do_string_sub(
6558 char_u *str,
6559 char_u *pat,
6560 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006561 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006562 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 int sublen;
6565 regmatch_T regmatch;
6566 int i;
6567 int do_all;
6568 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006569 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 garray_T ga;
6571 char_u *ret;
6572 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006573 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006575 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006577 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
6579 ga_init2(&ga, 1, 200);
6580
6581 do_all = (flags[0] == 'g');
6582
6583 regmatch.rm_ic = p_ic;
6584 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6585 if (regmatch.regprog != NULL)
6586 {
6587 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006588 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006591 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006592 if (regmatch.startp[0] == regmatch.endp[0])
6593 {
6594 if (zero_width == regmatch.startp[0])
6595 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006596 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006597 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006598 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6599 (size_t)i);
6600 ga.ga_len += i;
6601 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006602 continue;
6603 }
6604 zero_width = regmatch.startp[0];
6605 }
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 /*
6608 * Get some space for a temporary buffer to do the substitution
6609 * into. It will contain:
6610 * - The text up to where the match is.
6611 * - The substituted text.
6612 * - The text after the match.
6613 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006614 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006615 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6617 {
6618 ga_clear(&ga);
6619 break;
6620 }
6621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 i = (int)(regmatch.startp[0] - tail);
6624 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006626 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 + ga.ga_len + i, TRUE, TRUE, FALSE);
6628 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006629 tail = regmatch.endp[0];
6630 if (*tail == NUL)
6631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (!do_all)
6633 break;
6634 }
6635
6636 if (ga.ga_data != NULL)
6637 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6638
Bram Moolenaar473de612013-06-08 18:19:48 +02006639 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641
6642 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6643 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006644 if (p_cpo == empty_option)
6645 p_cpo = save_cpo;
6646 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006647 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006648 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 return ret;
6651}