blob: 415ed5a9d34eeac65acfed45b66e552ac149107b [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 Moolenaarc6538bc2019-08-03 18:17:11 +0200237 vim_memset(&funcexe, 0, sizeof(funcexe));
238 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;
256 vim_memset(&funcexe, 0, sizeof(funcexe));
257 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 Moolenaarc6538bc2019-08-03 18:17:11 +0200478 vim_memset(&funcexe, 0, sizeof(funcexe));
479 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 Moolenaar33570922005-01-25 22:26:29 +0000652 vim_memset(lp, 0, sizeof(lval_T));
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 Moolenaar4228bec2011-03-27 16:03:15 +0200874 return NULL;
875 }
876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100879 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200880 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100881 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100883 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100884 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200885 return NULL;
886 }
887
Bram Moolenaar31b81602019-02-10 22:14:27 +0100888 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100892 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100893 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
896 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100900 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 p = NULL;
903 break;
904 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100906 else if ((flags & GLV_READ_ONLY) == 0
907 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100908 {
909 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200910 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100911 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200912
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100913 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000915 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100916 else if (lp->ll_tv->v_type == VAR_BLOB)
917 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100918 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
919
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100920 /*
921 * Get the number and item for the only or first index of the List.
922 */
923 if (empty1)
924 lp->ll_n1 = 0;
925 else
926 // is number or string
927 lp->ll_n1 = (long)tv_get_number(&var1);
928 clear_tv(&var1);
929
930 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100931 || lp->ll_n1 > bloblen
932 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100933 {
934 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100935 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100936 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100937 return NULL;
938 }
939 if (lp->ll_range && !lp->ll_empty2)
940 {
941 lp->ll_n2 = (long)tv_get_number(&var2);
942 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100943 if (lp->ll_n2 < 0
944 || lp->ll_n2 >= bloblen
945 || lp->ll_n2 < lp->ll_n1)
946 {
947 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100948 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100949 return NULL;
950 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100951 }
952 lp->ll_blob = lp->ll_tv->vval.v_blob;
953 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100954 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000956 else
957 {
958 /*
959 * Get the number and item for the only or first index of the List.
960 */
961 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100965 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100966 clear_tv(&var1);
967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000968 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 lp->ll_list = lp->ll_tv->vval.v_list;
970 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
971 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000973 if (lp->ll_n1 < 0)
974 {
975 lp->ll_n1 = 0;
976 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
977 }
978 }
979 if (lp->ll_li == NULL)
980 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100981 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200982 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 }
986
987 /*
988 * May need to find the item or absolute index for the second
989 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 * When no index given: "lp->ll_empty2" is TRUE.
991 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001002 {
1003 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001004 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 }
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 if (lp->ll_n1 < 0)
1012 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1013 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001014 {
1015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001018 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001019 }
1020
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001023 }
1024
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001025 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 return p;
1028}
1029
1030/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001031 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001034clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035{
1036 vim_free(lp->ll_exp_name);
1037 vim_free(lp->ll_newkey);
1038}
1039
1040/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001041 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001043 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1044 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001047set_var_lval(
1048 lval_T *lp,
1049 char_u *endp,
1050 typval_T *rettv,
1051 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054{
1055 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001056 listitem_T *ri;
1057 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058
1059 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001060 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001061 cc = *endp;
1062 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001063 if (lp->ll_blob != NULL)
1064 {
1065 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001066
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 if (op != NULL && *op != '=')
1068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001069 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001070 return;
1071 }
1072
1073 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1074 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001075 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001076
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001077 if (lp->ll_empty2)
1078 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1079
1080 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001083 return;
1084 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001085 if (lp->ll_empty2)
1086 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001087
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001088 ir = 0;
1089 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1090 blob_set(lp->ll_blob, il,
1091 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001092 }
1093 else
1094 {
1095 val = (int)tv_get_number_chk(rettv, &error);
1096 if (!error)
1097 {
1098 garray_T *gap = &lp->ll_blob->bv_ga;
1099
1100 // Allow for appending a byte. Setting a byte beyond
1101 // the end is an error otherwise.
1102 if (lp->ll_n1 < gap->ga_len
1103 || (lp->ll_n1 == gap->ga_len
1104 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1105 {
1106 blob_set(lp->ll_blob, lp->ll_n1, val);
1107 if (lp->ll_n1 == gap->ga_len)
1108 ++gap->ga_len;
1109 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001110 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001111 }
1112 }
1113 }
1114 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001115 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001116 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001119 {
1120 emsg(_(e_cannot_mod));
1121 *endp = cc;
1122 return;
1123 }
1124
Bram Moolenaarff697e62019-02-12 22:28:33 +01001125 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001126 di = NULL;
1127 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1128 &tv, &di, TRUE, FALSE) == OK)
1129 {
1130 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001131 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1132 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001133 && tv_op(&tv, rettv, op) == OK)
1134 set_var(lp->ll_name, &tv, FALSE);
1135 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001138 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001140 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001142 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001143 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001144 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001145 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 else if (lp->ll_range)
1147 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001148 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001149 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001152 {
1153 emsg(_("E996: Cannot lock a range"));
1154 return;
1155 }
1156
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001157 /*
1158 * Check whether any of the list items is locked
1159 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001160 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001161 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001162 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001163 return;
1164 ri = ri->li_next;
1165 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1166 break;
1167 ll_li = ll_li->li_next;
1168 ++ll_n1;
1169 }
1170
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001171 /*
1172 * Assign the List values to the list items.
1173 */
1174 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001175 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001176 if (op != NULL && *op != '=')
1177 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1178 else
1179 {
1180 clear_tv(&lp->ll_li->li_tv);
1181 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1182 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 ri = ri->li_next;
1184 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1185 break;
1186 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001188 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001189 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 ri = NULL;
1192 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001193 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001194 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 lp->ll_li = lp->ll_li->li_next;
1196 ++lp->ll_n1;
1197 }
1198 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001200 else if (lp->ll_empty2
1201 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 }
1205 else
1206 {
1207 /*
1208 * Assign to a List or Dictionary item.
1209 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001211 {
1212 emsg(_("E996: Cannot lock a list or dict"));
1213 return;
1214 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (lp->ll_newkey != NULL)
1216 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001217 if (op != NULL && *op != '=')
1218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001220 return;
1221 }
1222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001223 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001224 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 if (di == NULL)
1226 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001227 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1228 {
1229 vim_free(di);
1230 return;
1231 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001233 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001234 else if (op != NULL && *op != '=')
1235 {
1236 tv_op(lp->ll_tv, rettv, op);
1237 return;
1238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 /*
1243 * Assign the value to the variable or list item.
1244 */
1245 if (copy)
1246 copy_tv(rettv, lp->ll_tv);
1247 else
1248 {
1249 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001250 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 }
1253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254}
1255
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001256/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001257 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1258 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001259 * Returns OK or FAIL.
1260 */
1261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001263{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001264 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001265 char_u numbuf[NUMBUFLEN];
1266 char_u *s;
1267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001268 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001269 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001270 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 {
1272 switch (tv1->v_type)
1273 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001274 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 case VAR_DICT:
1277 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001278 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001279 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001280 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001281 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001282 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 break;
1284
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 case VAR_BLOB:
1286 if (*op != '+' || tv2->v_type != VAR_BLOB)
1287 break;
1288 // BLOB += BLOB
1289 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1290 {
1291 blob_T *b1 = tv1->vval.v_blob;
1292 blob_T *b2 = tv2->vval.v_blob;
1293 int i, len = blob_len(b2);
1294 for (i = 0; i < len; i++)
1295 ga_append(&b1->bv_ga, blob_get(b2, i));
1296 }
1297 return OK;
1298
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001299 case VAR_LIST:
1300 if (*op != '+' || tv2->v_type != VAR_LIST)
1301 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001302 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1304 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1305 return OK;
1306
1307 case VAR_NUMBER:
1308 case VAR_STRING:
1309 if (tv2->v_type == VAR_LIST)
1310 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001311 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001313 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001314 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001315#ifdef FEAT_FLOAT
1316 if (tv2->v_type == VAR_FLOAT)
1317 {
1318 float_T f = n;
1319
Bram Moolenaarff697e62019-02-12 22:28:33 +01001320 if (*op == '%')
1321 break;
1322 switch (*op)
1323 {
1324 case '+': f += tv2->vval.v_float; break;
1325 case '-': f -= tv2->vval.v_float; break;
1326 case '*': f *= tv2->vval.v_float; break;
1327 case '/': f /= tv2->vval.v_float; break;
1328 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001329 clear_tv(tv1);
1330 tv1->v_type = VAR_FLOAT;
1331 tv1->vval.v_float = f;
1332 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001334#endif
1335 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001336 switch (*op)
1337 {
1338 case '+': n += tv_get_number(tv2); break;
1339 case '-': n -= tv_get_number(tv2); break;
1340 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001341 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1342 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001343 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001344 clear_tv(tv1);
1345 tv1->v_type = VAR_NUMBER;
1346 tv1->vval.v_number = n;
1347 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001348 }
1349 else
1350 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001351 if (tv2->v_type == VAR_FLOAT)
1352 break;
1353
Bram Moolenaarff697e62019-02-12 22:28:33 +01001354 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001355 s = tv_get_string(tv1);
1356 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001357 clear_tv(tv1);
1358 tv1->v_type = VAR_STRING;
1359 tv1->vval.v_string = s;
1360 }
1361 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001362
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001363 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001364#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001365 {
1366 float_T f;
1367
Bram Moolenaarff697e62019-02-12 22:28:33 +01001368 if (*op == '%' || *op == '.'
1369 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001370 && tv2->v_type != VAR_NUMBER
1371 && tv2->v_type != VAR_STRING))
1372 break;
1373 if (tv2->v_type == VAR_FLOAT)
1374 f = tv2->vval.v_float;
1375 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001376 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001377 switch (*op)
1378 {
1379 case '+': tv1->vval.v_float += f; break;
1380 case '-': tv1->vval.v_float -= f; break;
1381 case '*': tv1->vval.v_float *= f; break;
1382 case '/': tv1->vval.v_float /= f; break;
1383 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001384 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001385#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001386 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001387 }
1388 }
1389
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001390 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 return FAIL;
1392}
1393
1394/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001395 * Evaluate the expression used in a ":for var in expr" command.
1396 * "arg" points to "var".
1397 * Set "*errp" to TRUE for an error, FALSE otherwise;
1398 * Return a pointer that holds the info. Null when there is an error.
1399 */
1400 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_for_line(
1402 char_u *arg,
1403 int *errp,
1404 char_u **nextcmdp,
1405 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T tv;
1410 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001412 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001414 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 if (fi == NULL)
1416 return NULL;
1417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 if (expr == NULL)
1420 return fi;
1421
1422 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001423 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 return fi;
1427 }
1428
1429 if (skip)
1430 ++emsg_skip;
1431 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1432 {
1433 *errp = FALSE;
1434 if (!skip)
1435 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001436 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001437 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001438 l = tv.vval.v_list;
1439 if (l == NULL)
1440 {
1441 // a null list is like an empty list: do nothing
1442 clear_tv(&tv);
1443 }
1444 else
1445 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 // Need a real list here.
1447 range_list_materialize(l);
1448
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001449 // No need to increment the refcount, it's already set for
1450 // the list being used in "tv".
1451 fi->fi_list = l;
1452 list_add_watch(l, &fi->fi_lw);
1453 fi->fi_lw.lw_item = l->lv_first;
1454 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001455 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001457 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001458 fi->fi_bi = 0;
1459 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001460 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001461 typval_T btv;
1462
1463 // Make a copy, so that the iteration still works when the
1464 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001466 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001468 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001469 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 else
1471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001472 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001474 }
1475 }
1476 }
1477 if (skip)
1478 --emsg_skip;
1479
1480 return fi;
1481}
1482
1483/*
1484 * Use the first item in a ":for" list. Advance to the next.
1485 * Assign the values to the variable (list). "arg" points to the first one.
1486 * Return TRUE when a valid item was found, FALSE when at end of list or
1487 * something wrong.
1488 */
1489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001490next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001492 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001494 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1495 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001496 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001498 if (fi->fi_blob != NULL)
1499 {
1500 typval_T tv;
1501
1502 if (fi->fi_bi >= blob_len(fi->fi_blob))
1503 return FALSE;
1504 tv.v_type = VAR_NUMBER;
1505 tv.v_lock = VAR_FIXED;
1506 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1507 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001508 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001509 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001510 }
1511
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 item = fi->fi_lw.lw_item;
1513 if (item == NULL)
1514 result = FALSE;
1515 else
1516 {
1517 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001518 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001519 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520 }
1521 return result;
1522}
1523
1524/*
1525 * Free the structure used to store info used by ":for".
1526 */
1527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001528free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529{
Bram Moolenaar33570922005-01-25 22:26:29 +00001530 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001532 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001533 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001535 list_unref(fi->fi_list);
1536 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001537 if (fi != NULL && fi->fi_blob != NULL)
1538 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 vim_free(fi);
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543set_context_for_expression(
1544 expand_T *xp,
1545 char_u *arg,
1546 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 int got_eq = FALSE;
1549 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001550 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001552 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001553 {
1554 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001557 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001558 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559 {
1560 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001561 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001562 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 break;
1564 }
1565 return;
1566 }
1567 }
1568 else
1569 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1570 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 while ((xp->xp_pattern = vim_strpbrk(arg,
1572 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1573 {
1574 c = *xp->xp_pattern;
1575 if (c == '&')
1576 {
1577 c = xp->xp_pattern[1];
1578 if (c == '&')
1579 {
1580 ++xp->xp_pattern;
1581 xp->xp_context = cmdidx != CMD_let || got_eq
1582 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1583 }
1584 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001587 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1588 xp->xp_pattern += 2;
1589
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 else if (c == '$')
1593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001594 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 xp->xp_context = EXPAND_ENV_VARS;
1596 }
1597 else if (c == '=')
1598 {
1599 got_eq = TRUE;
1600 xp->xp_context = EXPAND_EXPRESSION;
1601 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001602 else if (c == '#'
1603 && xp->xp_context == EXPAND_EXPRESSION)
1604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001605 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001606 break;
1607 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001608 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 && xp->xp_context == EXPAND_FUNCTIONS
1610 && vim_strchr(xp->xp_pattern, '(') == NULL)
1611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001612 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 break;
1614 }
1615 else if (cmdidx != CMD_let || got_eq)
1616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1620 if (c == '\\' && xp->xp_pattern[1] != NUL)
1621 ++xp->xp_pattern;
1622 xp->xp_context = EXPAND_NOTHING;
1623 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001624 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1628 /* skip */ ;
1629 xp->xp_context = EXPAND_NOTHING;
1630 }
1631 else if (c == '|')
1632 {
1633 if (xp->xp_pattern[1] == '|')
1634 {
1635 ++xp->xp_pattern;
1636 xp->xp_context = EXPAND_EXPRESSION;
1637 }
1638 else
1639 xp->xp_context = EXPAND_COMMANDS;
1640 }
1641 else
1642 xp->xp_context = EXPAND_EXPRESSION;
1643 }
1644 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001645 // Doesn't look like something valid, expand as an expression
1646 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 arg = xp->xp_pattern;
1649 if (*arg != NUL)
1650 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1651 /* skip */ ;
1652 }
1653 xp->xp_pattern = arg;
1654}
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001657 * Return TRUE if "pat" matches "text".
1658 * Does not use 'cpo' and always uses 'magic'.
1659 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001660 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001661pattern_match(char_u *pat, char_u *text, int ic)
1662{
1663 int matches = FALSE;
1664 char_u *save_cpo;
1665 regmatch_T regmatch;
1666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001667 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001668 save_cpo = p_cpo;
1669 p_cpo = (char_u *)"";
1670 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1671 if (regmatch.regprog != NULL)
1672 {
1673 regmatch.rm_ic = ic;
1674 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1675 vim_regfree(regmatch.regprog);
1676 }
1677 p_cpo = save_cpo;
1678 return matches;
1679}
1680
1681/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001682 * Handle a name followed by "(". Both for just "name(arg)" and for
1683 * "expr->name(arg)".
1684 * Returns OK or FAIL.
1685 */
1686 static int
1687eval_func(
1688 char_u **arg, // points to "(", will be advanced
1689 char_u *name,
1690 int name_len,
1691 typval_T *rettv,
1692 int evaluate,
1693 typval_T *basetv) // "expr" for "expr->name(arg)"
1694{
1695 char_u *s = name;
1696 int len = name_len;
1697 partial_T *partial;
1698 int ret = OK;
1699
1700 if (!evaluate)
1701 check_vars(s, len);
1702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001703 // If "s" is the name of a variable of type VAR_FUNC
1704 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001705 s = deref_func_name(s, &len, &partial, !evaluate);
1706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001707 // Need to make a copy, in case evaluating the arguments makes
1708 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001709 s = vim_strsave(s);
1710 if (s == NULL)
1711 ret = FAIL;
1712 else
1713 {
1714 funcexe_T funcexe;
1715
1716 // Invoke the function.
1717 vim_memset(&funcexe, 0, sizeof(funcexe));
1718 funcexe.firstline = curwin->w_cursor.lnum;
1719 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001720 funcexe.evaluate = evaluate;
1721 funcexe.partial = partial;
1722 funcexe.basetv = basetv;
1723 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1724 }
1725 vim_free(s);
1726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001727 // If evaluate is FALSE rettv->v_type was not set in
1728 // get_func_tv, but it's needed in handle_subscript() to parse
1729 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001730 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1731 {
1732 rettv->vval.v_string = NULL;
1733 rettv->v_type = VAR_FUNC;
1734 }
1735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001736 // Stop the expression evaluation when immediately
1737 // aborting on error, or when an interrupt occurred or
1738 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001739 if (evaluate && aborting())
1740 {
1741 if (ret == OK)
1742 clear_tv(rettv);
1743 ret = FAIL;
1744 }
1745 return ret;
1746}
1747
1748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1752 */
1753
1754/*
1755 * Handle zero level expression.
1756 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001758 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 * Return OK or FAIL.
1760 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762eval0(
1763 char_u *arg,
1764 typval_T *rettv,
1765 char_u **nextcmd,
1766 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int ret;
1769 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001770 int did_emsg_before = did_emsg;
1771 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (ret == FAIL || !ends_excmd(*p))
1776 {
1777 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /*
1780 * Report the invalid expression unless the expression evaluation has
1781 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001782 * exception, or we already gave a more specific error.
1783 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001785 if (!aborting() && did_emsg == did_emsg_before
1786 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 ret = FAIL;
1789 }
1790 if (nextcmd != NULL)
1791 *nextcmd = check_nextcmd(p);
1792
1793 return ret;
1794}
1795
1796/*
1797 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001798 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 *
1800 * "arg" must point to the first non-white of the expression.
1801 * "arg" is advanced to the next non-white after the recognized expression.
1802 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001803 * Note: "rettv.v_lock" is not set.
1804 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 * Return OK or FAIL.
1806 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001811 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 /*
1814 * Get the first variable.
1815 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 return FAIL;
1818
1819 if ((*arg)[0] == '?')
1820 {
1821 result = FALSE;
1822 if (evaluate)
1823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001824 int error = FALSE;
1825
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001826 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001829 if (error)
1830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832
1833 /*
1834 * Get the second variable.
1835 */
1836 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001837 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 return FAIL;
1839
1840 /*
1841 * Check for the ":".
1842 */
1843 if ((*arg)[0] != ':')
1844 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 return FAIL;
1849 }
1850
1851 /*
1852 * Get the third variable.
1853 */
1854 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001855 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 return FAIL;
1860 }
1861 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864
1865 return OK;
1866}
1867
1868/*
1869 * Handle first level expression:
1870 * expr2 || expr2 || expr2 logical OR
1871 *
1872 * "arg" must point to the first non-white of the expression.
1873 * "arg" is advanced to the next non-white after the recognized expression.
1874 *
1875 * Return OK or FAIL.
1876 */
1877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001878eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 long result;
1882 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001883 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 /*
1886 * Get the first variable.
1887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return FAIL;
1890
1891 /*
1892 * Repeat until there is no following "||".
1893 */
1894 first = TRUE;
1895 result = FALSE;
1896 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1897 {
1898 if (evaluate && first)
1899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001900 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001903 if (error)
1904 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 first = FALSE;
1906 }
1907
1908 /*
1909 * Get the second variable.
1910 */
1911 *arg = skipwhite(*arg + 2);
1912 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1913 return FAIL;
1914
1915 /*
1916 * Compute the result.
1917 */
1918 if (evaluate && !result)
1919 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001920 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 if (error)
1924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 if (evaluate)
1927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 rettv->v_type = VAR_NUMBER;
1929 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 }
1932
1933 return OK;
1934}
1935
1936/*
1937 * Handle second level expression:
1938 * expr3 && expr3 && expr3 logical AND
1939 *
1940 * "arg" must point to the first non-white of the expression.
1941 * "arg" is advanced to the next non-white after the recognized expression.
1942 *
1943 * Return OK or FAIL.
1944 */
1945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001946eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 long result;
1950 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001951 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 /*
1954 * Get the first variable.
1955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return FAIL;
1958
1959 /*
1960 * Repeat until there is no following "&&".
1961 */
1962 first = TRUE;
1963 result = TRUE;
1964 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1965 {
1966 if (evaluate && first)
1967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001968 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001971 if (error)
1972 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 first = FALSE;
1974 }
1975
1976 /*
1977 * Get the second variable.
1978 */
1979 *arg = skipwhite(*arg + 2);
1980 if (eval4(arg, &var2, evaluate && result) == FAIL)
1981 return FAIL;
1982
1983 /*
1984 * Compute the result.
1985 */
1986 if (evaluate && result)
1987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001988 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001991 if (error)
1992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 if (evaluate)
1995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 rettv->v_type = VAR_NUMBER;
1997 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Handle third level expression:
2006 * var1 == var2
2007 * var1 =~ var2
2008 * var1 != var2
2009 * var1 !~ var2
2010 * var1 > var2
2011 * var1 >= var2
2012 * var1 < var2
2013 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002014 * var1 is var2
2015 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 *
2017 * "arg" must point to the first non-white of the expression.
2018 * "arg" is advanced to the next non-white after the recognized expression.
2019 *
2020 * Return OK or FAIL.
2021 */
2022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002023eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
Bram Moolenaar33570922005-01-25 22:26:29 +00002025 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 char_u *p;
2027 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002028 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 /*
2033 * Get the first variable.
2034 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002035 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return FAIL;
2037
2038 p = *arg;
2039 switch (p[0])
2040 {
2041 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002042 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002044 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 break;
2046 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002047 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002049 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
2051 case '>': if (p[1] != '=')
2052 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002053 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 len = 1;
2055 }
2056 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002057 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 break;
2059 case '<': if (p[1] != '=')
2060 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002061 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 len = 1;
2063 }
2064 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002065 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002067 case 'i': if (p[1] == 's')
2068 {
2069 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2070 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002071 i = p[len];
2072 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002073 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002074 }
2075 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077
2078 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002079 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002081 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002083 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (p[len] == '?')
2085 {
2086 ic = TRUE;
2087 ++len;
2088 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002089 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else if (p[len] == '#')
2091 {
2092 ic = FALSE;
2093 ++len;
2094 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 else
2097 ic = p_ic;
2098
2099 /*
2100 * Get the second variable.
2101 */
2102 *arg = skipwhite(p + len);
2103 if (eval5(arg, &var2, evaluate) == FAIL)
2104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 return FAIL;
2107 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002108 if (evaluate)
2109 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002110 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002111
2112 clear_tv(&var2);
2113 return ret;
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116
2117 return OK;
2118}
2119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 void
2121eval_addblob(typval_T *tv1, typval_T *tv2)
2122{
2123 blob_T *b1 = tv1->vval.v_blob;
2124 blob_T *b2 = tv2->vval.v_blob;
2125 blob_T *b = blob_alloc();
2126 int i;
2127
2128 if (b != NULL)
2129 {
2130 for (i = 0; i < blob_len(b1); i++)
2131 ga_append(&b->bv_ga, blob_get(b1, i));
2132 for (i = 0; i < blob_len(b2); i++)
2133 ga_append(&b->bv_ga, blob_get(b2, i));
2134
2135 clear_tv(tv1);
2136 rettv_blob_set(tv1, b);
2137 }
2138}
2139
2140 int
2141eval_addlist(typval_T *tv1, typval_T *tv2)
2142{
2143 typval_T var3;
2144
2145 // concatenate Lists
2146 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2147 {
2148 clear_tv(tv1);
2149 clear_tv(tv2);
2150 return FAIL;
2151 }
2152 clear_tv(tv1);
2153 *tv1 = var3;
2154 return OK;
2155}
2156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157/*
2158 * Handle fourth level expression:
2159 * + number addition
2160 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002161 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002162 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 *
2164 * "arg" must point to the first non-white of the expression.
2165 * "arg" is advanced to the next non-white after the recognized expression.
2166 *
2167 * Return OK or FAIL.
2168 */
2169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar33570922005-01-25 22:26:29 +00002172 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002174 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002175#ifdef FEAT_FLOAT
2176 float_T f1 = 0, f2 = 0;
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 char_u *s1, *s2;
2179 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2180 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002181 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 /*
2184 * Get the first variable.
2185 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002186 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188
2189 /*
2190 * Repeat computing, until no '+', '-' or '.' is following.
2191 */
2192 for (;;)
2193 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002194 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002196 concat = op == '.'
2197 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2198 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 break;
2200
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002201 if ((op != '+' || (rettv->v_type != VAR_LIST
2202 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002203#ifdef FEAT_FLOAT
2204 && (op == '.' || rettv->v_type != VAR_FLOAT)
2205#endif
2206 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002208 // For "list + ...", an illegal use of the first operand as
2209 // a number cannot be determined before evaluating the 2nd
2210 // operand: if this is also a list, all is ok.
2211 // For "something . ...", "something - ..." or "non-list + ...",
2212 // we know that the first operand needs to be a string or number
2213 // without evaluating the 2nd operand. So check before to avoid
2214 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002215 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 {
2217 clear_tv(rettv);
2218 return FAIL;
2219 }
2220 }
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /*
2223 * Get the second variable.
2224 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002225 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2226 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002228 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return FAIL;
2232 }
2233
2234 if (evaluate)
2235 {
2236 /*
2237 * Compute the result.
2238 */
2239 if (op == '.')
2240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002241 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002242 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002243 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002244 {
2245 clear_tv(rettv);
2246 clear_tv(&var2);
2247 return FAIL;
2248 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 clear_tv(rettv);
2251 rettv->v_type = VAR_STRING;
2252 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002254 else if (op == '+' && rettv->v_type == VAR_BLOB
2255 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002257 else if (op == '+' && rettv->v_type == VAR_LIST
2258 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002261 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 else
2264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002265 int error = FALSE;
2266
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002267#ifdef FEAT_FLOAT
2268 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002270 f1 = rettv->vval.v_float;
2271 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002273 else
2274#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002276 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002277 if (error)
2278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002279 // This can only happen for "list + non-list". For
2280 // "non-list + ..." or "something - ...", we returned
2281 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002282 clear_tv(rettv);
2283 return FAIL;
2284 }
2285#ifdef FEAT_FLOAT
2286 if (var2.v_type == VAR_FLOAT)
2287 f1 = n1;
2288#endif
2289 }
2290#ifdef FEAT_FLOAT
2291 if (var2.v_type == VAR_FLOAT)
2292 {
2293 f2 = var2.vval.v_float;
2294 n2 = 0;
2295 }
2296 else
2297#endif
2298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002299 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002300 if (error)
2301 {
2302 clear_tv(rettv);
2303 clear_tv(&var2);
2304 return FAIL;
2305 }
2306#ifdef FEAT_FLOAT
2307 if (rettv->v_type == VAR_FLOAT)
2308 f2 = n2;
2309#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002312
2313#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002314 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002315 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2316 {
2317 if (op == '+')
2318 f1 = f1 + f2;
2319 else
2320 f1 = f1 - f2;
2321 rettv->v_type = VAR_FLOAT;
2322 rettv->vval.v_float = f1;
2323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002325#endif
2326 {
2327 if (op == '+')
2328 n1 = n1 + n2;
2329 else
2330 n1 = n1 - n2;
2331 rettv->v_type = VAR_NUMBER;
2332 rettv->vval.v_number = n1;
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 }
2338 return OK;
2339}
2340
2341/*
2342 * Handle fifth level expression:
2343 * * number multiplication
2344 * / number division
2345 * % number modulo
2346 *
2347 * "arg" must point to the first non-white of the expression.
2348 * "arg" is advanced to the next non-white after the recognized expression.
2349 *
2350 * Return OK or FAIL.
2351 */
2352 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353eval6(
2354 char_u **arg,
2355 typval_T *rettv,
2356 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002357 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaar33570922005-01-25 22:26:29 +00002359 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002361 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002362#ifdef FEAT_FLOAT
2363 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002364 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002365#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368 /*
2369 * Get the first variable.
2370 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002371 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return FAIL;
2373
2374 /*
2375 * Repeat computing, until no '*', '/' or '%' is following.
2376 */
2377 for (;;)
2378 {
2379 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002380 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 break;
2382
2383 if (evaluate)
2384 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002385#ifdef FEAT_FLOAT
2386 if (rettv->v_type == VAR_FLOAT)
2387 {
2388 f1 = rettv->vval.v_float;
2389 use_float = TRUE;
2390 n1 = 0;
2391 }
2392 else
2393#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002394 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 if (error)
2397 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 else
2400 n1 = 0;
2401
2402 /*
2403 * Get the second variable.
2404 */
2405 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002406 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return FAIL;
2408
2409 if (evaluate)
2410 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002411#ifdef FEAT_FLOAT
2412 if (var2.v_type == VAR_FLOAT)
2413 {
2414 if (!use_float)
2415 {
2416 f1 = n1;
2417 use_float = TRUE;
2418 }
2419 f2 = var2.vval.v_float;
2420 n2 = 0;
2421 }
2422 else
2423#endif
2424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002425 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002426 clear_tv(&var2);
2427 if (error)
2428 return FAIL;
2429#ifdef FEAT_FLOAT
2430 if (use_float)
2431 f2 = n2;
2432#endif
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 /*
2436 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002437 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002439#ifdef FEAT_FLOAT
2440 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002442 if (op == '*')
2443 f1 = f1 * f2;
2444 else if (op == '/')
2445 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002446# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002447 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002448 if (f2 == 0.0)
2449 {
2450 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002451 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002452 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002453 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002454 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002455 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002456 }
2457 else
2458 f1 = f1 / f2;
2459# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002460 // We rely on the floating point library to handle divide
2461 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002462 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002463# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002466 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002468 return FAIL;
2469 }
2470 rettv->v_type = VAR_FLOAT;
2471 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002476 if (op == '*')
2477 n1 = n1 * n2;
2478 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002479 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002481 n1 = num_modulus(n1, n2);
2482
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002483 rettv->v_type = VAR_NUMBER;
2484 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487 }
2488
2489 return OK;
2490}
2491
2492/*
2493 * Handle sixth level expression:
2494 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002495 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002496 * "string" string constant
2497 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 * &option-name option value
2499 * @r register contents
2500 * identifier variable value
2501 * function() function call
2502 * $VAR environment variable
2503 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002504 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002506 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002507 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 *
2509 * Also handle:
2510 * ! in front logical NOT
2511 * - in front unary minus
2512 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 * trailing [] subscript in String or List
2514 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002515 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
2517 * "arg" must point to the first non-white of the expression.
2518 * "arg" is advanced to the next non-white after the recognized expression.
2519 *
2520 * Return OK or FAIL.
2521 */
2522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523eval7(
2524 char_u **arg,
2525 typval_T *rettv,
2526 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 int len;
2530 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 char_u *start_leader, *end_leader;
2532 int ret = OK;
2533 char_u *alias;
2534
2535 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002537 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002542 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
2544 start_leader = *arg;
2545 while (**arg == '!' || **arg == '-' || **arg == '+')
2546 *arg = skipwhite(*arg + 1);
2547 end_leader = *arg;
2548
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002549 if (**arg == '.' && (!isdigit(*(*arg + 1))
2550#ifdef FEAT_FLOAT
2551 || current_sctx.sc_version < 2
2552#endif
2553 ))
2554 {
2555 semsg(_(e_invexpr2), *arg);
2556 ++*arg;
2557 return FAIL;
2558 }
2559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 switch (**arg)
2561 {
2562 /*
2563 * Number constant.
2564 */
2565 case '0':
2566 case '1':
2567 case '2':
2568 case '3':
2569 case '4':
2570 case '5':
2571 case '6':
2572 case '7':
2573 case '8':
2574 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 break;
2577
2578 /*
2579 * String constant: "string".
2580 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 break;
2583
2584 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002588 break;
2589
2590 /*
2591 * List: [expr, expr]
2592 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 break;
2595
2596 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002597 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002598 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002599 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002600 {
2601 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002602 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002603 }
2604 else
2605 ret = NOTDONE;
2606 break;
2607
2608 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002609 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002610 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002612 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2613 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002614 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 break;
2616
2617 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002618 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002620 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 break;
2622
2623 /*
2624 * Environment variable: $VAR.
2625 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 break;
2628
2629 /*
2630 * Register contents: @r.
2631 */
2632 case '@': ++*arg;
2633 if (evaluate)
2634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002636 rettv->vval.v_string = get_reg_contents(**arg,
2637 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 if (**arg != NUL)
2640 ++*arg;
2641 break;
2642
2643 /*
2644 * nested expression: (expression).
2645 */
2646 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002647 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (**arg == ')')
2649 ++*arg;
2650 else if (ret == OK)
2651 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 ret = FAIL;
2655 }
2656 break;
2657
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 break;
2660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661
2662 if (ret == NOTDONE)
2663 {
2664 /*
2665 * Must be a variable or function name.
2666 * Can also be a curly-braces kind of name: {expr}.
2667 */
2668 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002669 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (alias != NULL)
2671 s = alias;
2672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002673 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 ret = FAIL;
2675 else
2676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002677 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002678 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002680 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002682 {
2683 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002687 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 *arg = skipwhite(*arg);
2691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002692 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2693 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002694 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002695 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2696 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 /*
2699 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2700 */
2701 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002702 ret = eval7_leader(rettv, start_leader, &end_leader);
2703 return ret;
2704}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002705
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002706/*
2707 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2708 * Adjusts "end_leaderp" until it is at "start_leader".
2709 */
2710 static int
2711eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2712{
2713 char_u *end_leader = *end_leaderp;
2714 int ret = OK;
2715 int error = FALSE;
2716 varnumber_T val = 0;
2717#ifdef FEAT_FLOAT
2718 float_T f = 0.0;
2719
2720 if (rettv->v_type == VAR_FLOAT)
2721 f = rettv->vval.v_float;
2722 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002723#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002724 val = tv_get_number_chk(rettv, &error);
2725 if (error)
2726 {
2727 clear_tv(rettv);
2728 ret = FAIL;
2729 }
2730 else
2731 {
2732 while (end_leader > start_leader)
2733 {
2734 --end_leader;
2735 if (*end_leader == '!')
2736 {
2737#ifdef FEAT_FLOAT
2738 if (rettv->v_type == VAR_FLOAT)
2739 f = !f;
2740 else
2741#endif
2742 val = !val;
2743 }
2744 else if (*end_leader == '-')
2745 {
2746#ifdef FEAT_FLOAT
2747 if (rettv->v_type == VAR_FLOAT)
2748 f = -f;
2749 else
2750#endif
2751 val = -val;
2752 }
2753 }
2754#ifdef FEAT_FLOAT
2755 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002758 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002761#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763 clear_tv(rettv);
2764 rettv->v_type = VAR_NUMBER;
2765 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002768 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 return ret;
2770}
2771
2772/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002773 * Call the function referred to in "rettv".
2774 */
2775 static int
2776call_func_rettv(
2777 char_u **arg,
2778 typval_T *rettv,
2779 int evaluate,
2780 dict_T *selfdict,
2781 typval_T *basetv)
2782{
2783 partial_T *pt = NULL;
2784 funcexe_T funcexe;
2785 typval_T functv;
2786 char_u *s;
2787 int ret;
2788
2789 // need to copy the funcref so that we can clear rettv
2790 if (evaluate)
2791 {
2792 functv = *rettv;
2793 rettv->v_type = VAR_UNKNOWN;
2794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002795 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002796 if (functv.v_type == VAR_PARTIAL)
2797 {
2798 pt = functv.vval.v_partial;
2799 s = partial_name(pt);
2800 }
2801 else
2802 s = functv.vval.v_string;
2803 }
2804 else
2805 s = (char_u *)"";
2806
2807 vim_memset(&funcexe, 0, sizeof(funcexe));
2808 funcexe.firstline = curwin->w_cursor.lnum;
2809 funcexe.lastline = curwin->w_cursor.lnum;
2810 funcexe.evaluate = evaluate;
2811 funcexe.partial = pt;
2812 funcexe.selfdict = selfdict;
2813 funcexe.basetv = basetv;
2814 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // Clear the funcref afterwards, so that deleting it while
2817 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002818 if (evaluate)
2819 clear_tv(&functv);
2820
2821 return ret;
2822}
2823
2824/*
2825 * Evaluate "->method()".
2826 * "*arg" points to the '-'.
2827 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2828 */
2829 static int
2830eval_lambda(
2831 char_u **arg,
2832 typval_T *rettv,
2833 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002834 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002835{
2836 typval_T base = *rettv;
2837 int ret;
2838
2839 // Skip over the ->.
2840 *arg += 2;
2841 rettv->v_type = VAR_UNKNOWN;
2842
2843 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002844 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002845 return FAIL;
2846 else if (**arg != '(')
2847 {
2848 if (verbose)
2849 {
2850 if (*skipwhite(*arg) == '(')
2851 semsg(_(e_nowhitespace));
2852 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002854 }
2855 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002856 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002857 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002858 else
2859 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2860
2861 // Clear the funcref afterwards, so that deleting it while
2862 // evaluating the arguments is possible (see test55).
2863 if (evaluate)
2864 clear_tv(&base);
2865
2866 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002867}
2868
2869/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002870 * Evaluate "->method()".
2871 * "*arg" points to the '-'.
2872 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2873 */
2874 static int
2875eval_method(
2876 char_u **arg,
2877 typval_T *rettv,
2878 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002879 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002880{
2881 char_u *name;
2882 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002883 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002884 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002885 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002886
2887 // Skip over the ->.
2888 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002889 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002890
Bram Moolenaarac92e252019-08-03 21:58:38 +02002891 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002892 len = get_name_len(arg, &alias, evaluate, TRUE);
2893 if (alias != NULL)
2894 name = alias;
2895
2896 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002897 {
2898 if (verbose)
2899 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002900 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002901 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002902 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002903 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 if (**arg != '(')
2905 {
2906 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 ret = FAIL;
2909 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002910 else if (VIM_ISWHITE((*arg)[-1]))
2911 {
2912 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002913 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002914 ret = FAIL;
2915 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002916 else
2917 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002918 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002919
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002920 // Clear the funcref afterwards, so that deleting it while
2921 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 if (evaluate)
2923 clear_tv(&base);
2924
Bram Moolenaarac92e252019-08-03 21:58:38 +02002925 return ret;
2926}
2927
2928/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002929 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2930 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002931 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2932 */
2933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002934eval_index(
2935 char_u **arg,
2936 typval_T *rettv,
2937 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002938 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002939{
2940 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002942 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002943 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002944 long len = -1;
2945 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002946 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002947 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002948
Bram Moolenaara03f2332016-02-06 18:09:59 +01002949 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002950 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002951 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002952 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002953 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002954 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 return FAIL;
2956 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002957#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002960 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002961#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002962 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002963 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002964 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002965 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002966 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002968 return FAIL;
2969 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002971 if (evaluate)
2972 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002973 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974
2975 case VAR_STRING:
2976 case VAR_NUMBER:
2977 case VAR_LIST:
2978 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002979 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002980 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002982
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002983 init_tv(&var1);
2984 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002985 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002987 /*
2988 * dict.name
2989 */
2990 key = *arg + 1;
2991 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2992 ;
2993 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002994 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002995 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002996 }
2997 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002998 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002999 /*
3000 * something[idx]
3001 *
3002 * Get the (first) variable from inside the [].
3003 */
3004 *arg = skipwhite(*arg + 1);
3005 if (**arg == ':')
3006 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003007 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003009 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003011 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003012 clear_tv(&var1);
3013 return FAIL;
3014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003015
3016 /*
3017 * Get the second variable from inside the [:].
3018 */
3019 if (**arg == ':')
3020 {
3021 range = TRUE;
3022 *arg = skipwhite(*arg + 1);
3023 if (**arg == ']')
3024 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003025 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 if (!empty1)
3028 clear_tv(&var1);
3029 return FAIL;
3030 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003031 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 if (!empty1)
3035 clear_tv(&var1);
3036 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003037 return FAIL;
3038 }
3039 }
3040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 if (**arg != ']')
3043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003044 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003045 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003046 clear_tv(&var1);
3047 if (range)
3048 clear_tv(&var2);
3049 return FAIL;
3050 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003051 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003052 }
3053
3054 if (evaluate)
3055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003056 n1 = 0;
3057 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003058 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003059 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003061 }
3062 if (range)
3063 {
3064 if (empty2)
3065 n2 = -1;
3066 else
3067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003068 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003070 }
3071 }
3072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003074 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003075 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003077 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003078 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003079 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003080 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003081 case VAR_SPECIAL:
3082 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003083 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003084 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003085
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003086 case VAR_NUMBER:
3087 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003088 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003089 len = (long)STRLEN(s);
3090 if (range)
3091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 // The resulting variable is a substring. If the indexes
3093 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003094 if (n1 < 0)
3095 {
3096 n1 = len + n1;
3097 if (n1 < 0)
3098 n1 = 0;
3099 }
3100 if (n2 < 0)
3101 n2 = len + n2;
3102 else if (n2 >= len)
3103 n2 = len;
3104 if (n1 >= len || n2 < 0 || n1 > n2)
3105 s = NULL;
3106 else
3107 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3108 }
3109 else
3110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 // The resulting variable is a string of a single
3112 // character. If the index is too big or negative the
3113 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003114 if (n1 >= len || n1 < 0)
3115 s = NULL;
3116 else
3117 s = vim_strnsave(s + n1, 1);
3118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 clear_tv(rettv);
3120 rettv->v_type = VAR_STRING;
3121 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003122 break;
3123
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003124 case VAR_BLOB:
3125 len = blob_len(rettv->vval.v_blob);
3126 if (range)
3127 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003128 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003129 // are out of range the result is empty.
3130 if (n1 < 0)
3131 {
3132 n1 = len + n1;
3133 if (n1 < 0)
3134 n1 = 0;
3135 }
3136 if (n2 < 0)
3137 n2 = len + n2;
3138 else if (n2 >= len)
3139 n2 = len - 1;
3140 if (n1 >= len || n2 < 0 || n1 > n2)
3141 {
3142 clear_tv(rettv);
3143 rettv->v_type = VAR_BLOB;
3144 rettv->vval.v_blob = NULL;
3145 }
3146 else
3147 {
3148 blob_T *blob = blob_alloc();
3149
3150 if (blob != NULL)
3151 {
3152 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3153 {
3154 blob_free(blob);
3155 return FAIL;
3156 }
3157 blob->bv_ga.ga_len = n2 - n1 + 1;
3158 for (i = n1; i <= n2; i++)
3159 blob_set(blob, i - n1,
3160 blob_get(rettv->vval.v_blob, i));
3161
3162 clear_tv(rettv);
3163 rettv_blob_set(rettv, blob);
3164 }
3165 }
3166 }
3167 else
3168 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003169 // The resulting variable is a byte value.
3170 // If the index is too big or negative that is an error.
3171 if (n1 < 0)
3172 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003173 if (n1 < len && n1 >= 0)
3174 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003175 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003176
3177 clear_tv(rettv);
3178 rettv->v_type = VAR_NUMBER;
3179 rettv->vval.v_number = v;
3180 }
3181 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003182 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003183 }
3184 break;
3185
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003188 if (n1 < 0)
3189 n1 = len + n1;
3190 if (!empty1 && (n1 < 0 || n1 >= len))
3191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003192 // For a range we allow invalid values and return an empty
3193 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003194 if (!range)
3195 {
3196 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003198 return FAIL;
3199 }
3200 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003201 }
3202 if (range)
3203 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 list_T *l;
3205 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003206
3207 if (n2 < 0)
3208 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003209 else if (n2 >= len)
3210 n2 = len - 1;
3211 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003212 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003213 l = list_alloc();
3214 if (l == NULL)
3215 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003216 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003217 n1 <= n2; ++n1)
3218 {
3219 if (list_append_tv(l, &item->li_tv) == FAIL)
3220 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003221 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003222 return FAIL;
3223 }
3224 item = item->li_next;
3225 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003226 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003227 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003228 }
3229 else
3230 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003231 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003232 clear_tv(rettv);
3233 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003234 }
3235 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236
3237 case VAR_DICT:
3238 if (range)
3239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003240 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003241 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242 if (len == -1)
3243 clear_tv(&var1);
3244 return FAIL;
3245 }
3246 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003248
3249 if (len == -1)
3250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003251 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003252 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003253 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003254 clear_tv(&var1);
3255 return FAIL;
3256 }
3257 }
3258
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003259 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003261 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003262 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 if (len == -1)
3264 clear_tv(&var1);
3265 if (item == NULL)
3266 return FAIL;
3267
3268 copy_tv(&item->di_tv, &var1);
3269 clear_tv(rettv);
3270 *rettv = var1;
3271 }
3272 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003273 }
3274 }
3275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003276 return OK;
3277}
3278
3279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * Get an option value.
3281 * "arg" points to the '&' or '+' before the option name.
3282 * "arg" is advanced to character after the option name.
3283 * Return OK or FAIL.
3284 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003286get_option_tv(
3287 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003288 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003289 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 char_u *option_end;
3292 long numval;
3293 char_u *stringval;
3294 int opt_type;
3295 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int ret = OK;
3298 int opt_flags;
3299
3300 /*
3301 * Isolate the option name and find its value.
3302 */
3303 option_end = find_option_end(arg, &opt_flags);
3304 if (option_end == NULL)
3305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003307 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309 }
3310
3311 if (!evaluate)
3312 {
3313 *arg = option_end;
3314 return OK;
3315 }
3316
3317 c = *option_end;
3318 *option_end = NUL;
3319 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003320 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003322 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 ret = FAIL;
3327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 rettv->v_type = VAR_STRING;
3333 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003335 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 rettv->v_type = VAR_NUMBER;
3338 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003340 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 rettv->v_type = VAR_NUMBER;
3343 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 rettv->v_type = VAR_STRING;
3348 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 }
3351 else if (working && (opt_type == -2 || opt_type == -1))
3352 ret = FAIL;
3353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003354 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 *arg = option_end;
3356
3357 return ret;
3358}
3359
3360/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3362 * Return OK or FAIL.
3363 */
3364 int
3365get_number_tv(
3366 char_u **arg,
3367 typval_T *rettv,
3368 int evaluate,
3369 int want_string UNUSED)
3370{
3371 int len;
3372#ifdef FEAT_FLOAT
3373 char_u *p;
3374 int get_float = FALSE;
3375
3376 // We accept a float when the format matches
3377 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3378 // strict to avoid backwards compatibility problems.
3379 // With script version 2 and later the leading digit can be
3380 // omitted.
3381 // Don't look for a float after the "." operator, so that
3382 // ":let vers = 1.2.3" doesn't fail.
3383 if (**arg == '.')
3384 p = *arg;
3385 else
3386 p = skipdigits(*arg + 1);
3387 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3388 {
3389 get_float = TRUE;
3390 p = skipdigits(p + 2);
3391 if (*p == 'e' || *p == 'E')
3392 {
3393 ++p;
3394 if (*p == '-' || *p == '+')
3395 ++p;
3396 if (!vim_isdigit(*p))
3397 get_float = FALSE;
3398 else
3399 p = skipdigits(p + 1);
3400 }
3401 if (ASCII_ISALPHA(*p) || *p == '.')
3402 get_float = FALSE;
3403 }
3404 if (get_float)
3405 {
3406 float_T f;
3407
3408 *arg += string2float(*arg, &f);
3409 if (evaluate)
3410 {
3411 rettv->v_type = VAR_FLOAT;
3412 rettv->vval.v_float = f;
3413 }
3414 }
3415 else
3416#endif
3417 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3418 {
3419 char_u *bp;
3420 blob_T *blob = NULL; // init for gcc
3421
3422 // Blob constant: 0z0123456789abcdef
3423 if (evaluate)
3424 blob = blob_alloc();
3425 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3426 {
3427 if (!vim_isxdigit(bp[1]))
3428 {
3429 if (blob != NULL)
3430 {
3431 emsg(_("E973: Blob literal should have an even number of hex characters"));
3432 ga_clear(&blob->bv_ga);
3433 VIM_CLEAR(blob);
3434 }
3435 return FAIL;
3436 }
3437 if (blob != NULL)
3438 ga_append(&blob->bv_ga,
3439 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3440 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3441 ++bp;
3442 }
3443 if (blob != NULL)
3444 rettv_blob_set(rettv, blob);
3445 *arg = bp;
3446 }
3447 else
3448 {
3449 varnumber_T n;
3450
3451 // decimal, hex or octal number
3452 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3453 ? STR2NR_NO_OCT + STR2NR_QUOTE
3454 : STR2NR_ALL, &n, NULL, 0, TRUE);
3455 if (len == 0)
3456 {
3457 semsg(_(e_invexpr2), *arg);
3458 return FAIL;
3459 }
3460 *arg += len;
3461 if (evaluate)
3462 {
3463 rettv->v_type = VAR_NUMBER;
3464 rettv->vval.v_number = n;
3465 }
3466 }
3467 return OK;
3468}
3469
3470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 * Allocate a variable for a string constant.
3472 * Return OK or FAIL.
3473 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003475get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 char_u *p;
3478 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int extra = 0;
3480
3481 /*
3482 * Find the end of the string, skipping backslashed characters.
3483 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003484 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 if (*p == '\\' && p[1] != NUL)
3487 {
3488 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489 // A "\<x>" form occupies at least 4 characters, and produces up
3490 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (*p == '<')
3492 extra += 2;
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 if (*p != '"')
3497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003498 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 return FAIL;
3500 }
3501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003502 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (!evaluate)
3504 {
3505 *arg = p + 1;
3506 return OK;
3507 }
3508
3509 /*
3510 * Copy the string into allocated memory, handling backslashed
3511 * characters.
3512 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003513 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (name == NULL)
3515 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003516 rettv->v_type = VAR_STRING;
3517 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
3521 if (*p == '\\')
3522 {
3523 switch (*++p)
3524 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 case 'b': *name++ = BS; ++p; break;
3526 case 'e': *name++ = ESC; ++p; break;
3527 case 'f': *name++ = FF; ++p; break;
3528 case 'n': *name++ = NL; ++p; break;
3529 case 'r': *name++ = CAR; ++p; break;
3530 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003534 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 case 'U':
3536 if (vim_isxdigit(p[1]))
3537 {
3538 int n, nr;
3539 int c = toupper(*p);
3540
3541 if (c == 'X')
3542 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003543 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003545 else
3546 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 nr = 0;
3548 while (--n >= 0 && vim_isxdigit(p[1]))
3549 {
3550 ++p;
3551 nr = (nr << 4) + hex2nr(*p);
3552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003554 // For "\u" store the number according to
3555 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003557 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 break;
3562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003563 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 case '0':
3565 case '1':
3566 case '2':
3567 case '3':
3568 case '4':
3569 case '5':
3570 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 case '7': *name = *p++ - '0';
3572 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 *name = (*name << 3) + *p++ - '0';
3575 if (*p >= '0' && *p <= '7')
3576 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003581 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003582 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3583 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (extra != 0)
3585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 break;
3593 }
3594 }
3595 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003601 ++p;
3602 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 return OK;
3605}
3606
3607/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 * Return OK or FAIL.
3610 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
3614 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003615 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003616 int reduce = 0;
3617
3618 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003619 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003620 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003621 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003626 break;
3627 ++reduce;
3628 ++p;
3629 }
3630 }
3631
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003635 return FAIL;
3636 }
3637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003638 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003639 if (!evaluate)
3640 {
3641 *arg = p + 1;
3642 return OK;
3643 }
3644
3645 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003647 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003648 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003649 if (str == NULL)
3650 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 rettv->v_type = VAR_STRING;
3652 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003653
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003656 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003659 break;
3660 ++p;
3661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003663 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003665 *arg = p + 1;
3666
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003667 return OK;
3668}
3669
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003670/*
3671 * Return the function name of the partial.
3672 */
3673 char_u *
3674partial_name(partial_T *pt)
3675{
3676 if (pt->pt_name != NULL)
3677 return pt->pt_name;
3678 return pt->pt_func->uf_name;
3679}
3680
Bram Moolenaarddecc252016-04-06 22:59:37 +02003681 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003682partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003683{
3684 int i;
3685
3686 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003687 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003688 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003689 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003690 if (pt->pt_name != NULL)
3691 {
3692 func_unref(pt->pt_name);
3693 vim_free(pt->pt_name);
3694 }
3695 else
3696 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003697 vim_free(pt);
3698}
3699
3700/*
3701 * Unreference a closure: decrement the reference count and free it when it
3702 * becomes zero.
3703 */
3704 void
3705partial_unref(partial_T *pt)
3706{
3707 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003708 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003709}
3710
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003711static int tv_equal_recurse_limit;
3712
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003713 static int
3714func_equal(
3715 typval_T *tv1,
3716 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003718{
3719 char_u *s1, *s2;
3720 dict_T *d1, *d2;
3721 int a1, a2;
3722 int i;
3723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003724 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003725 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003726 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003727 if (s1 != NULL && *s1 == NUL)
3728 s1 = NULL;
3729 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003730 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003731 if (s2 != NULL && *s2 == NUL)
3732 s2 = NULL;
3733 if (s1 == NULL || s2 == NULL)
3734 {
3735 if (s1 != s2)
3736 return FALSE;
3737 }
3738 else if (STRCMP(s1, s2) != 0)
3739 return FALSE;
3740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003742 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3743 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3744 if (d1 == NULL || d2 == NULL)
3745 {
3746 if (d1 != d2)
3747 return FALSE;
3748 }
3749 else if (!dict_equal(d1, d2, ic, TRUE))
3750 return FALSE;
3751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003753 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3754 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3755 if (a1 != a2)
3756 return FALSE;
3757 for (i = 0; i < a1; ++i)
3758 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3759 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3760 return FALSE;
3761
3762 return TRUE;
3763}
3764
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003765/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003766 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003767 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003768 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003769 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003771tv_equal(
3772 typval_T *tv1,
3773 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003774 int ic, // ignore case
3775 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003776{
3777 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003779 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003780 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003782 // Catch lists and dicts that have an endless loop by limiting
3783 // recursiveness to a limit. We guess they are equal then.
3784 // A fixed limit has the problem of still taking an awful long time.
3785 // Reduce the limit every time running into it. That should work fine for
3786 // deeply linked structures that are not recursively linked and catch
3787 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003788 if (!recursive)
3789 tv_equal_recurse_limit = 1000;
3790 if (recursive_cnt >= tv_equal_recurse_limit)
3791 {
3792 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003793 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003794 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003796 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3797 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003798 if ((tv1->v_type == VAR_FUNC
3799 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3800 && (tv2->v_type == VAR_FUNC
3801 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3802 {
3803 ++recursive_cnt;
3804 r = func_equal(tv1, tv2, ic);
3805 --recursive_cnt;
3806 return r;
3807 }
3808
3809 if (tv1->v_type != tv2->v_type)
3810 return FALSE;
3811
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003812 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003813 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003814 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003815 ++recursive_cnt;
3816 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3817 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003818 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003819
3820 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003821 ++recursive_cnt;
3822 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3823 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003824 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003825
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003826 case VAR_BLOB:
3827 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3828
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003829 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 case VAR_BOOL:
3831 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003832 return tv1->vval.v_number == tv2->vval.v_number;
3833
3834 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003835 s1 = tv_get_string_buf(tv1, buf1);
3836 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003837 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003838
Bram Moolenaar835dc632016-02-07 14:27:38 +01003839 case VAR_FLOAT:
3840#ifdef FEAT_FLOAT
3841 return tv1->vval.v_float == tv2->vval.v_float;
3842#endif
3843 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003845 return tv1->vval.v_job == tv2->vval.v_job;
3846#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003847 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003848#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003849 return tv1->vval.v_channel == tv2->vval.v_channel;
3850#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003852 case VAR_FUNC:
3853 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003854 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003856 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003859 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3860 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003861 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003862}
3863
3864/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003865 * Return the next (unique) copy ID.
3866 * Used for serializing nested structures.
3867 */
3868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003869get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003870{
3871 current_copyID += COPYID_INC;
3872 return current_copyID;
3873}
3874
3875/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003876 * Garbage collection for lists and dictionaries.
3877 *
3878 * We use reference counts to be able to free most items right away when they
3879 * are no longer used. But for composite items it's possible that it becomes
3880 * unused while the reference count is > 0: When there is a recursive
3881 * reference. Example:
3882 * :let l = [1, 2, 3]
3883 * :let d = {9: l}
3884 * :let l[1] = d
3885 *
3886 * Since this is quite unusual we handle this with garbage collection: every
3887 * once in a while find out which lists and dicts are not referenced from any
3888 * variable.
3889 *
3890 * Here is a good reference text about garbage collection (refers to Python
3891 * but it applies to all reference-counting mechanisms):
3892 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003893 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003894
3895/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003896 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003897 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003898 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003899 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003900 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003901garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003902{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003903 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003904 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003905 buf_T *buf;
3906 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003907 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003908 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003910 if (!testing)
3911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003912 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003913 want_garbage_collect = FALSE;
3914 may_garbage_collect = FALSE;
3915 garbage_collect_at_exit = FALSE;
3916 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003917
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003918 // The execution stack can grow big, limit the size.
3919 if (exestack.ga_maxlen - exestack.ga_len > 500)
3920 {
3921 size_t new_len;
3922 char_u *pp;
3923 int n;
3924
3925 // Keep 150% of the current size, with a minimum of the growth size.
3926 n = exestack.ga_len / 2;
3927 if (n < exestack.ga_growsize)
3928 n = exestack.ga_growsize;
3929
3930 // Don't make it bigger though.
3931 if (exestack.ga_len + n < exestack.ga_maxlen)
3932 {
3933 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3934 pp = vim_realloc(exestack.ga_data, new_len);
3935 if (pp == NULL)
3936 return FAIL;
3937 exestack.ga_maxlen = exestack.ga_len + n;
3938 exestack.ga_data = pp;
3939 }
3940 }
3941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003942 // We advance by two because we add one for items referenced through
3943 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003944 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003946 /*
3947 * 1. Go through all accessible variables and mark all lists and dicts
3948 * with copyID.
3949 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // Don't free variables in the previous_funccal list unless they are only
3952 // referenced through previous_funccal. This must be first, because if
3953 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003956 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003957 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003959 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003960 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003961 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3962 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003964 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003965 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003966 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3967 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003968 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003969 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3970 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003971#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003972 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3973 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3974 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003975 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003976 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003977 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3978 NULL, NULL);
3979#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003981 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003982 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003983 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3984 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003985 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003986 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003988 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003991 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003992 abort = abort || set_ref_in_functions(copyID);
3993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003994 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003998 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003999
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004000 // callbacks in buffers
4001 abort = abort || set_ref_in_buffers(copyID);
4002
Bram Moolenaar1dced572012-04-05 16:54:08 +02004003#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004004 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004005#endif
4006
Bram Moolenaardb913952012-06-29 12:54:53 +02004007#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004008 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004009#endif
4010
4011#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004012 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004013#endif
4014
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004015#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004016 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004017 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004018#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004019#ifdef FEAT_NETBEANS_INTG
4020 abort = abort || set_ref_in_nb_channel(copyID);
4021#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004022
Bram Moolenaare3188e22016-05-31 21:13:04 +02004023#ifdef FEAT_TIMERS
4024 abort = abort || set_ref_in_timer(copyID);
4025#endif
4026
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004027#ifdef FEAT_QUICKFIX
4028 abort = abort || set_ref_in_quickfix(copyID);
4029#endif
4030
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004031#ifdef FEAT_TERMINAL
4032 abort = abort || set_ref_in_term(copyID);
4033#endif
4034
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004035#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004036 abort = abort || set_ref_in_popups(copyID);
4037#endif
4038
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004040 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004041 /*
4042 * 2. Free lists and dictionaries that are not referenced.
4043 */
4044 did_free = free_unref_items(copyID);
4045
4046 /*
4047 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004052 else if (p_verbose > 0)
4053 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004054 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004055 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004056
4057 return did_free;
4058}
4059
4060/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004061 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004062 */
4063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004064free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004065{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004066 int did_free = FALSE;
4067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004068 // Let all "free" functions know that we are here. This means no
4069 // dictionaries, lists, channels or jobs are to be freed, because we will
4070 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004071 in_free_unref_items = TRUE;
4072
4073 /*
4074 * PASS 1: free the contents of the items. We don't free the items
4075 * themselves yet, so that it is possible to decrement refcount counters
4076 */
4077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004078 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004079 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004082 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004083
4084#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004085 // Go through the list of jobs and free items without the copyID. This
4086 // must happen before doing channels, because jobs refer to channels, but
4087 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004088 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004090 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4092#endif
4093
4094 /*
4095 * PASS 2: free the items themselves.
4096 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004097 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004098 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004099
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004100#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // Go through the list of jobs and free items without the copyID. This
4102 // must happen before doing channels, because jobs refer to channels, but
4103 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004104 free_unused_jobs(copyID, COPYID_MASK);
4105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004106 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004107 free_unused_channels(copyID, COPYID_MASK);
4108#endif
4109
4110 in_free_unref_items = FALSE;
4111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004112 return did_free;
4113}
4114
4115/*
4116 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004117 * "list_stack" is used to add lists to be marked. Can be NULL.
4118 *
4119 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004120 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004122set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004123{
4124 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004125 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004126 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004127 hashtab_T *cur_ht;
4128 ht_stack_T *ht_stack = NULL;
4129 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004130
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004131 cur_ht = ht;
4132 for (;;)
4133 {
4134 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004136 // Mark each item in the hashtab. If the item contains a hashtab
4137 // it is added to ht_stack, if it contains a list it is added to
4138 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 todo = (int)cur_ht->ht_used;
4140 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4141 if (!HASHITEM_EMPTY(hi))
4142 {
4143 --todo;
4144 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4145 &ht_stack, list_stack);
4146 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004147 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004148
4149 if (ht_stack == NULL)
4150 break;
4151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004152 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 cur_ht = ht_stack->ht;
4154 tempitem = ht_stack;
4155 ht_stack = ht_stack->prev;
4156 free(tempitem);
4157 }
4158
4159 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004160}
4161
4162/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004163 * Mark a dict and its items with "copyID".
4164 * Returns TRUE if setting references failed somehow.
4165 */
4166 int
4167set_ref_in_dict(dict_T *d, int copyID)
4168{
4169 if (d != NULL && d->dv_copyID != copyID)
4170 {
4171 d->dv_copyID = copyID;
4172 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4173 }
4174 return FALSE;
4175}
4176
4177/*
4178 * Mark a list and its items with "copyID".
4179 * Returns TRUE if setting references failed somehow.
4180 */
4181 int
4182set_ref_in_list(list_T *ll, int copyID)
4183{
4184 if (ll != NULL && ll->lv_copyID != copyID)
4185 {
4186 ll->lv_copyID = copyID;
4187 return set_ref_in_list_items(ll, copyID, NULL);
4188 }
4189 return FALSE;
4190}
4191
4192/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004193 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004194 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4195 *
4196 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004197 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004198 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004199set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004200{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004201 listitem_T *li;
4202 int abort = FALSE;
4203 list_T *cur_l;
4204 list_stack_T *list_stack = NULL;
4205 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207 cur_l = l;
4208 for (;;)
4209 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004210 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // Mark each item in the list. If the item contains a hashtab
4212 // it is added to ht_stack, if it contains a list it is added to
4213 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004214 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4215 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4216 ht_stack, &list_stack);
4217 if (list_stack == NULL)
4218 break;
4219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004221 cur_l = list_stack->list;
4222 tempitem = list_stack;
4223 list_stack = list_stack->prev;
4224 free(tempitem);
4225 }
4226
4227 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004228}
4229
4230/*
4231 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 * "list_stack" is used to add lists to be marked. Can be NULL.
4233 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4234 *
4235 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004236 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238set_ref_in_item(
4239 typval_T *tv,
4240 int copyID,
4241 ht_stack_T **ht_stack,
4242 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004243{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004244 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004245
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004246 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004247 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004248 dict_T *dd = tv->vval.v_dict;
4249
Bram Moolenaara03f2332016-02-06 18:09:59 +01004250 if (dd != NULL && dd->dv_copyID != copyID)
4251 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004252 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004253 dd->dv_copyID = copyID;
4254 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004255 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004256 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4257 }
4258 else
4259 {
4260 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4261 if (newitem == NULL)
4262 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004263 else
4264 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004265 newitem->ht = &dd->dv_hashtab;
4266 newitem->prev = *ht_stack;
4267 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004268 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004269 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004270 }
4271 }
4272 else if (tv->v_type == VAR_LIST)
4273 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004274 list_T *ll = tv->vval.v_list;
4275
Bram Moolenaara03f2332016-02-06 18:09:59 +01004276 if (ll != NULL && ll->lv_copyID != copyID)
4277 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004279 ll->lv_copyID = copyID;
4280 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004281 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004282 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004283 }
4284 else
4285 {
4286 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004287 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 if (newitem == NULL)
4289 abort = TRUE;
4290 else
4291 {
4292 newitem->list = ll;
4293 newitem->prev = *list_stack;
4294 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004295 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004296 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004297 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004298 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004299 else if (tv->v_type == VAR_FUNC)
4300 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004301 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004302 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004303 else if (tv->v_type == VAR_PARTIAL)
4304 {
4305 partial_T *pt = tv->vval.v_partial;
4306 int i;
4307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004308 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004309 if (pt != NULL)
4310 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004312
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004313 if (pt->pt_dict != NULL)
4314 {
4315 typval_T dtv;
4316
4317 dtv.v_type = VAR_DICT;
4318 dtv.vval.v_dict = pt->pt_dict;
4319 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4320 }
4321
4322 for (i = 0; i < pt->pt_argc; ++i)
4323 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4324 ht_stack, list_stack);
4325 }
4326 }
4327#ifdef FEAT_JOB_CHANNEL
4328 else if (tv->v_type == VAR_JOB)
4329 {
4330 job_T *job = tv->vval.v_job;
4331 typval_T dtv;
4332
4333 if (job != NULL && job->jv_copyID != copyID)
4334 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004335 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004336 if (job->jv_channel != NULL)
4337 {
4338 dtv.v_type = VAR_CHANNEL;
4339 dtv.vval.v_channel = job->jv_channel;
4340 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4341 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004342 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004343 {
4344 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004345 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004346 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4347 }
4348 }
4349 }
4350 else if (tv->v_type == VAR_CHANNEL)
4351 {
4352 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004353 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004354 typval_T dtv;
4355 jsonq_T *jq;
4356 cbq_T *cq;
4357
4358 if (ch != NULL && ch->ch_copyID != copyID)
4359 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004360 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004361 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004362 {
4363 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4364 jq = jq->jq_next)
4365 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4366 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4367 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004368 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004369 {
4370 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004371 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004372 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4373 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004374 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004375 {
4376 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004377 dtv.vval.v_partial =
4378 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4380 }
4381 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004382 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004383 {
4384 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004385 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004386 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4387 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004388 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004389 {
4390 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004391 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004392 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4393 }
4394 }
4395 }
4396#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004397 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004398}
4399
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 * Return a string with the string representation of a variable.
4402 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004404 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004405 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4406 * stings as "string()", otherwise does not put quotes around strings, as
4407 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004408 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4409 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004410 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004411 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004412 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004413echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004414 typval_T *tv,
4415 char_u **tofree,
4416 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004417 int copyID,
4418 int echo_style,
4419 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004420 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004421{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004422 static int recurse = 0;
4423 char_u *r = NULL;
4424
Bram Moolenaar33570922005-01-25 22:26:29 +00004425 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004426 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004427 if (!did_echo_string_emsg)
4428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004429 // Only give this message once for a recursive call to avoid
4430 // flooding the user with errors. And stop iterating over lists
4431 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004432 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004433 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004435 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004436 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004437 }
4438 ++recurse;
4439
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 switch (tv->v_type)
4441 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004442 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004443 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004444 {
4445 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004446 r = tv->vval.v_string;
4447 if (r == NULL)
4448 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004449 }
4450 else
4451 {
4452 *tofree = string_quote(tv->vval.v_string, FALSE);
4453 r = *tofree;
4454 }
4455 break;
4456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004458 if (echo_style)
4459 {
4460 *tofree = NULL;
4461 r = tv->vval.v_string;
4462 }
4463 else
4464 {
4465 *tofree = string_quote(tv->vval.v_string, TRUE);
4466 r = *tofree;
4467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004468 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004469
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004470 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004471 {
4472 partial_T *pt = tv->vval.v_partial;
4473 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004474 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004475 garray_T ga;
4476 int i;
4477 char_u *tf;
4478
4479 ga_init2(&ga, 1, 100);
4480 ga_concat(&ga, (char_u *)"function(");
4481 if (fname != NULL)
4482 {
4483 ga_concat(&ga, fname);
4484 vim_free(fname);
4485 }
4486 if (pt != NULL && pt->pt_argc > 0)
4487 {
4488 ga_concat(&ga, (char_u *)", [");
4489 for (i = 0; i < pt->pt_argc; ++i)
4490 {
4491 if (i > 0)
4492 ga_concat(&ga, (char_u *)", ");
4493 ga_concat(&ga,
4494 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4495 vim_free(tf);
4496 }
4497 ga_concat(&ga, (char_u *)"]");
4498 }
4499 if (pt != NULL && pt->pt_dict != NULL)
4500 {
4501 typval_T dtv;
4502
4503 ga_concat(&ga, (char_u *)", ");
4504 dtv.v_type = VAR_DICT;
4505 dtv.vval.v_dict = pt->pt_dict;
4506 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4507 vim_free(tf);
4508 }
4509 ga_concat(&ga, (char_u *)")");
4510
4511 *tofree = ga.ga_data;
4512 r = *tofree;
4513 break;
4514 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004515
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004516 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004517 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004518 break;
4519
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004521 if (tv->vval.v_list == NULL)
4522 {
4523 *tofree = NULL;
4524 r = NULL;
4525 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004526 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4527 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004528 {
4529 *tofree = NULL;
4530 r = (char_u *)"[...]";
4531 }
4532 else
4533 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004534 int old_copyID = tv->vval.v_list->lv_copyID;
4535
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004536 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004537 *tofree = list2string(tv, copyID, restore_copyID);
4538 if (restore_copyID)
4539 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004540 r = *tofree;
4541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004542 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004543
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004545 if (tv->vval.v_dict == NULL)
4546 {
4547 *tofree = NULL;
4548 r = NULL;
4549 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004550 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4551 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004552 {
4553 *tofree = NULL;
4554 r = (char_u *)"{...}";
4555 }
4556 else
4557 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004558 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004559 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004560 *tofree = dict2string(tv, copyID, restore_copyID);
4561 if (restore_copyID)
4562 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004563 r = *tofree;
4564 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004565 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004568 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004570 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004571 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004572 break;
4573
Bram Moolenaar835dc632016-02-07 14:27:38 +01004574 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004575 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004576 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004578 if (composite_val)
4579 {
4580 *tofree = string_quote(r, FALSE);
4581 r = *tofree;
4582 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004584
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004585 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004586#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004587 *tofree = NULL;
4588 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4589 r = numbuf;
4590 break;
4591#endif
4592
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004593 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004594 case VAR_SPECIAL:
4595 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004596 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004597 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004599
Bram Moolenaar8502c702014-06-17 12:51:16 +02004600 if (--recurse == 0)
4601 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004602 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004603}
4604
4605/*
4606 * Return a string with the string representation of a variable.
4607 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4608 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004609 * Does not put quotes around strings, as ":echo" displays values.
4610 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4611 * May return NULL.
4612 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004614echo_string(
4615 typval_T *tv,
4616 char_u **tofree,
4617 char_u *numbuf,
4618 int copyID)
4619{
4620 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4621}
4622
4623/*
4624 * Return a string with the string representation of a variable.
4625 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4626 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004627 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004628 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004629 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004630 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004631tv2string(
4632 typval_T *tv,
4633 char_u **tofree,
4634 char_u *numbuf,
4635 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004636{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004637 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638}
4639
4640/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004641 * Return string "str" in ' quotes, doubling ' characters.
4642 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004644 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004646string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647{
Bram Moolenaar33570922005-01-25 22:26:29 +00004648 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004649 char_u *p, *r, *s;
4650
Bram Moolenaar33570922005-01-25 22:26:29 +00004651 len = (function ? 13 : 3);
4652 if (str != NULL)
4653 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004654 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004655 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 if (*p == '\'')
4657 ++len;
4658 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659 s = r = alloc(len);
4660 if (r != NULL)
4661 {
4662 if (function)
4663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 r += 10;
4666 }
4667 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004669 if (str != NULL)
4670 for (p = str; *p != NUL; )
4671 {
4672 if (*p == '\'')
4673 *r++ = '\'';
4674 MB_COPY_CHAR(p, r);
4675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004677 if (function)
4678 *r++ = ')';
4679 *r++ = NUL;
4680 }
4681 return s;
4682}
4683
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004684#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004685/*
4686 * Convert the string "text" to a floating point number.
4687 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4688 * this always uses a decimal point.
4689 * Returns the length of the text that was consumed.
4690 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004692string2float(
4693 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004694 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695{
4696 char *s = (char *)text;
4697 float_T f;
4698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004699 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004700 if (STRNICMP(text, "inf", 3) == 0)
4701 {
4702 *value = INFINITY;
4703 return 3;
4704 }
4705 if (STRNICMP(text, "-inf", 3) == 0)
4706 {
4707 *value = -INFINITY;
4708 return 4;
4709 }
4710 if (STRNICMP(text, "nan", 3) == 0)
4711 {
4712 *value = NAN;
4713 return 3;
4714 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 f = strtod(s, &s);
4716 *value = f;
4717 return (int)((char_u *)s - text);
4718}
4719#endif
4720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * Get the value of an environment variable.
4723 * "arg" is pointing to the '$'. It is advanced to after the name.
4724 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004725 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004728get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 char_u *string = NULL;
4731 int len;
4732 int cc;
4733 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004734 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736 ++*arg;
4737 name = *arg;
4738 len = get_env_len(arg);
4739 if (evaluate)
4740 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004741 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004743
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004744 cc = name[len];
4745 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004747 string = vim_getenv(name, &mustfree);
4748 if (string != NULL && *string != NUL)
4749 {
4750 if (!mustfree)
4751 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004753 else
4754 {
4755 if (mustfree)
4756 vim_free(string);
4757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004759 string = expand_env_save(name - 1);
4760 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004761 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004762 }
4763 name[len] = cc;
4764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768
4769 return OK;
4770}
4771
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004774 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004776 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004777var2fpos(
4778 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 int dollar_lnum, // TRUE when $ is last line
4780 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004782 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004784 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004786 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004787 if (varp->v_type == VAR_LIST)
4788 {
4789 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004790 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004791 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004792 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004793
4794 l = varp->vval.v_list;
4795 if (l == NULL)
4796 return NULL;
4797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004799 pos.lnum = list_find_nr(l, 0L, &error);
4800 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004804 pos.col = list_find_nr(l, 1L, &error);
4805 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004806 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004807 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004810 li = list_find(l, 1L);
4811 if (li != NULL && li->li_tv.v_type == VAR_STRING
4812 && li->li_tv.vval.v_string != NULL
4813 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4814 pos.col = len + 1;
4815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004817 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004819 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004822 pos.coladd = list_find_nr(l, 2L, &error);
4823 if (error)
4824 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004825
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004826 return &pos;
4827 }
4828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (name == NULL)
4831 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004832 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004835 {
4836 if (VIsual_active)
4837 return &VIsual;
4838 return &curwin->w_cursor;
4839 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004842 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4844 return NULL;
4845 return pp;
4846 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004847
Bram Moolenaara5525202006-03-02 22:52:09 +00004848 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004849
Bram Moolenaar477933c2007-07-17 14:32:23 +00004850 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004851 {
4852 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004854 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004855 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004856 // In silent Ex mode topline is zero, but that's not a valid line
4857 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004858 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004859 return &pos;
4860 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004861 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004862 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004863 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004865 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004866 return &pos;
4867 }
4868 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004871 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 pos.lnum = curbuf->b_ml.ml_line_count;
4874 pos.col = 0;
4875 }
4876 else
4877 {
4878 pos.lnum = curwin->w_cursor.lnum;
4879 pos.col = (colnr_T)STRLEN(ml_get_curline());
4880 }
4881 return &pos;
4882 }
4883 return NULL;
4884}
4885
4886/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004887 * Convert list in "arg" into a position and optional file number.
4888 * When "fnump" is NULL there is no file number, only 3 items.
4889 * Note that the column is passed on as-is, the caller may want to decrement
4890 * it to use 1 for the first column.
4891 * Return FAIL when conversion is not possible, doesn't check the position for
4892 * validity.
4893 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895list2fpos(
4896 typval_T *arg,
4897 pos_T *posp,
4898 int *fnump,
4899 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004900{
4901 list_T *l = arg->vval.v_list;
4902 long i = 0;
4903 long n;
4904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4906 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004907 if (arg->v_type != VAR_LIST
4908 || l == NULL
4909 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004910 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004911 return FAIL;
4912
4913 if (fnump != NULL)
4914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004916 if (n < 0)
4917 return FAIL;
4918 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004920 *fnump = n;
4921 }
4922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004924 if (n < 0)
4925 return FAIL;
4926 posp->lnum = n;
4927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004929 if (n < 0)
4930 return FAIL;
4931 posp->col = n;
4932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004934 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004935 posp->coladd = 0;
4936 else
4937 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004938
Bram Moolenaar493c1782014-05-28 14:34:46 +02004939 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004940 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004941
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004942 return OK;
4943}
4944
4945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 * Get the length of an environment variable name.
4947 * Advance "arg" to the first character after the name.
4948 * Return 0 for error.
4949 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004951get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 char_u *p;
4954 int len;
4955
4956 for (p = *arg; vim_isIDc(*p); ++p)
4957 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 return 0;
4960
4961 len = (int)(p - *arg);
4962 *arg = p;
4963 return len;
4964}
4965
4966/*
4967 * Get the length of the name of a function or internal variable.
4968 * "arg" is advanced to the first non-white character after the name.
4969 * Return 0 if something is wrong.
4970 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004972get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 char_u *p;
4975 int len;
4976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004979 {
4980 if (*p == ':')
4981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // "s:" is start of "s:var", but "n:" is not and can be used in
4983 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004984 len = (int)(p - *arg);
4985 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4986 || len > 1)
4987 break;
4988 }
4989 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 return 0;
4992
4993 len = (int)(p - *arg);
4994 *arg = skipwhite(p);
4995
4996 return len;
4997}
4998
4999/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005000 * Get the length of the name of a variable or function.
5001 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 * Return -1 if curly braces expansion failed.
5004 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 * If the name contains 'magic' {}'s, expand them and return the
5006 * expanded name in an allocated string via 'alias' - caller must free.
5007 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005009get_name_len(
5010 char_u **arg,
5011 char_u **alias,
5012 int evaluate,
5013 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 char_u *p;
5017 char_u *expr_start;
5018 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005020 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5023 && (*arg)[2] == (int)KE_SNR)
5024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *arg += 3;
5027 return get_id_len(arg) + 3;
5028 }
5029 len = eval_fname_script(*arg);
5030 if (len > 0)
5031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *arg += len;
5034 }
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005039 p = find_name_end(*arg, &expr_start, &expr_end,
5040 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (expr_start != NULL)
5042 {
5043 char_u *temp_string;
5044
5045 if (!evaluate)
5046 {
5047 len += (int)(p - *arg);
5048 *arg = skipwhite(p);
5049 return len;
5050 }
5051
5052 /*
5053 * Include any <SID> etc in the expanded string:
5054 * Thus the -len here.
5055 */
5056 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5057 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005058 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *alias = temp_string;
5060 *arg = skipwhite(p);
5061 return (int)STRLEN(temp_string);
5062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005065 // Only give an error when there is something, otherwise it will be
5066 // reported at a higher level.
5067 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005068 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 return len;
5071}
5072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073/*
5074 * Find the end of a variable or function name, taking care of magic braces.
5075 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5076 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005077 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 * Return a pointer to just after the name. Equal to "arg" if there is no
5079 * valid name.
5080 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082find_name_end(
5083 char_u *arg,
5084 char_u **expr_start,
5085 char_u **expr_end,
5086 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 int mb_nest = 0;
5089 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005091 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 *expr_start = NULL;
5096 *expr_end = NULL;
5097 }
5098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // Quick check for valid starting character.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005100 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5101 return arg;
5102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 for (p = arg; *p != NUL
5104 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005105 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005106 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005108 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005110 if (*p == '\'')
5111 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005112 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005113 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005114 ;
5115 if (*p == NUL)
5116 break;
5117 }
5118 else if (*p == '"')
5119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005121 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005122 if (*p == '\\' && p[1] != NUL)
5123 ++p;
5124 if (*p == NUL)
5125 break;
5126 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005127 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // "s:" is start of "s:var", but "n:" is not and can be used in
5130 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005131 len = (int)(p - arg);
5132 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005133 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005134 break;
5135 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005137 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 if (*p == '[')
5140 ++br_nest;
5141 else if (*p == ']')
5142 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 if (*p == '{')
5148 {
5149 mb_nest++;
5150 if (expr_start != NULL && *expr_start == NULL)
5151 *expr_start = p;
5152 }
5153 else if (*p == '}')
5154 {
5155 mb_nest--;
5156 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5157 *expr_end = p;
5158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161
5162 return p;
5163}
5164
5165/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005166 * Expands out the 'magic' {}'s in a variable/function name.
5167 * Note that this can call itself recursively, to deal with
5168 * constructs like foo{bar}{baz}{bam}
5169 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5170 * "in_start" ^
5171 * "expr_start" ^
5172 * "expr_end" ^
5173 * "in_end" ^
5174 *
5175 * Returns a new allocated string, which the caller must free.
5176 * Returns NULL for failure.
5177 */
5178 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005179make_expanded_name(
5180 char_u *in_start,
5181 char_u *expr_start,
5182 char_u *expr_end,
5183 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184{
5185 char_u c1;
5186 char_u *retval = NULL;
5187 char_u *temp_result;
5188 char_u *nextcmd = NULL;
5189
5190 if (expr_end == NULL || in_end == NULL)
5191 return NULL;
5192 *expr_start = NUL;
5193 *expr_end = NUL;
5194 c1 = *in_end;
5195 *in_end = NUL;
5196
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005197 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 if (temp_result != NULL && nextcmd == NULL)
5199 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005200 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5201 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005202 if (retval != NULL)
5203 {
5204 STRCPY(retval, in_start);
5205 STRCAT(retval, temp_result);
5206 STRCAT(retval, expr_end + 1);
5207 }
5208 }
5209 vim_free(temp_result);
5210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 *expr_start = '{';
5213 *expr_end = '}';
5214
5215 if (retval != NULL)
5216 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005217 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005218 if (expr_start != NULL)
5219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005220 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 temp_result = make_expanded_name(retval, expr_start,
5222 expr_end, temp_result);
5223 vim_free(retval);
5224 retval = temp_result;
5225 }
5226 }
5227
5228 return retval;
5229}
5230
5231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005233 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005236eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005238 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5239}
5240
5241/*
5242 * Return TRUE if character "c" can be used as the first character in a
5243 * variable or function name (excluding '{' and '}').
5244 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005247{
5248 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249}
5250
5251/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005252 * Handle:
5253 * - expr[expr], expr[expr:expr] subscript
5254 * - ".name" lookup
5255 * - function call with Funcref variable: func(expr)
5256 * - method call: var->method()
5257 *
5258 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005261handle_subscript(
5262 char_u **arg,
5263 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005264 int evaluate, // do more than finding the end
5265 int verbose, // give error messages
5266 char_u *start_leader, // start of '!' and '-' prefixes
5267 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268{
5269 int ret = OK;
5270 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271
Bram Moolenaar61343f02019-07-20 21:11:13 +02005272 // "." is ".name" lookup when we found a dict or when evaluating and
5273 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005275 && (((**arg == '['
5276 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005277 || (!evaluate
5278 && (*arg)[1] != '.'
5279 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005280 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005281 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005282 && !VIM_ISWHITE(*(*arg - 1)))
5283 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284 {
5285 if (**arg == '(')
5286 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005287 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005288
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005289 // Stop the expression evaluation when immediately aborting on
5290 // error, or when an interrupt occurred or an exception was thrown
5291 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292 if (aborting())
5293 {
5294 if (ret == OK)
5295 clear_tv(rettv);
5296 ret = FAIL;
5297 }
5298 dict_unref(selfdict);
5299 selfdict = NULL;
5300 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005301 else if (**arg == '-')
5302 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005303 // Expression "-1.0->method()" applies the leader "-" before
5304 // applying ->.
5305 if (evaluate && *end_leaderp > start_leader)
5306 ret = eval7_leader(rettv, start_leader, end_leaderp);
5307 if (ret == OK)
5308 {
5309 if ((*arg)[2] == '{')
5310 // expr->{lambda}()
5311 ret = eval_lambda(arg, rettv, evaluate, verbose);
5312 else
5313 // expr->name()
5314 ret = eval_method(arg, rettv, evaluate, verbose);
5315 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005316 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005318 {
5319 dict_unref(selfdict);
5320 if (rettv->v_type == VAR_DICT)
5321 {
5322 selfdict = rettv->vval.v_dict;
5323 if (selfdict != NULL)
5324 ++selfdict->dv_refcount;
5325 }
5326 else
5327 selfdict = NULL;
5328 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5329 {
5330 clear_tv(rettv);
5331 ret = FAIL;
5332 }
5333 }
5334 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5337 // Don't do this when "Func" is already a partial that was bound
5338 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005339 if (selfdict != NULL
5340 && (rettv->v_type == VAR_FUNC
5341 || (rettv->v_type == VAR_PARTIAL
5342 && (rettv->vval.v_partial->pt_auto
5343 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005345
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346 dict_unref(selfdict);
5347 return ret;
5348}
5349
5350/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005351 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 * value).
5353 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005354 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005355alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005357 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358}
5359
5360/*
5361 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 * The string "s" must have been allocated, it is consumed.
5363 * Return NULL for out of memory, the variable otherwise.
5364 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005365 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005366alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 rettv = alloc_tv();
5371 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 rettv->v_type = VAR_STRING;
5374 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376 else
5377 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379}
5380
5381/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005385free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 if (varp != NULL)
5388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 switch (varp->v_type)
5390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005392 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005393 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005394 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 vim_free(varp->vval.v_string);
5396 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005397 case VAR_PARTIAL:
5398 partial_unref(varp->vval.v_partial);
5399 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005400 case VAR_BLOB:
5401 blob_unref(varp->vval.v_blob);
5402 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 case VAR_LIST:
5404 list_unref(varp->vval.v_list);
5405 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005406 case VAR_DICT:
5407 dict_unref(varp->vval.v_dict);
5408 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005409 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005410#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005411 job_unref(varp->vval.v_job);
5412 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005413#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005414 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005416 channel_unref(varp->vval.v_channel);
5417 break;
5418#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005419 case VAR_NUMBER:
5420 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005421 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005423 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005424 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005425 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 vim_free(varp);
5428 }
5429}
5430
5431/*
5432 * Free the memory for a variable value and set the value to NULL or 0.
5433 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 if (varp != NULL)
5438 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005442 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005444 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005445 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005447 case VAR_PARTIAL:
5448 partial_unref(varp->vval.v_partial);
5449 varp->vval.v_partial = NULL;
5450 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005451 case VAR_BLOB:
5452 blob_unref(varp->vval.v_blob);
5453 varp->vval.v_blob = NULL;
5454 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 case VAR_LIST:
5456 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005457 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 case VAR_DICT:
5460 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005461 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005464 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005465 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 varp->vval.v_number = 0;
5467 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005468 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005470 varp->vval.v_float = 0.0;
5471 break;
5472#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005473 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005474#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475 job_unref(varp->vval.v_job);
5476 varp->vval.v_job = NULL;
5477#endif
5478 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005479 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005480#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005481 channel_unref(varp->vval.v_channel);
5482 varp->vval.v_channel = NULL;
5483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005488 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490}
5491
5492/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 * Set the value of a variable to NULL without freeing items.
5494 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497{
5498 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500}
5501
5502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 * Get the number value of a variable.
5504 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005505 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005506 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005507 * caller of incompatible types: it sets *denote to TRUE if "denote"
5508 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005510 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005511tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005513 int error = FALSE;
5514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005515 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516}
5517
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005518 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005519tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005520{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005521 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005526 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005527 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005528#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005529 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005530 break;
5531#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005533 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005534 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 break;
5536 case VAR_STRING:
5537 if (varp->vval.v_string != NULL)
5538 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005539 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005540 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005541 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005542 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005543 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005544 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005545 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005546 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005547 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005548 case VAR_SPECIAL:
5549 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005550 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005551#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005553 break;
5554#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005555 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005557 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005558 break;
5559#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005560 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005561 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005562 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005563 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005565 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005569 n = -1;
5570 else
5571 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573}
5574
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005575#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005576 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005577tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005578{
5579 switch (varp->v_type)
5580 {
5581 case VAR_NUMBER:
5582 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005583 case VAR_FLOAT:
5584 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005586 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005588 break;
5589 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005591 break;
5592 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005593 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005594 break;
5595 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005596 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005597 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005598 case VAR_BOOL:
5599 emsg(_("E362: Using a boolean value as a Float"));
5600 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005601 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005603 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005605# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005606 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005607 break;
5608# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005609 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005610# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005611 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005612 break;
5613# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005614 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005616 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005617 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005619 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005620 break;
5621 }
5622 return 0;
5623}
5624#endif
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Get the string value of a variable.
5628 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005629 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5630 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 * If the String variable has never been set, return an empty string.
5632 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005633 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005634 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005636 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005637tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 static char_u mybuf[NUMBUFLEN];
5640
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005641 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005644 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005645tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005647 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005648
5649 return res != NULL ? res : (char_u *)"";
5650}
5651
Bram Moolenaar7d647822014-04-05 21:28:56 +02005652/*
5653 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5654 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005655 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005656tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005657{
5658 static char_u mybuf[NUMBUFLEN];
5659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005661}
5662
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005663 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005664tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005665{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005668 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005669 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005670 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 return buf;
5672 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005673 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005674 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 break;
5676 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005677 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 break;
5679 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005680 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005682 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005683#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005684 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005685 break;
5686#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 case VAR_STRING:
5688 if (varp->vval.v_string != NULL)
5689 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005690 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005691 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005692 case VAR_SPECIAL:
5693 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5694 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005695 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005696 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005697 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005698 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005699#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005700 {
5701 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005702 char *status;
5703
5704 if (job == NULL)
5705 return (char_u *)"no process";
5706 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005707 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005708 : "run";
5709# ifdef UNIX
5710 vim_snprintf((char *)buf, NUMBUFLEN,
5711 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005712# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005713 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005714 "process %ld %s",
5715 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005716 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005717# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005718 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005719 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5720# endif
5721 return buf;
5722 }
5723#endif
5724 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005725 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005727 {
5728 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005729 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005730
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005731 if (channel == NULL)
5732 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5733 else
5734 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005735 "channel %d %s", channel->ch_id, status);
5736 return buf;
5737 }
5738#endif
5739 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005740 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005742 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746}
5747
5748/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005749 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5750 * string() on Dict, List, etc.
5751 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005752 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005753tv_stringify(typval_T *varp, char_u *buf)
5754{
5755 if (varp->v_type == VAR_LIST
5756 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005757 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005758 || varp->v_type == VAR_FUNC
5759 || varp->v_type == VAR_PARTIAL
5760 || varp->v_type == VAR_FLOAT)
5761 {
5762 typval_T tmp;
5763
5764 f_string(varp, &tmp);
5765 tv_get_string_buf(&tmp, buf);
5766 clear_tv(varp);
5767 *varp = tmp;
5768 return tmp.vval.v_string;
5769 }
5770 return tv_get_string_buf(varp, buf);
5771}
5772
5773/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005774 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5775 * Also give an error message, using "name" or _("name") when use_gettext is
5776 * TRUE.
5777 */
5778 static int
5779tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5780{
5781 int lock = 0;
5782
5783 switch (tv->v_type)
5784 {
5785 case VAR_BLOB:
5786 if (tv->vval.v_blob != NULL)
5787 lock = tv->vval.v_blob->bv_lock;
5788 break;
5789 case VAR_LIST:
5790 if (tv->vval.v_list != NULL)
5791 lock = tv->vval.v_list->lv_lock;
5792 break;
5793 case VAR_DICT:
5794 if (tv->vval.v_dict != NULL)
5795 lock = tv->vval.v_dict->dv_lock;
5796 break;
5797 default:
5798 break;
5799 }
5800 return var_check_lock(tv->v_lock, name, use_gettext)
5801 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5802}
5803
5804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005807 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005808 * It is OK for "from" and "to" to point to the same item. This is used to
5809 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005812copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005815 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 switch (from->v_type)
5817 {
5818 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005819 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005820 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 to->vval.v_number = from->vval.v_number;
5822 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005823 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005824#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005825 to->vval.v_float = from->vval.v_float;
5826 break;
5827#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005828 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005830 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005831 if (to->vval.v_job != NULL)
5832 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005833 break;
5834#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005835 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005836#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005837 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005838 if (to->vval.v_channel != NULL)
5839 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005840 break;
5841#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 case VAR_STRING:
5843 case VAR_FUNC:
5844 if (from->vval.v_string == NULL)
5845 to->vval.v_string = NULL;
5846 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005847 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005849 if (from->v_type == VAR_FUNC)
5850 func_ref(to->vval.v_string);
5851 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005853 case VAR_PARTIAL:
5854 if (from->vval.v_partial == NULL)
5855 to->vval.v_partial = NULL;
5856 else
5857 {
5858 to->vval.v_partial = from->vval.v_partial;
5859 ++to->vval.v_partial->pt_refcount;
5860 }
5861 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005862 case VAR_BLOB:
5863 if (from->vval.v_blob == NULL)
5864 to->vval.v_blob = NULL;
5865 else
5866 {
5867 to->vval.v_blob = from->vval.v_blob;
5868 ++to->vval.v_blob->bv_refcount;
5869 }
5870 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 case VAR_LIST:
5872 if (from->vval.v_list == NULL)
5873 to->vval.v_list = NULL;
5874 else
5875 {
5876 to->vval.v_list = from->vval.v_list;
5877 ++to->vval.v_list->lv_refcount;
5878 }
5879 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 case VAR_DICT:
5881 if (from->vval.v_dict == NULL)
5882 to->vval.v_dict = NULL;
5883 else
5884 {
5885 to->vval.v_dict = from->vval.v_dict;
5886 ++to->vval.v_dict->dv_refcount;
5887 }
5888 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005889 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005891 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 break;
5893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
5896/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897 * Make a copy of an item.
5898 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005899 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5900 * reference to an already copied list/dict can be used.
5901 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005902 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005904item_copy(
5905 typval_T *from,
5906 typval_T *to,
5907 int deep,
5908 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909{
5910 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005911 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005915 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005916 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917 }
5918 ++recurse;
5919
5920 switch (from->v_type)
5921 {
5922 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005923 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005924 case VAR_STRING:
5925 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005926 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005927 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005928 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005929 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005930 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 copy_tv(from, to);
5932 break;
5933 case VAR_LIST:
5934 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005936 if (from->vval.v_list == NULL)
5937 to->vval.v_list = NULL;
5938 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 to->vval.v_list = from->vval.v_list->lv_copylist;
5942 ++to->vval.v_list->lv_refcount;
5943 }
5944 else
5945 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5946 if (to->vval.v_list == NULL)
5947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005949 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005951 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952 case VAR_DICT:
5953 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005954 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005955 if (from->vval.v_dict == NULL)
5956 to->vval.v_dict = NULL;
5957 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5961 ++to->vval.v_dict->dv_refcount;
5962 }
5963 else
5964 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5965 if (to->vval.v_dict == NULL)
5966 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005967 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005968 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005970 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005971 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 }
5973 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005974 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005975}
5976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 void
5978echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5979{
5980 char_u *tofree;
5981 char_u numbuf[NUMBUFLEN];
5982 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5983
5984 if (*atstart)
5985 {
5986 *atstart = FALSE;
5987 // Call msg_start() after eval1(), evaluating the expression
5988 // may cause a message to appear.
5989 if (with_space)
5990 {
5991 // Mark the saved text as finishing the line, so that what
5992 // follows is displayed on a new line when scrolling back
5993 // at the more prompt.
5994 msg_sb_eol();
5995 msg_start();
5996 }
5997 }
5998 else if (with_space)
5999 msg_puts_attr(" ", echo_attr);
6000
6001 if (p != NULL)
6002 for ( ; *p != NUL && !got_int; ++p)
6003 {
6004 if (*p == '\n' || *p == '\r' || *p == TAB)
6005 {
6006 if (*p != TAB && *needclr)
6007 {
6008 // remove any text still there from the command
6009 msg_clr_eos();
6010 *needclr = FALSE;
6011 }
6012 msg_putchar_attr(*p, echo_attr);
6013 }
6014 else
6015 {
6016 if (has_mbyte)
6017 {
6018 int i = (*mb_ptr2len)(p);
6019
6020 (void)msg_outtrans_len_attr(p, i, echo_attr);
6021 p += i - 1;
6022 }
6023 else
6024 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6025 }
6026 }
6027 vim_free(tofree);
6028}
6029
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 * ":echo expr1 ..." print each argument separated with a space, add a
6032 * newline at the end.
6033 * ":echon expr1 ..." print each argument plain.
6034 */
6035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 char_u *p;
6041 int needclr = TRUE;
6042 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006043 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006044 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 if (eap->skip)
6047 ++emsg_skip;
6048 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
6049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 // If eval1() causes an error message the text from the command may
6051 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006052 need_clr_eos = needclr;
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
6057 /*
6058 * Report the invalid expression unless the expression evaluation
6059 * has been cancelled due to an aborting error, an interrupt, or an
6060 * exception.
6061 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006062 if (!aborting() && did_emsg == did_emsg_before
6063 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006064 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006065 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 break;
6067 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006068 need_clr_eos = FALSE;
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 arg = skipwhite(arg);
6075 }
6076 eap->nextcmd = check_nextcmd(arg);
6077
6078 if (eap->skip)
6079 --emsg_skip;
6080 else
6081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (needclr)
6084 msg_clr_eos();
6085 if (eap->cmdidx == CMD_echo)
6086 msg_end();
6087 }
6088}
6089
6090/*
6091 * ":echohl {name}".
6092 */
6093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006096 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097}
6098
6099/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006100 * Returns the :echo attribute
6101 */
6102 int
6103get_echo_attr(void)
6104{
6105 return echo_attr;
6106}
6107
6108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 * ":execute expr1 ..." execute the result of an expression.
6110 * ":echomsg expr1 ..." Print a message
6111 * ":echoerr expr1 ..." Print an error
6112 * Each gets spaces around each argument and a newline at the end for
6113 * echo commands
6114 */
6115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 int ret = OK;
6121 char_u *p;
6122 garray_T ga;
6123 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006124 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126 ga_init2(&ga, 1, 80);
6127
6128 if (eap->skip)
6129 ++emsg_skip;
6130 while (*arg != NUL && *arg != '|' && *arg != '\n')
6131 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006132 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6133 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 if (!eap->skip)
6137 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006138 char_u buf[NUMBUFLEN];
6139
6140 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006141 {
6142 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6143 {
6144 emsg(_(e_inval_string));
6145 p = NULL;
6146 }
6147 else
6148 p = tv_get_string_buf(&rettv, buf);
6149 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006150 else
6151 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006152 if (p == NULL)
6153 {
6154 clear_tv(&rettv);
6155 ret = FAIL;
6156 break;
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 len = (int)STRLEN(p);
6159 if (ga_grow(&ga, len + 2) == FAIL)
6160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 ret = FAIL;
6163 break;
6164 }
6165 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 ga.ga_len += len;
6169 }
6170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 arg = skipwhite(arg);
6173 }
6174
6175 if (ret != FAIL && ga.ga_data != NULL)
6176 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006177 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006179 // Mark the already saved text as finishing the line, so that what
6180 // follows is displayed on a new line when scrolling back at the
6181 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006182 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006183 }
6184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006186 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006187 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006188 out_flush();
6189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 else if (eap->cmdidx == CMD_echoerr)
6191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006194 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (!force_abort)
6196 did_emsg = save_did_emsg;
6197 }
6198 else if (eap->cmdidx == CMD_execute)
6199 do_cmdline((char_u *)ga.ga_data,
6200 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6201 }
6202
6203 ga_clear(&ga);
6204
6205 if (eap->skip)
6206 --emsg_skip;
6207
6208 eap->nextcmd = check_nextcmd(arg);
6209}
6210
6211/*
6212 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6213 * "arg" points to the "&" or '+' when called, to "option" when returning.
6214 * Returns NULL when no option name found. Otherwise pointer to the char
6215 * after the option name.
6216 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006217 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006218find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 char_u *p = *arg;
6221
6222 ++p;
6223 if (*p == 'g' && p[1] == ':')
6224 {
6225 *opt_flags = OPT_GLOBAL;
6226 p += 2;
6227 }
6228 else if (*p == 'l' && p[1] == ':')
6229 {
6230 *opt_flags = OPT_LOCAL;
6231 p += 2;
6232 }
6233 else
6234 *opt_flags = 0;
6235
6236 if (!ASCII_ISALPHA(*p))
6237 return NULL;
6238 *arg = p;
6239
6240 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 else
6243 while (ASCII_ISALPHA(*p))
6244 ++p;
6245 return p;
6246}
6247
6248/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006249 * Display script name where an item was last set.
6250 * Should only be invoked when 'verbose' is non-zero.
6251 */
6252 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006253last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006254{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006255 char_u *p;
6256
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006257 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006258 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006259 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006260 if (p != NULL)
6261 {
6262 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006263 msg_puts(_("\n\tLast set from "));
6264 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006265 if (script_ctx.sc_lnum > 0)
6266 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006267 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006268 msg_outnum((long)script_ctx.sc_lnum);
6269 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006270 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006271 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006272 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006273 }
6274}
6275
Bram Moolenaar61be3762019-03-19 23:04:17 +01006276/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006277 * Compare "typ1" and "typ2". Put the result in "typ1".
6278 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006279 int
6280typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006281 typval_T *typ1, // first operand
6282 typval_T *typ2, // second operand
6283 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006285{
6286 int i;
6287 varnumber_T n1, n2;
6288 char_u *s1, *s2;
6289 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006290 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006291
Bram Moolenaar31988702018-02-13 12:57:42 +01006292 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // For "is" a different type always means FALSE, for "notis"
6295 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006296 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006297 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006298 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6299 {
6300 if (type_is)
6301 {
6302 n1 = (typ1->v_type == typ2->v_type
6303 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006304 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006305 n1 = !n1;
6306 }
6307 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006308 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006309 {
6310 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006311 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006312 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006313 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006314 clear_tv(typ1);
6315 return FAIL;
6316 }
6317 else
6318 {
6319 // Compare two Blobs for being equal or unequal.
6320 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006321 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006322 n1 = !n1;
6323 }
6324 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006325 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6326 {
6327 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006328 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006329 n1 = (typ1->v_type == typ2->v_type
6330 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006331 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006332 n1 = !n1;
6333 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006334 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006335 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006336 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006337 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006338 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006339 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006340 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006341 clear_tv(typ1);
6342 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006343 }
6344 else
6345 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006347 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6348 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006349 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006350 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006351 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006352 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006353
6354 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6355 {
6356 if (type_is)
6357 {
6358 n1 = (typ1->v_type == typ2->v_type
6359 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006360 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006361 n1 = !n1;
6362 }
6363 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006364 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006365 {
6366 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006367 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006368 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006369 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006370 clear_tv(typ1);
6371 return FAIL;
6372 }
6373 else
6374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006375 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006376 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6377 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006378 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006379 n1 = !n1;
6380 }
6381 }
6382
6383 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6384 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6385 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006386 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6387 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006389 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006390 clear_tv(typ1);
6391 return FAIL;
6392 }
6393 if ((typ1->v_type == VAR_PARTIAL
6394 && typ1->vval.v_partial == NULL)
6395 || (typ2->v_type == VAR_PARTIAL
6396 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006397 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006398 n1 = FALSE;
6399 else if (type_is)
6400 {
6401 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006402 // strings are considered the same if their value is
6403 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006404 n1 = tv_equal(typ1, typ2, ic, FALSE);
6405 else if (typ1->v_type == VAR_PARTIAL
6406 && typ2->v_type == VAR_PARTIAL)
6407 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6408 else
6409 n1 = FALSE;
6410 }
6411 else
6412 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006413 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006414 n1 = !n1;
6415 }
6416
6417#ifdef FEAT_FLOAT
6418 /*
6419 * If one of the two variables is a float, compare as a float.
6420 * When using "=~" or "!~", always compare as string.
6421 */
6422 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006423 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006424 {
6425 float_T f1, f2;
6426
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006427 f1 = tv_get_float(typ1);
6428 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006429 n1 = FALSE;
6430 switch (type)
6431 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006432 case EXPR_IS:
6433 case EXPR_EQUAL: n1 = (f1 == f2); break;
6434 case EXPR_ISNOT:
6435 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6436 case EXPR_GREATER: n1 = (f1 > f2); break;
6437 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6438 case EXPR_SMALLER: n1 = (f1 < f2); break;
6439 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6440 case EXPR_UNKNOWN:
6441 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006443 }
6444 }
6445#endif
6446
6447 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006448 * If one of the two variables is a number, compare as a number.
6449 * When using "=~" or "!~", always compare as string.
6450 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006451 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006452 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006454 n1 = tv_get_number(typ1);
6455 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006456 switch (type)
6457 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006458 case EXPR_IS:
6459 case EXPR_EQUAL: n1 = (n1 == n2); break;
6460 case EXPR_ISNOT:
6461 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6462 case EXPR_GREATER: n1 = (n1 > n2); break;
6463 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6464 case EXPR_SMALLER: n1 = (n1 < n2); break;
6465 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6466 case EXPR_UNKNOWN:
6467 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006469 }
6470 }
6471 else
6472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006473 s1 = tv_get_string_buf(typ1, buf1);
6474 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006475 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006476 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6477 else
6478 i = 0;
6479 n1 = FALSE;
6480 switch (type)
6481 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006482 case EXPR_IS:
6483 case EXPR_EQUAL: n1 = (i == 0); break;
6484 case EXPR_ISNOT:
6485 case EXPR_NEQUAL: n1 = (i != 0); break;
6486 case EXPR_GREATER: n1 = (i > 0); break;
6487 case EXPR_GEQUAL: n1 = (i >= 0); break;
6488 case EXPR_SMALLER: n1 = (i < 0); break;
6489 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006490
Bram Moolenaar87396072019-12-31 22:36:18 +01006491 case EXPR_MATCH:
6492 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006493 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006494 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006495 n1 = !n1;
6496 break;
6497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006499 }
6500 }
6501 clear_tv(typ1);
6502 typ1->v_type = VAR_NUMBER;
6503 typ1->vval.v_number = n1;
6504
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006505 return OK;
6506}
6507
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006508 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006509typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006510{
6511 char_u *tofree;
6512 char_u numbuf[NUMBUFLEN];
6513 char_u *ret = NULL;
6514
6515 if (arg == NULL)
6516 return vim_strsave((char_u *)"(does not exist)");
6517 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006519 if (ret != NULL && tofree == NULL)
6520 ret = vim_strsave(ret);
6521 return ret;
6522}
6523
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006524#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
6526/*
6527 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006528 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 * "flags" can be "g" to do a global substitute.
6530 * Returns an allocated string, NULL for error.
6531 */
6532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006533do_string_sub(
6534 char_u *str,
6535 char_u *pat,
6536 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006537 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 int sublen;
6541 regmatch_T regmatch;
6542 int i;
6543 int do_all;
6544 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006545 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 garray_T ga;
6547 char_u *ret;
6548 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006549 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006553 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
6555 ga_init2(&ga, 1, 200);
6556
6557 do_all = (flags[0] == 'g');
6558
6559 regmatch.rm_ic = p_ic;
6560 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6561 if (regmatch.regprog != NULL)
6562 {
6563 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006564 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006568 if (regmatch.startp[0] == regmatch.endp[0])
6569 {
6570 if (zero_width == regmatch.startp[0])
6571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006573 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006574 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6575 (size_t)i);
6576 ga.ga_len += i;
6577 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006578 continue;
6579 }
6580 zero_width = regmatch.startp[0];
6581 }
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 /*
6584 * Get some space for a temporary buffer to do the substitution
6585 * into. It will contain:
6586 * - The text up to where the match is.
6587 * - The substituted text.
6588 * - The text after the match.
6589 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006590 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006591 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6593 {
6594 ga_clear(&ga);
6595 break;
6596 }
6597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 i = (int)(regmatch.startp[0] - tail);
6600 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006601 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006602 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 + ga.ga_len + i, TRUE, TRUE, FALSE);
6604 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006605 tail = regmatch.endp[0];
6606 if (*tail == NUL)
6607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (!do_all)
6609 break;
6610 }
6611
6612 if (ga.ga_data != NULL)
6613 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6614
Bram Moolenaar473de612013-06-08 18:19:48 +02006615 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617
6618 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6619 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006620 if (p_cpo == empty_option)
6621 p_cpo = save_cpo;
6622 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006624 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626 return ret;
6627}