blob: 3b789dadc674ab6813ac5d349c6ea262311df3e0 [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 Moolenaar4c683752020-04-05 21:38:23 +02001275 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 case VAR_DICT:
1278 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001279 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001280 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001281 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001282 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001283 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001284 break;
1285
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 case VAR_BLOB:
1287 if (*op != '+' || tv2->v_type != VAR_BLOB)
1288 break;
1289 // BLOB += BLOB
1290 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1291 {
1292 blob_T *b1 = tv1->vval.v_blob;
1293 blob_T *b2 = tv2->vval.v_blob;
1294 int i, len = blob_len(b2);
1295 for (i = 0; i < len; i++)
1296 ga_append(&b1->bv_ga, blob_get(b2, i));
1297 }
1298 return OK;
1299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 case VAR_LIST:
1301 if (*op != '+' || tv2->v_type != VAR_LIST)
1302 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001303 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1305 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1306 return OK;
1307
1308 case VAR_NUMBER:
1309 case VAR_STRING:
1310 if (tv2->v_type == VAR_LIST)
1311 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001312 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001313 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001314 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001315 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001316#ifdef FEAT_FLOAT
1317 if (tv2->v_type == VAR_FLOAT)
1318 {
1319 float_T f = n;
1320
Bram Moolenaarff697e62019-02-12 22:28:33 +01001321 if (*op == '%')
1322 break;
1323 switch (*op)
1324 {
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 case '/': f /= tv2->vval.v_float; break;
1329 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001330 clear_tv(tv1);
1331 tv1->v_type = VAR_FLOAT;
1332 tv1->vval.v_float = f;
1333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001335#endif
1336 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001337 switch (*op)
1338 {
1339 case '+': n += tv_get_number(tv2); break;
1340 case '-': n -= tv_get_number(tv2); break;
1341 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001342 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1343 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001345 clear_tv(tv1);
1346 tv1->v_type = VAR_NUMBER;
1347 tv1->vval.v_number = n;
1348 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 }
1350 else
1351 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001352 if (tv2->v_type == VAR_FLOAT)
1353 break;
1354
Bram Moolenaarff697e62019-02-12 22:28:33 +01001355 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001356 s = tv_get_string(tv1);
1357 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001358 clear_tv(tv1);
1359 tv1->v_type = VAR_STRING;
1360 tv1->vval.v_string = s;
1361 }
1362 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001363
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001364 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001365#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001366 {
1367 float_T f;
1368
Bram Moolenaarff697e62019-02-12 22:28:33 +01001369 if (*op == '%' || *op == '.'
1370 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001371 && tv2->v_type != VAR_NUMBER
1372 && tv2->v_type != VAR_STRING))
1373 break;
1374 if (tv2->v_type == VAR_FLOAT)
1375 f = tv2->vval.v_float;
1376 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001377 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001378 switch (*op)
1379 {
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 case '/': tv1->vval.v_float /= f; break;
1384 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001385 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001386#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001387 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 }
1389 }
1390
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001391 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392 return FAIL;
1393}
1394
1395/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001396 * Evaluate the expression used in a ":for var in expr" command.
1397 * "arg" points to "var".
1398 * Set "*errp" to TRUE for an error, FALSE otherwise;
1399 * Return a pointer that holds the info. Null when there is an error.
1400 */
1401 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_for_line(
1403 char_u *arg,
1404 int *errp,
1405 char_u **nextcmdp,
1406 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001409 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 typval_T tv;
1411 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001413 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001415 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416 if (fi == NULL)
1417 return NULL;
1418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 if (expr == NULL)
1421 return fi;
1422
1423 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001424 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427 return fi;
1428 }
1429
1430 if (skip)
1431 ++emsg_skip;
1432 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1433 {
1434 *errp = FALSE;
1435 if (!skip)
1436 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001437 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001438 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001439 l = tv.vval.v_list;
1440 if (l == NULL)
1441 {
1442 // a null list is like an empty list: do nothing
1443 clear_tv(&tv);
1444 }
1445 else
1446 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 // Need a real list here.
1448 range_list_materialize(l);
1449
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001450 // No need to increment the refcount, it's already set for
1451 // the list being used in "tv".
1452 fi->fi_list = l;
1453 list_add_watch(l, &fi->fi_lw);
1454 fi->fi_lw.lw_item = l->lv_first;
1455 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001456 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001458 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001459 fi->fi_bi = 0;
1460 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001462 typval_T btv;
1463
1464 // Make a copy, so that the iteration still works when the
1465 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001467 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001469 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001470 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471 else
1472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001473 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 }
1476 }
1477 }
1478 if (skip)
1479 --emsg_skip;
1480
1481 return fi;
1482}
1483
1484/*
1485 * Use the first item in a ":for" list. Advance to the next.
1486 * Assign the values to the variable (list). "arg" points to the first one.
1487 * Return TRUE when a valid item was found, FALSE when at end of list or
1488 * something wrong.
1489 */
1490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001493 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001494 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001495 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1496 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001497 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001499 if (fi->fi_blob != NULL)
1500 {
1501 typval_T tv;
1502
1503 if (fi->fi_bi >= blob_len(fi->fi_blob))
1504 return FALSE;
1505 tv.v_type = VAR_NUMBER;
1506 tv.v_lock = VAR_FIXED;
1507 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1508 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001509 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001510 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001511 }
1512
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513 item = fi->fi_lw.lw_item;
1514 if (item == NULL)
1515 result = FALSE;
1516 else
1517 {
1518 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001519 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001520 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 }
1522 return result;
1523}
1524
1525/*
1526 * Free the structure used to store info used by ":for".
1527 */
1528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530{
Bram Moolenaar33570922005-01-25 22:26:29 +00001531 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001533 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001534 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001536 list_unref(fi->fi_list);
1537 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001538 if (fi != NULL && fi->fi_blob != NULL)
1539 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001540 vim_free(fi);
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544set_context_for_expression(
1545 expand_T *xp,
1546 char_u *arg,
1547 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 int got_eq = FALSE;
1550 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001553 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 {
1555 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001558 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 {
1561 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001562 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001563 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564 break;
1565 }
1566 return;
1567 }
1568 }
1569 else
1570 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1571 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 while ((xp->xp_pattern = vim_strpbrk(arg,
1573 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1574 {
1575 c = *xp->xp_pattern;
1576 if (c == '&')
1577 {
1578 c = xp->xp_pattern[1];
1579 if (c == '&')
1580 {
1581 ++xp->xp_pattern;
1582 xp->xp_context = cmdidx != CMD_let || got_eq
1583 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1584 }
1585 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001588 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1589 xp->xp_pattern += 2;
1590
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 else if (c == '$')
1594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001595 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 xp->xp_context = EXPAND_ENV_VARS;
1597 }
1598 else if (c == '=')
1599 {
1600 got_eq = TRUE;
1601 xp->xp_context = EXPAND_EXPRESSION;
1602 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001603 else if (c == '#'
1604 && xp->xp_context == EXPAND_EXPRESSION)
1605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001606 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001607 break;
1608 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001609 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 && xp->xp_context == EXPAND_FUNCTIONS
1611 && vim_strchr(xp->xp_pattern, '(') == NULL)
1612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001613 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 break;
1615 }
1616 else if (cmdidx != CMD_let || got_eq)
1617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001618 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1621 if (c == '\\' && xp->xp_pattern[1] != NUL)
1622 ++xp->xp_pattern;
1623 xp->xp_context = EXPAND_NOTHING;
1624 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001625 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001627 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1629 /* skip */ ;
1630 xp->xp_context = EXPAND_NOTHING;
1631 }
1632 else if (c == '|')
1633 {
1634 if (xp->xp_pattern[1] == '|')
1635 {
1636 ++xp->xp_pattern;
1637 xp->xp_context = EXPAND_EXPRESSION;
1638 }
1639 else
1640 xp->xp_context = EXPAND_COMMANDS;
1641 }
1642 else
1643 xp->xp_context = EXPAND_EXPRESSION;
1644 }
1645 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001646 // Doesn't look like something valid, expand as an expression
1647 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 arg = xp->xp_pattern;
1650 if (*arg != NUL)
1651 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1652 /* skip */ ;
1653 }
1654 xp->xp_pattern = arg;
1655}
1656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001658 * Return TRUE if "pat" matches "text".
1659 * Does not use 'cpo' and always uses 'magic'.
1660 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001661 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001662pattern_match(char_u *pat, char_u *text, int ic)
1663{
1664 int matches = FALSE;
1665 char_u *save_cpo;
1666 regmatch_T regmatch;
1667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001668 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001669 save_cpo = p_cpo;
1670 p_cpo = (char_u *)"";
1671 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1672 if (regmatch.regprog != NULL)
1673 {
1674 regmatch.rm_ic = ic;
1675 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1676 vim_regfree(regmatch.regprog);
1677 }
1678 p_cpo = save_cpo;
1679 return matches;
1680}
1681
1682/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001683 * Handle a name followed by "(". Both for just "name(arg)" and for
1684 * "expr->name(arg)".
1685 * Returns OK or FAIL.
1686 */
1687 static int
1688eval_func(
1689 char_u **arg, // points to "(", will be advanced
1690 char_u *name,
1691 int name_len,
1692 typval_T *rettv,
1693 int evaluate,
1694 typval_T *basetv) // "expr" for "expr->name(arg)"
1695{
1696 char_u *s = name;
1697 int len = name_len;
1698 partial_T *partial;
1699 int ret = OK;
1700
1701 if (!evaluate)
1702 check_vars(s, len);
1703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001704 // If "s" is the name of a variable of type VAR_FUNC
1705 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001706 s = deref_func_name(s, &len, &partial, !evaluate);
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Need to make a copy, in case evaluating the arguments makes
1709 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001710 s = vim_strsave(s);
1711 if (s == NULL)
1712 ret = FAIL;
1713 else
1714 {
1715 funcexe_T funcexe;
1716
1717 // Invoke the function.
1718 vim_memset(&funcexe, 0, sizeof(funcexe));
1719 funcexe.firstline = curwin->w_cursor.lnum;
1720 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001721 funcexe.evaluate = evaluate;
1722 funcexe.partial = partial;
1723 funcexe.basetv = basetv;
1724 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1725 }
1726 vim_free(s);
1727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 // If evaluate is FALSE rettv->v_type was not set in
1729 // get_func_tv, but it's needed in handle_subscript() to parse
1730 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001731 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1732 {
1733 rettv->vval.v_string = NULL;
1734 rettv->v_type = VAR_FUNC;
1735 }
1736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001737 // Stop the expression evaluation when immediately
1738 // aborting on error, or when an interrupt occurred or
1739 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001740 if (evaluate && aborting())
1741 {
1742 if (ret == OK)
1743 clear_tv(rettv);
1744 ret = FAIL;
1745 }
1746 return ret;
1747}
1748
1749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1753 */
1754
1755/*
1756 * Handle zero level expression.
1757 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001759 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 * Return OK or FAIL.
1761 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763eval0(
1764 char_u *arg,
1765 typval_T *rettv,
1766 char_u **nextcmd,
1767 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 int ret;
1770 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001771 int did_emsg_before = did_emsg;
1772 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (ret == FAIL || !ends_excmd(*p))
1777 {
1778 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Report the invalid expression unless the expression evaluation has
1782 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001783 * exception, or we already gave a more specific error.
1784 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001786 if (!aborting() && did_emsg == did_emsg_before
1787 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001788 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 ret = FAIL;
1790 }
1791 if (nextcmd != NULL)
1792 *nextcmd = check_nextcmd(p);
1793
1794 return ret;
1795}
1796
1797/*
1798 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001799 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *
1801 * "arg" must point to the first non-white of the expression.
1802 * "arg" is advanced to the next non-white after the recognized expression.
1803 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001804 * Note: "rettv.v_lock" is not set.
1805 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 * Return OK or FAIL.
1807 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001808 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 /*
1815 * Get the first variable.
1816 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 return FAIL;
1819
1820 if ((*arg)[0] == '?')
1821 {
1822 result = FALSE;
1823 if (evaluate)
1824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001825 int error = FALSE;
1826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001827 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001830 if (error)
1831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833
1834 /*
1835 * Get the second variable.
1836 */
1837 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001838 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 return FAIL;
1840
1841 /*
1842 * Check for the ":".
1843 */
1844 if ((*arg)[0] != ':')
1845 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 return FAIL;
1850 }
1851
1852 /*
1853 * Get the third variable.
1854 */
1855 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001856 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 return FAIL;
1861 }
1862 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865
1866 return OK;
1867}
1868
1869/*
1870 * Handle first level expression:
1871 * expr2 || expr2 || expr2 logical OR
1872 *
1873 * "arg" must point to the first non-white of the expression.
1874 * "arg" is advanced to the next non-white after the recognized expression.
1875 *
1876 * Return OK or FAIL.
1877 */
1878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001879eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 long result;
1883 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 /*
1887 * Get the first variable.
1888 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 return FAIL;
1891
1892 /*
1893 * Repeat until there is no following "||".
1894 */
1895 first = TRUE;
1896 result = FALSE;
1897 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1898 {
1899 if (evaluate && first)
1900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001901 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001904 if (error)
1905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 first = FALSE;
1907 }
1908
1909 /*
1910 * Get the second variable.
1911 */
1912 *arg = skipwhite(*arg + 2);
1913 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1914 return FAIL;
1915
1916 /*
1917 * Compute the result.
1918 */
1919 if (evaluate && !result)
1920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001921 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001924 if (error)
1925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
1927 if (evaluate)
1928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 rettv->v_type = VAR_NUMBER;
1930 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 }
1933
1934 return OK;
1935}
1936
1937/*
1938 * Handle second level expression:
1939 * expr3 && expr3 && expr3 logical AND
1940 *
1941 * "arg" must point to the first non-white of the expression.
1942 * "arg" is advanced to the next non-white after the recognized expression.
1943 *
1944 * Return OK or FAIL.
1945 */
1946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 long result;
1951 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001952 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 /*
1955 * Get the first variable.
1956 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 return FAIL;
1959
1960 /*
1961 * Repeat until there is no following "&&".
1962 */
1963 first = TRUE;
1964 result = TRUE;
1965 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1966 {
1967 if (evaluate && first)
1968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001969 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001972 if (error)
1973 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 first = FALSE;
1975 }
1976
1977 /*
1978 * Get the second variable.
1979 */
1980 *arg = skipwhite(*arg + 2);
1981 if (eval4(arg, &var2, evaluate && result) == FAIL)
1982 return FAIL;
1983
1984 /*
1985 * Compute the result.
1986 */
1987 if (evaluate && result)
1988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001989 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 if (error)
1993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 if (evaluate)
1996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 rettv->v_type = VAR_NUMBER;
1998 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Handle third level expression:
2007 * var1 == var2
2008 * var1 =~ var2
2009 * var1 != var2
2010 * var1 !~ var2
2011 * var1 > var2
2012 * var1 >= var2
2013 * var1 < var2
2014 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002015 * var1 is var2
2016 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 *
2018 * "arg" must point to the first non-white of the expression.
2019 * "arg" is advanced to the next non-white after the recognized expression.
2020 *
2021 * Return OK or FAIL.
2022 */
2023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
Bram Moolenaar33570922005-01-25 22:26:29 +00002026 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 char_u *p;
2028 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002029 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 /*
2034 * Get the first variable.
2035 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return FAIL;
2038
2039 p = *arg;
2040 switch (p[0])
2041 {
2042 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002043 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002045 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
2047 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002048 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002050 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 break;
2052 case '>': if (p[1] != '=')
2053 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002054 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 len = 1;
2056 }
2057 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002058 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
2060 case '<': if (p[1] != '=')
2061 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002062 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 len = 1;
2064 }
2065 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002066 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002068 case 'i': if (p[1] == 's')
2069 {
2070 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2071 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002072 i = p[len];
2073 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002074 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002075 }
2076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078
2079 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002080 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002082 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002084 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (p[len] == '?')
2086 {
2087 ic = TRUE;
2088 ++len;
2089 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002090 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else if (p[len] == '#')
2092 {
2093 ic = FALSE;
2094 ++len;
2095 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002096 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 else
2098 ic = p_ic;
2099
2100 /*
2101 * Get the second variable.
2102 */
2103 *arg = skipwhite(p + len);
2104 if (eval5(arg, &var2, evaluate) == FAIL)
2105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return FAIL;
2108 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002109 if (evaluate)
2110 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002111 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002112
2113 clear_tv(&var2);
2114 return ret;
2115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
2118 return OK;
2119}
2120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 void
2122eval_addblob(typval_T *tv1, typval_T *tv2)
2123{
2124 blob_T *b1 = tv1->vval.v_blob;
2125 blob_T *b2 = tv2->vval.v_blob;
2126 blob_T *b = blob_alloc();
2127 int i;
2128
2129 if (b != NULL)
2130 {
2131 for (i = 0; i < blob_len(b1); i++)
2132 ga_append(&b->bv_ga, blob_get(b1, i));
2133 for (i = 0; i < blob_len(b2); i++)
2134 ga_append(&b->bv_ga, blob_get(b2, i));
2135
2136 clear_tv(tv1);
2137 rettv_blob_set(tv1, b);
2138 }
2139}
2140
2141 int
2142eval_addlist(typval_T *tv1, typval_T *tv2)
2143{
2144 typval_T var3;
2145
2146 // concatenate Lists
2147 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2148 {
2149 clear_tv(tv1);
2150 clear_tv(tv2);
2151 return FAIL;
2152 }
2153 clear_tv(tv1);
2154 *tv1 = var3;
2155 return OK;
2156}
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158/*
2159 * Handle fourth level expression:
2160 * + number addition
2161 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002162 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002163 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 *
2165 * "arg" must point to the first non-white of the expression.
2166 * "arg" is advanced to the next non-white after the recognized expression.
2167 *
2168 * Return OK or FAIL.
2169 */
2170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar33570922005-01-25 22:26:29 +00002173 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002175 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002176#ifdef FEAT_FLOAT
2177 float_T f1 = 0, f2 = 0;
2178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 char_u *s1, *s2;
2180 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2181 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002182 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
2185 * Get the first variable.
2186 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002187 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 return FAIL;
2189
2190 /*
2191 * Repeat computing, until no '+', '-' or '.' is following.
2192 */
2193 for (;;)
2194 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002195 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002197 concat = op == '.'
2198 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2199 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 break;
2201
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002202 if ((op != '+' || (rettv->v_type != VAR_LIST
2203 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002204#ifdef FEAT_FLOAT
2205 && (op == '.' || rettv->v_type != VAR_FLOAT)
2206#endif
2207 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002209 // For "list + ...", an illegal use of the first operand as
2210 // a number cannot be determined before evaluating the 2nd
2211 // operand: if this is also a list, all is ok.
2212 // For "something . ...", "something - ..." or "non-list + ...",
2213 // we know that the first operand needs to be a string or number
2214 // without evaluating the 2nd operand. So check before to avoid
2215 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002216 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002217 {
2218 clear_tv(rettv);
2219 return FAIL;
2220 }
2221 }
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 /*
2224 * Get the second variable.
2225 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002226 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2227 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002229 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return FAIL;
2233 }
2234
2235 if (evaluate)
2236 {
2237 /*
2238 * Compute the result.
2239 */
2240 if (op == '.')
2241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002242 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002243 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002244 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002245 {
2246 clear_tv(rettv);
2247 clear_tv(&var2);
2248 return FAIL;
2249 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 clear_tv(rettv);
2252 rettv->v_type = VAR_STRING;
2253 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002255 else if (op == '+' && rettv->v_type == VAR_BLOB
2256 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002258 else if (op == '+' && rettv->v_type == VAR_LIST
2259 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002260 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002262 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 else
2265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 int error = FALSE;
2267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002268#ifdef FEAT_FLOAT
2269 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002271 f1 = rettv->vval.v_float;
2272 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002273 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002274 else
2275#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002278 if (error)
2279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002280 // This can only happen for "list + non-list". For
2281 // "non-list + ..." or "something - ...", we returned
2282 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002283 clear_tv(rettv);
2284 return FAIL;
2285 }
2286#ifdef FEAT_FLOAT
2287 if (var2.v_type == VAR_FLOAT)
2288 f1 = n1;
2289#endif
2290 }
2291#ifdef FEAT_FLOAT
2292 if (var2.v_type == VAR_FLOAT)
2293 {
2294 f2 = var2.vval.v_float;
2295 n2 = 0;
2296 }
2297 else
2298#endif
2299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002300 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002301 if (error)
2302 {
2303 clear_tv(rettv);
2304 clear_tv(&var2);
2305 return FAIL;
2306 }
2307#ifdef FEAT_FLOAT
2308 if (rettv->v_type == VAR_FLOAT)
2309 f2 = n2;
2310#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002313
2314#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002315 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002316 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2317 {
2318 if (op == '+')
2319 f1 = f1 + f2;
2320 else
2321 f1 = f1 - f2;
2322 rettv->v_type = VAR_FLOAT;
2323 rettv->vval.v_float = f1;
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002326#endif
2327 {
2328 if (op == '+')
2329 n1 = n1 + n2;
2330 else
2331 n1 = n1 - n2;
2332 rettv->v_type = VAR_NUMBER;
2333 rettv->vval.v_number = n1;
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 }
2339 return OK;
2340}
2341
2342/*
2343 * Handle fifth level expression:
2344 * * number multiplication
2345 * / number division
2346 * % number modulo
2347 *
2348 * "arg" must point to the first non-white of the expression.
2349 * "arg" is advanced to the next non-white after the recognized expression.
2350 *
2351 * Return OK or FAIL.
2352 */
2353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002354eval6(
2355 char_u **arg,
2356 typval_T *rettv,
2357 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar33570922005-01-25 22:26:29 +00002360 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002362 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002363#ifdef FEAT_FLOAT
2364 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002365 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002366#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002367 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 /*
2370 * Get the first variable.
2371 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002372 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 return FAIL;
2374
2375 /*
2376 * Repeat computing, until no '*', '/' or '%' is following.
2377 */
2378 for (;;)
2379 {
2380 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002381 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 break;
2383
2384 if (evaluate)
2385 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002386#ifdef FEAT_FLOAT
2387 if (rettv->v_type == VAR_FLOAT)
2388 {
2389 f1 = rettv->vval.v_float;
2390 use_float = TRUE;
2391 n1 = 0;
2392 }
2393 else
2394#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002395 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 if (error)
2398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 else
2401 n1 = 0;
2402
2403 /*
2404 * Get the second variable.
2405 */
2406 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002407 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return FAIL;
2409
2410 if (evaluate)
2411 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002412#ifdef FEAT_FLOAT
2413 if (var2.v_type == VAR_FLOAT)
2414 {
2415 if (!use_float)
2416 {
2417 f1 = n1;
2418 use_float = TRUE;
2419 }
2420 f2 = var2.vval.v_float;
2421 n2 = 0;
2422 }
2423 else
2424#endif
2425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002426 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002427 clear_tv(&var2);
2428 if (error)
2429 return FAIL;
2430#ifdef FEAT_FLOAT
2431 if (use_float)
2432 f2 = n2;
2433#endif
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /*
2437 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002438 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002440#ifdef FEAT_FLOAT
2441 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443 if (op == '*')
2444 f1 = f1 * f2;
2445 else if (op == '/')
2446 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002447# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002449 if (f2 == 0.0)
2450 {
2451 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002452 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002453 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002454 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002455 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002456 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002457 }
2458 else
2459 f1 = f1 / f2;
2460# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002461 // We rely on the floating point library to handle divide
2462 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002463 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002464# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002469 return FAIL;
2470 }
2471 rettv->v_type = VAR_FLOAT;
2472 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002477 if (op == '*')
2478 n1 = n1 * n2;
2479 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002480 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002482 n1 = num_modulus(n1, n2);
2483
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002484 rettv->v_type = VAR_NUMBER;
2485 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488 }
2489
2490 return OK;
2491}
2492
2493/*
2494 * Handle sixth level expression:
2495 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002496 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002497 * "string" string constant
2498 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * &option-name option value
2500 * @r register contents
2501 * identifier variable value
2502 * function() function call
2503 * $VAR environment variable
2504 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002505 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002507 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002508 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 *
2510 * Also handle:
2511 * ! in front logical NOT
2512 * - in front unary minus
2513 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 * trailing [] subscript in String or List
2515 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002516 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 *
2518 * "arg" must point to the first non-white of the expression.
2519 * "arg" is advanced to the next non-white after the recognized expression.
2520 *
2521 * Return OK or FAIL.
2522 */
2523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524eval7(
2525 char_u **arg,
2526 typval_T *rettv,
2527 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 int len;
2531 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 char_u *start_leader, *end_leader;
2533 int ret = OK;
2534 char_u *alias;
2535
2536 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002538 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002543 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
2545 start_leader = *arg;
2546 while (**arg == '!' || **arg == '-' || **arg == '+')
2547 *arg = skipwhite(*arg + 1);
2548 end_leader = *arg;
2549
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002550 if (**arg == '.' && (!isdigit(*(*arg + 1))
2551#ifdef FEAT_FLOAT
2552 || current_sctx.sc_version < 2
2553#endif
2554 ))
2555 {
2556 semsg(_(e_invexpr2), *arg);
2557 ++*arg;
2558 return FAIL;
2559 }
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 switch (**arg)
2562 {
2563 /*
2564 * Number constant.
2565 */
2566 case '0':
2567 case '1':
2568 case '2':
2569 case '3':
2570 case '4':
2571 case '5':
2572 case '6':
2573 case '7':
2574 case '8':
2575 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 break;
2578
2579 /*
2580 * String constant: "string".
2581 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 break;
2584
2585 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002589 break;
2590
2591 /*
2592 * List: [expr, expr]
2593 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596
2597 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002598 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002599 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002600 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002601 {
2602 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002603 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002604 }
2605 else
2606 ret = NOTDONE;
2607 break;
2608
2609 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002610 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002611 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002613 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2614 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002615 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 break;
2617
2618 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002619 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002621 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 break;
2623
2624 /*
2625 * Environment variable: $VAR.
2626 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 break;
2629
2630 /*
2631 * Register contents: @r.
2632 */
2633 case '@': ++*arg;
2634 if (evaluate)
2635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002637 rettv->vval.v_string = get_reg_contents(**arg,
2638 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 if (**arg != NUL)
2641 ++*arg;
2642 break;
2643
2644 /*
2645 * nested expression: (expression).
2646 */
2647 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002648 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (**arg == ')')
2650 ++*arg;
2651 else if (ret == OK)
2652 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ret = FAIL;
2656 }
2657 break;
2658
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 break;
2661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662
2663 if (ret == NOTDONE)
2664 {
2665 /*
2666 * Must be a variable or function name.
2667 * Can also be a curly-braces kind of name: {expr}.
2668 */
2669 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002670 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (alias != NULL)
2672 s = alias;
2673
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002674 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 ret = FAIL;
2676 else
2677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002678 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002679 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002681 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002683 {
2684 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002688 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 *arg = skipwhite(*arg);
2692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002693 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2694 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002695 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002696 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2697 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /*
2700 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2701 */
2702 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002703 ret = eval7_leader(rettv, start_leader, &end_leader);
2704 return ret;
2705}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002706
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002707/*
2708 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2709 * Adjusts "end_leaderp" until it is at "start_leader".
2710 */
2711 static int
2712eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2713{
2714 char_u *end_leader = *end_leaderp;
2715 int ret = OK;
2716 int error = FALSE;
2717 varnumber_T val = 0;
2718#ifdef FEAT_FLOAT
2719 float_T f = 0.0;
2720
2721 if (rettv->v_type == VAR_FLOAT)
2722 f = rettv->vval.v_float;
2723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002725 val = tv_get_number_chk(rettv, &error);
2726 if (error)
2727 {
2728 clear_tv(rettv);
2729 ret = FAIL;
2730 }
2731 else
2732 {
2733 while (end_leader > start_leader)
2734 {
2735 --end_leader;
2736 if (*end_leader == '!')
2737 {
2738#ifdef FEAT_FLOAT
2739 if (rettv->v_type == VAR_FLOAT)
2740 f = !f;
2741 else
2742#endif
2743 val = !val;
2744 }
2745 else if (*end_leader == '-')
2746 {
2747#ifdef FEAT_FLOAT
2748 if (rettv->v_type == VAR_FLOAT)
2749 f = -f;
2750 else
2751#endif
2752 val = -val;
2753 }
2754 }
2755#ifdef FEAT_FLOAT
2756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002758 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002759 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002764 clear_tv(rettv);
2765 rettv->v_type = VAR_NUMBER;
2766 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002769 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 return ret;
2771}
2772
2773/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002774 * Call the function referred to in "rettv".
2775 */
2776 static int
2777call_func_rettv(
2778 char_u **arg,
2779 typval_T *rettv,
2780 int evaluate,
2781 dict_T *selfdict,
2782 typval_T *basetv)
2783{
2784 partial_T *pt = NULL;
2785 funcexe_T funcexe;
2786 typval_T functv;
2787 char_u *s;
2788 int ret;
2789
2790 // need to copy the funcref so that we can clear rettv
2791 if (evaluate)
2792 {
2793 functv = *rettv;
2794 rettv->v_type = VAR_UNKNOWN;
2795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002796 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002797 if (functv.v_type == VAR_PARTIAL)
2798 {
2799 pt = functv.vval.v_partial;
2800 s = partial_name(pt);
2801 }
2802 else
2803 s = functv.vval.v_string;
2804 }
2805 else
2806 s = (char_u *)"";
2807
2808 vim_memset(&funcexe, 0, sizeof(funcexe));
2809 funcexe.firstline = curwin->w_cursor.lnum;
2810 funcexe.lastline = curwin->w_cursor.lnum;
2811 funcexe.evaluate = evaluate;
2812 funcexe.partial = pt;
2813 funcexe.selfdict = selfdict;
2814 funcexe.basetv = basetv;
2815 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002817 // Clear the funcref afterwards, so that deleting it while
2818 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002819 if (evaluate)
2820 clear_tv(&functv);
2821
2822 return ret;
2823}
2824
2825/*
2826 * Evaluate "->method()".
2827 * "*arg" points to the '-'.
2828 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2829 */
2830 static int
2831eval_lambda(
2832 char_u **arg,
2833 typval_T *rettv,
2834 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002836{
2837 typval_T base = *rettv;
2838 int ret;
2839
2840 // Skip over the ->.
2841 *arg += 2;
2842 rettv->v_type = VAR_UNKNOWN;
2843
2844 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002845 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002846 return FAIL;
2847 else if (**arg != '(')
2848 {
2849 if (verbose)
2850 {
2851 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002852 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002853 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002855 }
2856 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002857 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002858 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002859 else
2860 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2861
2862 // Clear the funcref afterwards, so that deleting it while
2863 // evaluating the arguments is possible (see test55).
2864 if (evaluate)
2865 clear_tv(&base);
2866
2867 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002868}
2869
2870/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002871 * Evaluate "->method()".
2872 * "*arg" points to the '-'.
2873 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2874 */
2875 static int
2876eval_method(
2877 char_u **arg,
2878 typval_T *rettv,
2879 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002880 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002881{
2882 char_u *name;
2883 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002884 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002885 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002886 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002887
2888 // Skip over the ->.
2889 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002890 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002891
Bram Moolenaarac92e252019-08-03 21:58:38 +02002892 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002893 len = get_name_len(arg, &alias, evaluate, TRUE);
2894 if (alias != NULL)
2895 name = alias;
2896
2897 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002898 {
2899 if (verbose)
2900 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002901 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002902 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002903 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002904 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905 if (**arg != '(')
2906 {
2907 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002909 ret = FAIL;
2910 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002911 else if (VIM_ISWHITE((*arg)[-1]))
2912 {
2913 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002914 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002915 ret = FAIL;
2916 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002917 else
2918 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002919 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002920
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002921 // Clear the funcref afterwards, so that deleting it while
2922 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002923 if (evaluate)
2924 clear_tv(&base);
2925
Bram Moolenaarac92e252019-08-03 21:58:38 +02002926 return ret;
2927}
2928
2929/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002930 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2931 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002932 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2933 */
2934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935eval_index(
2936 char_u **arg,
2937 typval_T *rettv,
2938 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002939 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002940{
2941 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002943 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002944 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002945 long len = -1;
2946 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002947 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002949
Bram Moolenaara03f2332016-02-06 18:09:59 +01002950 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002952 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002953 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002954 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002956 return FAIL;
2957 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002958#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002959 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002960 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002961 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002962#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002963 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002964 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002965 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002966 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002967 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002969 return FAIL;
2970 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002971 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002973 if (evaluate)
2974 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002975 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976
2977 case VAR_STRING:
2978 case VAR_NUMBER:
2979 case VAR_LIST:
2980 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002981 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002982 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002983 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002984
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002985 init_tv(&var1);
2986 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002987 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002989 /*
2990 * dict.name
2991 */
2992 key = *arg + 1;
2993 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2994 ;
2995 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002996 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002998 }
2999 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 /*
3002 * something[idx]
3003 *
3004 * Get the (first) variable from inside the [].
3005 */
3006 *arg = skipwhite(*arg + 1);
3007 if (**arg == ':')
3008 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003009 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003010 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003011 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003012 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 clear_tv(&var1);
3015 return FAIL;
3016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003017
3018 /*
3019 * Get the second variable from inside the [:].
3020 */
3021 if (**arg == ':')
3022 {
3023 range = TRUE;
3024 *arg = skipwhite(*arg + 1);
3025 if (**arg == ']')
3026 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003027 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 if (!empty1)
3030 clear_tv(&var1);
3031 return FAIL;
3032 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003033 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003036 if (!empty1)
3037 clear_tv(&var1);
3038 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003039 return FAIL;
3040 }
3041 }
3042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003043 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003044 if (**arg != ']')
3045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003046 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003047 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048 clear_tv(&var1);
3049 if (range)
3050 clear_tv(&var2);
3051 return FAIL;
3052 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003053 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003054 }
3055
3056 if (evaluate)
3057 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003058 n1 = 0;
3059 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003061 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003063 }
3064 if (range)
3065 {
3066 if (empty2)
3067 n2 = -1;
3068 else
3069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003070 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003072 }
3073 }
3074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003077 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003078 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003080 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003081 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003082 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003083 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003084 case VAR_SPECIAL:
3085 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003086 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003087 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003088
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003089 case VAR_NUMBER:
3090 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003091 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003092 len = (long)STRLEN(s);
3093 if (range)
3094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003095 // The resulting variable is a substring. If the indexes
3096 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003097 if (n1 < 0)
3098 {
3099 n1 = len + n1;
3100 if (n1 < 0)
3101 n1 = 0;
3102 }
3103 if (n2 < 0)
3104 n2 = len + n2;
3105 else if (n2 >= len)
3106 n2 = len;
3107 if (n1 >= len || n2 < 0 || n1 > n2)
3108 s = NULL;
3109 else
3110 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3111 }
3112 else
3113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 // The resulting variable is a string of a single
3115 // character. If the index is too big or negative the
3116 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 if (n1 >= len || n1 < 0)
3118 s = NULL;
3119 else
3120 s = vim_strnsave(s + n1, 1);
3121 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
3123 rettv->v_type = VAR_STRING;
3124 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003125 break;
3126
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003127 case VAR_BLOB:
3128 len = blob_len(rettv->vval.v_blob);
3129 if (range)
3130 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003131 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003132 // are out of range the result is empty.
3133 if (n1 < 0)
3134 {
3135 n1 = len + n1;
3136 if (n1 < 0)
3137 n1 = 0;
3138 }
3139 if (n2 < 0)
3140 n2 = len + n2;
3141 else if (n2 >= len)
3142 n2 = len - 1;
3143 if (n1 >= len || n2 < 0 || n1 > n2)
3144 {
3145 clear_tv(rettv);
3146 rettv->v_type = VAR_BLOB;
3147 rettv->vval.v_blob = NULL;
3148 }
3149 else
3150 {
3151 blob_T *blob = blob_alloc();
3152
3153 if (blob != NULL)
3154 {
3155 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3156 {
3157 blob_free(blob);
3158 return FAIL;
3159 }
3160 blob->bv_ga.ga_len = n2 - n1 + 1;
3161 for (i = n1; i <= n2; i++)
3162 blob_set(blob, i - n1,
3163 blob_get(rettv->vval.v_blob, i));
3164
3165 clear_tv(rettv);
3166 rettv_blob_set(rettv, blob);
3167 }
3168 }
3169 }
3170 else
3171 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003172 // The resulting variable is a byte value.
3173 // If the index is too big or negative that is an error.
3174 if (n1 < 0)
3175 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003176 if (n1 < len && n1 >= 0)
3177 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003178 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003179
3180 clear_tv(rettv);
3181 rettv->v_type = VAR_NUMBER;
3182 rettv->vval.v_number = v;
3183 }
3184 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003185 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003186 }
3187 break;
3188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003189 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003190 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003191 if (n1 < 0)
3192 n1 = len + n1;
3193 if (!empty1 && (n1 < 0 || n1 >= len))
3194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 // For a range we allow invalid values and return an empty
3196 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003197 if (!range)
3198 {
3199 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003201 return FAIL;
3202 }
3203 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003204 }
3205 if (range)
3206 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 list_T *l;
3208 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003209
3210 if (n2 < 0)
3211 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003212 else if (n2 >= len)
3213 n2 = len - 1;
3214 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003215 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003216 l = list_alloc();
3217 if (l == NULL)
3218 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003219 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003220 n1 <= n2; ++n1)
3221 {
3222 if (list_append_tv(l, &item->li_tv) == FAIL)
3223 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003224 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003225 return FAIL;
3226 }
3227 item = item->li_next;
3228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003230 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003231 }
3232 else
3233 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003234 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 clear_tv(rettv);
3236 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003237 }
3238 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003239
3240 case VAR_DICT:
3241 if (range)
3242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003243 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003244 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003245 if (len == -1)
3246 clear_tv(&var1);
3247 return FAIL;
3248 }
3249 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003251
3252 if (len == -1)
3253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003254 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003255 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003257 clear_tv(&var1);
3258 return FAIL;
3259 }
3260 }
3261
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003262 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003264 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266 if (len == -1)
3267 clear_tv(&var1);
3268 if (item == NULL)
3269 return FAIL;
3270
3271 copy_tv(&item->di_tv, &var1);
3272 clear_tv(rettv);
3273 *rettv = var1;
3274 }
3275 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003276 }
3277 }
3278
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 return OK;
3280}
3281
3282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 * Get an option value.
3284 * "arg" points to the '&' or '+' before the option name.
3285 * "arg" is advanced to character after the option name.
3286 * Return OK or FAIL.
3287 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003289get_option_tv(
3290 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 char_u *option_end;
3295 long numval;
3296 char_u *stringval;
3297 int opt_type;
3298 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003299 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 int ret = OK;
3301 int opt_flags;
3302
3303 /*
3304 * Isolate the option name and find its value.
3305 */
3306 option_end = find_option_end(arg, &opt_flags);
3307 if (option_end == NULL)
3308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003309 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003310 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return FAIL;
3312 }
3313
3314 if (!evaluate)
3315 {
3316 *arg = option_end;
3317 return OK;
3318 }
3319
3320 c = *option_end;
3321 *option_end = NUL;
3322 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003325 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 ret = FAIL;
3330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003333 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 rettv->v_type = VAR_STRING;
3336 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003338 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 rettv->v_type = VAR_NUMBER;
3341 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003343 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 rettv->v_type = VAR_NUMBER;
3346 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 rettv->v_type = VAR_STRING;
3351 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353 }
3354 else if (working && (opt_type == -2 || opt_type == -1))
3355 ret = FAIL;
3356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003357 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 *arg = option_end;
3359
3360 return ret;
3361}
3362
3363/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3365 * Return OK or FAIL.
3366 */
3367 int
3368get_number_tv(
3369 char_u **arg,
3370 typval_T *rettv,
3371 int evaluate,
3372 int want_string UNUSED)
3373{
3374 int len;
3375#ifdef FEAT_FLOAT
3376 char_u *p;
3377 int get_float = FALSE;
3378
3379 // We accept a float when the format matches
3380 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3381 // strict to avoid backwards compatibility problems.
3382 // With script version 2 and later the leading digit can be
3383 // omitted.
3384 // Don't look for a float after the "." operator, so that
3385 // ":let vers = 1.2.3" doesn't fail.
3386 if (**arg == '.')
3387 p = *arg;
3388 else
3389 p = skipdigits(*arg + 1);
3390 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3391 {
3392 get_float = TRUE;
3393 p = skipdigits(p + 2);
3394 if (*p == 'e' || *p == 'E')
3395 {
3396 ++p;
3397 if (*p == '-' || *p == '+')
3398 ++p;
3399 if (!vim_isdigit(*p))
3400 get_float = FALSE;
3401 else
3402 p = skipdigits(p + 1);
3403 }
3404 if (ASCII_ISALPHA(*p) || *p == '.')
3405 get_float = FALSE;
3406 }
3407 if (get_float)
3408 {
3409 float_T f;
3410
3411 *arg += string2float(*arg, &f);
3412 if (evaluate)
3413 {
3414 rettv->v_type = VAR_FLOAT;
3415 rettv->vval.v_float = f;
3416 }
3417 }
3418 else
3419#endif
3420 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3421 {
3422 char_u *bp;
3423 blob_T *blob = NULL; // init for gcc
3424
3425 // Blob constant: 0z0123456789abcdef
3426 if (evaluate)
3427 blob = blob_alloc();
3428 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3429 {
3430 if (!vim_isxdigit(bp[1]))
3431 {
3432 if (blob != NULL)
3433 {
3434 emsg(_("E973: Blob literal should have an even number of hex characters"));
3435 ga_clear(&blob->bv_ga);
3436 VIM_CLEAR(blob);
3437 }
3438 return FAIL;
3439 }
3440 if (blob != NULL)
3441 ga_append(&blob->bv_ga,
3442 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3443 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3444 ++bp;
3445 }
3446 if (blob != NULL)
3447 rettv_blob_set(rettv, blob);
3448 *arg = bp;
3449 }
3450 else
3451 {
3452 varnumber_T n;
3453
3454 // decimal, hex or octal number
3455 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3456 ? STR2NR_NO_OCT + STR2NR_QUOTE
3457 : STR2NR_ALL, &n, NULL, 0, TRUE);
3458 if (len == 0)
3459 {
3460 semsg(_(e_invexpr2), *arg);
3461 return FAIL;
3462 }
3463 *arg += len;
3464 if (evaluate)
3465 {
3466 rettv->v_type = VAR_NUMBER;
3467 rettv->vval.v_number = n;
3468 }
3469 }
3470 return OK;
3471}
3472
3473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * Allocate a variable for a string constant.
3475 * Return OK or FAIL.
3476 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003478get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 char_u *p;
3481 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int extra = 0;
3483
3484 /*
3485 * Find the end of the string, skipping backslashed characters.
3486 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003487 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 if (*p == '\\' && p[1] != NUL)
3490 {
3491 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // A "\<x>" form occupies at least 4 characters, and produces up
3493 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (*p == '<')
3495 extra += 2;
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498
3499 if (*p != '"')
3500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003501 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return FAIL;
3503 }
3504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (!evaluate)
3507 {
3508 *arg = p + 1;
3509 return OK;
3510 }
3511
3512 /*
3513 * Copy the string into allocated memory, handling backslashed
3514 * characters.
3515 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003516 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (name == NULL)
3518 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 rettv->v_type = VAR_STRING;
3520 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 if (*p == '\\')
3525 {
3526 switch (*++p)
3527 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003528 case 'b': *name++ = BS; ++p; break;
3529 case 'e': *name++ = ESC; ++p; break;
3530 case 'f': *name++ = FF; ++p; break;
3531 case 'n': *name++ = NL; ++p; break;
3532 case 'r': *name++ = CAR; ++p; break;
3533 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003537 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 case 'U':
3539 if (vim_isxdigit(p[1]))
3540 {
3541 int n, nr;
3542 int c = toupper(*p);
3543
3544 if (c == 'X')
3545 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003546 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003548 else
3549 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 nr = 0;
3551 while (--n >= 0 && vim_isxdigit(p[1]))
3552 {
3553 ++p;
3554 nr = (nr << 4) + hex2nr(*p);
3555 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003556 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 // For "\u" store the number according to
3558 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
3565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 case '0':
3568 case '1':
3569 case '2':
3570 case '3':
3571 case '4':
3572 case '5':
3573 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 case '7': *name = *p++ - '0';
3575 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 *name = (*name << 3) + *p++ - '0';
3578 if (*p >= '0' && *p <= '7')
3579 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 break;
3583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003585 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3586 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (extra != 0)
3588 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 break;
3591 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003592 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 break;
3596 }
3597 }
3598 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003603 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003604 ++p;
3605 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return OK;
3608}
3609
3610/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * Return OK or FAIL.
3613 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003618 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003619 int reduce = 0;
3620
3621 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003623 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003624 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003629 break;
3630 ++reduce;
3631 ++p;
3632 }
3633 }
3634
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003637 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003638 return FAIL;
3639 }
3640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003641 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003642 if (!evaluate)
3643 {
3644 *arg = p + 1;
3645 return OK;
3646 }
3647
3648 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003650 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003651 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003652 if (str == NULL)
3653 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 rettv->v_type = VAR_STRING;
3655 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003662 break;
3663 ++p;
3664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003668 *arg = p + 1;
3669
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003670 return OK;
3671}
3672
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003673/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003674 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003675 */
3676 char_u *
3677partial_name(partial_T *pt)
3678{
3679 if (pt->pt_name != NULL)
3680 return pt->pt_name;
3681 return pt->pt_func->uf_name;
3682}
3683
Bram Moolenaarddecc252016-04-06 22:59:37 +02003684 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003685partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003686{
3687 int i;
3688
3689 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003690 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003691 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003692 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 if (pt->pt_name != NULL)
3694 {
3695 func_unref(pt->pt_name);
3696 vim_free(pt->pt_name);
3697 }
3698 else
3699 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003700 vim_free(pt);
3701}
3702
3703/*
3704 * Unreference a closure: decrement the reference count and free it when it
3705 * becomes zero.
3706 */
3707 void
3708partial_unref(partial_T *pt)
3709{
3710 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003711 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003712}
3713
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003714static int tv_equal_recurse_limit;
3715
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003716 static int
3717func_equal(
3718 typval_T *tv1,
3719 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003721{
3722 char_u *s1, *s2;
3723 dict_T *d1, *d2;
3724 int a1, a2;
3725 int i;
3726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003727 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003728 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003730 if (s1 != NULL && *s1 == NUL)
3731 s1 = NULL;
3732 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003734 if (s2 != NULL && *s2 == NUL)
3735 s2 = NULL;
3736 if (s1 == NULL || s2 == NULL)
3737 {
3738 if (s1 != s2)
3739 return FALSE;
3740 }
3741 else if (STRCMP(s1, s2) != 0)
3742 return FALSE;
3743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003745 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3746 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3747 if (d1 == NULL || d2 == NULL)
3748 {
3749 if (d1 != d2)
3750 return FALSE;
3751 }
3752 else if (!dict_equal(d1, d2, ic, TRUE))
3753 return FALSE;
3754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003756 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3757 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3758 if (a1 != a2)
3759 return FALSE;
3760 for (i = 0; i < a1; ++i)
3761 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3762 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3763 return FALSE;
3764
3765 return TRUE;
3766}
3767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003768/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003769 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003770 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003771 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003772 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774tv_equal(
3775 typval_T *tv1,
3776 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 int ic, // ignore case
3778 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003779{
3780 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003782 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003783 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // Catch lists and dicts that have an endless loop by limiting
3786 // recursiveness to a limit. We guess they are equal then.
3787 // A fixed limit has the problem of still taking an awful long time.
3788 // Reduce the limit every time running into it. That should work fine for
3789 // deeply linked structures that are not recursively linked and catch
3790 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003791 if (!recursive)
3792 tv_equal_recurse_limit = 1000;
3793 if (recursive_cnt >= tv_equal_recurse_limit)
3794 {
3795 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003796 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003797 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3800 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003801 if ((tv1->v_type == VAR_FUNC
3802 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3803 && (tv2->v_type == VAR_FUNC
3804 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3805 {
3806 ++recursive_cnt;
3807 r = func_equal(tv1, tv2, ic);
3808 --recursive_cnt;
3809 return r;
3810 }
3811
3812 if (tv1->v_type != tv2->v_type)
3813 return FALSE;
3814
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003815 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003816 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003817 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003818 ++recursive_cnt;
3819 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3820 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003821 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003822
3823 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003824 ++recursive_cnt;
3825 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3826 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003827 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003828
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003829 case VAR_BLOB:
3830 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3831
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003832 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 case VAR_BOOL:
3834 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003835 return tv1->vval.v_number == tv2->vval.v_number;
3836
3837 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003838 s1 = tv_get_string_buf(tv1, buf1);
3839 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003840 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003841
Bram Moolenaar835dc632016-02-07 14:27:38 +01003842 case VAR_FLOAT:
3843#ifdef FEAT_FLOAT
3844 return tv1->vval.v_float == tv2->vval.v_float;
3845#endif
3846 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003847#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003848 return tv1->vval.v_job == tv2->vval.v_job;
3849#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003850 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003852 return tv1->vval.v_channel == tv2->vval.v_channel;
3853#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003855 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003856 return tv1->vval.v_partial == tv2->vval.v_partial;
3857
3858 case VAR_FUNC:
3859 return tv1->vval.v_string == tv2->vval.v_string;
3860
Bram Moolenaar835dc632016-02-07 14:27:38 +01003861 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003862 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003864 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003865 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3868 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003869 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870}
3871
3872/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003873 * Return the next (unique) copy ID.
3874 * Used for serializing nested structures.
3875 */
3876 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003877get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003878{
3879 current_copyID += COPYID_INC;
3880 return current_copyID;
3881}
3882
3883/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003884 * Garbage collection for lists and dictionaries.
3885 *
3886 * We use reference counts to be able to free most items right away when they
3887 * are no longer used. But for composite items it's possible that it becomes
3888 * unused while the reference count is > 0: When there is a recursive
3889 * reference. Example:
3890 * :let l = [1, 2, 3]
3891 * :let d = {9: l}
3892 * :let l[1] = d
3893 *
3894 * Since this is quite unusual we handle this with garbage collection: every
3895 * once in a while find out which lists and dicts are not referenced from any
3896 * variable.
3897 *
3898 * Here is a good reference text about garbage collection (refers to Python
3899 * but it applies to all reference-counting mechanisms):
3900 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003901 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003902
3903/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003904 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003905 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003906 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003907 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003909garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003910{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003911 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003912 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003913 buf_T *buf;
3914 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003915 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003916 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003917
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003918 if (!testing)
3919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003920 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003921 want_garbage_collect = FALSE;
3922 may_garbage_collect = FALSE;
3923 garbage_collect_at_exit = FALSE;
3924 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003925
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003926 // The execution stack can grow big, limit the size.
3927 if (exestack.ga_maxlen - exestack.ga_len > 500)
3928 {
3929 size_t new_len;
3930 char_u *pp;
3931 int n;
3932
3933 // Keep 150% of the current size, with a minimum of the growth size.
3934 n = exestack.ga_len / 2;
3935 if (n < exestack.ga_growsize)
3936 n = exestack.ga_growsize;
3937
3938 // Don't make it bigger though.
3939 if (exestack.ga_len + n < exestack.ga_maxlen)
3940 {
3941 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3942 pp = vim_realloc(exestack.ga_data, new_len);
3943 if (pp == NULL)
3944 return FAIL;
3945 exestack.ga_maxlen = exestack.ga_len + n;
3946 exestack.ga_data = pp;
3947 }
3948 }
3949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003950 // We advance by two because we add one for items referenced through
3951 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003952 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003954 /*
3955 * 1. Go through all accessible variables and mark all lists and dicts
3956 * with copyID.
3957 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003959 // Don't free variables in the previous_funccal list unless they are only
3960 // referenced through previous_funccal. This must be first, because if
3961 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003964 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003965 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003967 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003968 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003969 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3970 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003972 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003974 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3975 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003976 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3978 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003979#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003980 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003981 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3982 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003983 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003984 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003985 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3986 NULL, NULL);
3987#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003989 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003990 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003991 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3992 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003993 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003994 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003997 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004000 abort = abort || set_ref_in_functions(copyID);
4001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004002 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004005 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004006 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004007
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004008 // callbacks in buffers
4009 abort = abort || set_ref_in_buffers(copyID);
4010
Bram Moolenaar1dced572012-04-05 16:54:08 +02004011#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004012 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004013#endif
4014
Bram Moolenaardb913952012-06-29 12:54:53 +02004015#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004016 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004017#endif
4018
4019#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004021#endif
4022
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004024 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004025 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004026#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004027#ifdef FEAT_NETBEANS_INTG
4028 abort = abort || set_ref_in_nb_channel(copyID);
4029#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004030
Bram Moolenaare3188e22016-05-31 21:13:04 +02004031#ifdef FEAT_TIMERS
4032 abort = abort || set_ref_in_timer(copyID);
4033#endif
4034
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004035#ifdef FEAT_QUICKFIX
4036 abort = abort || set_ref_in_quickfix(copyID);
4037#endif
4038
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004039#ifdef FEAT_TERMINAL
4040 abort = abort || set_ref_in_term(copyID);
4041#endif
4042
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004043#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004044 abort = abort || set_ref_in_popups(copyID);
4045#endif
4046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004047 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004048 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 /*
4050 * 2. Free lists and dictionaries that are not referenced.
4051 */
4052 did_free = free_unref_items(copyID);
4053
4054 /*
4055 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004057 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004059 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 else if (p_verbose > 0)
4061 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004062 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004063 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004064
4065 return did_free;
4066}
4067
4068/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004069 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004070 */
4071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004072free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004073{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004074 int did_free = FALSE;
4075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004076 // Let all "free" functions know that we are here. This means no
4077 // dictionaries, lists, channels or jobs are to be freed, because we will
4078 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004079 in_free_unref_items = TRUE;
4080
4081 /*
4082 * PASS 1: free the contents of the items. We don't free the items
4083 * themselves yet, so that it is possible to decrement refcount counters
4084 */
4085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004086 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004087 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004090 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091
4092#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // Go through the list of jobs and free items without the copyID. This
4094 // must happen before doing channels, because jobs refer to channels, but
4095 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004096 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004098 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004099 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4100#endif
4101
4102 /*
4103 * PASS 2: free the items themselves.
4104 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004105 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004106 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004107
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004108#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004109 // Go through the list of jobs and free items without the copyID. This
4110 // must happen before doing channels, because jobs refer to channels, but
4111 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004112 free_unused_jobs(copyID, COPYID_MASK);
4113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004114 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004115 free_unused_channels(copyID, COPYID_MASK);
4116#endif
4117
4118 in_free_unref_items = FALSE;
4119
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004120 return did_free;
4121}
4122
4123/*
4124 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004125 * "list_stack" is used to add lists to be marked. Can be NULL.
4126 *
4127 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004128 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004129 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004130set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004131{
4132 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004133 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004134 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004135 hashtab_T *cur_ht;
4136 ht_stack_T *ht_stack = NULL;
4137 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004138
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 cur_ht = ht;
4140 for (;;)
4141 {
4142 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // Mark each item in the hashtab. If the item contains a hashtab
4145 // it is added to ht_stack, if it contains a list it is added to
4146 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004147 todo = (int)cur_ht->ht_used;
4148 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4149 if (!HASHITEM_EMPTY(hi))
4150 {
4151 --todo;
4152 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4153 &ht_stack, list_stack);
4154 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004155 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004156
4157 if (ht_stack == NULL)
4158 break;
4159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004161 cur_ht = ht_stack->ht;
4162 tempitem = ht_stack;
4163 ht_stack = ht_stack->prev;
4164 free(tempitem);
4165 }
4166
4167 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004168}
4169
4170/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004171 * Mark a dict and its items with "copyID".
4172 * Returns TRUE if setting references failed somehow.
4173 */
4174 int
4175set_ref_in_dict(dict_T *d, int copyID)
4176{
4177 if (d != NULL && d->dv_copyID != copyID)
4178 {
4179 d->dv_copyID = copyID;
4180 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4181 }
4182 return FALSE;
4183}
4184
4185/*
4186 * Mark a list and its items with "copyID".
4187 * Returns TRUE if setting references failed somehow.
4188 */
4189 int
4190set_ref_in_list(list_T *ll, int copyID)
4191{
4192 if (ll != NULL && ll->lv_copyID != copyID)
4193 {
4194 ll->lv_copyID = copyID;
4195 return set_ref_in_list_items(ll, copyID, NULL);
4196 }
4197 return FALSE;
4198}
4199
4200/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004201 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004202 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4203 *
4204 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004205 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004206 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004207set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004208{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004209 listitem_T *li;
4210 int abort = FALSE;
4211 list_T *cur_l;
4212 list_stack_T *list_stack = NULL;
4213 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004214
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004215 cur_l = l;
4216 for (;;)
4217 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004218 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004219 // Mark each item in the list. If the item contains a hashtab
4220 // it is added to ht_stack, if it contains a list it is added to
4221 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004222 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4223 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4224 ht_stack, &list_stack);
4225 if (list_stack == NULL)
4226 break;
4227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004228 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004229 cur_l = list_stack->list;
4230 tempitem = list_stack;
4231 list_stack = list_stack->prev;
4232 free(tempitem);
4233 }
4234
4235 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004236}
4237
4238/*
4239 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 * "list_stack" is used to add lists to be marked. Can be NULL.
4241 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4242 *
4243 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004244 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246set_ref_in_item(
4247 typval_T *tv,
4248 int copyID,
4249 ht_stack_T **ht_stack,
4250 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004251{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004252 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004253
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004255 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004256 dict_T *dd = tv->vval.v_dict;
4257
Bram Moolenaara03f2332016-02-06 18:09:59 +01004258 if (dd != NULL && dd->dv_copyID != copyID)
4259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004260 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004261 dd->dv_copyID = copyID;
4262 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004263 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004264 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4265 }
4266 else
4267 {
4268 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4269 if (newitem == NULL)
4270 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 else
4272 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004273 newitem->ht = &dd->dv_hashtab;
4274 newitem->prev = *ht_stack;
4275 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004276 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004277 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004278 }
4279 }
4280 else if (tv->v_type == VAR_LIST)
4281 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004282 list_T *ll = tv->vval.v_list;
4283
Bram Moolenaara03f2332016-02-06 18:09:59 +01004284 if (ll != NULL && ll->lv_copyID != copyID)
4285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004287 ll->lv_copyID = copyID;
4288 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004289 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004290 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004291 }
4292 else
4293 {
4294 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004295 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004296 if (newitem == NULL)
4297 abort = TRUE;
4298 else
4299 {
4300 newitem->list = ll;
4301 newitem->prev = *list_stack;
4302 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004303 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004304 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004305 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004306 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004307 else if (tv->v_type == VAR_FUNC)
4308 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004309 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004310 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 else if (tv->v_type == VAR_PARTIAL)
4312 {
4313 partial_T *pt = tv->vval.v_partial;
4314 int i;
4315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004316 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004317 if (pt != NULL)
4318 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004319 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004320
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004321 if (pt->pt_dict != NULL)
4322 {
4323 typval_T dtv;
4324
4325 dtv.v_type = VAR_DICT;
4326 dtv.vval.v_dict = pt->pt_dict;
4327 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4328 }
4329
4330 for (i = 0; i < pt->pt_argc; ++i)
4331 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4332 ht_stack, list_stack);
4333 }
4334 }
4335#ifdef FEAT_JOB_CHANNEL
4336 else if (tv->v_type == VAR_JOB)
4337 {
4338 job_T *job = tv->vval.v_job;
4339 typval_T dtv;
4340
4341 if (job != NULL && job->jv_copyID != copyID)
4342 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004343 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004344 if (job->jv_channel != NULL)
4345 {
4346 dtv.v_type = VAR_CHANNEL;
4347 dtv.vval.v_channel = job->jv_channel;
4348 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4349 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004350 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004351 {
4352 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004353 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004354 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4355 }
4356 }
4357 }
4358 else if (tv->v_type == VAR_CHANNEL)
4359 {
4360 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004361 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004362 typval_T dtv;
4363 jsonq_T *jq;
4364 cbq_T *cq;
4365
4366 if (ch != NULL && ch->ch_copyID != copyID)
4367 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004368 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004369 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004370 {
4371 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4372 jq = jq->jq_next)
4373 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4374 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4375 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004376 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004377 {
4378 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004379 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004380 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4381 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004382 if (ch->ch_part[part].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 =
4386 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4388 }
4389 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004390 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 {
4392 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004393 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004394 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4395 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004396 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 {
4398 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004399 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004400 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4401 }
4402 }
4403 }
4404#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004405 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004406}
4407
Bram Moolenaar8c711452005-01-14 21:53:12 +00004408/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 * Return a string with the string representation of a variable.
4410 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004412 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004413 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4414 * stings as "string()", otherwise does not put quotes around strings, as
4415 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004416 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4417 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004418 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004420 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004421echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004422 typval_T *tv,
4423 char_u **tofree,
4424 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004425 int copyID,
4426 int echo_style,
4427 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004428 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004429{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004430 static int recurse = 0;
4431 char_u *r = NULL;
4432
Bram Moolenaar33570922005-01-25 22:26:29 +00004433 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004434 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004435 if (!did_echo_string_emsg)
4436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004437 // Only give this message once for a recursive call to avoid
4438 // flooding the user with errors. And stop iterating over lists
4439 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004440 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004441 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004443 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004444 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004445 }
4446 ++recurse;
4447
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 switch (tv->v_type)
4449 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004450 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004451 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004452 {
4453 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004454 r = tv->vval.v_string;
4455 if (r == NULL)
4456 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004457 }
4458 else
4459 {
4460 *tofree = string_quote(tv->vval.v_string, FALSE);
4461 r = *tofree;
4462 }
4463 break;
4464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004466 if (echo_style)
4467 {
4468 *tofree = NULL;
4469 r = tv->vval.v_string;
4470 }
4471 else
4472 {
4473 *tofree = string_quote(tv->vval.v_string, TRUE);
4474 r = *tofree;
4475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004476 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004477
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004478 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004479 {
4480 partial_T *pt = tv->vval.v_partial;
4481 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004482 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004483 garray_T ga;
4484 int i;
4485 char_u *tf;
4486
4487 ga_init2(&ga, 1, 100);
4488 ga_concat(&ga, (char_u *)"function(");
4489 if (fname != NULL)
4490 {
4491 ga_concat(&ga, fname);
4492 vim_free(fname);
4493 }
4494 if (pt != NULL && pt->pt_argc > 0)
4495 {
4496 ga_concat(&ga, (char_u *)", [");
4497 for (i = 0; i < pt->pt_argc; ++i)
4498 {
4499 if (i > 0)
4500 ga_concat(&ga, (char_u *)", ");
4501 ga_concat(&ga,
4502 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4503 vim_free(tf);
4504 }
4505 ga_concat(&ga, (char_u *)"]");
4506 }
4507 if (pt != NULL && pt->pt_dict != NULL)
4508 {
4509 typval_T dtv;
4510
4511 ga_concat(&ga, (char_u *)", ");
4512 dtv.v_type = VAR_DICT;
4513 dtv.vval.v_dict = pt->pt_dict;
4514 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4515 vim_free(tf);
4516 }
4517 ga_concat(&ga, (char_u *)")");
4518
4519 *tofree = ga.ga_data;
4520 r = *tofree;
4521 break;
4522 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004523
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004524 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004525 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004526 break;
4527
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004529 if (tv->vval.v_list == NULL)
4530 {
4531 *tofree = NULL;
4532 r = NULL;
4533 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004534 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4535 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004536 {
4537 *tofree = NULL;
4538 r = (char_u *)"[...]";
4539 }
4540 else
4541 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004542 int old_copyID = tv->vval.v_list->lv_copyID;
4543
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004544 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004545 *tofree = list2string(tv, copyID, restore_copyID);
4546 if (restore_copyID)
4547 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004548 r = *tofree;
4549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004550 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004551
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004553 if (tv->vval.v_dict == NULL)
4554 {
4555 *tofree = NULL;
4556 r = NULL;
4557 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004558 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4559 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004560 {
4561 *tofree = NULL;
4562 r = (char_u *)"{...}";
4563 }
4564 else
4565 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004566 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004567 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004568 *tofree = dict2string(tv, copyID, restore_copyID);
4569 if (restore_copyID)
4570 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004571 r = *tofree;
4572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004573 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004576 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004577 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004579 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004580 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004581 break;
4582
Bram Moolenaar835dc632016-02-07 14:27:38 +01004583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004584 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004585 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004586 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004587 if (composite_val)
4588 {
4589 *tofree = string_quote(r, FALSE);
4590 r = *tofree;
4591 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004593
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004594 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004595#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004596 *tofree = NULL;
4597 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4598 r = numbuf;
4599 break;
4600#endif
4601
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004602 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004603 case VAR_SPECIAL:
4604 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004605 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004606 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004608
Bram Moolenaar8502c702014-06-17 12:51:16 +02004609 if (--recurse == 0)
4610 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004611 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004612}
4613
4614/*
4615 * Return a string with the string representation of a variable.
4616 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4617 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004618 * Does not put quotes around strings, as ":echo" displays values.
4619 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4620 * May return NULL.
4621 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004623echo_string(
4624 typval_T *tv,
4625 char_u **tofree,
4626 char_u *numbuf,
4627 int copyID)
4628{
4629 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4630}
4631
4632/*
4633 * Return a string with the string representation of a variable.
4634 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4635 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004636 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004637 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004638 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004640tv2string(
4641 typval_T *tv,
4642 char_u **tofree,
4643 char_u *numbuf,
4644 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004645{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004646 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647}
4648
4649/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004650 * Return string "str" in ' quotes, doubling ' characters.
4651 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004653 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004654 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004655string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004656{
Bram Moolenaar33570922005-01-25 22:26:29 +00004657 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658 char_u *p, *r, *s;
4659
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 len = (function ? 13 : 3);
4661 if (str != NULL)
4662 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004663 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004664 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004665 if (*p == '\'')
4666 ++len;
4667 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004668 s = r = alloc(len);
4669 if (r != NULL)
4670 {
4671 if (function)
4672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004674 r += 10;
4675 }
4676 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004678 if (str != NULL)
4679 for (p = str; *p != NUL; )
4680 {
4681 if (*p == '\'')
4682 *r++ = '\'';
4683 MB_COPY_CHAR(p, r);
4684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004686 if (function)
4687 *r++ = ')';
4688 *r++ = NUL;
4689 }
4690 return s;
4691}
4692
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004693#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694/*
4695 * Convert the string "text" to a floating point number.
4696 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4697 * this always uses a decimal point.
4698 * Returns the length of the text that was consumed.
4699 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004701string2float(
4702 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004703 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704{
4705 char *s = (char *)text;
4706 float_T f;
4707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004709 if (STRNICMP(text, "inf", 3) == 0)
4710 {
4711 *value = INFINITY;
4712 return 3;
4713 }
4714 if (STRNICMP(text, "-inf", 3) == 0)
4715 {
4716 *value = -INFINITY;
4717 return 4;
4718 }
4719 if (STRNICMP(text, "nan", 3) == 0)
4720 {
4721 *value = NAN;
4722 return 3;
4723 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 f = strtod(s, &s);
4725 *value = f;
4726 return (int)((char_u *)s - text);
4727}
4728#endif
4729
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 * Get the value of an environment variable.
4732 * "arg" is pointing to the '$'. It is advanced to after the name.
4733 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004734 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004737get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 char_u *string = NULL;
4740 int len;
4741 int cc;
4742 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004743 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 ++*arg;
4746 name = *arg;
4747 len = get_env_len(arg);
4748 if (evaluate)
4749 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004750 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004752
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004753 cc = name[len];
4754 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004756 string = vim_getenv(name, &mustfree);
4757 if (string != NULL && *string != NUL)
4758 {
4759 if (!mustfree)
4760 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004762 else
4763 {
4764 if (mustfree)
4765 vim_free(string);
4766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004768 string = expand_env_save(name - 1);
4769 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004770 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004771 }
4772 name[len] = cc;
4773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 rettv->v_type = VAR_STRING;
4775 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777
4778 return OK;
4779}
4780
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004783 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004785 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004786var2fpos(
4787 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004788 int dollar_lnum, // TRUE when $ is last line
4789 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004791 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004793 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004796 if (varp->v_type == VAR_LIST)
4797 {
4798 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004799 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004800 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004801 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004802
4803 l = varp->vval.v_list;
4804 if (l == NULL)
4805 return NULL;
4806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004808 pos.lnum = list_find_nr(l, 0L, &error);
4809 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004813 pos.col = list_find_nr(l, 1L, &error);
4814 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004815 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004816 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004819 li = list_find(l, 1L);
4820 if (li != NULL && li->li_tv.v_type == VAR_STRING
4821 && li->li_tv.vval.v_string != NULL
4822 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4823 pos.col = len + 1;
4824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004825 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004826 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004828 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004830 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004831 pos.coladd = list_find_nr(l, 2L, &error);
4832 if (error)
4833 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004834
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004835 return &pos;
4836 }
4837
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 if (name == NULL)
4840 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004841 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004844 {
4845 if (VIsual_active)
4846 return &VIsual;
4847 return &curwin->w_cursor;
4848 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004851 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4853 return NULL;
4854 return pp;
4855 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004856
Bram Moolenaara5525202006-03-02 22:52:09 +00004857 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004858
Bram Moolenaar477933c2007-07-17 14:32:23 +00004859 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004860 {
4861 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004862 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004863 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004864 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 // In silent Ex mode topline is zero, but that's not a valid line
4866 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004867 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004868 return &pos;
4869 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004870 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004871 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004872 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004874 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004875 return &pos;
4876 }
4877 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004878 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004880 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 pos.lnum = curbuf->b_ml.ml_line_count;
4883 pos.col = 0;
4884 }
4885 else
4886 {
4887 pos.lnum = curwin->w_cursor.lnum;
4888 pos.col = (colnr_T)STRLEN(ml_get_curline());
4889 }
4890 return &pos;
4891 }
4892 return NULL;
4893}
4894
4895/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004896 * Convert list in "arg" into a position and optional file number.
4897 * When "fnump" is NULL there is no file number, only 3 items.
4898 * Note that the column is passed on as-is, the caller may want to decrement
4899 * it to use 1 for the first column.
4900 * Return FAIL when conversion is not possible, doesn't check the position for
4901 * validity.
4902 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904list2fpos(
4905 typval_T *arg,
4906 pos_T *posp,
4907 int *fnump,
4908 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004909{
4910 list_T *l = arg->vval.v_list;
4911 long i = 0;
4912 long n;
4913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4915 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004916 if (arg->v_type != VAR_LIST
4917 || l == NULL
4918 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004919 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004920 return FAIL;
4921
4922 if (fnump != NULL)
4923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004925 if (n < 0)
4926 return FAIL;
4927 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004929 *fnump = n;
4930 }
4931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004933 if (n < 0)
4934 return FAIL;
4935 posp->lnum = n;
4936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004938 if (n < 0)
4939 return FAIL;
4940 posp->col = n;
4941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004943 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004944 posp->coladd = 0;
4945 else
4946 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004947
Bram Moolenaar493c1782014-05-28 14:34:46 +02004948 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004949 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004950
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004951 return OK;
4952}
4953
4954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 * Get the length of an environment variable name.
4956 * Advance "arg" to the first character after the name.
4957 * Return 0 for error.
4958 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 char_u *p;
4963 int len;
4964
4965 for (p = *arg; vim_isIDc(*p); ++p)
4966 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 return 0;
4969
4970 len = (int)(p - *arg);
4971 *arg = p;
4972 return len;
4973}
4974
4975/*
4976 * Get the length of the name of a function or internal variable.
4977 * "arg" is advanced to the first non-white character after the name.
4978 * Return 0 if something is wrong.
4979 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004981get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
4983 char_u *p;
4984 int len;
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004988 {
4989 if (*p == ':')
4990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // "s:" is start of "s:var", but "n:" is not and can be used in
4992 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004993 len = (int)(p - *arg);
4994 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4995 || len > 1)
4996 break;
4997 }
4998 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 return 0;
5001
5002 len = (int)(p - *arg);
5003 *arg = skipwhite(p);
5004
5005 return len;
5006}
5007
5008/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005009 * Get the length of the name of a variable or function.
5010 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005012 * Return -1 if curly braces expansion failed.
5013 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * If the name contains 'magic' {}'s, expand them and return the
5015 * expanded name in an allocated string via 'alias' - caller must free.
5016 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018get_name_len(
5019 char_u **arg,
5020 char_u **alias,
5021 int evaluate,
5022 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 char_u *p;
5026 char_u *expr_start;
5027 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5032 && (*arg)[2] == (int)KE_SNR)
5033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *arg += 3;
5036 return get_id_len(arg) + 3;
5037 }
5038 len = eval_fname_script(*arg);
5039 if (len > 0)
5040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *arg += len;
5043 }
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005048 p = find_name_end(*arg, &expr_start, &expr_end,
5049 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (expr_start != NULL)
5051 {
5052 char_u *temp_string;
5053
5054 if (!evaluate)
5055 {
5056 len += (int)(p - *arg);
5057 *arg = skipwhite(p);
5058 return len;
5059 }
5060
5061 /*
5062 * Include any <SID> etc in the expanded string:
5063 * Thus the -len here.
5064 */
5065 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5066 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 *alias = temp_string;
5069 *arg = skipwhite(p);
5070 return (int)STRLEN(temp_string);
5071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005074 // Only give an error when there is something, otherwise it will be
5075 // reported at a higher level.
5076 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005077 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 return len;
5080}
5081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082/*
5083 * Find the end of a variable or function name, taking care of magic braces.
5084 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5085 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005086 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 * Return a pointer to just after the name. Equal to "arg" if there is no
5088 * valid name.
5089 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005091find_name_end(
5092 char_u *arg,
5093 char_u **expr_start,
5094 char_u **expr_end,
5095 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 int mb_nest = 0;
5098 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005100 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 *expr_start = NULL;
5105 *expr_end = NULL;
5106 }
5107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // Quick check for valid starting character.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005109 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5110 return arg;
5111
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 for (p = arg; *p != NUL
5113 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005115 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005117 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005119 if (*p == '\'')
5120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005122 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005123 ;
5124 if (*p == NUL)
5125 break;
5126 }
5127 else if (*p == '"')
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005130 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005131 if (*p == '\\' && p[1] != NUL)
5132 ++p;
5133 if (*p == NUL)
5134 break;
5135 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005136 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // "s:" is start of "s:var", but "n:" is not and can be used in
5139 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005140 len = (int)(p - arg);
5141 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005142 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005143 break;
5144 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 if (*p == '[')
5149 ++br_nest;
5150 else if (*p == ']')
5151 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 if (*p == '{')
5157 {
5158 mb_nest++;
5159 if (expr_start != NULL && *expr_start == NULL)
5160 *expr_start = p;
5161 }
5162 else if (*p == '}')
5163 {
5164 mb_nest--;
5165 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5166 *expr_end = p;
5167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
5171 return p;
5172}
5173
5174/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005175 * Expands out the 'magic' {}'s in a variable/function name.
5176 * Note that this can call itself recursively, to deal with
5177 * constructs like foo{bar}{baz}{bam}
5178 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5179 * "in_start" ^
5180 * "expr_start" ^
5181 * "expr_end" ^
5182 * "in_end" ^
5183 *
5184 * Returns a new allocated string, which the caller must free.
5185 * Returns NULL for failure.
5186 */
5187 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005188make_expanded_name(
5189 char_u *in_start,
5190 char_u *expr_start,
5191 char_u *expr_end,
5192 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005193{
5194 char_u c1;
5195 char_u *retval = NULL;
5196 char_u *temp_result;
5197 char_u *nextcmd = NULL;
5198
5199 if (expr_end == NULL || in_end == NULL)
5200 return NULL;
5201 *expr_start = NUL;
5202 *expr_end = NUL;
5203 c1 = *in_end;
5204 *in_end = NUL;
5205
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005206 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005207 if (temp_result != NULL && nextcmd == NULL)
5208 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005209 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5210 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005211 if (retval != NULL)
5212 {
5213 STRCPY(retval, in_start);
5214 STRCAT(retval, temp_result);
5215 STRCAT(retval, expr_end + 1);
5216 }
5217 }
5218 vim_free(temp_result);
5219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005220 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 *expr_start = '{';
5222 *expr_end = '}';
5223
5224 if (retval != NULL)
5225 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005226 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (expr_start != NULL)
5228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230 temp_result = make_expanded_name(retval, expr_start,
5231 expr_end, temp_result);
5232 vim_free(retval);
5233 retval = temp_result;
5234 }
5235 }
5236
5237 return retval;
5238}
5239
5240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005242 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005244 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005245eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005247 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5248}
5249
5250/*
5251 * Return TRUE if character "c" can be used as the first character in a
5252 * variable or function name (excluding '{' and '}').
5253 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005255eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005256{
5257 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258}
5259
5260/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005261 * Handle:
5262 * - expr[expr], expr[expr:expr] subscript
5263 * - ".name" lookup
5264 * - function call with Funcref variable: func(expr)
5265 * - method call: var->method()
5266 *
5267 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005270handle_subscript(
5271 char_u **arg,
5272 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005273 int evaluate, // do more than finding the end
5274 int verbose, // give error messages
5275 char_u *start_leader, // start of '!' and '-' prefixes
5276 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277{
5278 int ret = OK;
5279 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280
Bram Moolenaar61343f02019-07-20 21:11:13 +02005281 // "." is ".name" lookup when we found a dict or when evaluating and
5282 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005284 && (((**arg == '['
5285 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005286 || (!evaluate
5287 && (*arg)[1] != '.'
5288 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005289 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005290 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005291 && !VIM_ISWHITE(*(*arg - 1)))
5292 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005293 {
5294 if (**arg == '(')
5295 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005296 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005297
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005298 // Stop the expression evaluation when immediately aborting on
5299 // error, or when an interrupt occurred or an exception was thrown
5300 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 if (aborting())
5302 {
5303 if (ret == OK)
5304 clear_tv(rettv);
5305 ret = FAIL;
5306 }
5307 dict_unref(selfdict);
5308 selfdict = NULL;
5309 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005310 else if (**arg == '-')
5311 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005312 // Expression "-1.0->method()" applies the leader "-" before
5313 // applying ->.
5314 if (evaluate && *end_leaderp > start_leader)
5315 ret = eval7_leader(rettv, start_leader, end_leaderp);
5316 if (ret == OK)
5317 {
5318 if ((*arg)[2] == '{')
5319 // expr->{lambda}()
5320 ret = eval_lambda(arg, rettv, evaluate, verbose);
5321 else
5322 // expr->name()
5323 ret = eval_method(arg, rettv, evaluate, verbose);
5324 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005325 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005327 {
5328 dict_unref(selfdict);
5329 if (rettv->v_type == VAR_DICT)
5330 {
5331 selfdict = rettv->vval.v_dict;
5332 if (selfdict != NULL)
5333 ++selfdict->dv_refcount;
5334 }
5335 else
5336 selfdict = NULL;
5337 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5338 {
5339 clear_tv(rettv);
5340 ret = FAIL;
5341 }
5342 }
5343 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5346 // Don't do this when "Func" is already a partial that was bound
5347 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005348 if (selfdict != NULL
5349 && (rettv->v_type == VAR_FUNC
5350 || (rettv->v_type == VAR_PARTIAL
5351 && (rettv->vval.v_partial->pt_auto
5352 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005354
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 dict_unref(selfdict);
5356 return ret;
5357}
5358
5359/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005360 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 * value).
5362 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005363 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005366 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367}
5368
5369/*
5370 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * The string "s" must have been allocated, it is consumed.
5372 * Return NULL for out of memory, the variable otherwise.
5373 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005374 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005375alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 rettv = alloc_tv();
5380 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 rettv->v_type = VAR_STRING;
5383 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 else
5386 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388}
5389
5390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005394free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 if (varp != NULL)
5397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 switch (varp->v_type)
5399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005401 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005403 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 vim_free(varp->vval.v_string);
5405 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005406 case VAR_PARTIAL:
5407 partial_unref(varp->vval.v_partial);
5408 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005409 case VAR_BLOB:
5410 blob_unref(varp->vval.v_blob);
5411 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 case VAR_LIST:
5413 list_unref(varp->vval.v_list);
5414 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005415 case VAR_DICT:
5416 dict_unref(varp->vval.v_dict);
5417 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005418 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005419#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005420 job_unref(varp->vval.v_job);
5421 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005422#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005423 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005425 channel_unref(varp->vval.v_channel);
5426 break;
5427#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005428 case VAR_NUMBER:
5429 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005430 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005431 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005433 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005434 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005435 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 vim_free(varp);
5438 }
5439}
5440
5441/*
5442 * Free the memory for a variable value and set the value to NULL or 0.
5443 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 if (varp != NULL)
5448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005452 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005454 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005455 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005457 case VAR_PARTIAL:
5458 partial_unref(varp->vval.v_partial);
5459 varp->vval.v_partial = NULL;
5460 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005461 case VAR_BLOB:
5462 blob_unref(varp->vval.v_blob);
5463 varp->vval.v_blob = NULL;
5464 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 case VAR_LIST:
5466 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005467 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 case VAR_DICT:
5470 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005471 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005474 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005475 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 varp->vval.v_number = 0;
5477 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005478 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005480 varp->vval.v_float = 0.0;
5481 break;
5482#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005484#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 job_unref(varp->vval.v_job);
5486 varp->vval.v_job = NULL;
5487#endif
5488 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005489 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005491 channel_unref(varp->vval.v_channel);
5492 varp->vval.v_channel = NULL;
5493#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005495 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005499 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501}
5502
5503/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 * Set the value of a variable to NULL without freeing items.
5505 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508{
5509 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511}
5512
5513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 * Get the number value of a variable.
5515 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005517 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 * caller of incompatible types: it sets *denote to TRUE if "denote"
5519 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005521 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005522tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 int error = FALSE;
5525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005526 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005527}
5528
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005529 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005530tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005531{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005532 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005537 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005540 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005541 break;
5542#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005544 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005545 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 break;
5547 case VAR_STRING:
5548 if (varp->vval.v_string != NULL)
5549 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005550 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005551 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005552 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005553 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005554 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005555 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005556 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005557 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005558 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005559 case VAR_SPECIAL:
5560 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005561 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005563 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005564 break;
5565#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005566 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005567#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005568 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005569 break;
5570#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005571 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005572 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005573 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005574 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005575 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005577 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005580 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005581 n = -1;
5582 else
5583 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005587#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005589tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005590{
5591 switch (varp->v_type)
5592 {
5593 case VAR_NUMBER:
5594 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005595 case VAR_FLOAT:
5596 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005597 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005598 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005599 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005600 break;
5601 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005603 break;
5604 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005605 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005606 break;
5607 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005608 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005609 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005610 case VAR_BOOL:
5611 emsg(_("E362: Using a boolean value as a Float"));
5612 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005613 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005615 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005616 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005617# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005618 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005619 break;
5620# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005621 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005622# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005623 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005624 break;
5625# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005626 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005627 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005628 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005629 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005630 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005632 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005633 break;
5634 }
5635 return 0;
5636}
5637#endif
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 * Get the string value of a variable.
5641 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005642 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5643 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 * If the String variable has never been set, return an empty string.
5645 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005646 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005647 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005649 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005650tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 static char_u mybuf[NUMBUFLEN];
5653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005654 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655}
5656
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005658tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005661
5662 return res != NULL ? res : (char_u *)"";
5663}
5664
Bram Moolenaar7d647822014-04-05 21:28:56 +02005665/*
5666 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5667 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005668 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005669tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005670{
5671 static char_u mybuf[NUMBUFLEN];
5672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005673 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005674}
5675
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005676 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005677tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005678{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005682 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005683 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005684 return buf;
5685 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005686 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005687 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 break;
5689 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005690 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 break;
5692 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005693 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005695 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005696#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005697 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005698 break;
5699#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700 case VAR_STRING:
5701 if (varp->vval.v_string != NULL)
5702 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005703 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005704 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005705 case VAR_SPECIAL:
5706 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5707 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005708 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005709 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005710 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005711 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005712#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005713 {
5714 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005715 char *status;
5716
5717 if (job == NULL)
5718 return (char_u *)"no process";
5719 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005720 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005721 : "run";
5722# ifdef UNIX
5723 vim_snprintf((char *)buf, NUMBUFLEN,
5724 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005725# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005726 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005727 "process %ld %s",
5728 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005729 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005730# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005731 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005732 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5733# endif
5734 return buf;
5735 }
5736#endif
5737 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005738 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005739#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005740 {
5741 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005742 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005743
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005744 if (channel == NULL)
5745 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5746 else
5747 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005748 "channel %d %s", channel->ch_id, status);
5749 return buf;
5750 }
5751#endif
5752 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005753 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005754 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005756 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005759 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761
5762/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005763 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5764 * string() on Dict, List, etc.
5765 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005766 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005767tv_stringify(typval_T *varp, char_u *buf)
5768{
5769 if (varp->v_type == VAR_LIST
5770 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005771 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005772 || varp->v_type == VAR_FUNC
5773 || varp->v_type == VAR_PARTIAL
5774 || varp->v_type == VAR_FLOAT)
5775 {
5776 typval_T tmp;
5777
5778 f_string(varp, &tmp);
5779 tv_get_string_buf(&tmp, buf);
5780 clear_tv(varp);
5781 *varp = tmp;
5782 return tmp.vval.v_string;
5783 }
5784 return tv_get_string_buf(varp, buf);
5785}
5786
5787/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005788 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5789 * Also give an error message, using "name" or _("name") when use_gettext is
5790 * TRUE.
5791 */
5792 static int
5793tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5794{
5795 int lock = 0;
5796
5797 switch (tv->v_type)
5798 {
5799 case VAR_BLOB:
5800 if (tv->vval.v_blob != NULL)
5801 lock = tv->vval.v_blob->bv_lock;
5802 break;
5803 case VAR_LIST:
5804 if (tv->vval.v_list != NULL)
5805 lock = tv->vval.v_list->lv_lock;
5806 break;
5807 case VAR_DICT:
5808 if (tv->vval.v_dict != NULL)
5809 lock = tv->vval.v_dict->dv_lock;
5810 break;
5811 default:
5812 break;
5813 }
5814 return var_check_lock(tv->v_lock, name, use_gettext)
5815 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5816}
5817
5818/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005821 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005822 * It is OK for "from" and "to" to point to the same item. This is used to
5823 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005829 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 switch (from->v_type)
5831 {
5832 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005833 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005834 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 to->vval.v_number = from->vval.v_number;
5836 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005837 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005838#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005839 to->vval.v_float = from->vval.v_float;
5840 break;
5841#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005842 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005845 if (to->vval.v_job != NULL)
5846 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 break;
5848#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005849 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005850#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005851 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005852 if (to->vval.v_channel != NULL)
5853 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005854 break;
5855#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 case VAR_STRING:
5857 case VAR_FUNC:
5858 if (from->vval.v_string == NULL)
5859 to->vval.v_string = NULL;
5860 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005861 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005863 if (from->v_type == VAR_FUNC)
5864 func_ref(to->vval.v_string);
5865 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005867 case VAR_PARTIAL:
5868 if (from->vval.v_partial == NULL)
5869 to->vval.v_partial = NULL;
5870 else
5871 {
5872 to->vval.v_partial = from->vval.v_partial;
5873 ++to->vval.v_partial->pt_refcount;
5874 }
5875 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005876 case VAR_BLOB:
5877 if (from->vval.v_blob == NULL)
5878 to->vval.v_blob = NULL;
5879 else
5880 {
5881 to->vval.v_blob = from->vval.v_blob;
5882 ++to->vval.v_blob->bv_refcount;
5883 }
5884 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 case VAR_LIST:
5886 if (from->vval.v_list == NULL)
5887 to->vval.v_list = NULL;
5888 else
5889 {
5890 to->vval.v_list = from->vval.v_list;
5891 ++to->vval.v_list->lv_refcount;
5892 }
5893 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 case VAR_DICT:
5895 if (from->vval.v_dict == NULL)
5896 to->vval.v_dict = NULL;
5897 else
5898 {
5899 to->vval.v_dict = from->vval.v_dict;
5900 ++to->vval.v_dict->dv_refcount;
5901 }
5902 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005903 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005904 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005906 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 break;
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909}
5910
5911/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 * Make a copy of an item.
5913 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005914 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5915 * reference to an already copied list/dict can be used.
5916 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919item_copy(
5920 typval_T *from,
5921 typval_T *to,
5922 int deep,
5923 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005924{
5925 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005926 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005930 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005931 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 }
5933 ++recurse;
5934
5935 switch (from->v_type)
5936 {
5937 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005938 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005939 case VAR_STRING:
5940 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005941 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005942 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005943 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005944 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005945 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946 copy_tv(from, to);
5947 break;
5948 case VAR_LIST:
5949 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005950 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005951 if (from->vval.v_list == NULL)
5952 to->vval.v_list = NULL;
5953 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005955 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005956 to->vval.v_list = from->vval.v_list->lv_copylist;
5957 ++to->vval.v_list->lv_refcount;
5958 }
5959 else
5960 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5961 if (to->vval.v_list == NULL)
5962 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005963 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005964 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005966 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005967 case VAR_DICT:
5968 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005969 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005970 if (from->vval.v_dict == NULL)
5971 to->vval.v_dict = NULL;
5972 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5976 ++to->vval.v_dict->dv_refcount;
5977 }
5978 else
5979 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5980 if (to->vval.v_dict == NULL)
5981 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005982 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005983 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005984 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005986 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005988 }
5989 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005990 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991}
5992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 void
5994echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5995{
5996 char_u *tofree;
5997 char_u numbuf[NUMBUFLEN];
5998 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5999
6000 if (*atstart)
6001 {
6002 *atstart = FALSE;
6003 // Call msg_start() after eval1(), evaluating the expression
6004 // may cause a message to appear.
6005 if (with_space)
6006 {
6007 // Mark the saved text as finishing the line, so that what
6008 // follows is displayed on a new line when scrolling back
6009 // at the more prompt.
6010 msg_sb_eol();
6011 msg_start();
6012 }
6013 }
6014 else if (with_space)
6015 msg_puts_attr(" ", echo_attr);
6016
6017 if (p != NULL)
6018 for ( ; *p != NUL && !got_int; ++p)
6019 {
6020 if (*p == '\n' || *p == '\r' || *p == TAB)
6021 {
6022 if (*p != TAB && *needclr)
6023 {
6024 // remove any text still there from the command
6025 msg_clr_eos();
6026 *needclr = FALSE;
6027 }
6028 msg_putchar_attr(*p, echo_attr);
6029 }
6030 else
6031 {
6032 if (has_mbyte)
6033 {
6034 int i = (*mb_ptr2len)(p);
6035
6036 (void)msg_outtrans_len_attr(p, i, echo_attr);
6037 p += i - 1;
6038 }
6039 else
6040 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6041 }
6042 }
6043 vim_free(tofree);
6044}
6045
Bram Moolenaare9a41262005-01-15 22:18:47 +00006046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * ":echo expr1 ..." print each argument separated with a space, add a
6048 * newline at the end.
6049 * ":echon expr1 ..." print each argument plain.
6050 */
6051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 char_u *p;
6057 int needclr = TRUE;
6058 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006059 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006060 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062 if (eap->skip)
6063 ++emsg_skip;
6064 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
6065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006066 // If eval1() causes an error message the text from the command may
6067 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006068 need_clr_eos = needclr;
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006071 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 /*
6074 * Report the invalid expression unless the expression evaluation
6075 * has been cancelled due to an aborting error, an interrupt, or an
6076 * exception.
6077 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006078 if (!aborting() && did_emsg == did_emsg_before
6079 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006080 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006081 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 break;
6083 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006084 need_clr_eos = FALSE;
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 arg = skipwhite(arg);
6091 }
6092 eap->nextcmd = check_nextcmd(arg);
6093
6094 if (eap->skip)
6095 --emsg_skip;
6096 else
6097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (needclr)
6100 msg_clr_eos();
6101 if (eap->cmdidx == CMD_echo)
6102 msg_end();
6103 }
6104}
6105
6106/*
6107 * ":echohl {name}".
6108 */
6109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006112 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}
6114
6115/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006116 * Returns the :echo attribute
6117 */
6118 int
6119get_echo_attr(void)
6120{
6121 return echo_attr;
6122}
6123
6124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 * ":execute expr1 ..." execute the result of an expression.
6126 * ":echomsg expr1 ..." Print a message
6127 * ":echoerr expr1 ..." Print an error
6128 * Each gets spaces around each argument and a newline at the end for
6129 * echo commands
6130 */
6131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006132ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 int ret = OK;
6137 char_u *p;
6138 garray_T ga;
6139 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006140 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 ga_init2(&ga, 1, 80);
6143
6144 if (eap->skip)
6145 ++emsg_skip;
6146 while (*arg != NUL && *arg != '|' && *arg != '\n')
6147 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006148 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6149 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 if (!eap->skip)
6153 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006154 char_u buf[NUMBUFLEN];
6155
6156 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006157 {
6158 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6159 {
6160 emsg(_(e_inval_string));
6161 p = NULL;
6162 }
6163 else
6164 p = tv_get_string_buf(&rettv, buf);
6165 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006166 else
6167 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006168 if (p == NULL)
6169 {
6170 clear_tv(&rettv);
6171 ret = FAIL;
6172 break;
6173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 len = (int)STRLEN(p);
6175 if (ga_grow(&ga, len + 2) == FAIL)
6176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 ret = FAIL;
6179 break;
6180 }
6181 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 ga.ga_len += len;
6185 }
6186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006187 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 arg = skipwhite(arg);
6189 }
6190
6191 if (ret != FAIL && ga.ga_data != NULL)
6192 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006193 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 // Mark the already saved text as finishing the line, so that what
6196 // follows is displayed on a new line when scrolling back at the
6197 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006198 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006199 }
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006202 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006203 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006204 out_flush();
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 else if (eap->cmdidx == CMD_echoerr)
6207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006208 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006210 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if (!force_abort)
6212 did_emsg = save_did_emsg;
6213 }
6214 else if (eap->cmdidx == CMD_execute)
6215 do_cmdline((char_u *)ga.ga_data,
6216 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6217 }
6218
6219 ga_clear(&ga);
6220
6221 if (eap->skip)
6222 --emsg_skip;
6223
6224 eap->nextcmd = check_nextcmd(arg);
6225}
6226
6227/*
6228 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6229 * "arg" points to the "&" or '+' when called, to "option" when returning.
6230 * Returns NULL when no option name found. Otherwise pointer to the char
6231 * after the option name.
6232 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 char_u *p = *arg;
6237
6238 ++p;
6239 if (*p == 'g' && p[1] == ':')
6240 {
6241 *opt_flags = OPT_GLOBAL;
6242 p += 2;
6243 }
6244 else if (*p == 'l' && p[1] == ':')
6245 {
6246 *opt_flags = OPT_LOCAL;
6247 p += 2;
6248 }
6249 else
6250 *opt_flags = 0;
6251
6252 if (!ASCII_ISALPHA(*p))
6253 return NULL;
6254 *arg = p;
6255
6256 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006257 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 else
6259 while (ASCII_ISALPHA(*p))
6260 ++p;
6261 return p;
6262}
6263
6264/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006265 * Display script name where an item was last set.
6266 * Should only be invoked when 'verbose' is non-zero.
6267 */
6268 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006269last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006270{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006271 char_u *p;
6272
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006273 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006274 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006275 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006276 if (p != NULL)
6277 {
6278 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006279 msg_puts(_("\n\tLast set from "));
6280 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006281 if (script_ctx.sc_lnum > 0)
6282 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006283 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006284 msg_outnum((long)script_ctx.sc_lnum);
6285 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006286 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006287 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006288 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006289 }
6290}
6291
Bram Moolenaar61be3762019-03-19 23:04:17 +01006292/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006293 * Compare "typ1" and "typ2". Put the result in "typ1".
6294 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006295 int
6296typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006297 typval_T *typ1, // first operand
6298 typval_T *typ2, // second operand
6299 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006301{
6302 int i;
6303 varnumber_T n1, n2;
6304 char_u *s1, *s2;
6305 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006306 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006307
Bram Moolenaar31988702018-02-13 12:57:42 +01006308 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // For "is" a different type always means FALSE, for "notis"
6311 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006312 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006313 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006314 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6315 {
6316 if (type_is)
6317 {
6318 n1 = (typ1->v_type == typ2->v_type
6319 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006320 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006321 n1 = !n1;
6322 }
6323 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006324 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006325 {
6326 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006327 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006328 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006329 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006330 clear_tv(typ1);
6331 return FAIL;
6332 }
6333 else
6334 {
6335 // Compare two Blobs for being equal or unequal.
6336 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006337 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006338 n1 = !n1;
6339 }
6340 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006341 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6342 {
6343 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006344 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006345 n1 = (typ1->v_type == typ2->v_type
6346 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006347 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006348 n1 = !n1;
6349 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006350 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006351 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006352 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006353 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006354 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006355 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006356 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006357 clear_tv(typ1);
6358 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006359 }
6360 else
6361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006362 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006363 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6364 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006365 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006366 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006367 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006368 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006369
6370 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6371 {
6372 if (type_is)
6373 {
6374 n1 = (typ1->v_type == typ2->v_type
6375 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006376 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006377 n1 = !n1;
6378 }
6379 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006380 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006381 {
6382 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006383 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006384 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006385 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006386 clear_tv(typ1);
6387 return FAIL;
6388 }
6389 else
6390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006391 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006392 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6393 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006394 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006395 n1 = !n1;
6396 }
6397 }
6398
6399 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6400 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6401 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006402 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6403 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006405 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006406 clear_tv(typ1);
6407 return FAIL;
6408 }
6409 if ((typ1->v_type == VAR_PARTIAL
6410 && typ1->vval.v_partial == NULL)
6411 || (typ2->v_type == VAR_PARTIAL
6412 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006413 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006414 n1 = FALSE;
6415 else if (type_is)
6416 {
6417 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006418 // strings are considered the same if their value is
6419 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006420 n1 = tv_equal(typ1, typ2, ic, FALSE);
6421 else if (typ1->v_type == VAR_PARTIAL
6422 && typ2->v_type == VAR_PARTIAL)
6423 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6424 else
6425 n1 = FALSE;
6426 }
6427 else
6428 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006429 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006430 n1 = !n1;
6431 }
6432
6433#ifdef FEAT_FLOAT
6434 /*
6435 * If one of the two variables is a float, compare as a float.
6436 * When using "=~" or "!~", always compare as string.
6437 */
6438 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006439 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006440 {
6441 float_T f1, f2;
6442
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006443 f1 = tv_get_float(typ1);
6444 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006445 n1 = FALSE;
6446 switch (type)
6447 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006448 case EXPR_IS:
6449 case EXPR_EQUAL: n1 = (f1 == f2); break;
6450 case EXPR_ISNOT:
6451 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6452 case EXPR_GREATER: n1 = (f1 > f2); break;
6453 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6454 case EXPR_SMALLER: n1 = (f1 < f2); break;
6455 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6456 case EXPR_UNKNOWN:
6457 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006459 }
6460 }
6461#endif
6462
6463 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006464 * If one of the two variables is a number, compare as a number.
6465 * When using "=~" or "!~", always compare as string.
6466 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006467 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006468 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006469 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006470 n1 = tv_get_number(typ1);
6471 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006472 switch (type)
6473 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006474 case EXPR_IS:
6475 case EXPR_EQUAL: n1 = (n1 == n2); break;
6476 case EXPR_ISNOT:
6477 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6478 case EXPR_GREATER: n1 = (n1 > n2); break;
6479 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6480 case EXPR_SMALLER: n1 = (n1 < n2); break;
6481 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6482 case EXPR_UNKNOWN:
6483 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006485 }
6486 }
6487 else
6488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006489 s1 = tv_get_string_buf(typ1, buf1);
6490 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006491 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006492 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6493 else
6494 i = 0;
6495 n1 = FALSE;
6496 switch (type)
6497 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006498 case EXPR_IS:
6499 case EXPR_EQUAL: n1 = (i == 0); break;
6500 case EXPR_ISNOT:
6501 case EXPR_NEQUAL: n1 = (i != 0); break;
6502 case EXPR_GREATER: n1 = (i > 0); break;
6503 case EXPR_GEQUAL: n1 = (i >= 0); break;
6504 case EXPR_SMALLER: n1 = (i < 0); break;
6505 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006506
Bram Moolenaar87396072019-12-31 22:36:18 +01006507 case EXPR_MATCH:
6508 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006509 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006510 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006511 n1 = !n1;
6512 break;
6513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006515 }
6516 }
6517 clear_tv(typ1);
6518 typ1->v_type = VAR_NUMBER;
6519 typ1->vval.v_number = n1;
6520
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006521 return OK;
6522}
6523
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006524 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006525typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006526{
6527 char_u *tofree;
6528 char_u numbuf[NUMBUFLEN];
6529 char_u *ret = NULL;
6530
6531 if (arg == NULL)
6532 return vim_strsave((char_u *)"(does not exist)");
6533 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006534 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006535 if (ret != NULL && tofree == NULL)
6536 ret = vim_strsave(ret);
6537 return ret;
6538}
6539
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006540#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542/*
6543 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006544 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 * "flags" can be "g" to do a global substitute.
6546 * Returns an allocated string, NULL for error.
6547 */
6548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006549do_string_sub(
6550 char_u *str,
6551 char_u *pat,
6552 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006553 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 int sublen;
6557 regmatch_T regmatch;
6558 int i;
6559 int do_all;
6560 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006561 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 garray_T ga;
6563 char_u *ret;
6564 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006565 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006569 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571 ga_init2(&ga, 1, 200);
6572
6573 do_all = (flags[0] == 'g');
6574
6575 regmatch.rm_ic = p_ic;
6576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6577 if (regmatch.regprog != NULL)
6578 {
6579 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006580 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006584 if (regmatch.startp[0] == regmatch.endp[0])
6585 {
6586 if (zero_width == regmatch.startp[0])
6587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006589 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006590 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6591 (size_t)i);
6592 ga.ga_len += i;
6593 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006594 continue;
6595 }
6596 zero_width = regmatch.startp[0];
6597 }
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /*
6600 * Get some space for a temporary buffer to do the substitution
6601 * into. It will contain:
6602 * - The text up to where the match is.
6603 * - The substituted text.
6604 * - The text after the match.
6605 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006606 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006607 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6609 {
6610 ga_clear(&ga);
6611 break;
6612 }
6613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 i = (int)(regmatch.startp[0] - tail);
6616 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006617 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006618 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 + ga.ga_len + i, TRUE, TRUE, FALSE);
6620 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006621 tail = regmatch.endp[0];
6622 if (*tail == NUL)
6623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (!do_all)
6625 break;
6626 }
6627
6628 if (ga.ga_data != NULL)
6629 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6630
Bram Moolenaar473de612013-06-08 18:19:48 +02006631 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633
6634 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6635 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006636 if (p_cpo == empty_option)
6637 p_cpo = save_cpo;
6638 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006639 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006640 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642 return ret;
6643}