blob: 40c6feb70645fc7eaa8bbfdea9c81992cd16cee3 [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 Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
Bram Moolenaar99880f92021-01-20 21:23:14 +010066 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010068 if (in_vim9script())
69 emsg(_(e_divide_by_zero));
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
Bram Moolenaar99880f92021-01-20 21:23:14 +010089 if (n2 == 0 && in_vim9script())
90 emsg(_(e_divide_by_zero));
Bram Moolenaare21c1582019-03-02 11:57:09 +010091 return (n2 == 0) ? 0 : (n1 % n2);
92}
93
Bram Moolenaara1fa8922017-01-12 20:06:33 +010094#if defined(EBCDIC) || defined(PROTO)
95/*
96 * Compare struct fst by function name.
97 */
98 static int
99compare_func_name(const void *s1, const void *s2)
100{
101 struct fst *p1 = (struct fst *)s1;
102 struct fst *p2 = (struct fst *)s2;
103
104 return STRCMP(p1->f_name, p2->f_name);
105}
106
107/*
108 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100109 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100110 * On machines using EBCDIC we have to sort it.
111 */
112 static void
113sortFunctions(void)
114{
115 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
116
117 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
118}
119#endif
120
Bram Moolenaar33570922005-01-25 22:26:29 +0000121/*
122 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000123 */
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200128 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000129
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130#ifdef EBCDIC
131 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100132 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200133 */
134 sortFunctions();
135#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000136}
137
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138#if defined(EXITFREE) || defined(PROTO)
139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100140eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000141{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200142 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100143 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200144 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000145
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200146 // autoloaded script names
147 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000148
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200149 // unreferenced lists and dicts
150 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200151
152 // functions not garbage collected
153 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000154}
155#endif
156
Bram Moolenaare6b53242020-07-01 17:28:33 +0200157 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200158fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
159{
160 CLEAR_FIELD(*evalarg);
161 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
162 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
163 {
164 evalarg->eval_getline = eap->getline;
165 evalarg->eval_cookie = eap->cookie;
166 }
167}
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/*
170 * Top level evaluation function, returning a boolean.
171 * Sets "error" to TRUE if there was an error.
172 * Return TRUE or FALSE.
173 */
174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100175eval_to_bool(
176 char_u *arg,
177 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200178 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100179 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180{
Bram Moolenaar33570922005-01-25 22:26:29 +0000181 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200182 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200183 evalarg_T evalarg;
184
Bram Moolenaar37c83712020-06-30 21:18:36 +0200185 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187 if (skip)
188 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200189 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 else
192 {
193 *error = FALSE;
194 if (!skip)
195 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200196 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200197 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200198 else
199 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000200 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 }
202 }
203 if (skip)
204 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200205 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200207 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208}
209
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210/*
211 * Call eval1() and give an error message if not done at a lower level.
212 */
213 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200214eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100216 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 int ret;
218 int did_emsg_before = did_emsg;
219 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200220 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200222 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
223
224 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 if (ret == FAIL)
226 {
227 // Report the invalid expression unless the expression evaluation has
228 // been cancelled due to an aborting error, an interrupt, or an
229 // exception, or we already gave a more specific error.
230 // Also check called_emsg for when using assert_fails().
231 if (!aborting() && did_emsg == did_emsg_before
232 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100233 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100234 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200235 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100236 return ret;
237}
238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200240 * Return whether a typval is a valid expression to pass to eval_expr_typval()
241 * or eval_expr_to_bool(). An empty string returns FALSE;
242 */
243 int
244eval_expr_valid_arg(typval_T *tv)
245{
246 return tv->v_type != VAR_UNKNOWN
247 && (tv->v_type != VAR_STRING
248 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
249}
250
251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 * Evaluate an expression, which can be a function, partial or string.
253 * Pass arguments "argv[argc]".
254 * Return the result in "rettv" and OK or FAIL.
255 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200256 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100257eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
258{
259 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200261 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100262
263 if (expr->v_type == VAR_FUNC)
264 {
265 s = expr->vval.v_string;
266 if (s == NULL || *s == NUL)
267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200268 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200269 funcexe.evaluate = TRUE;
270 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 return FAIL;
272 }
273 else if (expr->v_type == VAR_PARTIAL)
274 {
275 partial_T *partial = expr->vval.v_partial;
276
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200277 if (partial == NULL)
278 return FAIL;
279
Bram Moolenaar822ba242020-05-24 23:00:18 +0200280 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200281 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200283 if (call_def_function(partial->pt_func, argc, argv,
284 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 return FAIL;
286 }
287 else
288 {
289 s = partial_name(partial);
290 if (s == NULL || *s == NUL)
291 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200292 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 funcexe.evaluate = TRUE;
294 funcexe.partial = partial;
295 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
296 return FAIL;
297 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 }
299 else
300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100301 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 if (s == NULL)
303 return FAIL;
304 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200305 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100306 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200307 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200309 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100310 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100311 return FAIL;
312 }
313 }
314 return OK;
315}
316
317/*
318 * Like eval_to_bool() but using a typval_T instead of a string.
319 * Works for string, funcref and partial.
320 */
321 int
322eval_expr_to_bool(typval_T *expr, int *error)
323{
324 typval_T rettv;
325 int res;
326
327 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
328 {
329 *error = TRUE;
330 return FALSE;
331 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200332 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100333 clear_tv(&rettv);
334 return res;
335}
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337/*
338 * Top level evaluation function, returning a string. If "skip" is TRUE,
339 * only parsing to "nextcmd" is done, without reporting errors. Return
340 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
341 */
342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100343eval_to_string_skip(
344 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200345 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100346 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
Bram Moolenaar37c83712020-06-30 21:18:36 +0200352 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (skip)
354 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200355 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 retval = NULL;
357 else
358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100359 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000360 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 }
362 if (skip)
363 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200364 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
366 return retval;
367}
368
369/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370 * Skip over an expression at "*pp".
371 * Return FAIL for an error, OK otherwise.
372 */
373 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200374skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000375{
Bram Moolenaar33570922005-01-25 22:26:29 +0000376 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377
378 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200379 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000380}
381
382/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383 * Skip over an expression at "*pp".
384 * If in Vim9 script and line breaks are encountered, the lines are
385 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200386 * "arg" is advanced to just after the expression.
387 * "start" is set to the start of the expression, "end" to just after the end.
388 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 * Return FAIL for an error, OK otherwise.
390 */
391 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200392skip_expr_concatenate(
393 char_u **arg,
394 char_u **start,
395 char_u **end,
396 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397{
398 typval_T rettv;
399 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200400 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200401 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 int evaluate = evalarg == NULL
404 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200406 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 {
409 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200410 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 ++gap->ga_len;
413 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415
416 // Don't evaluate the expression.
417 if (evalarg != NULL)
418 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200419 *arg = skipwhite(*arg);
420 res = eval1(arg, &rettv, evalarg);
421 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 if (evalarg != NULL)
423 evalarg->eval_flags = save_flags;
424
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200425 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200428 if (evalarg->eval_ga.ga_len == 1)
429 {
430 // just one line, no need to concatenate
431 ga_clear(gap);
432 gap->ga_itemsize = 0;
433 }
434 else
435 {
436 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200437 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200438
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200439 // Line breaks encountered, concatenate all the lines.
440 *((char_u **)gap->ga_data) = *start;
441 p = ga_concat_strings(gap, "");
442
443 // free the lines only when using getsourceline()
444 if (evalarg->eval_cookie != NULL)
445 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200446 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200448 // Do not free the last line, "arg" points into it, free it
449 // later.
450 vim_free(evalarg->eval_tofree);
451 evalarg->eval_tofree =
452 ((char_u **)gap->ga_data)[gap->ga_len - 1];
453 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200454 ga_clear_strings(gap);
455 }
456 else
457 ga_clear(gap);
458 gap->ga_itemsize = 0;
459 if (p == NULL)
460 return FAIL;
461 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200462 vim_free(evalarg->eval_tofree_lambda);
463 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 // Compute "end" relative to the end.
465 *end = *start + STRLEN(*start) - endoff;
466 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200467 }
468
469 return res;
470}
471
472/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100473 * Convert "tv" to a string.
474 * When "convert" is TRUE convert a List into a sequence of lines and convert
475 * a Float to a String.
476 * Returns an allocated string (NULL when out of memory).
477 */
478 char_u *
479typval2string(typval_T *tv, int convert)
480{
481 garray_T ga;
482 char_u *retval;
483#ifdef FEAT_FLOAT
484 char_u numbuf[NUMBUFLEN];
485#endif
486
487 if (convert && tv->v_type == VAR_LIST)
488 {
489 ga_init2(&ga, (int)sizeof(char), 80);
490 if (tv->vval.v_list != NULL)
491 {
492 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
493 if (tv->vval.v_list->lv_len > 0)
494 ga_append(&ga, NL);
495 }
496 ga_append(&ga, NUL);
497 retval = (char_u *)ga.ga_data;
498 }
499#ifdef FEAT_FLOAT
500 else if (convert && tv->v_type == VAR_FLOAT)
501 {
502 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
503 retval = vim_strsave(numbuf);
504 }
505#endif
506 else
507 retval = vim_strsave(tv_get_string(tv));
508 return retval;
509}
510
511/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200512 * Top level evaluation function, returning a string. Does not handle line
513 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000514 * When "convert" is TRUE convert a List into a sequence of lines and convert
515 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * Return pointer to allocated memory, or NULL for failure.
517 */
518 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100519eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100520 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521 int convert,
522 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
Bram Moolenaar33570922005-01-25 22:26:29 +0000524 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100526 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100528 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
529 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 retval = NULL;
531 else
532 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100533 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000534 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
538 return retval;
539}
540
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100541 char_u *
542eval_to_string(
543 char_u *arg,
544 int convert)
545{
546 return eval_to_string_eap(arg, convert, NULL);
547}
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000550 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200551 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200552 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100555eval_to_string_safe(
556 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100557 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
559 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200560 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200561 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200563 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200564 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000565 if (use_sandbox)
566 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200567 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200568 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000569 if (use_sandbox)
570 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200571 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return retval;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/*
578 * Top level evaluation function, returning a number.
579 * Evaluates "expr" silently.
580 * Returns -1 for an error.
581 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200582 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000587 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 ++emsg_off;
590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200591 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 retval = -1;
593 else
594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100595 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598 --emsg_off;
599
600 return retval;
601}
602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000604 * Top level evaluation function.
605 * Returns an allocated typval_T with the result.
606 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607 */
608 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200609eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000610{
611 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200612 evalarg_T evalarg;
613
614 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200616 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200617 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaar37c83712020-06-30 21:18:36 +0200620 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621 return tv;
622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100625 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
627 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631call_vim_function(
632 char_u *func,
633 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200634 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200635 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000637 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200638 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100640 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200641 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200642 funcexe.firstline = curwin->w_cursor.lnum;
643 funcexe.lastline = curwin->w_cursor.lnum;
644 funcexe.evaluate = TRUE;
645 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000646 if (ret == FAIL)
647 clear_tv(rettv);
648
649 return ret;
650}
651
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100652/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100653 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100654 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200655 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
656 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100657 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200658 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100659call_func_retnr(
660 char_u *func,
661 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200662 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100663{
664 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200665 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666
Bram Moolenaarded27a12018-08-01 19:06:03 +0200667 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668 return -1;
669
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100670 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100671 clear_tv(&rettv);
672 return retval;
673}
674
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000675/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100676 * Call Vim script function like call_func_retnr() and drop the result.
677 * Returns FAIL when calling the function fails.
678 */
679 int
680call_func_noret(
681 char_u *func,
682 int argc,
683 typval_T *argv)
684{
685 typval_T rettv;
686
687 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
688 return FAIL;
689 clear_tv(&rettv);
690 return OK;
691}
692
693/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100694 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100695 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000697 */
698 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100699call_func_retstr(
700 char_u *func,
701 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200702 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000703{
704 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000705 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000706
Bram Moolenaarded27a12018-08-01 19:06:03 +0200707 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708 return NULL;
709
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100710 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 return retval;
713}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000714
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000715/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100716 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100717 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000718 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000719 */
720 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100721call_func_retlist(
722 char_u *func,
723 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200724 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000725{
726 typval_T rettv;
727
Bram Moolenaarded27a12018-08-01 19:06:03 +0200728 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729 return NULL;
730
731 if (rettv.v_type != VAR_LIST)
732 {
733 clear_tv(&rettv);
734 return NULL;
735 }
736
737 return rettv.vval.v_list;
738}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#ifdef FEAT_FOLDING
741/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100742 * Evaluate "arg", which is 'foldexpr'.
743 * Note: caller must set "curwin" to match "arg".
744 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
745 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 */
747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100748eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200751 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000753 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
754 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000757 if (use_sandbox)
758 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200759 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200761 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 retval = 0;
763 else
764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100765 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000766 if (tv.v_type == VAR_NUMBER)
767 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000768 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 retval = 0;
770 else
771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100772 // If the result is a string, check if there is a non-digit before
773 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000774 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (!VIM_ISDIGIT(*s) && *s != '-')
776 *cp = *s++;
777 retval = atol((char *)s);
778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000782 if (use_sandbox)
783 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200784 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200785 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200787 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 * Get an lval: variable, Dict item or List item that can be assigned a value
793 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
794 * "name.key", "name.key[expr]" etc.
795 * Indexing only works if "name" is an existing List or Dictionary.
796 * "name" points to the start of the name.
797 * If "rettv" is not NULL it points to the value to be assigned.
798 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
799 * wrong; must end in space or cmd separator.
800 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100801 * flags:
802 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100803 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804 * GLV_NO_AUTOLOAD: do not use script autoloading
805 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000807 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200810 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100811get_lval(
812 char_u *name,
813 typval_T *rettv,
814 lval_T *lp,
815 int unlet,
816 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100817 int flags, // GLV_ values
818 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000820 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 char_u *expr_start, *expr_end;
822 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000823 dictitem_T *v;
824 typval_T var1;
825 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000827 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000829 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100830 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100831 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100832 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200835 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100837 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100839 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200841 lp->ll_name_end = find_name_end(name, NULL, NULL,
842 FNE_INCL_BR | fne_flags);
843 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 }
845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000847 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if (expr_start != NULL)
850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100852 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 && *p != '[' && *p != '.')
854 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200855 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 return NULL;
857 }
858
859 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
860 if (lp->ll_exp_name == NULL)
861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100862 // Report an invalid expression in braces, unless the
863 // expression evaluation has been cancelled due to an
864 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 if (!aborting() && !quiet)
866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000867 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 return NULL;
870 }
871 }
872 lp->ll_name = lp->ll_exp_name;
873 }
874 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 lp->ll_name = name;
877
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200878 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200880 // "a: type" is declaring variable "a" with a type, not "a:".
881 if (p == name + 2 && p[-1] == ':')
882 {
883 --p;
884 lp->ll_name_end = p;
885 }
886 if (*p == ':')
887 {
888 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
889 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200891 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100892 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
893 if (lp->ll_type == NULL && !quiet)
894 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200895 lp->ll_name_end = tp;
896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897 }
898 }
899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100900 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
902 return p;
903
904 cc = *p;
905 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100906 // When we would write to the variable pass &ht and prevent autoload.
907 writing = !(flags & GLV_READ_ONLY);
908 v = find_var(lp->ll_name, writing ? &ht : NULL,
909 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200911 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000913 if (v == NULL)
914 return NULL;
915
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100916 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
917 {
918 if (!quiet)
919 semsg(_(e_variable_already_declared), lp->ll_name);
920 return NULL;
921 }
922
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 /*
924 * Loop until no more [idx] or .key is following.
925 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000926 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100927 var1.v_type = VAR_UNKNOWN;
928 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000930 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200932 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100933 && !(lp->ll_tv->v_type == VAR_BLOB
934 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100937 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100943 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000946
Bram Moolenaar10c65862020-10-08 21:16:42 +0200947 if (in_vim9script() && lp->ll_valtype == NULL
948 && lp->ll_tv == &v->di_tv
949 && ht != NULL && ht == get_script_local_ht())
950 {
951 svar_T *sv = find_typval_in_script(lp->ll_tv);
952
953 // Vim9 script local variable: get the type
954 if (sv != NULL)
955 lp->ll_valtype = sv->sv_type;
956 }
957
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 len = -1;
959 if (*p == '.')
960 {
961 key = p + 1;
962 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
963 ;
964 if (len == 0)
965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 }
970 p = key + len;
971 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000972 else
973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100974 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000975 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 if (*p == ':')
977 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000978 else
979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200981 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100983 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100985 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000986 clear_tv(&var1);
987 return NULL;
988 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200989 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 }
991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100992 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 if (*p == ':')
994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200998 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001001 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001002 if (rettv != NULL
1003 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001004 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001005 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001006 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001009 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001010 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 }
1013 p = skipwhite(p + 1);
1014 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001016 else
1017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001019 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001020 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001025 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001026 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001027 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001028 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001029 clear_tv(&var2);
1030 return NULL;
1031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001037
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001042 clear_tv(&var1);
1043 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 }
1046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 ++p;
1049 }
1050
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 {
1053 if (len == -1)
1054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001055 // "[key]": get key from "var1"
1056 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001057 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001061 }
1062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001063 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001064
1065 // a NULL dict is equivalent with an empty dict
1066 if (lp->ll_tv->vval.v_dict == NULL)
1067 {
1068 lp->ll_tv->vval.v_dict = dict_alloc();
1069 if (lp->ll_tv->vval.v_dict == NULL)
1070 {
1071 clear_tv(&var1);
1072 return NULL;
1073 }
1074 ++lp->ll_tv->vval.v_dict->dv_refcount;
1075 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001076 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001077
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001078 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001080 // When assigning to a scope dictionary check that a function and
1081 // variable name is valid (only variable name unless it is l: or
1082 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001083 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001084 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001085 int prevval;
1086 int wrong;
1087
1088 if (len != -1)
1089 {
1090 prevval = key[len];
1091 key[len] = NUL;
1092 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001093 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001094 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1096 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001097 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001098 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001099 if (len != -1)
1100 key[len] = prevval;
1101 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001102 {
1103 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001104 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001105 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001106 }
1107
Bram Moolenaar10c65862020-10-08 21:16:42 +02001108 if (lp->ll_valtype != NULL)
1109 // use the type of the member
1110 lp->ll_valtype = lp->ll_valtype->tt_member;
1111
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001113 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001114 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001115 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001116 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001118 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001119 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001120 return NULL;
1121 }
1122
Bram Moolenaar31b81602019-02-10 22:14:27 +01001123 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 }
1131 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 p = NULL;
1138 break;
1139 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001141 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001142 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1143 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001144 {
1145 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001146 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001147 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001148
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001149 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001152 else if (lp->ll_tv->v_type == VAR_BLOB)
1153 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001154 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1155
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001156 /*
1157 * Get the number and item for the only or first index of the List.
1158 */
1159 if (empty1)
1160 lp->ll_n1 = 0;
1161 else
1162 // is number or string
1163 lp->ll_n1 = (long)tv_get_number(&var1);
1164 clear_tv(&var1);
1165
1166 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001167 || lp->ll_n1 > bloblen
1168 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169 {
1170 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001171 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001172 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001173 return NULL;
1174 }
1175 if (lp->ll_range && !lp->ll_empty2)
1176 {
1177 lp->ll_n2 = (long)tv_get_number(&var2);
1178 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 if (lp->ll_n2 < 0
1180 || lp->ll_n2 >= bloblen
1181 || lp->ll_n2 < lp->ll_n1)
1182 {
1183 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001184 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001185 return NULL;
1186 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001187 }
1188 lp->ll_blob = lp->ll_tv->vval.v_blob;
1189 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001190 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001191 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001192 else
1193 {
1194 /*
1195 * Get the number and item for the only or first index of the List.
1196 */
1197 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001199 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001200 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001201 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001202 clear_tv(&var1);
1203
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 lp->ll_list = lp->ll_tv->vval.v_list;
1206 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1207 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001209 if (lp->ll_n1 < 0)
1210 {
1211 lp->ll_n1 = 0;
1212 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1213 }
1214 }
1215 if (lp->ll_li == NULL)
1216 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001217 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001218 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 }
1222
Bram Moolenaar10c65862020-10-08 21:16:42 +02001223 if (lp->ll_valtype != NULL)
1224 // use the type of the member
1225 lp->ll_valtype = lp->ll_valtype->tt_member;
1226
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 /*
1228 * May need to find the item or absolute index for the second
1229 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 * When no index given: "lp->ll_empty2" is TRUE.
1231 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001235 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001236 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001242 {
1243 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 }
1249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001250 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (lp->ll_n1 < 0)
1252 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1253 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001254 {
1255 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 }
1260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 }
1264
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001265 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return p;
1268}
1269
1270/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275{
1276 vim_free(lp->ll_exp_name);
1277 vim_free(lp->ll_newkey);
1278}
1279
1280/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001281 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001283 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1284 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287set_var_lval(
1288 lval_T *lp,
1289 char_u *endp,
1290 typval_T *rettv,
1291 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001292 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294{
1295 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 listitem_T *ri;
1297 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298
1299 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001301 cc = *endp;
1302 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 if (lp->ll_blob != NULL)
1304 {
1305 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 if (op != NULL && *op != '=')
1308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001309 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 return;
1311 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001312 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001313 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314
1315 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1316 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001317 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001319 if (lp->ll_empty2)
1320 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1321
1322 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001324 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325 return;
1326 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001327 if (lp->ll_empty2)
1328 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 ir = 0;
1331 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1332 blob_set(lp->ll_blob, il,
1333 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334 }
1335 else
1336 {
1337 val = (int)tv_get_number_chk(rettv, &error);
1338 if (!error)
1339 {
1340 garray_T *gap = &lp->ll_blob->bv_ga;
1341
1342 // Allow for appending a byte. Setting a byte beyond
1343 // the end is an error otherwise.
1344 if (lp->ll_n1 < gap->ga_len
1345 || (lp->ll_n1 == gap->ga_len
1346 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1347 {
1348 blob_set(lp->ll_blob, lp->ll_n1, val);
1349 if (lp->ll_n1 == gap->ga_len)
1350 ++gap->ga_len;
1351 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001353 }
1354 }
1355 }
1356 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001358 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001360 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001361 {
1362 emsg(_(e_cannot_mod));
1363 *endp = cc;
1364 return;
1365 }
1366
Bram Moolenaarff697e62019-02-12 22:28:33 +01001367 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001368 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001369 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001370 &tv, &di, TRUE, FALSE) == OK)
1371 {
1372 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001373 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1374 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001375 && tv_op(&tv, rettv, op) == OK)
1376 set_var(lp->ll_name, &tv, FALSE);
1377 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001380 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001381 {
1382 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001383 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001384 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001386 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001387 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001389 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001390 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001391 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001392 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001393 else if (lp->ll_range)
1394 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001395 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001396 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001397
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001398 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001399 {
1400 emsg(_("E996: Cannot lock a range"));
1401 return;
1402 }
1403
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001404 /*
1405 * Check whether any of the list items is locked
1406 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001407 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001408 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001409 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001410 return;
1411 ri = ri->li_next;
1412 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1413 break;
1414 ll_li = ll_li->li_next;
1415 ++ll_n1;
1416 }
1417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 /*
1419 * Assign the List values to the list items.
1420 */
1421 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001423 if (op != NULL && *op != '=')
1424 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1425 else
1426 {
1427 clear_tv(&lp->ll_li->li_tv);
1428 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 ri = ri->li_next;
1431 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1432 break;
1433 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001435 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001436 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 ri = NULL;
1439 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001440 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 lp->ll_li = lp->ll_li->li_next;
1443 ++lp->ll_n1;
1444 }
1445 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001446 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 else if (lp->ll_empty2
1448 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001450 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451 }
1452 else
1453 {
1454 /*
1455 * Assign to a List or Dictionary item.
1456 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001457 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001458 {
1459 emsg(_("E996: Cannot lock a list or dict"));
1460 return;
1461 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001462
1463 if (lp->ll_valtype != NULL
1464 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1465 return;
1466
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 if (lp->ll_newkey != NULL)
1468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469 if (op != NULL && *op != '=')
1470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001471 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 return;
1473 }
1474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001475 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001476 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 if (di == NULL)
1478 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001479 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1480 {
1481 vim_free(di);
1482 return;
1483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486 else if (op != NULL && *op != '=')
1487 {
1488 tv_op(lp->ll_tv, rettv, op);
1489 return;
1490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 /*
1495 * Assign the value to the variable or list item.
1496 */
1497 if (copy)
1498 copy_tv(rettv, lp->ll_tv);
1499 else
1500 {
1501 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001502 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 }
1505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506}
1507
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001508/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1510 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 * Returns OK or FAIL.
1512 */
1513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001516 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001517 char_u numbuf[NUMBUFLEN];
1518 char_u *s;
1519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001520 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001521 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001522 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 {
1524 switch (tv1->v_type)
1525 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001526 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001527 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 case VAR_DICT:
1530 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001531 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001532 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001533 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001534 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 break;
1537
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001538 case VAR_BLOB:
1539 if (*op != '+' || tv2->v_type != VAR_BLOB)
1540 break;
1541 // BLOB += BLOB
1542 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1543 {
1544 blob_T *b1 = tv1->vval.v_blob;
1545 blob_T *b2 = tv2->vval.v_blob;
1546 int i, len = blob_len(b2);
1547 for (i = 0; i < len; i++)
1548 ga_append(&b1->bv_ga, blob_get(b2, i));
1549 }
1550 return OK;
1551
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 case VAR_LIST:
1553 if (*op != '+' || tv2->v_type != VAR_LIST)
1554 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001555 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001556 if (tv2->vval.v_list != NULL)
1557 {
1558 if (tv1->vval.v_list == NULL)
1559 {
1560 tv1->vval.v_list = tv2->vval.v_list;
1561 ++tv1->vval.v_list->lv_refcount;
1562 }
1563 else
1564 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 return OK;
1567
1568 case VAR_NUMBER:
1569 case VAR_STRING:
1570 if (tv2->v_type == VAR_LIST)
1571 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001575 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576#ifdef FEAT_FLOAT
1577 if (tv2->v_type == VAR_FLOAT)
1578 {
1579 float_T f = n;
1580
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 if (*op == '%')
1582 break;
1583 switch (*op)
1584 {
1585 case '+': f += tv2->vval.v_float; break;
1586 case '-': f -= tv2->vval.v_float; break;
1587 case '*': f *= tv2->vval.v_float; break;
1588 case '/': f /= tv2->vval.v_float; break;
1589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 clear_tv(tv1);
1591 tv1->v_type = VAR_FLOAT;
1592 tv1->vval.v_float = f;
1593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595#endif
1596 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 switch (*op)
1598 {
1599 case '+': n += tv_get_number(tv2); break;
1600 case '-': n -= tv_get_number(tv2); break;
1601 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001602 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1603 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001604 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 clear_tv(tv1);
1606 tv1->v_type = VAR_NUMBER;
1607 tv1->vval.v_number = n;
1608 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 }
1610 else
1611 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612 if (tv2->v_type == VAR_FLOAT)
1613 break;
1614
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 s = tv_get_string(tv1);
1617 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 clear_tv(tv1);
1619 tv1->v_type = VAR_STRING;
1620 tv1->vval.v_string = s;
1621 }
1622 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001625#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 {
1627 float_T f;
1628
Bram Moolenaarff697e62019-02-12 22:28:33 +01001629 if (*op == '%' || *op == '.'
1630 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 && tv2->v_type != VAR_NUMBER
1632 && tv2->v_type != VAR_STRING))
1633 break;
1634 if (tv2->v_type == VAR_FLOAT)
1635 f = tv2->vval.v_float;
1636 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001637 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001638 switch (*op)
1639 {
1640 case '+': tv1->vval.v_float += f; break;
1641 case '-': tv1->vval.v_float -= f; break;
1642 case '*': tv1->vval.v_float *= f; break;
1643 case '/': tv1->vval.v_float /= f; break;
1644 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001645 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001646#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001647 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 }
1649 }
1650
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001651 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001652 return FAIL;
1653}
1654
1655/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 * Evaluate the expression used in a ":for var in expr" command.
1657 * "arg" points to "var".
1658 * Set "*errp" to TRUE for an error, FALSE otherwise;
1659 * Return a pointer that holds the info. Null when there is an error.
1660 */
1661 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662eval_for_line(
1663 char_u *arg,
1664 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001665 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001666 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667{
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001670 typval_T tv;
1671 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001672 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001674 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001676 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 if (fi == NULL)
1678 return NULL;
1679
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001680 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 if (expr == NULL)
1682 return fi;
1683
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 expr = skipwhite_and_linebreak(expr, evalarg);
1685 if (expr[0] != 'i' || expr[1] != 'n'
1686 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 return fi;
1690 }
1691
1692 if (skip)
1693 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001694 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1695 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 {
1697 *errp = FALSE;
1698 if (!skip)
1699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 l = tv.vval.v_list;
1703 if (l == NULL)
1704 {
1705 // a null list is like an empty list: do nothing
1706 clear_tv(&tv);
1707 }
1708 else
1709 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001711 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 // No need to increment the refcount, it's already set for
1714 // the list being used in "tv".
1715 fi->fi_list = l;
1716 list_add_watch(l, &fi->fi_lw);
1717 fi->fi_lw.lw_item = l->lv_first;
1718 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001719 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001721 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 fi->fi_bi = 0;
1723 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001725 typval_T btv;
1726
1727 // Make a copy, so that the iteration still works when the
1728 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001733 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 else
1735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001736 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 }
1739 }
1740 }
1741 if (skip)
1742 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001743 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744
1745 return fi;
1746}
1747
1748/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001749 * Used when looping over a :for line, skip the "in expr" part.
1750 */
1751 void
1752skip_for_lines(void *fi_void, evalarg_T *evalarg)
1753{
1754 forinfo_T *fi = (forinfo_T *)fi_void;
1755 int i;
1756
1757 for (i = 0; i < fi->fi_break_count; ++i)
1758 eval_next_line(evalarg);
1759}
1760
1761/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 * Use the first item in a ":for" list. Advance to the next.
1763 * Assign the values to the variable (list). "arg" points to the first one.
1764 * Return TRUE when a valid item was found, FALSE when at end of list or
1765 * something wrong.
1766 */
1767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001768next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001772 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001775 if (fi->fi_blob != NULL)
1776 {
1777 typval_T tv;
1778
1779 if (fi->fi_bi >= blob_len(fi->fi_blob))
1780 return FALSE;
1781 tv.v_type = VAR_NUMBER;
1782 tv.v_lock = VAR_FIXED;
1783 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1784 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001785 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001786 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001787 }
1788
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 item = fi->fi_lw.lw_item;
1790 if (item == NULL)
1791 result = FALSE;
1792 else
1793 {
1794 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001795 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001796 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 }
1798 return result;
1799}
1800
1801/*
1802 * Free the structure used to store info used by ":for".
1803 */
1804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806{
Bram Moolenaar33570922005-01-25 22:26:29 +00001807 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001809 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001810 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001812 list_unref(fi->fi_list);
1813 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001814 if (fi != NULL && fi->fi_blob != NULL)
1815 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 vim_free(fi);
1817}
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820set_context_for_expression(
1821 expand_T *xp,
1822 char_u *arg,
1823 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001825 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001829 if (cmdidx == CMD_let || cmdidx == CMD_var
1830 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 {
1832 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001833 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001835 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001836 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 {
1838 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001839 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001840 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 break;
1842 }
1843 return;
1844 }
1845 }
1846 else
1847 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1848 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 while ((xp->xp_pattern = vim_strpbrk(arg,
1850 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1851 {
1852 c = *xp->xp_pattern;
1853 if (c == '&')
1854 {
1855 c = xp->xp_pattern[1];
1856 if (c == '&')
1857 {
1858 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001859 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001864 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1865 xp->xp_pattern += 2;
1866
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 else if (c == '$')
1870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 xp->xp_context = EXPAND_ENV_VARS;
1873 }
1874 else if (c == '=')
1875 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001876 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 xp->xp_context = EXPAND_EXPRESSION;
1878 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001879 else if (c == '#'
1880 && xp->xp_context == EXPAND_EXPRESSION)
1881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001883 break;
1884 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001885 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 && xp->xp_context == EXPAND_FUNCTIONS
1887 && vim_strchr(xp->xp_pattern, '(') == NULL)
1888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001889 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 break;
1891 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001892 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001894 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1897 if (c == '\\' && xp->xp_pattern[1] != NUL)
1898 ++xp->xp_pattern;
1899 xp->xp_context = EXPAND_NOTHING;
1900 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001903 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1905 /* skip */ ;
1906 xp->xp_context = EXPAND_NOTHING;
1907 }
1908 else if (c == '|')
1909 {
1910 if (xp->xp_pattern[1] == '|')
1911 {
1912 ++xp->xp_pattern;
1913 xp->xp_context = EXPAND_EXPRESSION;
1914 }
1915 else
1916 xp->xp_context = EXPAND_COMMANDS;
1917 }
1918 else
1919 xp->xp_context = EXPAND_EXPRESSION;
1920 }
1921 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 // Doesn't look like something valid, expand as an expression
1923 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 arg = xp->xp_pattern;
1926 if (*arg != NUL)
1927 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1928 /* skip */ ;
1929 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001930
1931 // ":exe one two" completes "two"
1932 if ((cmdidx == CMD_execute
1933 || cmdidx == CMD_echo
1934 || cmdidx == CMD_echon
1935 || cmdidx == CMD_echomsg)
1936 && xp->xp_context == EXPAND_EXPRESSION)
1937 {
1938 for (;;)
1939 {
1940 char_u *n = skiptowhite(arg);
1941
1942 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1943 break;
1944 arg = skipwhite(n);
1945 }
1946 }
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 xp->xp_pattern = arg;
1949}
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001952 * Return TRUE if "pat" matches "text".
1953 * Does not use 'cpo' and always uses 'magic'.
1954 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001955 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001956pattern_match(char_u *pat, char_u *text, int ic)
1957{
1958 int matches = FALSE;
1959 char_u *save_cpo;
1960 regmatch_T regmatch;
1961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001962 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001963 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001964 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001965 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1966 if (regmatch.regprog != NULL)
1967 {
1968 regmatch.rm_ic = ic;
1969 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1970 vim_regfree(regmatch.regprog);
1971 }
1972 p_cpo = save_cpo;
1973 return matches;
1974}
1975
1976/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001977 * Handle a name followed by "(". Both for just "name(arg)" and for
1978 * "expr->name(arg)".
1979 * Returns OK or FAIL.
1980 */
1981 static int
1982eval_func(
1983 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001984 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001985 char_u *name,
1986 int name_len,
1987 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001988 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001989 typval_T *basetv) // "expr" for "expr->name(arg)"
1990{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001991 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 char_u *s = name;
1993 int len = name_len;
1994 partial_T *partial;
1995 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001996 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001997
1998 if (!evaluate)
1999 check_vars(s, len);
2000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // If "s" is the name of a variable of type VAR_FUNC
2002 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 s = deref_func_name(s, &len, &partial,
2004 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002006 // Need to make a copy, in case evaluating the arguments makes
2007 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002009 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010 ret = FAIL;
2011 else
2012 {
2013 funcexe_T funcexe;
2014
2015 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002016 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002017 funcexe.firstline = curwin->w_cursor.lnum;
2018 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 funcexe.evaluate = evaluate;
2020 funcexe.partial = partial;
2021 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002022 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002023 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 }
2025 vim_free(s);
2026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // If evaluate is FALSE rettv->v_type was not set in
2028 // get_func_tv, but it's needed in handle_subscript() to parse
2029 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2031 {
2032 rettv->vval.v_string = NULL;
2033 rettv->v_type = VAR_FUNC;
2034 }
2035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002036 // Stop the expression evaluation when immediately
2037 // aborting on error, or when an interrupt occurred or
2038 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 if (evaluate && aborting())
2040 {
2041 if (ret == OK)
2042 clear_tv(rettv);
2043 ret = FAIL;
2044 }
2045 return ret;
2046}
2047
2048/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002049 * Get the next line source line without advancing. But do skip over comment
2050 * lines.
2051 */
2052 static char_u *
2053getline_peek_skip_comments(evalarg_T *evalarg)
2054{
2055 for (;;)
2056 {
2057 char_u *next = getline_peek(evalarg->eval_getline,
2058 evalarg->eval_cookie);
2059 char_u *p;
2060
2061 if (next == NULL)
2062 break;
2063 p = skipwhite(next);
2064 if (*p != NUL && !vim9_comment_start(p))
2065 return next;
2066 (void)eval_next_line(evalarg);
2067 }
2068 return NULL;
2069}
2070
2071/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002072 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2073 * comment) and there is a next line, return the next line (skipping blanks)
2074 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002075 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2076 * "arg" must point somewhere inside a line, not at the start.
2077 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002078 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002079eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2080{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002081 char_u *p = skipwhite(arg);
2082
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002083 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002084 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002086 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002087 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002089 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002090
2091 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002092 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002097 {
2098 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100 }
2101 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103}
2104
2105/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002106 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002107 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002108 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002109eval_next_line(evalarg_T *evalarg)
2110{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002111 garray_T *gap = &evalarg->eval_ga;
2112 char_u *line;
2113
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002114 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002115 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2116 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002117 else
2118 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002119 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002120 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2121 {
2122 // Going to concatenate the lines after parsing.
2123 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2124 ++gap->ga_len;
2125 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002126 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002127 {
2128 vim_free(evalarg->eval_tofree);
2129 evalarg->eval_tofree = line;
2130 }
2131 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002132}
2133
Bram Moolenaare6b53242020-07-01 17:28:33 +02002134/*
2135 * Call eval_next_non_blank() and get the next line if needed.
2136 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002137 char_u *
2138skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2139{
2140 int getnext;
2141 char_u *p = skipwhite(arg);
2142
Bram Moolenaar962d7212020-07-04 14:15:00 +02002143 if (evalarg == NULL)
2144 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002145 eval_next_non_blank(p, evalarg, &getnext);
2146 if (getnext)
2147 return eval_next_line(evalarg);
2148 return p;
2149}
2150
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002151/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002152 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002153 */
2154 void
2155clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2156{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002157 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002158 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002160 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002161 if (eap != NULL)
2162 {
2163 // We may need to keep the original command line, e.g. for
2164 // ":let" it has the variable names. But we may also need the
2165 // new one, "nextcmd" points into it. Keep both.
2166 vim_free(eap->cmdline_tofree);
2167 eap->cmdline_tofree = *eap->cmdlinep;
2168 *eap->cmdlinep = evalarg->eval_tofree;
2169 }
2170 else
2171 vim_free(evalarg->eval_tofree);
2172 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174
2175 vim_free(evalarg->eval_tofree_lambda);
2176 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002177 }
2178}
2179
2180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2184 */
2185
2186/*
2187 * Handle zero level expression.
2188 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002190 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * Return OK or FAIL.
2193 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195eval0(
2196 char_u *arg,
2197 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002198 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int ret;
2202 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002203 int did_emsg_before = did_emsg;
2204 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002208 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002209 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002210
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002211 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 /*
2216 * Report the invalid expression unless the expression evaluation has
2217 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002218 * exception, or we already gave a more specific error.
2219 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002221 if (!aborting()
2222 && did_emsg == did_emsg_before
2223 && called_emsg == called_emsg_before
2224 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002225 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002226
2227 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002228 // a next command to avoid more errors, unless "|" is following, which
2229 // could only be a command separator.
2230 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2231 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002232 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002234
2235 if (eap != NULL)
2236 eap->nextcmd = check_nextcmd(p);
2237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 return ret;
2239}
2240
2241/*
2242 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002243 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002244 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 *
2246 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002247 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002249 * Note: "rettv.v_lock" is not set.
2250 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Return OK or FAIL.
2252 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002253 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002256 char_u *p;
2257 int getnext;
2258
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002259 CLEAR_POINTER(rettv);
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /*
2262 * Get the first variable.
2263 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 return FAIL;
2266
Bram Moolenaar793648f2020-06-26 21:28:25 +02002267 p = eval_next_non_blank(*arg, evalarg, &getnext);
2268 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002270 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 int result;
2272 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002273 evalarg_T *evalarg_used = evalarg;
2274 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002276 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002277 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278
2279 if (evalarg == NULL)
2280 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002281 CLEAR_FIELD(local_evalarg);
2282 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 orig_flags = evalarg_used->eval_flags;
2285 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002287 if (getnext)
2288 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002289 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002290 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002291 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002292 {
2293 error_white_both(p, 1);
2294 clear_tv(rettv);
2295 return FAIL;
2296 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002297 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002298 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303 int error = FALSE;
2304
Bram Moolenaar13106602020-10-04 16:06:05 +02002305 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002306 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002307 else if (vim9script)
2308 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002309 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002311 if (error || !op_falsy || !result)
2312 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002313 if (error)
2314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316
2317 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002318 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002320 if (op_falsy)
2321 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002322 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002323 {
2324 error_white_both(p, 1);
2325 clear_tv(rettv);
2326 return FAIL;
2327 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002329 evalarg_used->eval_flags = (op_falsy ? !result : result)
2330 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2331 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002332 {
2333 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002335 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 if (!op_falsy || !result)
2337 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002339 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002341 /*
2342 * Check for the ":".
2343 */
2344 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2345 if (*p != ':')
2346 {
2347 emsg(_(e_missing_colon));
2348 if (evaluate && result)
2349 clear_tv(rettv);
2350 evalarg_used->eval_flags = orig_flags;
2351 return FAIL;
2352 }
2353 if (getnext)
2354 *arg = eval_next_line(evalarg_used);
2355 else
2356 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002357 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 {
2359 error_white_both(p, 1);
2360 clear_tv(rettv);
2361 evalarg_used->eval_flags = orig_flags;
2362 return FAIL;
2363 }
2364 *arg = p;
2365 }
2366
2367 /*
2368 * Get the third variable. Recursive!
2369 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002370 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002371 {
2372 error_white_both(p, 1);
2373 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002374 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002375 return FAIL;
2376 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002377 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2378 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002379 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002380 if (eval1(arg, &var2, evalarg_used) == FAIL)
2381 {
2382 if (evaluate && result)
2383 clear_tv(rettv);
2384 evalarg_used->eval_flags = orig_flags;
2385 return FAIL;
2386 }
2387 if (evaluate && !result)
2388 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002390
2391 if (evalarg == NULL)
2392 clear_evalarg(&local_evalarg, NULL);
2393 else
2394 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396
2397 return OK;
2398}
2399
2400/*
2401 * Handle first level expression:
2402 * expr2 || expr2 || expr2 logical OR
2403 *
2404 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002405 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 *
2407 * Return OK or FAIL.
2408 */
2409 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002410eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002412 char_u *p;
2413 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /*
2416 * Get the first variable.
2417 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return FAIL;
2420
2421 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002424 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002427 evalarg_T *evalarg_used = evalarg;
2428 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 int evaluate;
2430 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 long result = FALSE;
2432 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002433 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002434 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002435
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 if (evalarg == NULL)
2437 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 CLEAR_FIELD(local_evalarg);
2439 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002441 orig_flags = evalarg_used->eval_flags;
2442 evaluate = orig_flags & EVAL_EVALUATE;
2443 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002445 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002446 result = tv_get_bool_chk(rettv, &error);
2447 else if (tv_get_number_chk(rettv, &error) != 0)
2448 result = TRUE;
2449 clear_tv(rettv);
2450 if (error)
2451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
2454 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 while (p[0] == '|' && p[1] == '|')
2458 {
2459 if (getnext)
2460 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002461 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002462 {
2463 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2464 {
2465 error_white_both(p, 2);
2466 clear_tv(rettv);
2467 return FAIL;
2468 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002469 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002470 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471
2472 /*
2473 * Get the second variable.
2474 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002475 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2476 {
2477 error_white_both(p, 2);
2478 clear_tv(rettv);
2479 return FAIL;
2480 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2482 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486
2487 /*
2488 * Compute the result.
2489 */
2490 if (evaluate && !result)
2491 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002492 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002493 result = tv_get_bool_chk(&var2, &error);
2494 else if (tv_get_number_chk(&var2, &error) != 0)
2495 result = TRUE;
2496 clear_tv(&var2);
2497 if (error)
2498 return FAIL;
2499 }
2500 if (evaluate)
2501 {
2502 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002503 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002504 rettv->v_type = VAR_BOOL;
2505 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002506 }
2507 else
2508 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002509 rettv->v_type = VAR_NUMBER;
2510 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002511 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513
2514 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002516
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002517 if (evalarg == NULL)
2518 clear_evalarg(&local_evalarg, NULL);
2519 else
2520 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522
2523 return OK;
2524}
2525
2526/*
2527 * Handle second level expression:
2528 * expr3 && expr3 && expr3 logical AND
2529 *
2530 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002531 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 *
2533 * Return OK or FAIL.
2534 */
2535 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002538 char_u *p;
2539 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 /*
2542 * Get the first variable.
2543 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 return FAIL;
2546
2547 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002548 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002550 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553 evalarg_T *evalarg_used = evalarg;
2554 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555 int orig_flags;
2556 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 long result = TRUE;
2558 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002559 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002560 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002561
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562 if (evalarg == NULL)
2563 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 CLEAR_FIELD(local_evalarg);
2565 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002566 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 orig_flags = evalarg_used->eval_flags;
2568 evaluate = orig_flags & EVAL_EVALUATE;
2569 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002571 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002572 result = tv_get_bool_chk(rettv, &error);
2573 else if (tv_get_number_chk(rettv, &error) == 0)
2574 result = FALSE;
2575 clear_tv(rettv);
2576 if (error)
2577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
2580 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 while (p[0] == '&' && p[1] == '&')
2584 {
2585 if (getnext)
2586 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002587 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002588 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002589 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002590 {
2591 error_white_both(p, 2);
2592 clear_tv(rettv);
2593 return FAIL;
2594 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002595 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002596 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597
2598 /*
2599 * Get the second variable.
2600 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002601 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2602 {
2603 error_white_both(p, 2);
2604 clear_tv(rettv);
2605 return FAIL;
2606 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2608 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002610 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002613
2614 /*
2615 * Compute the result.
2616 */
2617 if (evaluate && result)
2618 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002619 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002620 result = tv_get_bool_chk(&var2, &error);
2621 else if (tv_get_number_chk(&var2, &error) == 0)
2622 result = FALSE;
2623 clear_tv(&var2);
2624 if (error)
2625 return FAIL;
2626 }
2627 if (evaluate)
2628 {
2629 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002630 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 rettv->v_type = VAR_BOOL;
2632 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002633 }
2634 else
2635 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 rettv->v_type = VAR_NUMBER;
2637 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002638 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640
2641 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002643
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002644 if (evalarg == NULL)
2645 clear_evalarg(&local_evalarg, NULL);
2646 else
2647 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
2650 return OK;
2651}
2652
2653/*
2654 * Handle third level expression:
2655 * var1 == var2
2656 * var1 =~ var2
2657 * var1 != var2
2658 * var1 !~ var2
2659 * var1 > var2
2660 * var1 >= var2
2661 * var1 < var2
2662 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002663 * var1 is var2
2664 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 *
2666 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002667 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 *
2669 * Return OK or FAIL.
2670 */
2671 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002675 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002676 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002678 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 /*
2681 * Get the first variable.
2682 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002683 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 return FAIL;
2685
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002686 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002687 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002690 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002692 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002694 typval_T var2;
2695 int ic;
2696 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002697 int evaluate = evalarg == NULL
2698 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002699
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002700 if (getnext)
2701 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002702 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2703 {
2704 error_white_both(p, len);
2705 clear_tv(rettv);
2706 return FAIL;
2707 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002708
Bram Moolenaar696ba232020-07-29 21:20:41 +02002709 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2710 {
2711 semsg(_(e_invexpr2), p);
2712 clear_tv(rettv);
2713 return FAIL;
2714 }
2715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002716 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (p[len] == '?')
2718 {
2719 ic = TRUE;
2720 ++len;
2721 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002722 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 else if (p[len] == '#')
2724 {
2725 ic = FALSE;
2726 ++len;
2727 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002728 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002730 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * Get the second variable.
2734 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002735 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2736 {
2737 error_white_both(p, 1);
2738 clear_tv(rettv);
2739 return FAIL;
2740 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002741 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002742 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return FAIL;
2746 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002747 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002748 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002749 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002750
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002751 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002752 {
2753 ret = FAIL;
2754 clear_tv(rettv);
2755 }
2756 else
2757 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002758 clear_tv(&var2);
2759 return ret;
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762
2763 return OK;
2764}
2765
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002766/*
2767 * Make a copy of blob "tv1" and append blob "tv2".
2768 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 void
2770eval_addblob(typval_T *tv1, typval_T *tv2)
2771{
2772 blob_T *b1 = tv1->vval.v_blob;
2773 blob_T *b2 = tv2->vval.v_blob;
2774 blob_T *b = blob_alloc();
2775 int i;
2776
2777 if (b != NULL)
2778 {
2779 for (i = 0; i < blob_len(b1); i++)
2780 ga_append(&b->bv_ga, blob_get(b1, i));
2781 for (i = 0; i < blob_len(b2); i++)
2782 ga_append(&b->bv_ga, blob_get(b2, i));
2783
2784 clear_tv(tv1);
2785 rettv_blob_set(tv1, b);
2786 }
2787}
2788
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002789/*
2790 * Make a copy of list "tv1" and append list "tv2".
2791 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 int
2793eval_addlist(typval_T *tv1, typval_T *tv2)
2794{
2795 typval_T var3;
2796
2797 // concatenate Lists
2798 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2799 {
2800 clear_tv(tv1);
2801 clear_tv(tv2);
2802 return FAIL;
2803 }
2804 clear_tv(tv1);
2805 *tv1 = var3;
2806 return OK;
2807}
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809/*
2810 * Handle fourth level expression:
2811 * + number addition
2812 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002813 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002814 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 *
2816 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002817 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 *
2819 * Return OK or FAIL.
2820 */
2821 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 /*
2825 * Get the first variable.
2826 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return FAIL;
2829
2830 /*
2831 * Repeat computing, until no '+', '-' or '.' is following.
2832 */
2833 for (;;)
2834 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002835 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836 int getnext;
2837 char_u *p;
2838 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002839 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 int concat;
2841 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002842 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002844 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002845 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002846 p = eval_next_non_blank(*arg, evalarg, &getnext);
2847 op = *p;
2848 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002849 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002851
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002852 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002853 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002855 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002856 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002857 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002858 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002859 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002860 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002861 clear_tv(rettv);
2862 return FAIL;
2863 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002864 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002865 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002866 if ((op != '+' || (rettv->v_type != VAR_LIST
2867 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002868#ifdef FEAT_FLOAT
2869 && (op == '.' || rettv->v_type != VAR_FLOAT)
2870#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002871 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002872 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002873 int error = FALSE;
2874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002875 // For "list + ...", an illegal use of the first operand as
2876 // a number cannot be determined before evaluating the 2nd
2877 // operand: if this is also a list, all is ok.
2878 // For "something . ...", "something - ..." or "non-list + ...",
2879 // we know that the first operand needs to be a string or number
2880 // without evaluating the 2nd operand. So check before to avoid
2881 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002882 if (op != '.')
2883 tv_get_number_chk(rettv, &error);
2884 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 {
2886 clear_tv(rettv);
2887 return FAIL;
2888 }
2889 }
2890
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 /*
2892 * Get the second variable.
2893 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002894 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002895 {
2896 error_white_both(p, oplen);
2897 clear_tv(rettv);
2898 return FAIL;
2899 }
2900 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002901 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return FAIL;
2905 }
2906
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002907 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 /*
2910 * Compute the result.
2911 */
2912 if (op == '.')
2913 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2915 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002916 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917
Bram Moolenaar2e086612020-08-25 22:37:48 +02002918 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002919 || var2.v_type == VAR_CHANNEL
2920 || var2.v_type == VAR_JOB))
2921 emsg(_(e_inval_string));
2922#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002923 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002924 {
2925 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2926 var2.vval.v_float);
2927 s2 = buf2;
2928 }
2929#endif
2930 else
2931 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002932 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002933 {
2934 clear_tv(rettv);
2935 clear_tv(&var2);
2936 return FAIL;
2937 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 clear_tv(rettv);
2940 rettv->v_type = VAR_STRING;
2941 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002943 else if (op == '+' && rettv->v_type == VAR_BLOB
2944 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002946 else if (op == '+' && rettv->v_type == VAR_LIST
2947 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002948 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002950 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 else
2953 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002954 int error = FALSE;
2955 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002956#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002957 float_T f1 = 0, f2 = 0;
2958
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002959 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002961 f1 = rettv->vval.v_float;
2962 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002964 else
2965#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002967 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002968 if (error)
2969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002970 // This can only happen for "list + non-list". For
2971 // "non-list + ..." or "something - ...", we returned
2972 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 clear_tv(rettv);
2974 return FAIL;
2975 }
2976#ifdef FEAT_FLOAT
2977 if (var2.v_type == VAR_FLOAT)
2978 f1 = n1;
2979#endif
2980 }
2981#ifdef FEAT_FLOAT
2982 if (var2.v_type == VAR_FLOAT)
2983 {
2984 f2 = var2.vval.v_float;
2985 n2 = 0;
2986 }
2987 else
2988#endif
2989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002990 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991 if (error)
2992 {
2993 clear_tv(rettv);
2994 clear_tv(&var2);
2995 return FAIL;
2996 }
2997#ifdef FEAT_FLOAT
2998 if (rettv->v_type == VAR_FLOAT)
2999 f2 = n2;
3000#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003
3004#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003005 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3007 {
3008 if (op == '+')
3009 f1 = f1 + f2;
3010 else
3011 f1 = f1 - f2;
3012 rettv->v_type = VAR_FLOAT;
3013 rettv->vval.v_float = f1;
3014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016#endif
3017 {
3018 if (op == '+')
3019 n1 = n1 + n2;
3020 else
3021 n1 = n1 - n2;
3022 rettv->v_type = VAR_NUMBER;
3023 rettv->vval.v_number = n1;
3024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003026 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 }
3028 }
3029 return OK;
3030}
3031
3032/*
3033 * Handle fifth level expression:
3034 * * number multiplication
3035 * / number division
3036 * % number modulo
3037 *
3038 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003039 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 *
3041 * Return OK or FAIL.
3042 */
3043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044eval6(
3045 char_u **arg,
3046 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003048 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003051 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /*
3055 * Get the first variable.
3056 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return FAIL;
3059
3060 /*
3061 * Repeat computing, until no '*', '/' or '%' is following.
3062 */
3063 for (;;)
3064 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003065 int evaluate;
3066 int getnext;
3067 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003069 int op;
3070 varnumber_T n1, n2;
3071#ifdef FEAT_FLOAT
3072 float_T f1, f2;
3073#endif
3074 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075
Bram Moolenaar9d489562020-07-30 20:08:50 +02003076 p = eval_next_non_blank(*arg, evalarg, &getnext);
3077 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003078 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003080
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003081 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003082 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003083 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003084 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003085 {
3086 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3087 {
3088 error_white_both(p, 1);
3089 clear_tv(rettv);
3090 return FAIL;
3091 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003092 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095#ifdef FEAT_FLOAT
3096 f1 = 0;
3097 f2 = 0;
3098#endif
3099 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003100 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#ifdef FEAT_FLOAT
3103 if (rettv->v_type == VAR_FLOAT)
3104 {
3105 f1 = rettv->vval.v_float;
3106 use_float = TRUE;
3107 n1 = 0;
3108 }
3109 else
3110#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003112 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003113 if (error)
3114 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
3116 else
3117 n1 = 0;
3118
3119 /*
3120 * Get the second variable.
3121 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003122 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3123 {
3124 error_white_both(p, 1);
3125 clear_tv(rettv);
3126 return FAIL;
3127 }
3128 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 return FAIL;
3131
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003132 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#ifdef FEAT_FLOAT
3135 if (var2.v_type == VAR_FLOAT)
3136 {
3137 if (!use_float)
3138 {
3139 f1 = n1;
3140 use_float = TRUE;
3141 }
3142 f2 = var2.vval.v_float;
3143 n2 = 0;
3144 }
3145 else
3146#endif
3147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 clear_tv(&var2);
3150 if (error)
3151 return FAIL;
3152#ifdef FEAT_FLOAT
3153 if (use_float)
3154 f2 = n2;
3155#endif
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /*
3159 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162#ifdef FEAT_FLOAT
3163 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 if (op == '*')
3166 f1 = f1 * f2;
3167 else if (op == '/')
3168 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003169# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003170 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003171 if (f2 == 0.0)
3172 {
3173 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003174 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003175 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003176 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003177 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003178 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003179 }
3180 else
3181 f1 = f1 / f2;
3182# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // We rely on the floating point library to handle divide
3184 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003185 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 return FAIL;
3192 }
3193 rettv->v_type = VAR_FLOAT;
3194 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003199 if (op == '*')
3200 n1 = n1 * n2;
3201 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003202 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003204 n1 = num_modulus(n1, n2);
3205
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 rettv->v_type = VAR_NUMBER;
3207 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 }
3211
3212 return OK;
3213}
3214
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003215 int
3216eval_leader(char_u **arg, int vim9)
3217{
3218 char_u *s = *arg;
3219 char_u *p = *arg;
3220
3221 while (*p == '!' || *p == '-' || *p == '+')
3222 {
3223 char_u *n = skipwhite(p + 1);
3224
3225 // ++, --, -+ and +- are not accepted in Vim9 script
3226 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3227 {
3228 semsg(_(e_invexpr2), s);
3229 return FAIL;
3230 }
3231 p = n;
3232 }
3233 *arg = p;
3234 return OK;
3235}
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237/*
3238 * Handle sixth level expression:
3239 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003240 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003241 * "string" string constant
3242 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * &option-name option value
3244 * @r register contents
3245 * identifier variable value
3246 * function() function call
3247 * $VAR environment variable
3248 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003249 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003251 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003252 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 *
3254 * Also handle:
3255 * ! in front logical NOT
3256 * - in front unary minus
3257 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003258 * trailing [] subscript in String or List
3259 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003260 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 *
3262 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003263 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 *
3265 * Return OK or FAIL.
3266 */
3267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval7(
3269 char_u **arg,
3270 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003271 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003274 int evaluate = evalarg != NULL
3275 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 int len;
3277 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 char_u *start_leader, *end_leader;
3279 int ret = OK;
3280 char_u *alias;
3281
3282 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003284 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003286 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003289 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 */
3291 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003292 if (eval_leader(arg, in_vim9script()) == FAIL)
3293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 end_leader = *arg;
3295
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003296 if (**arg == '.' && (!isdigit(*(*arg + 1))
3297#ifdef FEAT_FLOAT
3298 || current_sctx.sc_version < 2
3299#endif
3300 ))
3301 {
3302 semsg(_(e_invexpr2), *arg);
3303 ++*arg;
3304 return FAIL;
3305 }
3306
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 switch (**arg)
3308 {
3309 /*
3310 * Number constant.
3311 */
3312 case '0':
3313 case '1':
3314 case '2':
3315 case '3':
3316 case '4':
3317 case '5':
3318 case '6':
3319 case '7':
3320 case '8':
3321 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003322 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003323
3324 // Apply prefixed "-" and "+" now. Matters especially when
3325 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003326 if (ret == OK && evaluate && end_leader > start_leader
3327 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003328 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 break;
3330
3331 /*
3332 * String constant: "string".
3333 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003334 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 break;
3336
3337 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003338 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003340 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003341 break;
3342
3343 /*
3344 * List: [expr, expr]
3345 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003346 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 break;
3348
3349 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003350 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003351 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003352 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003353 {
3354 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003355 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003356 }
3357 else
3358 ret = NOTDONE;
3359 break;
3360
3361 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003362 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003363 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003364 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003365 case '{': if (in_vim9script())
3366 ret = NOTDONE;
3367 else
3368 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003369 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003370 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371 break;
3372
3373 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003374 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003376 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 break;
3378
3379 /*
3380 * Environment variable: $VAR.
3381 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 break;
3384
3385 /*
3386 * Register contents: @r.
3387 */
3388 case '@': ++*arg;
3389 if (evaluate)
3390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003392 rettv->vval.v_string = get_reg_contents(**arg,
3393 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 if (**arg != NUL)
3396 ++*arg;
3397 break;
3398
3399 /*
3400 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003401 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003403 case '(': ret = NOTDONE;
3404 if (in_vim9script())
3405 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3406 if (ret == NOTDONE)
3407 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003408 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003409 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003410
Bram Moolenaar9215f012020-06-27 21:18:00 +02003411 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412 if (**arg == ')')
3413 ++*arg;
3414 else if (ret == OK)
3415 {
3416 emsg(_(e_missing_close));
3417 clear_tv(rettv);
3418 ret = FAIL;
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421 break;
3422
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 break;
3425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003426
3427 if (ret == NOTDONE)
3428 {
3429 /*
3430 * Must be a variable or function name.
3431 * Can also be a curly-braces kind of name: {expr}.
3432 */
3433 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003434 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003435 if (alias != NULL)
3436 s = alias;
3437
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003438 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003439 ret = FAIL;
3440 else
3441 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003442 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3443
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003444 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3445 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003446 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003447 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003448 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003449 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003450 else if (flags & EVAL_CONSTANT)
3451 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003453 {
3454 // get the value of "true", "false" or a variable
3455 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3456 {
3457 rettv->v_type = VAR_BOOL;
3458 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003459 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003460 }
3461 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003462 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003463 {
3464 rettv->v_type = VAR_BOOL;
3465 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003466 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003467 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003468 else if (len == 4 && in_vim9script()
3469 && STRNCMP(s, "null", 4) == 0)
3470 {
3471 rettv->v_type = VAR_SPECIAL;
3472 rettv->vval.v_number = VVAL_NULL;
3473 ret = OK;
3474 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003475 else
3476 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003479 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003481 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003485 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003486 }
3487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003488 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3489 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003490 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003491 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 /*
3494 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3495 */
3496 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003497 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003498 return ret;
3499}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003500
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003501/*
3502 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003503 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003504 * Adjusts "end_leaderp" until it is at "start_leader".
3505 */
3506 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003507eval7_leader(
3508 typval_T *rettv,
3509 int numeric_only,
3510 char_u *start_leader,
3511 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003512{
3513 char_u *end_leader = *end_leaderp;
3514 int ret = OK;
3515 int error = FALSE;
3516 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003517 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003518#ifdef FEAT_FLOAT
3519 float_T f = 0.0;
3520
3521 if (rettv->v_type == VAR_FLOAT)
3522 f = rettv->vval.v_float;
3523 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003524#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003525 {
3526 while (VIM_ISWHITE(end_leader[-1]))
3527 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003528 if (in_vim9script() && end_leader[-1] == '!')
3529 val = tv2bool(rettv);
3530 else
3531 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003532 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003533 if (error)
3534 {
3535 clear_tv(rettv);
3536 ret = FAIL;
3537 }
3538 else
3539 {
3540 while (end_leader > start_leader)
3541 {
3542 --end_leader;
3543 if (*end_leader == '!')
3544 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003545 if (numeric_only)
3546 {
3547 ++end_leader;
3548 break;
3549 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003550#ifdef FEAT_FLOAT
3551 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003552 {
3553 if (in_vim9script())
3554 {
3555 rettv->v_type = VAR_BOOL;
3556 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3557 }
3558 else
3559 f = !f;
3560 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003561 else
3562#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003563 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003564 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003565 type = VAR_BOOL;
3566 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003567 }
3568 else if (*end_leader == '-')
3569 {
3570#ifdef FEAT_FLOAT
3571 if (rettv->v_type == VAR_FLOAT)
3572 f = -f;
3573 else
3574#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003575 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003576 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003577 type = VAR_NUMBER;
3578 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003579 }
3580 }
3581#ifdef FEAT_FLOAT
3582 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003585 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003587 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003588#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003590 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003591 if (in_vim9script())
3592 rettv->v_type = type;
3593 else
3594 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003595 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003598 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return ret;
3600}
3601
3602/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003603 * Call the function referred to in "rettv".
3604 */
3605 static int
3606call_func_rettv(
3607 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003608 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003609 typval_T *rettv,
3610 int evaluate,
3611 dict_T *selfdict,
3612 typval_T *basetv)
3613{
3614 partial_T *pt = NULL;
3615 funcexe_T funcexe;
3616 typval_T functv;
3617 char_u *s;
3618 int ret;
3619
3620 // need to copy the funcref so that we can clear rettv
3621 if (evaluate)
3622 {
3623 functv = *rettv;
3624 rettv->v_type = VAR_UNKNOWN;
3625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003627 if (functv.v_type == VAR_PARTIAL)
3628 {
3629 pt = functv.vval.v_partial;
3630 s = partial_name(pt);
3631 }
3632 else
3633 s = functv.vval.v_string;
3634 }
3635 else
3636 s = (char_u *)"";
3637
Bram Moolenaara80faa82020-04-12 19:37:17 +02003638 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003639 funcexe.firstline = curwin->w_cursor.lnum;
3640 funcexe.lastline = curwin->w_cursor.lnum;
3641 funcexe.evaluate = evaluate;
3642 funcexe.partial = pt;
3643 funcexe.selfdict = selfdict;
3644 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003645 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // Clear the funcref afterwards, so that deleting it while
3648 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003649 if (evaluate)
3650 clear_tv(&functv);
3651
3652 return ret;
3653}
3654
3655/*
3656 * Evaluate "->method()".
3657 * "*arg" points to the '-'.
3658 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3659 */
3660 static int
3661eval_lambda(
3662 char_u **arg,
3663 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003664 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003665 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003666{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003667 int evaluate = evalarg != NULL
3668 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003669 typval_T base = *rettv;
3670 int ret;
3671
3672 // Skip over the ->.
3673 *arg += 2;
3674 rettv->v_type = VAR_UNKNOWN;
3675
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003676 if (**arg == '{')
3677 {
3678 // ->{lambda}()
3679 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3680 }
3681 else
3682 {
3683 // ->(lambda)()
3684 ++*arg;
3685 ret = eval1(arg, rettv, evalarg);
3686 *arg = skipwhite_and_linebreak(*arg, evalarg);
3687 if (**arg != ')')
3688 {
3689 emsg(_(e_missing_close));
3690 ret = FAIL;
3691 }
3692 ++*arg;
3693 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003694 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003695 return FAIL;
3696 else if (**arg != '(')
3697 {
3698 if (verbose)
3699 {
3700 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003701 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003702 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003704 }
3705 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003706 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003707 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003708 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003709 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003710
3711 // Clear the funcref afterwards, so that deleting it while
3712 // evaluating the arguments is possible (see test55).
3713 if (evaluate)
3714 clear_tv(&base);
3715
3716 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003717}
3718
3719/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003720 * Evaluate "->method()".
3721 * "*arg" points to the '-'.
3722 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3723 */
3724 static int
3725eval_method(
3726 char_u **arg,
3727 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003728 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003730{
3731 char_u *name;
3732 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003733 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003734 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003735 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003736 int evaluate = evalarg != NULL
3737 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003738
3739 // Skip over the ->.
3740 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003741 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003742
Bram Moolenaarac92e252019-08-03 21:58:38 +02003743 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003744 len = get_name_len(arg, &alias, evaluate, TRUE);
3745 if (alias != NULL)
3746 name = alias;
3747
3748 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003749 {
3750 if (verbose)
3751 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003752 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003753 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003754 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003755 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003756 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003757 if (**arg != '(')
3758 {
3759 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003761 ret = FAIL;
3762 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003763 else if (VIM_ISWHITE((*arg)[-1]))
3764 {
3765 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003766 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003767 ret = FAIL;
3768 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003769 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003770 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003771 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003772 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003773
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003774 // Clear the funcref afterwards, so that deleting it while
3775 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003776 if (evaluate)
3777 clear_tv(&base);
3778
Bram Moolenaarac92e252019-08-03 21:58:38 +02003779 return ret;
3780}
3781
3782/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003783 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3784 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003785 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3786 */
3787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003788eval_index(
3789 char_u **arg,
3790 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003791 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003792 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003793{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003794 int evaluate = evalarg != NULL
3795 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003796 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003797 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003798 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003799 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003800 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003801 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003802
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003803 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3804 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003805
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003806 init_tv(&var1);
3807 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003808 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003810 /*
3811 * dict.name
3812 */
3813 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003814 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003815 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003816 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003817 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003818 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003819 }
3820 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003822 /*
3823 * something[idx]
3824 *
3825 * Get the (first) variable from inside the [].
3826 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003827 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 if (**arg == ':')
3829 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003830 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003832 else if (vim9 && **arg == ':')
3833 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003834 semsg(_(e_white_space_required_before_and_after_str_at_str),
3835 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003836 clear_tv(&var1);
3837 return FAIL;
3838 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003839 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003841 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 clear_tv(&var1);
3843 return FAIL;
3844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845
3846 /*
3847 * Get the second variable from inside the [:].
3848 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003849 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003850 if (**arg == ':')
3851 {
3852 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003853 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003854 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003855 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003856 semsg(_(e_white_space_required_before_and_after_str_at_str),
3857 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003858 if (!empty1)
3859 clear_tv(&var1);
3860 return FAIL;
3861 }
3862 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 if (**arg == ']')
3864 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003865 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 if (!empty1)
3868 clear_tv(&var1);
3869 return FAIL;
3870 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003871 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 if (!empty1)
3875 clear_tv(&var1);
3876 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003877 return FAIL;
3878 }
3879 }
3880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003882 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003883 if (**arg != ']')
3884 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003885 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003886 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003887 clear_tv(&var1);
3888 if (range)
3889 clear_tv(&var2);
3890 return FAIL;
3891 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003892 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003893 }
3894
3895 if (evaluate)
3896 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003897 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003898 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003899 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003900
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003901 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003903 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003904 clear_tv(&var2);
3905 return res;
3906 }
3907 return OK;
3908}
3909
3910/*
3911 * Check if "rettv" can have an [index] or [sli:ce]
3912 */
3913 int
3914check_can_index(typval_T *rettv, int evaluate, int verbose)
3915{
3916 switch (rettv->v_type)
3917 {
3918 case VAR_FUNC:
3919 case VAR_PARTIAL:
3920 if (verbose)
3921 emsg(_("E695: Cannot index a Funcref"));
3922 return FAIL;
3923 case VAR_FLOAT:
3924#ifdef FEAT_FLOAT
3925 if (verbose)
3926 emsg(_(e_float_as_string));
3927 return FAIL;
3928#endif
3929 case VAR_BOOL:
3930 case VAR_SPECIAL:
3931 case VAR_JOB:
3932 case VAR_CHANNEL:
3933 if (verbose)
3934 emsg(_(e_cannot_index_special_variable));
3935 return FAIL;
3936 case VAR_UNKNOWN:
3937 case VAR_ANY:
3938 case VAR_VOID:
3939 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003941 emsg(_(e_cannot_index_special_variable));
3942 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003943 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003944 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003945
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003946 case VAR_STRING:
3947 case VAR_LIST:
3948 case VAR_DICT:
3949 case VAR_BLOB:
3950 break;
3951 case VAR_NUMBER:
3952 if (in_vim9script())
3953 emsg(_(e_cannot_index_number));
3954 break;
3955 }
3956 return OK;
3957}
3958
3959/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003960 * slice() function
3961 */
3962 void
3963f_slice(typval_T *argvars, typval_T *rettv)
3964{
3965 if (check_can_index(argvars, TRUE, FALSE) == OK)
3966 {
3967 copy_tv(argvars, rettv);
3968 eval_index_inner(rettv, TRUE, argvars + 1,
3969 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
3970 TRUE, NULL, 0, FALSE);
3971 }
3972}
3973
3974/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003975 * Apply index or range to "rettv".
3976 * "var1" is the first index, NULL for [:expr].
3977 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01003978 * "exclusive" is TRUE for slice(): second index is exclusive, use character
3979 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003980 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3981 */
3982 int
3983eval_index_inner(
3984 typval_T *rettv,
3985 int is_range,
3986 typval_T *var1,
3987 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003988 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003989 char_u *key,
3990 int keylen,
3991 int verbose)
3992{
Bram Moolenaar6601b622021-01-13 21:47:15 +01003993 varnumber_T n1, n2 = 0;
3994 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003995
3996 n1 = 0;
3997 if (var1 != NULL && rettv->v_type != VAR_DICT)
3998 n1 = tv_get_number(var1);
3999
4000 if (is_range)
4001 {
4002 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004004 if (verbose)
4005 emsg(_(e_cannot_slice_dictionary));
4006 return FAIL;
4007 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004008 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004009 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004010 else
4011 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004013
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004014 switch (rettv->v_type)
4015 {
4016 case VAR_UNKNOWN:
4017 case VAR_ANY:
4018 case VAR_VOID:
4019 case VAR_FUNC:
4020 case VAR_PARTIAL:
4021 case VAR_FLOAT:
4022 case VAR_BOOL:
4023 case VAR_SPECIAL:
4024 case VAR_JOB:
4025 case VAR_CHANNEL:
4026 break; // not evaluating, skipping over subscript
4027
4028 case VAR_NUMBER:
4029 case VAR_STRING:
4030 {
4031 char_u *s = tv_get_string(rettv);
4032
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004033 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004034 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004035 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004036 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004037 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004038 else
4039 s = char_from_string(s, n1);
4040 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004041 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 // The resulting variable is a substring. If the indexes
4044 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004045 if (n1 < 0)
4046 {
4047 n1 = len + n1;
4048 if (n1 < 0)
4049 n1 = 0;
4050 }
4051 if (n2 < 0)
4052 n2 = len + n2;
4053 else if (n2 >= len)
4054 n2 = len;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004055 if (exclusive)
4056 --n2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004057 if (n1 >= len || n2 < 0 || n1 > n2)
4058 s = NULL;
4059 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004060 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004061 }
4062 else
4063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // The resulting variable is a string of a single
4065 // character. If the index is too big or negative the
4066 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004067 if (n1 >= len || n1 < 0)
4068 s = NULL;
4069 else
4070 s = vim_strnsave(s + n1, 1);
4071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(rettv);
4073 rettv->v_type = VAR_STRING;
4074 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004075 }
4076 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004077
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 case VAR_BLOB:
4079 len = blob_len(rettv->vval.v_blob);
4080 if (is_range)
4081 {
4082 // The resulting variable is a sub-blob. If the indexes
4083 // are out of range the result is empty.
4084 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004085 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004086 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004087 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004088 n1 = 0;
4089 }
4090 if (n2 < 0)
4091 n2 = len + n2;
4092 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004093 n2 = len - (exclusive ? 0 : 1);
4094 if (exclusive)
4095 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004096 if (n1 >= len || n2 < 0 || n1 > n2)
4097 {
4098 clear_tv(rettv);
4099 rettv->v_type = VAR_BLOB;
4100 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004101 }
4102 else
4103 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004104 blob_T *blob = blob_alloc();
4105 long i;
4106
4107 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004108 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004112 return FAIL;
4113 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004114 blob->bv_ga.ga_len = n2 - n1 + 1;
4115 for (i = n1; i <= n2; i++)
4116 blob_set(blob, i - n1,
4117 blob_get(rettv->vval.v_blob, i));
4118
4119 clear_tv(rettv);
4120 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 }
4124 else
4125 {
4126 // The resulting variable is a byte value.
4127 // If the index is too big or negative that is an error.
4128 if (n1 < 0)
4129 n1 = len + n1;
4130 if (n1 < len && n1 >= 0)
4131 {
4132 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004133
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 clear_tv(rettv);
4135 rettv->v_type = VAR_NUMBER;
4136 rettv->vval.v_number = v;
4137 }
4138 else
4139 semsg(_(e_blobidx), n1);
4140 }
4141 break;
4142
4143 case VAR_LIST:
4144 if (var1 == NULL)
4145 n1 = 0;
4146 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004147 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004148 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004149 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 return FAIL;
4151 break;
4152
4153 case VAR_DICT:
4154 {
4155 dictitem_T *item;
4156 typval_T tmp;
4157
4158 if (key == NULL)
4159 {
4160 key = tv_get_string_chk(var1);
4161 if (key == NULL)
4162 return FAIL;
4163 }
4164
4165 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4166
4167 if (item == NULL && verbose)
4168 semsg(_(e_dictkey), key);
4169 if (item == NULL)
4170 return FAIL;
4171
4172 copy_tv(&item->di_tv, &tmp);
4173 clear_tv(rettv);
4174 *rettv = tmp;
4175 }
4176 break;
4177 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004178 return OK;
4179}
4180
4181/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004182 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004183 */
4184 char_u *
4185partial_name(partial_T *pt)
4186{
4187 if (pt->pt_name != NULL)
4188 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004189 if (pt->pt_func != NULL)
4190 return pt->pt_func->uf_name;
4191 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004192}
4193
Bram Moolenaarddecc252016-04-06 22:59:37 +02004194 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004195partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004196{
4197 int i;
4198
4199 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004200 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004201 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004202 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 if (pt->pt_name != NULL)
4204 {
4205 func_unref(pt->pt_name);
4206 vim_free(pt->pt_name);
4207 }
4208 else
4209 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004210
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004211 // Decrease the reference count for the context of a closure. If down
4212 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004213 if (pt->pt_funcstack != NULL)
4214 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004215 --pt->pt_funcstack->fs_refcount;
4216 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004217 }
4218
Bram Moolenaarddecc252016-04-06 22:59:37 +02004219 vim_free(pt);
4220}
4221
4222/*
4223 * Unreference a closure: decrement the reference count and free it when it
4224 * becomes zero.
4225 */
4226 void
4227partial_unref(partial_T *pt)
4228{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004229 if (pt != NULL)
4230 {
4231 if (--pt->pt_refcount <= 0)
4232 partial_free(pt);
4233
4234 // If the reference count goes down to one, the funcstack may be the
4235 // only reference and can be freed if no other partials reference it.
4236 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4237 funcstack_check_refcount(pt->pt_funcstack);
4238 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004239}
4240
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004241/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004242 * Return the next (unique) copy ID.
4243 * Used for serializing nested structures.
4244 */
4245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004247{
4248 current_copyID += COPYID_INC;
4249 return current_copyID;
4250}
4251
4252/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004253 * Garbage collection for lists and dictionaries.
4254 *
4255 * We use reference counts to be able to free most items right away when they
4256 * are no longer used. But for composite items it's possible that it becomes
4257 * unused while the reference count is > 0: When there is a recursive
4258 * reference. Example:
4259 * :let l = [1, 2, 3]
4260 * :let d = {9: l}
4261 * :let l[1] = d
4262 *
4263 * Since this is quite unusual we handle this with garbage collection: every
4264 * once in a while find out which lists and dicts are not referenced from any
4265 * variable.
4266 *
4267 * Here is a good reference text about garbage collection (refers to Python
4268 * but it applies to all reference-counting mechanisms):
4269 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004270 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004271
4272/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004273 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004274 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004275 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004276 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004277 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004278garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004279{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004280 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004281 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004282 buf_T *buf;
4283 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004284 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004285 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004286
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004287 if (!testing)
4288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004289 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004290 want_garbage_collect = FALSE;
4291 may_garbage_collect = FALSE;
4292 garbage_collect_at_exit = FALSE;
4293 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004294
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004295 // The execution stack can grow big, limit the size.
4296 if (exestack.ga_maxlen - exestack.ga_len > 500)
4297 {
4298 size_t new_len;
4299 char_u *pp;
4300 int n;
4301
4302 // Keep 150% of the current size, with a minimum of the growth size.
4303 n = exestack.ga_len / 2;
4304 if (n < exestack.ga_growsize)
4305 n = exestack.ga_growsize;
4306
4307 // Don't make it bigger though.
4308 if (exestack.ga_len + n < exestack.ga_maxlen)
4309 {
4310 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4311 pp = vim_realloc(exestack.ga_data, new_len);
4312 if (pp == NULL)
4313 return FAIL;
4314 exestack.ga_maxlen = exestack.ga_len + n;
4315 exestack.ga_data = pp;
4316 }
4317 }
4318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004319 // We advance by two because we add one for items referenced through
4320 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004321 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004322
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004323 /*
4324 * 1. Go through all accessible variables and mark all lists and dicts
4325 * with copyID.
4326 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004328 // Don't free variables in the previous_funccal list unless they are only
4329 // referenced through previous_funccal. This must be first, because if
4330 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004334 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004336 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004337 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004338 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4339 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004342 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4344 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004345 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004346 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4347 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004348#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004349 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004350 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4351 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004352 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004353 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004354 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4355 NULL, NULL);
4356#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004359 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4361 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004362 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004363 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004366 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004369 abort = abort || set_ref_in_functions(copyID);
4370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004371 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004374 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004375 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004376
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004377 // callbacks in buffers
4378 abort = abort || set_ref_in_buffers(copyID);
4379
Bram Moolenaar1dced572012-04-05 16:54:08 +02004380#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004381 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004382#endif
4383
Bram Moolenaardb913952012-06-29 12:54:53 +02004384#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004386#endif
4387
4388#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004389 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004390#endif
4391
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004392#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004393 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004394 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004395#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004396#ifdef FEAT_NETBEANS_INTG
4397 abort = abort || set_ref_in_nb_channel(copyID);
4398#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004399
Bram Moolenaare3188e22016-05-31 21:13:04 +02004400#ifdef FEAT_TIMERS
4401 abort = abort || set_ref_in_timer(copyID);
4402#endif
4403
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004404#ifdef FEAT_QUICKFIX
4405 abort = abort || set_ref_in_quickfix(copyID);
4406#endif
4407
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004408#ifdef FEAT_TERMINAL
4409 abort = abort || set_ref_in_term(copyID);
4410#endif
4411
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004412#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004413 abort = abort || set_ref_in_popups(copyID);
4414#endif
4415
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004416 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004417 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004418 /*
4419 * 2. Free lists and dictionaries that are not referenced.
4420 */
4421 did_free = free_unref_items(copyID);
4422
4423 /*
4424 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004428 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004429 else if (p_verbose > 0)
4430 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004431 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004432 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004433
4434 return did_free;
4435}
4436
4437/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004438 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004439 */
4440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004441free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004442{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004443 int did_free = FALSE;
4444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004445 // Let all "free" functions know that we are here. This means no
4446 // dictionaries, lists, channels or jobs are to be freed, because we will
4447 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004448 in_free_unref_items = TRUE;
4449
4450 /*
4451 * PASS 1: free the contents of the items. We don't free the items
4452 * themselves yet, so that it is possible to decrement refcount counters
4453 */
4454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004456 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004458 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004459 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004460
4461#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004462 // Go through the list of jobs and free items without the copyID. This
4463 // must happen before doing channels, because jobs refer to channels, but
4464 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004465 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004467 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004468 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4469#endif
4470
4471 /*
4472 * PASS 2: free the items themselves.
4473 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004474 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004475 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004476
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // Go through the list of jobs and free items without the copyID. This
4479 // must happen before doing channels, because jobs refer to channels, but
4480 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004481 free_unused_jobs(copyID, COPYID_MASK);
4482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004484 free_unused_channels(copyID, COPYID_MASK);
4485#endif
4486
4487 in_free_unref_items = FALSE;
4488
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004489 return did_free;
4490}
4491
4492/*
4493 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004494 * "list_stack" is used to add lists to be marked. Can be NULL.
4495 *
4496 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004497 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004499set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004500{
4501 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004502 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004503 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 hashtab_T *cur_ht;
4505 ht_stack_T *ht_stack = NULL;
4506 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 cur_ht = ht;
4509 for (;;)
4510 {
4511 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // Mark each item in the hashtab. If the item contains a hashtab
4514 // it is added to ht_stack, if it contains a list it is added to
4515 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004516 todo = (int)cur_ht->ht_used;
4517 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4518 if (!HASHITEM_EMPTY(hi))
4519 {
4520 --todo;
4521 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4522 &ht_stack, list_stack);
4523 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004524 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004525
4526 if (ht_stack == NULL)
4527 break;
4528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 cur_ht = ht_stack->ht;
4531 tempitem = ht_stack;
4532 ht_stack = ht_stack->prev;
4533 free(tempitem);
4534 }
4535
4536 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004537}
4538
4539/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004540 * Mark a dict and its items with "copyID".
4541 * Returns TRUE if setting references failed somehow.
4542 */
4543 int
4544set_ref_in_dict(dict_T *d, int copyID)
4545{
4546 if (d != NULL && d->dv_copyID != copyID)
4547 {
4548 d->dv_copyID = copyID;
4549 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4550 }
4551 return FALSE;
4552}
4553
4554/*
4555 * Mark a list and its items with "copyID".
4556 * Returns TRUE if setting references failed somehow.
4557 */
4558 int
4559set_ref_in_list(list_T *ll, int copyID)
4560{
4561 if (ll != NULL && ll->lv_copyID != copyID)
4562 {
4563 ll->lv_copyID = copyID;
4564 return set_ref_in_list_items(ll, copyID, NULL);
4565 }
4566 return FALSE;
4567}
4568
4569/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004570 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004571 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4572 *
4573 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004575 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004576set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004577{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004578 listitem_T *li;
4579 int abort = FALSE;
4580 list_T *cur_l;
4581 list_stack_T *list_stack = NULL;
4582 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004583
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004584 cur_l = l;
4585 for (;;)
4586 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004587 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 // Mark each item in the list. If the item contains a hashtab
4589 // it is added to ht_stack, if it contains a list it is added to
4590 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004591 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4592 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4593 ht_stack, &list_stack);
4594 if (list_stack == NULL)
4595 break;
4596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004598 cur_l = list_stack->list;
4599 tempitem = list_stack;
4600 list_stack = list_stack->prev;
4601 free(tempitem);
4602 }
4603
4604 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004605}
4606
4607/*
4608 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 * "list_stack" is used to add lists to be marked. Can be NULL.
4610 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4611 *
4612 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004613 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004615set_ref_in_item(
4616 typval_T *tv,
4617 int copyID,
4618 ht_stack_T **ht_stack,
4619 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004620{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004621 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004622
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004623 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004624 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004625 dict_T *dd = tv->vval.v_dict;
4626
Bram Moolenaara03f2332016-02-06 18:09:59 +01004627 if (dd != NULL && dd->dv_copyID != copyID)
4628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004630 dd->dv_copyID = copyID;
4631 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004632 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004633 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4634 }
4635 else
4636 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004637 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4638
Bram Moolenaara03f2332016-02-06 18:09:59 +01004639 if (newitem == NULL)
4640 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004641 else
4642 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004643 newitem->ht = &dd->dv_hashtab;
4644 newitem->prev = *ht_stack;
4645 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004646 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004647 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004648 }
4649 }
4650 else if (tv->v_type == VAR_LIST)
4651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004652 list_T *ll = tv->vval.v_list;
4653
Bram Moolenaara03f2332016-02-06 18:09:59 +01004654 if (ll != NULL && ll->lv_copyID != copyID)
4655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004656 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004657 ll->lv_copyID = copyID;
4658 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004659 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004660 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004661 }
4662 else
4663 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004664 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4665
Bram Moolenaara03f2332016-02-06 18:09:59 +01004666 if (newitem == NULL)
4667 abort = TRUE;
4668 else
4669 {
4670 newitem->list = ll;
4671 newitem->prev = *list_stack;
4672 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004673 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004674 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004675 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004676 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004677 else if (tv->v_type == VAR_FUNC)
4678 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004679 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004680 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004681 else if (tv->v_type == VAR_PARTIAL)
4682 {
4683 partial_T *pt = tv->vval.v_partial;
4684 int i;
4685
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004686 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004687 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004688 // Didn't see this partial yet.
4689 pt->pt_copyID = copyID;
4690
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004691 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004692
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004693 if (pt->pt_dict != NULL)
4694 {
4695 typval_T dtv;
4696
4697 dtv.v_type = VAR_DICT;
4698 dtv.vval.v_dict = pt->pt_dict;
4699 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4700 }
4701
4702 for (i = 0; i < pt->pt_argc; ++i)
4703 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4704 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004705 if (pt->pt_funcstack != NULL)
4706 {
4707 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4708
4709 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4710 abort = abort || set_ref_in_item(stack + i, copyID,
4711 ht_stack, list_stack);
4712 }
4713
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004714 }
4715 }
4716#ifdef FEAT_JOB_CHANNEL
4717 else if (tv->v_type == VAR_JOB)
4718 {
4719 job_T *job = tv->vval.v_job;
4720 typval_T dtv;
4721
4722 if (job != NULL && job->jv_copyID != copyID)
4723 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004724 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004725 if (job->jv_channel != NULL)
4726 {
4727 dtv.v_type = VAR_CHANNEL;
4728 dtv.vval.v_channel = job->jv_channel;
4729 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4730 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004731 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 {
4733 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004734 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004735 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4736 }
4737 }
4738 }
4739 else if (tv->v_type == VAR_CHANNEL)
4740 {
4741 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004742 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004743 typval_T dtv;
4744 jsonq_T *jq;
4745 cbq_T *cq;
4746
4747 if (ch != NULL && ch->ch_copyID != copyID)
4748 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004749 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004750 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004751 {
4752 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4753 jq = jq->jq_next)
4754 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4755 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4756 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004757 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004758 {
4759 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004760 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004761 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4762 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004763 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 {
4765 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004766 dtv.vval.v_partial =
4767 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4769 }
4770 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004771 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004772 {
4773 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004774 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4776 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004777 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 {
4779 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004780 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004781 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4782 }
4783 }
4784 }
4785#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004786 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004787}
4788
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 * Return a string with the string representation of a variable.
4791 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004792 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004793 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004794 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004795 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004796 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004797 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4798 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004799 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004801 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004802echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004803 typval_T *tv,
4804 char_u **tofree,
4805 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004806 int copyID,
4807 int echo_style,
4808 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004809 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004811 static int recurse = 0;
4812 char_u *r = NULL;
4813
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004815 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004816 if (!did_echo_string_emsg)
4817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // Only give this message once for a recursive call to avoid
4819 // flooding the user with errors. And stop iterating over lists
4820 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004821 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004824 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004825 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004826 }
4827 ++recurse;
4828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 switch (tv->v_type)
4830 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004831 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004832 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004833 {
4834 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004835 r = tv->vval.v_string;
4836 if (r == NULL)
4837 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004838 }
4839 else
4840 {
4841 *tofree = string_quote(tv->vval.v_string, FALSE);
4842 r = *tofree;
4843 }
4844 break;
4845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004847 if (echo_style)
4848 {
4849 *tofree = NULL;
4850 r = tv->vval.v_string;
4851 }
4852 else
4853 {
4854 *tofree = string_quote(tv->vval.v_string, TRUE);
4855 r = *tofree;
4856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004857 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004858
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004859 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004860 {
4861 partial_T *pt = tv->vval.v_partial;
4862 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004863 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004864 garray_T ga;
4865 int i;
4866 char_u *tf;
4867
4868 ga_init2(&ga, 1, 100);
4869 ga_concat(&ga, (char_u *)"function(");
4870 if (fname != NULL)
4871 {
4872 ga_concat(&ga, fname);
4873 vim_free(fname);
4874 }
4875 if (pt != NULL && pt->pt_argc > 0)
4876 {
4877 ga_concat(&ga, (char_u *)", [");
4878 for (i = 0; i < pt->pt_argc; ++i)
4879 {
4880 if (i > 0)
4881 ga_concat(&ga, (char_u *)", ");
4882 ga_concat(&ga,
4883 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4884 vim_free(tf);
4885 }
4886 ga_concat(&ga, (char_u *)"]");
4887 }
4888 if (pt != NULL && pt->pt_dict != NULL)
4889 {
4890 typval_T dtv;
4891
4892 ga_concat(&ga, (char_u *)", ");
4893 dtv.v_type = VAR_DICT;
4894 dtv.vval.v_dict = pt->pt_dict;
4895 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4896 vim_free(tf);
4897 }
4898 ga_concat(&ga, (char_u *)")");
4899
4900 *tofree = ga.ga_data;
4901 r = *tofree;
4902 break;
4903 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004904
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004905 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004906 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004907 break;
4908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004910 if (tv->vval.v_list == NULL)
4911 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004912 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004913 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004914 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004915 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004916 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4917 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004918 {
4919 *tofree = NULL;
4920 r = (char_u *)"[...]";
4921 }
4922 else
4923 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004924 int old_copyID = tv->vval.v_list->lv_copyID;
4925
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004926 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004927 *tofree = list2string(tv, copyID, restore_copyID);
4928 if (restore_copyID)
4929 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004930 r = *tofree;
4931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004932 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004933
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004935 if (tv->vval.v_dict == NULL)
4936 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004937 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004938 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004939 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004940 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004941 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4942 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004943 {
4944 *tofree = NULL;
4945 r = (char_u *)"{...}";
4946 }
4947 else
4948 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004949 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004950
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004951 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952 *tofree = dict2string(tv, copyID, restore_copyID);
4953 if (restore_copyID)
4954 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004955 r = *tofree;
4956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004957 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004960 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004961 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004963 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004964 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004965 break;
4966
Bram Moolenaar835dc632016-02-07 14:27:38 +01004967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004969 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004970 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004971 if (composite_val)
4972 {
4973 *tofree = string_quote(r, FALSE);
4974 r = *tofree;
4975 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004979#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 *tofree = NULL;
4981 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4982 r = numbuf;
4983 break;
4984#endif
4985
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004986 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004987 case VAR_SPECIAL:
4988 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004989 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004990 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992
Bram Moolenaar8502c702014-06-17 12:51:16 +02004993 if (--recurse == 0)
4994 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004995 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004996}
4997
4998/*
4999 * Return a string with the string representation of a variable.
5000 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5001 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005002 * Does not put quotes around strings, as ":echo" displays values.
5003 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5004 * May return NULL.
5005 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005007echo_string(
5008 typval_T *tv,
5009 char_u **tofree,
5010 char_u *numbuf,
5011 int copyID)
5012{
5013 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5014}
5015
5016/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005017 * Return string "str" in ' quotes, doubling ' characters.
5018 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005020 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005021 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005022string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023{
Bram Moolenaar33570922005-01-25 22:26:29 +00005024 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005025 char_u *p, *r, *s;
5026
Bram Moolenaar33570922005-01-25 22:26:29 +00005027 len = (function ? 13 : 3);
5028 if (str != NULL)
5029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005030 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005031 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005032 if (*p == '\'')
5033 ++len;
5034 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035 s = r = alloc(len);
5036 if (r != NULL)
5037 {
5038 if (function)
5039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 r += 10;
5042 }
5043 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005045 if (str != NULL)
5046 for (p = str; *p != NUL; )
5047 {
5048 if (*p == '\'')
5049 *r++ = '\'';
5050 MB_COPY_CHAR(p, r);
5051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005053 if (function)
5054 *r++ = ')';
5055 *r++ = NUL;
5056 }
5057 return s;
5058}
5059
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005060#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061/*
5062 * Convert the string "text" to a floating point number.
5063 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5064 * this always uses a decimal point.
5065 * Returns the length of the text that was consumed.
5066 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005068string2float(
5069 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071{
5072 char *s = (char *)text;
5073 float_T f;
5074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005076 if (STRNICMP(text, "inf", 3) == 0)
5077 {
5078 *value = INFINITY;
5079 return 3;
5080 }
5081 if (STRNICMP(text, "-inf", 3) == 0)
5082 {
5083 *value = -INFINITY;
5084 return 4;
5085 }
5086 if (STRNICMP(text, "nan", 3) == 0)
5087 {
5088 *value = NAN;
5089 return 3;
5090 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 f = strtod(s, &s);
5092 *value = f;
5093 return (int)((char_u *)s - text);
5094}
5095#endif
5096
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005098 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5099 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005100 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005101 */
5102 int
5103buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5104{
5105 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005106 char_u *t;
5107 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005108
5109 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5110 return -1;
5111
5112 if (lnum > buf->b_ml.ml_line_count)
5113 lnum = buf->b_ml.ml_line_count;
5114
5115 str = ml_get_buf(buf, lnum, FALSE);
5116 if (str == NULL)
5117 return -1;
5118
5119 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005120 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005121
Bram Moolenaar91458462021-01-13 20:08:38 +01005122 // count the number of characters
5123 t = str;
5124 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5125 t += mb_ptr2len(t);
5126
5127 // In insert mode, when the cursor is at the end of a non-empty line,
5128 // byteidx points to the NUL character immediately past the end of the
5129 // string. In this case, add one to the character count.
5130 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5131 count++;
5132
5133 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005134}
5135
5136/*
5137 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005138 * byte index. Works only for loaded buffers. Returns -1 on failure.
5139 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005140 */
5141 int
5142buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5143{
5144 char_u *str;
5145 char_u *t;
5146
5147 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5148 return -1;
5149
5150 if (lnum > buf->b_ml.ml_line_count)
5151 lnum = buf->b_ml.ml_line_count;
5152
5153 str = ml_get_buf(buf, lnum, FALSE);
5154 if (str == NULL)
5155 return -1;
5156
5157 // Convert the character offset to a byte offset
5158 t = str;
5159 while (*t != NUL && --charidx > 0)
5160 t += mb_ptr2len(t);
5161
Bram Moolenaar91458462021-01-13 20:08:38 +01005162 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005163}
5164
5165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005167 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005170var2fpos(
5171 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005173 int *fnum, // set to fnum for '0, 'A, etc.
5174 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005176 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005178 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005180 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005181 if (varp->v_type == VAR_LIST)
5182 {
5183 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005184 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005185 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005186 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005187
5188 l = varp->vval.v_list;
5189 if (l == NULL)
5190 return NULL;
5191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005192 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005193 pos.lnum = list_find_nr(l, 0L, &error);
5194 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005195 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005196 if (charcol)
5197 len = (long)mb_charlen(ml_get(pos.lnum));
5198 else
5199 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005200
Bram Moolenaarec65d772020-08-20 22:29:12 +02005201 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005203 li = list_find(l, 1L);
5204 if (li != NULL && li->li_tv.v_type == VAR_STRING
5205 && li->li_tv.vval.v_string != NULL
5206 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005207 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005208 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005209 }
5210 else
5211 {
5212 pos.col = list_find_nr(l, 1L, &error);
5213 if (error)
5214 return NULL;
5215 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005217 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005218 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005220 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005221
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005222 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005223 pos.coladd = list_find_nr(l, 2L, &error);
5224 if (error)
5225 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005226
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005227 return &pos;
5228 }
5229
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005230 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 if (name == NULL)
5232 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005234 {
5235 pos = curwin->w_cursor;
5236 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005237 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005238 return &pos;
5239 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005241 {
5242 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005243 pos = VIsual;
5244 else
5245 pos = curwin->w_cursor;
5246 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005247 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005248 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005249 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005252 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5254 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005256 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 return pp;
5258 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005259
Bram Moolenaara5525202006-03-02 22:52:09 +00005260 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005261
Bram Moolenaar477933c2007-07-17 14:32:23 +00005262 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005263 {
5264 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005266 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005267 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // In silent Ex mode topline is zero, but that's not a valid line
5269 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005270 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005271 return &pos;
5272 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005274 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005275 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005277 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005278 return &pos;
5279 }
5280 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005283 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 pos.lnum = curbuf->b_ml.ml_line_count;
5286 pos.col = 0;
5287 }
5288 else
5289 {
5290 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005291 if (charcol)
5292 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5293 else
5294 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 return &pos;
5297 }
5298 return NULL;
5299}
5300
5301/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005302 * Convert list in "arg" into a position and optional file number.
5303 * When "fnump" is NULL there is no file number, only 3 items.
5304 * Note that the column is passed on as-is, the caller may want to decrement
5305 * it to use 1 for the first column.
5306 * Return FAIL when conversion is not possible, doesn't check the position for
5307 * validity.
5308 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310list2fpos(
5311 typval_T *arg,
5312 pos_T *posp,
5313 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005314 colnr_T *curswantp,
5315 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005316{
5317 list_T *l = arg->vval.v_list;
5318 long i = 0;
5319 long n;
5320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5322 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005323 if (arg->v_type != VAR_LIST
5324 || l == NULL
5325 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005326 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005327 return FAIL;
5328
5329 if (fnump != NULL)
5330 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005331 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005332 if (n < 0)
5333 return FAIL;
5334 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005336 *fnump = n;
5337 }
5338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005339 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005340 if (n < 0)
5341 return FAIL;
5342 posp->lnum = n;
5343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005345 if (n < 0)
5346 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005347 // If character position is specified, then convert to byte position
5348 if (charcol)
5349 {
5350 buf_T *buf;
5351
5352 // Get the text for the specified line in a loaded buffer
5353 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5354 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5355 return FAIL;
5356
Bram Moolenaar91458462021-01-13 20:08:38 +01005357 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005358 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005359 posp->col = n;
5360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005363 posp->coladd = 0;
5364 else
5365 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005366
Bram Moolenaar493c1782014-05-28 14:34:46 +02005367 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005368 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005369
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005370 return OK;
5371}
5372
5373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 * Get the length of an environment variable name.
5375 * Advance "arg" to the first character after the name.
5376 * Return 0 for error.
5377 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005379get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 char_u *p;
5382 int len;
5383
5384 for (p = *arg; vim_isIDc(*p); ++p)
5385 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return 0;
5388
5389 len = (int)(p - *arg);
5390 *arg = p;
5391 return len;
5392}
5393
5394/*
5395 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005396 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 * Return 0 if something is wrong.
5398 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005400get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 char_u *p;
5403 int len;
5404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005407 {
5408 if (*p == ':')
5409 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 // "s:" is start of "s:var", but "n:" is not and can be used in
5411 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005412 len = (int)(p - *arg);
5413 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5414 || len > 1)
5415 break;
5416 }
5417 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return 0;
5420
5421 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005422 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
5424 return len;
5425}
5426
5427/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005428 * Get the length of the name of a variable or function.
5429 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005431 * Return -1 if curly braces expansion failed.
5432 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 * If the name contains 'magic' {}'s, expand them and return the
5434 * expanded name in an allocated string via 'alias' - caller must free.
5435 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005437get_name_len(
5438 char_u **arg,
5439 char_u **alias,
5440 int evaluate,
5441 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 char_u *p;
5445 char_u *expr_start;
5446 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005448 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5451 && (*arg)[2] == (int)KE_SNR)
5452 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 *arg += 3;
5455 return get_id_len(arg) + 3;
5456 }
5457 len = eval_fname_script(*arg);
5458 if (len > 0)
5459 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005460 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 *arg += len;
5462 }
5463
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005467 p = find_name_end(*arg, &expr_start, &expr_end,
5468 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (expr_start != NULL)
5470 {
5471 char_u *temp_string;
5472
5473 if (!evaluate)
5474 {
5475 len += (int)(p - *arg);
5476 *arg = skipwhite(p);
5477 return len;
5478 }
5479
5480 /*
5481 * Include any <SID> etc in the expanded string:
5482 * Thus the -len here.
5483 */
5484 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5485 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005486 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 *alias = temp_string;
5488 *arg = skipwhite(p);
5489 return (int)STRLEN(temp_string);
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005493 // Only give an error when there is something, otherwise it will be
5494 // reported at a higher level.
5495 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005496 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
5498 return len;
5499}
5500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501/*
5502 * Find the end of a variable or function name, taking care of magic braces.
5503 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5504 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005505 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 * Return a pointer to just after the name. Equal to "arg" if there is no
5507 * valid name.
5508 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510find_name_end(
5511 char_u *arg,
5512 char_u **expr_start,
5513 char_u **expr_end,
5514 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 int mb_nest = 0;
5517 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005519 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005520 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 *expr_start = NULL;
5525 *expr_end = NULL;
5526 }
5527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005528 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005529 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5530 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005531 return arg;
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 for (p = arg; *p != NUL
5534 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005535 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005536 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005537 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005539 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005541 if (*p == '\'')
5542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005544 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005545 ;
5546 if (*p == NUL)
5547 break;
5548 }
5549 else if (*p == '"')
5550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005551 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005552 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005553 if (*p == '\\' && p[1] != NUL)
5554 ++p;
5555 if (*p == NUL)
5556 break;
5557 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005558 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // "s:" is start of "s:var", but "n:" is not and can be used in
5561 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005562 len = (int)(p - arg);
5563 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005564 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005565 break;
5566 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 if (*p == '[')
5571 ++br_nest;
5572 else if (*p == ']')
5573 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005575
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005576 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 if (*p == '{')
5579 {
5580 mb_nest++;
5581 if (expr_start != NULL && *expr_start == NULL)
5582 *expr_start = p;
5583 }
5584 else if (*p == '}')
5585 {
5586 mb_nest--;
5587 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5588 *expr_end = p;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592
5593 return p;
5594}
5595
5596/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005597 * Expands out the 'magic' {}'s in a variable/function name.
5598 * Note that this can call itself recursively, to deal with
5599 * constructs like foo{bar}{baz}{bam}
5600 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5601 * "in_start" ^
5602 * "expr_start" ^
5603 * "expr_end" ^
5604 * "in_end" ^
5605 *
5606 * Returns a new allocated string, which the caller must free.
5607 * Returns NULL for failure.
5608 */
5609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005610make_expanded_name(
5611 char_u *in_start,
5612 char_u *expr_start,
5613 char_u *expr_end,
5614 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005615{
5616 char_u c1;
5617 char_u *retval = NULL;
5618 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005619
5620 if (expr_end == NULL || in_end == NULL)
5621 return NULL;
5622 *expr_start = NUL;
5623 *expr_end = NUL;
5624 c1 = *in_end;
5625 *in_end = NUL;
5626
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005627 temp_result = eval_to_string(expr_start + 1, FALSE);
5628 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005630 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5631 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632 if (retval != NULL)
5633 {
5634 STRCPY(retval, in_start);
5635 STRCAT(retval, temp_result);
5636 STRCAT(retval, expr_end + 1);
5637 }
5638 }
5639 vim_free(temp_result);
5640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005641 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005642 *expr_start = '{';
5643 *expr_end = '}';
5644
5645 if (retval != NULL)
5646 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005647 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005648 if (expr_start != NULL)
5649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005650 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005651 temp_result = make_expanded_name(retval, expr_start,
5652 expr_end, temp_result);
5653 vim_free(retval);
5654 retval = temp_result;
5655 }
5656 }
5657
5658 return retval;
5659}
5660
5661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005663 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005666eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005668 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005669}
5670
5671/*
5672 * Return TRUE if character "c" can be used as the first character in a
5673 * variable or function name (excluding '{' and '}').
5674 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005677{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005678 return ASCII_ISALPHA(c) || c == '_';
5679}
5680
5681/*
5682 * Return TRUE if character "c" can be used as the first character of a
5683 * dictionary key.
5684 */
5685 int
5686eval_isdictc(int c)
5687{
5688 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689}
5690
5691/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005692 * Handle:
5693 * - expr[expr], expr[expr:expr] subscript
5694 * - ".name" lookup
5695 * - function call with Funcref variable: func(expr)
5696 * - method call: var->method()
5697 *
5698 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005699 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701handle_subscript(
5702 char_u **arg,
5703 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005704 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005705 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005707 int evaluate = evalarg != NULL
5708 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005709 int ret = OK;
5710 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005711 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005712 int getnext;
5713 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005714
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005715 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005716 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005717 // When at the end of the line and ".name" or "->{" or "->X" follows in
5718 // the next line then consume the line break.
5719 p = eval_next_non_blank(*arg, evalarg, &getnext);
5720 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005721 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005722 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005723 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005724 {
5725 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005726 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005727 check_white = FALSE;
5728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005729
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005730 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5731 || rettv->v_type == VAR_PARTIAL))
5732 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005733 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005734 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5735 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005736
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005737 // Stop the expression evaluation when immediately aborting on
5738 // error, or when an interrupt occurred or an exception was thrown
5739 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740 if (aborting())
5741 {
5742 if (ret == OK)
5743 clear_tv(rettv);
5744 ret = FAIL;
5745 }
5746 dict_unref(selfdict);
5747 selfdict = NULL;
5748 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005749 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005750 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005751 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005752 if (ret == OK)
5753 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005754 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5755 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005756 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005757 else
5758 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005759 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005760 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005761 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005762 // "." is ".name" lookup when we found a dict or when evaluating and
5763 // scriptversion is at least 2, where string concatenation is "..".
5764 else if (**arg == '['
5765 || (**arg == '.' && (rettv->v_type == VAR_DICT
5766 || (!evaluate
5767 && (*arg)[1] != '.'
5768 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005769 {
5770 dict_unref(selfdict);
5771 if (rettv->v_type == VAR_DICT)
5772 {
5773 selfdict = rettv->vval.v_dict;
5774 if (selfdict != NULL)
5775 ++selfdict->dv_refcount;
5776 }
5777 else
5778 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005779 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005780 {
5781 clear_tv(rettv);
5782 ret = FAIL;
5783 }
5784 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005785 else
5786 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005787 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5790 // Don't do this when "Func" is already a partial that was bound
5791 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005792 if (selfdict != NULL
5793 && (rettv->v_type == VAR_FUNC
5794 || (rettv->v_type == VAR_PARTIAL
5795 && (rettv->vval.v_partial->pt_auto
5796 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005798
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005799 dict_unref(selfdict);
5800 return ret;
5801}
5802
5803/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005804 * Make a copy of an item.
5805 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005806 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5807 * reference to an already copied list/dict can be used.
5808 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005809 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005811item_copy(
5812 typval_T *from,
5813 typval_T *to,
5814 int deep,
5815 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005816{
5817 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005818 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005819
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005822 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005823 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005824 }
5825 ++recurse;
5826
5827 switch (from->v_type)
5828 {
5829 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005830 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005831 case VAR_STRING:
5832 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005833 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005834 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005835 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005836 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005837 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 copy_tv(from, to);
5839 break;
5840 case VAR_LIST:
5841 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005843 if (from->vval.v_list == NULL)
5844 to->vval.v_list = NULL;
5845 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005847 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005848 to->vval.v_list = from->vval.v_list->lv_copylist;
5849 ++to->vval.v_list->lv_refcount;
5850 }
5851 else
5852 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5853 if (to->vval.v_list == NULL)
5854 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005856 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005858 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859 case VAR_DICT:
5860 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005861 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 if (from->vval.v_dict == NULL)
5863 to->vval.v_dict = NULL;
5864 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005866 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005867 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5868 ++to->vval.v_dict->dv_refcount;
5869 }
5870 else
5871 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5872 if (to->vval.v_dict == NULL)
5873 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005875 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005876 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005878 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 }
5881 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883}
5884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 void
5886echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5887{
5888 char_u *tofree;
5889 char_u numbuf[NUMBUFLEN];
5890 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5891
5892 if (*atstart)
5893 {
5894 *atstart = FALSE;
5895 // Call msg_start() after eval1(), evaluating the expression
5896 // may cause a message to appear.
5897 if (with_space)
5898 {
5899 // Mark the saved text as finishing the line, so that what
5900 // follows is displayed on a new line when scrolling back
5901 // at the more prompt.
5902 msg_sb_eol();
5903 msg_start();
5904 }
5905 }
5906 else if (with_space)
5907 msg_puts_attr(" ", echo_attr);
5908
5909 if (p != NULL)
5910 for ( ; *p != NUL && !got_int; ++p)
5911 {
5912 if (*p == '\n' || *p == '\r' || *p == TAB)
5913 {
5914 if (*p != TAB && *needclr)
5915 {
5916 // remove any text still there from the command
5917 msg_clr_eos();
5918 *needclr = FALSE;
5919 }
5920 msg_putchar_attr(*p, echo_attr);
5921 }
5922 else
5923 {
5924 if (has_mbyte)
5925 {
5926 int i = (*mb_ptr2len)(p);
5927
5928 (void)msg_outtrans_len_attr(p, i, echo_attr);
5929 p += i - 1;
5930 }
5931 else
5932 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5933 }
5934 }
5935 vim_free(tofree);
5936}
5937
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * ":echo expr1 ..." print each argument separated with a space, add a
5940 * newline at the end.
5941 * ":echon expr1 ..." print each argument plain.
5942 */
5943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005944ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 char_u *p;
5949 int needclr = TRUE;
5950 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005951 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005952 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005953 evalarg_T evalarg;
5954
Bram Moolenaare707c882020-07-01 13:07:37 +02005955 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
5957 if (eap->skip)
5958 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005959 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 // If eval1() causes an error message the text from the command may
5962 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005963 need_clr_eos = needclr;
5964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005966 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 /*
5969 * Report the invalid expression unless the expression evaluation
5970 * has been cancelled due to an aborting error, an interrupt, or an
5971 * exception.
5972 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005973 if (!aborting() && did_emsg == did_emsg_before
5974 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005975 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 break;
5978 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005979 need_clr_eos = FALSE;
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 arg = skipwhite(arg);
5986 }
5987 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005988 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 if (eap->skip)
5991 --emsg_skip;
5992 else
5993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 if (needclr)
5996 msg_clr_eos();
5997 if (eap->cmdidx == CMD_echo)
5998 msg_end();
5999 }
6000}
6001
6002/*
6003 * ":echohl {name}".
6004 */
6005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006008 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009}
6010
6011/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006012 * Returns the :echo attribute
6013 */
6014 int
6015get_echo_attr(void)
6016{
6017 return echo_attr;
6018}
6019
6020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 * ":execute expr1 ..." execute the result of an expression.
6022 * ":echomsg expr1 ..." Print a message
6023 * ":echoerr expr1 ..." Print an error
6024 * Each gets spaces around each argument and a newline at the end for
6025 * echo commands
6026 */
6027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 int ret = OK;
6033 char_u *p;
6034 garray_T ga;
6035 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036
6037 ga_init2(&ga, 1, 80);
6038
6039 if (eap->skip)
6040 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006041 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006043 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006044 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047 if (!eap->skip)
6048 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006049 char_u buf[NUMBUFLEN];
6050
6051 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006052 {
6053 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6054 {
6055 emsg(_(e_inval_string));
6056 p = NULL;
6057 }
6058 else
6059 p = tv_get_string_buf(&rettv, buf);
6060 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006061 else
6062 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006063 if (p == NULL)
6064 {
6065 clear_tv(&rettv);
6066 ret = FAIL;
6067 break;
6068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 len = (int)STRLEN(p);
6070 if (ga_grow(&ga, len + 2) == FAIL)
6071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006072 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 ret = FAIL;
6074 break;
6075 }
6076 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 ga.ga_len += len;
6080 }
6081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 arg = skipwhite(arg);
6084 }
6085
6086 if (ret != FAIL && ga.ga_data != NULL)
6087 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006088 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006090 // Mark the already saved text as finishing the line, so that what
6091 // follows is displayed on a new line when scrolling back at the
6092 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006093 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006094 }
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006097 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006098 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006099 out_flush();
6100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 else if (eap->cmdidx == CMD_echoerr)
6102 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006103 int save_did_emsg = did_emsg;
6104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006105 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006106 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (!force_abort)
6108 did_emsg = save_did_emsg;
6109 }
6110 else if (eap->cmdidx == CMD_execute)
6111 do_cmdline((char_u *)ga.ga_data,
6112 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6113 }
6114
6115 ga_clear(&ga);
6116
6117 if (eap->skip)
6118 --emsg_skip;
6119
6120 eap->nextcmd = check_nextcmd(arg);
6121}
6122
6123/*
6124 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6125 * "arg" points to the "&" or '+' when called, to "option" when returning.
6126 * Returns NULL when no option name found. Otherwise pointer to the char
6127 * after the option name.
6128 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006129 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006130find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 char_u *p = *arg;
6133
6134 ++p;
6135 if (*p == 'g' && p[1] == ':')
6136 {
6137 *opt_flags = OPT_GLOBAL;
6138 p += 2;
6139 }
6140 else if (*p == 'l' && p[1] == ':')
6141 {
6142 *opt_flags = OPT_LOCAL;
6143 p += 2;
6144 }
6145 else
6146 *opt_flags = 0;
6147
6148 if (!ASCII_ISALPHA(*p))
6149 return NULL;
6150 *arg = p;
6151
6152 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006153 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 else
6155 while (ASCII_ISALPHA(*p))
6156 ++p;
6157 return p;
6158}
6159
6160/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006161 * Display script name where an item was last set.
6162 * Should only be invoked when 'verbose' is non-zero.
6163 */
6164 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006165last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006166{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006167 char_u *p;
6168
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006169 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006170 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006171 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006172 if (p != NULL)
6173 {
6174 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006175 msg_puts(_("\n\tLast set from "));
6176 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006177 if (script_ctx.sc_lnum > 0)
6178 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006179 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006180 msg_outnum((long)script_ctx.sc_lnum);
6181 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006182 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006183 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006184 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006185 }
6186}
6187
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006188#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190/*
6191 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006192 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 * "flags" can be "g" to do a global substitute.
6194 * Returns an allocated string, NULL for error.
6195 */
6196 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197do_string_sub(
6198 char_u *str,
6199 char_u *pat,
6200 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006201 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006202 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 int sublen;
6205 regmatch_T regmatch;
6206 int i;
6207 int do_all;
6208 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006209 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 garray_T ga;
6211 char_u *ret;
6212 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006213 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006217 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 ga_init2(&ga, 1, 200);
6220
6221 do_all = (flags[0] == 'g');
6222
6223 regmatch.rm_ic = p_ic;
6224 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6225 if (regmatch.regprog != NULL)
6226 {
6227 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006228 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006231 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006232 if (regmatch.startp[0] == regmatch.endp[0])
6233 {
6234 if (zero_width == regmatch.startp[0])
6235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006236 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006237 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006238 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6239 (size_t)i);
6240 ga.ga_len += i;
6241 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006242 continue;
6243 }
6244 zero_width = regmatch.startp[0];
6245 }
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 /*
6248 * Get some space for a temporary buffer to do the substitution
6249 * into. It will contain:
6250 * - The text up to where the match is.
6251 * - The substituted text.
6252 * - The text after the match.
6253 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006254 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006255 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6257 {
6258 ga_clear(&ga);
6259 break;
6260 }
6261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 i = (int)(regmatch.startp[0] - tail);
6264 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006266 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 + ga.ga_len + i, TRUE, TRUE, FALSE);
6268 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006269 tail = regmatch.endp[0];
6270 if (*tail == NUL)
6271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (!do_all)
6273 break;
6274 }
6275
6276 if (ga.ga_data != NULL)
6277 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6278
Bram Moolenaar473de612013-06-08 18:19:48 +02006279 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281
6282 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6283 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006284 if (p_cpo == empty_option)
6285 p_cpo = save_cpo;
6286 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006288 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006289 // If it's still empty it was changed and restored, need to restore in
6290 // the complicated way.
6291 if (*p_cpo == NUL)
6292 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006293 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
6296 return ret;
6297}