blob: 13ee49aac4f4a28dec6d21898eb585eed174b984 [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
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100470 * Convert "tv" to a string.
471 * When "convert" is TRUE convert a List into a sequence of lines and convert
472 * a Float to a String.
473 * Returns an allocated string (NULL when out of memory).
474 */
475 char_u *
476typval2string(typval_T *tv, int convert)
477{
478 garray_T ga;
479 char_u *retval;
480#ifdef FEAT_FLOAT
481 char_u numbuf[NUMBUFLEN];
482#endif
483
484 if (convert && tv->v_type == VAR_LIST)
485 {
486 ga_init2(&ga, (int)sizeof(char), 80);
487 if (tv->vval.v_list != NULL)
488 {
489 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
490 if (tv->vval.v_list->lv_len > 0)
491 ga_append(&ga, NL);
492 }
493 ga_append(&ga, NUL);
494 retval = (char_u *)ga.ga_data;
495 }
496#ifdef FEAT_FLOAT
497 else if (convert && tv->v_type == VAR_FLOAT)
498 {
499 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
500 retval = vim_strsave(numbuf);
501 }
502#endif
503 else
504 retval = vim_strsave(tv_get_string(tv));
505 return retval;
506}
507
508/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200509 * Top level evaluation function, returning a string. Does not handle line
510 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000511 * When "convert" is TRUE convert a List into a sequence of lines and convert
512 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * Return pointer to allocated memory, or NULL for failure.
514 */
515 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100516eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100517 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 int convert,
519 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
Bram Moolenaar33570922005-01-25 22:26:29 +0000521 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100525 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
526 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 retval = NULL;
528 else
529 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100530 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000531 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100533 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 return retval;
536}
537
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 char_u *
539eval_to_string(
540 char_u *arg,
541 int convert)
542{
543 return eval_to_string_eap(arg, convert, NULL);
544}
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200548 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200549 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552eval_to_string_safe(
553 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200557 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200558 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200565 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000566 if (use_sandbox)
567 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200568 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200569 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200570 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 return retval;
572}
573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574/*
575 * Top level evaluation function, returning a number.
576 * Evaluates "expr" silently.
577 * Returns -1 for an error.
578 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200579 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100580eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
Bram Moolenaar33570922005-01-25 22:26:29 +0000582 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200583 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000584 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 ++emsg_off;
587
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200588 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 retval = -1;
590 else
591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100592 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000593 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 }
595 --emsg_off;
596
597 return retval;
598}
599
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000600/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000601 * Top level evaluation function.
602 * Returns an allocated typval_T with the result.
603 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000604 */
605 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200606eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607{
608 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200609 evalarg_T evalarg;
610
611 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000612
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200613 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200614 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100615 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000616
Bram Moolenaar37c83712020-06-30 21:18:36 +0200617 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618 return tv;
619}
620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100622 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200623 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
624 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000625 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628call_vim_function(
629 char_u *func,
630 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200631 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200632 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000634 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200635 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100637 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200638 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200639 funcexe.firstline = curwin->w_cursor.lnum;
640 funcexe.lastline = curwin->w_cursor.lnum;
641 funcexe.evaluate = TRUE;
642 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000643 if (ret == FAIL)
644 clear_tv(rettv);
645
646 return ret;
647}
648
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100649/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100650 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100651 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200652 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
653 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100654 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200655 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656call_func_retnr(
657 char_u *func,
658 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200659 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100660{
661 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200662 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100663
Bram Moolenaarded27a12018-08-01 19:06:03 +0200664 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665 return -1;
666
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100667 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668 clear_tv(&rettv);
669 return retval;
670}
671
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000672/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100673 * Call Vim script function like call_func_retnr() and drop the result.
674 * Returns FAIL when calling the function fails.
675 */
676 int
677call_func_noret(
678 char_u *func,
679 int argc,
680 typval_T *argv)
681{
682 typval_T rettv;
683
684 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
685 return FAIL;
686 clear_tv(&rettv);
687 return OK;
688}
689
690/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100691 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100692 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000693 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000694 */
695 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100696call_func_retstr(
697 char_u *func,
698 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200699 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000700{
701 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000702 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000703
Bram Moolenaarded27a12018-08-01 19:06:03 +0200704 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705 return NULL;
706
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100707 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 return retval;
710}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000712/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100713 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100714 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000715 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716 */
717 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100718call_func_retlist(
719 char_u *func,
720 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200721 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722{
723 typval_T rettv;
724
Bram Moolenaarded27a12018-08-01 19:06:03 +0200725 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000726 return NULL;
727
728 if (rettv.v_type != VAR_LIST)
729 {
730 clear_tv(&rettv);
731 return NULL;
732 }
733
734 return rettv.vval.v_list;
735}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#ifdef FEAT_FOLDING
738/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100739 * Evaluate "arg", which is 'foldexpr'.
740 * Note: caller must set "curwin" to match "arg".
741 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
742 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaar33570922005-01-25 22:26:29 +0000747 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200748 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000750 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
751 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
753 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000754 if (use_sandbox)
755 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200756 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200758 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 retval = 0;
760 else
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000763 if (tv.v_type == VAR_NUMBER)
764 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000765 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 retval = 0;
767 else
768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 // If the result is a string, check if there is a non-digit before
770 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (!VIM_ISDIGIT(*s) && *s != '-')
773 *cp = *s++;
774 retval = atol((char *)s);
775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000779 if (use_sandbox)
780 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200781 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200782 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200784 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786#endif
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000789 * Get an lval: variable, Dict item or List item that can be assigned a value
790 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
791 * "name.key", "name.key[expr]" etc.
792 * Indexing only works if "name" is an existing List or Dictionary.
793 * "name" points to the start of the name.
794 * If "rettv" is not NULL it points to the value to be assigned.
795 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
796 * wrong; must end in space or cmd separator.
797 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100798 * flags:
799 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100800 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100801 * GLV_NO_AUTOLOAD: do not use script autoloading
802 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000804 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000806 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100808get_lval(
809 char_u *name,
810 typval_T *rettv,
811 lval_T *lp,
812 int unlet,
813 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 int flags, // GLV_ values
815 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 char_u *expr_start, *expr_end;
819 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000820 dictitem_T *v;
821 typval_T var1;
822 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000825 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100827 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100828 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100829 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100831 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200832 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100834 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100836 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200838 lp->ll_name_end = find_name_end(name, NULL, NULL,
839 FNE_INCL_BR | fne_flags);
840 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 }
842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000844 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 if (expr_start != NULL)
847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100849 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 && *p != '[' && *p != '.')
851 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200852 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 return NULL;
854 }
855
856 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
857 if (lp->ll_exp_name == NULL)
858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100859 // Report an invalid expression in braces, unless the
860 // expression evaluation has been cancelled due to an
861 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if (!aborting() && !quiet)
863 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000864 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
867 }
868 }
869 lp->ll_name = lp->ll_exp_name;
870 }
871 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 lp->ll_name = name;
874
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200875 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200877 // "a: type" is declaring variable "a" with a type, not "a:".
878 if (p == name + 2 && p[-1] == ':')
879 {
880 --p;
881 lp->ll_name_end = p;
882 }
883 if (*p == ':')
884 {
885 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
886 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200888 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100889 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
890 if (lp->ll_type == NULL && !quiet)
891 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 lp->ll_name_end = tp;
893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 }
895 }
896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
899 return p;
900
901 cc = *p;
902 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100903 // When we would write to the variable pass &ht and prevent autoload.
904 writing = !(flags & GLV_READ_ONLY);
905 v = find_var(lp->ll_name, writing ? &ht : NULL,
906 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200908 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000910 if (v == NULL)
911 return NULL;
912
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100913 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
914 {
915 if (!quiet)
916 semsg(_(e_variable_already_declared), lp->ll_name);
917 return NULL;
918 }
919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 /*
921 * Loop until no more [idx] or .key is following.
922 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000923 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100924 var1.v_type = VAR_UNKNOWN;
925 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200929 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100930 && !(lp->ll_tv->v_type == VAR_BLOB
931 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100934 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000937 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000943
Bram Moolenaar10c65862020-10-08 21:16:42 +0200944 if (in_vim9script() && lp->ll_valtype == NULL
945 && lp->ll_tv == &v->di_tv
946 && ht != NULL && ht == get_script_local_ht())
947 {
948 svar_T *sv = find_typval_in_script(lp->ll_tv);
949
950 // Vim9 script local variable: get the type
951 if (sv != NULL)
952 lp->ll_valtype = sv->sv_type;
953 }
954
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 len = -1;
956 if (*p == '.')
957 {
958 key = p + 1;
959 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
960 ;
961 if (len == 0)
962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 }
967 p = key + len;
968 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000969 else
970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000972 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 if (*p == ':')
974 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000975 else
976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200978 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100980 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100982 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000983 clear_tv(&var1);
984 return NULL;
985 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200986 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 }
988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 if (*p == ':')
991 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200995 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000998 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100999 if (rettv != NULL
1000 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001001 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001002 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001003 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001006 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001007 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 }
1010 p = skipwhite(p + 1);
1011 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 else
1014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001016 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001017 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001019 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001022 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001025 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001026 clear_tv(&var2);
1027 return NULL;
1028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001034
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001036 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001038 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 clear_tv(&var1);
1040 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
1043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001044 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 ++p;
1046 }
1047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 {
1050 if (len == -1)
1051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001052 // "[key]": get key from "var1"
1053 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001054 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 }
1059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001060 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001061
1062 // a NULL dict is equivalent with an empty dict
1063 if (lp->ll_tv->vval.v_dict == NULL)
1064 {
1065 lp->ll_tv->vval.v_dict = dict_alloc();
1066 if (lp->ll_tv->vval.v_dict == NULL)
1067 {
1068 clear_tv(&var1);
1069 return NULL;
1070 }
1071 ++lp->ll_tv->vval.v_dict->dv_refcount;
1072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001073 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001074
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001075 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001077 // When assigning to a scope dictionary check that a function and
1078 // variable name is valid (only variable name unless it is l: or
1079 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001080 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001081 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001082 int prevval;
1083 int wrong;
1084
1085 if (len != -1)
1086 {
1087 prevval = key[len];
1088 key[len] = NUL;
1089 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001090 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001092 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1093 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001094 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001095 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001096 if (len != -1)
1097 key[len] = prevval;
1098 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001099 {
1100 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001101 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001102 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001103 }
1104
Bram Moolenaar10c65862020-10-08 21:16:42 +02001105 if (lp->ll_valtype != NULL)
1106 // use the type of the member
1107 lp->ll_valtype = lp->ll_valtype->tt_member;
1108
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001111 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001112 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001113 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001115 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001116 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 return NULL;
1118 }
1119
Bram Moolenaar31b81602019-02-10 22:14:27 +01001120 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001124 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001127 }
1128 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001131 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 p = NULL;
1135 break;
1136 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001137 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001138 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001139 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1140 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001141 {
1142 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001143 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001144 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001145
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001146 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001149 else if (lp->ll_tv->v_type == VAR_BLOB)
1150 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001151 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1152
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001153 /*
1154 * Get the number and item for the only or first index of the List.
1155 */
1156 if (empty1)
1157 lp->ll_n1 = 0;
1158 else
1159 // is number or string
1160 lp->ll_n1 = (long)tv_get_number(&var1);
1161 clear_tv(&var1);
1162
1163 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 || lp->ll_n1 > bloblen
1165 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 {
1167 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001168 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001169 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 return NULL;
1171 }
1172 if (lp->ll_range && !lp->ll_empty2)
1173 {
1174 lp->ll_n2 = (long)tv_get_number(&var2);
1175 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001176 if (lp->ll_n2 < 0
1177 || lp->ll_n2 >= bloblen
1178 || lp->ll_n2 < lp->ll_n1)
1179 {
1180 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 return NULL;
1183 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001184 }
1185 lp->ll_blob = lp->ll_tv->vval.v_blob;
1186 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001187 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001188 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 else
1190 {
1191 /*
1192 * Get the number and item for the only or first index of the List.
1193 */
1194 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001196 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001197 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001198 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001199 clear_tv(&var1);
1200
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 lp->ll_list = lp->ll_tv->vval.v_list;
1203 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1204 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001206 if (lp->ll_n1 < 0)
1207 {
1208 lp->ll_n1 = 0;
1209 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1210 }
1211 }
1212 if (lp->ll_li == NULL)
1213 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001214 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001215 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 }
1219
Bram Moolenaar10c65862020-10-08 21:16:42 +02001220 if (lp->ll_valtype != NULL)
1221 // use the type of the member
1222 lp->ll_valtype = lp->ll_valtype->tt_member;
1223
Bram Moolenaar8c711452005-01-14 21:53:12 +00001224 /*
1225 * May need to find the item or absolute index for the second
1226 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 * When no index given: "lp->ll_empty2" is TRUE.
1228 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001232 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001233 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001239 {
1240 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001241 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001243 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 }
1246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 if (lp->ll_n1 < 0)
1249 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1250 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001251 {
1252 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001253 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001255 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001256 }
1257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 }
1261
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001262 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 return p;
1265}
1266
1267/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272{
1273 vim_free(lp->ll_exp_name);
1274 vim_free(lp->ll_newkey);
1275}
1276
1277/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001278 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001280 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1281 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284set_var_lval(
1285 lval_T *lp,
1286 char_u *endp,
1287 typval_T *rettv,
1288 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001289 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001290 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291{
1292 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001293 listitem_T *ri;
1294 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295
1296 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001298 cc = *endp;
1299 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 if (lp->ll_blob != NULL)
1301 {
1302 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 if (op != NULL && *op != '=')
1305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001306 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 return;
1308 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001309 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001310 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311
1312 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1313 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001314 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001316 if (lp->ll_empty2)
1317 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1318
1319 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322 return;
1323 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001324 if (lp->ll_empty2)
1325 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001327 ir = 0;
1328 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1329 blob_set(lp->ll_blob, il,
1330 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 }
1332 else
1333 {
1334 val = (int)tv_get_number_chk(rettv, &error);
1335 if (!error)
1336 {
1337 garray_T *gap = &lp->ll_blob->bv_ga;
1338
1339 // Allow for appending a byte. Setting a byte beyond
1340 // the end is an error otherwise.
1341 if (lp->ll_n1 < gap->ga_len
1342 || (lp->ll_n1 == gap->ga_len
1343 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1344 {
1345 blob_set(lp->ll_blob, lp->ll_n1, val);
1346 if (lp->ll_n1 == gap->ga_len)
1347 ++gap->ga_len;
1348 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001349 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 }
1351 }
1352 }
1353 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001355 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001357 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001358 {
1359 emsg(_(e_cannot_mod));
1360 *endp = cc;
1361 return;
1362 }
1363
Bram Moolenaarff697e62019-02-12 22:28:33 +01001364 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001366 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001367 &tv, &di, TRUE, FALSE) == OK)
1368 {
1369 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001370 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1371 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001372 && tv_op(&tv, rettv, op) == OK)
1373 set_var(lp->ll_name, &tv, FALSE);
1374 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001377 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001378 {
1379 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001380 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001381 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001383 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001384 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001386 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001387 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001388 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001389 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001390 else if (lp->ll_range)
1391 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001393 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001394
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001395 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001396 {
1397 emsg(_("E996: Cannot lock a range"));
1398 return;
1399 }
1400
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001401 /*
1402 * Check whether any of the list items is locked
1403 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001404 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001405 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001406 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407 return;
1408 ri = ri->li_next;
1409 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1410 break;
1411 ll_li = ll_li->li_next;
1412 ++ll_n1;
1413 }
1414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 /*
1416 * Assign the List values to the list items.
1417 */
1418 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420 if (op != NULL && *op != '=')
1421 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1422 else
1423 {
1424 clear_tv(&lp->ll_li->li_tv);
1425 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 ri = ri->li_next;
1428 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1429 break;
1430 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001432 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001433 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 ri = NULL;
1436 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 lp->ll_li = lp->ll_li->li_next;
1440 ++lp->ll_n1;
1441 }
1442 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001443 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 else if (lp->ll_empty2
1445 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001447 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 }
1449 else
1450 {
1451 /*
1452 * Assign to a List or Dictionary item.
1453 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001454 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001455 {
1456 emsg(_("E996: Cannot lock a list or dict"));
1457 return;
1458 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001459
1460 if (lp->ll_valtype != NULL
1461 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1462 return;
1463
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 if (lp->ll_newkey != NULL)
1465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 if (op != NULL && *op != '=')
1467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001468 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469 return;
1470 }
1471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001472 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001473 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (di == NULL)
1475 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001476 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1477 {
1478 vim_free(di);
1479 return;
1480 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 else if (op != NULL && *op != '=')
1484 {
1485 tv_op(lp->ll_tv, rettv, op);
1486 return;
1487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001490
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 /*
1492 * Assign the value to the variable or list item.
1493 */
1494 if (copy)
1495 copy_tv(rettv, lp->ll_tv);
1496 else
1497 {
1498 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001499 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 }
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503}
1504
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001506 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1507 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 * Returns OK or FAIL.
1509 */
1510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001511tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001513 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 char_u numbuf[NUMBUFLEN];
1515 char_u *s;
1516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001517 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001518 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001519 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 {
1521 switch (tv1->v_type)
1522 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001523 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001524 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 case VAR_DICT:
1527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001528 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001529 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001530 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001531 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001532 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533 break;
1534
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001535 case VAR_BLOB:
1536 if (*op != '+' || tv2->v_type != VAR_BLOB)
1537 break;
1538 // BLOB += BLOB
1539 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1540 {
1541 blob_T *b1 = tv1->vval.v_blob;
1542 blob_T *b2 = tv2->vval.v_blob;
1543 int i, len = blob_len(b2);
1544 for (i = 0; i < len; i++)
1545 ga_append(&b1->bv_ga, blob_get(b2, i));
1546 }
1547 return OK;
1548
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 case VAR_LIST:
1550 if (*op != '+' || tv2->v_type != VAR_LIST)
1551 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001552 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001553 if (tv2->vval.v_list != NULL)
1554 {
1555 if (tv1->vval.v_list == NULL)
1556 {
1557 tv1->vval.v_list = tv2->vval.v_list;
1558 ++tv1->vval.v_list->lv_refcount;
1559 }
1560 else
1561 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1562 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 return OK;
1564
1565 case VAR_NUMBER:
1566 case VAR_STRING:
1567 if (tv2->v_type == VAR_LIST)
1568 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001569 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001572 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573#ifdef FEAT_FLOAT
1574 if (tv2->v_type == VAR_FLOAT)
1575 {
1576 float_T f = n;
1577
Bram Moolenaarff697e62019-02-12 22:28:33 +01001578 if (*op == '%')
1579 break;
1580 switch (*op)
1581 {
1582 case '+': f += tv2->vval.v_float; break;
1583 case '-': f -= tv2->vval.v_float; break;
1584 case '*': f *= tv2->vval.v_float; break;
1585 case '/': f /= tv2->vval.v_float; break;
1586 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587 clear_tv(tv1);
1588 tv1->v_type = VAR_FLOAT;
1589 tv1->vval.v_float = f;
1590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592#endif
1593 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 switch (*op)
1595 {
1596 case '+': n += tv_get_number(tv2); break;
1597 case '-': n -= tv_get_number(tv2); break;
1598 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001599 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1600 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001601 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 clear_tv(tv1);
1603 tv1->v_type = VAR_NUMBER;
1604 tv1->vval.v_number = n;
1605 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 }
1607 else
1608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 if (tv2->v_type == VAR_FLOAT)
1610 break;
1611
Bram Moolenaarff697e62019-02-12 22:28:33 +01001612 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001613 s = tv_get_string(tv1);
1614 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 clear_tv(tv1);
1616 tv1->v_type = VAR_STRING;
1617 tv1->vval.v_string = s;
1618 }
1619 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001622#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 {
1624 float_T f;
1625
Bram Moolenaarff697e62019-02-12 22:28:33 +01001626 if (*op == '%' || *op == '.'
1627 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628 && tv2->v_type != VAR_NUMBER
1629 && tv2->v_type != VAR_STRING))
1630 break;
1631 if (tv2->v_type == VAR_FLOAT)
1632 f = tv2->vval.v_float;
1633 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001635 switch (*op)
1636 {
1637 case '+': tv1->vval.v_float += f; break;
1638 case '-': tv1->vval.v_float -= f; break;
1639 case '*': tv1->vval.v_float *= f; break;
1640 case '/': tv1->vval.v_float /= f; break;
1641 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001644 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 }
1646 }
1647
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001648 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649 return FAIL;
1650}
1651
1652/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 * Evaluate the expression used in a ":for var in expr" command.
1654 * "arg" points to "var".
1655 * Set "*errp" to TRUE for an error, FALSE otherwise;
1656 * Return a pointer that holds the info. Null when there is an error.
1657 */
1658 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659eval_for_line(
1660 char_u *arg,
1661 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001662 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001663 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664{
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 typval_T tv;
1668 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001669 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001673 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 if (fi == NULL)
1675 return NULL;
1676
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001677 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 if (expr == NULL)
1679 return fi;
1680
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001681 expr = skipwhite_and_linebreak(expr, evalarg);
1682 if (expr[0] != 'i' || expr[1] != 'n'
1683 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 return fi;
1687 }
1688
1689 if (skip)
1690 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1692 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 {
1694 *errp = FALSE;
1695 if (!skip)
1696 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001698 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 l = tv.vval.v_list;
1700 if (l == NULL)
1701 {
1702 // a null list is like an empty list: do nothing
1703 clear_tv(&tv);
1704 }
1705 else
1706 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001708 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 // No need to increment the refcount, it's already set for
1711 // the list being used in "tv".
1712 fi->fi_list = l;
1713 list_add_watch(l, &fi->fi_lw);
1714 fi->fi_lw.lw_item = l->lv_first;
1715 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001716 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001717 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001718 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001719 fi->fi_bi = 0;
1720 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 typval_T btv;
1723
1724 // Make a copy, so that the iteration still works when the
1725 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001727 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001728 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001729 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001730 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 else
1732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001733 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 }
1736 }
1737 }
1738 if (skip)
1739 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001740 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741
1742 return fi;
1743}
1744
1745/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001746 * Used when looping over a :for line, skip the "in expr" part.
1747 */
1748 void
1749skip_for_lines(void *fi_void, evalarg_T *evalarg)
1750{
1751 forinfo_T *fi = (forinfo_T *)fi_void;
1752 int i;
1753
1754 for (i = 0; i < fi->fi_break_count; ++i)
1755 eval_next_line(evalarg);
1756}
1757
1758/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 * Use the first item in a ":for" list. Advance to the next.
1760 * Assign the values to the variable (list). "arg" points to the first one.
1761 * Return TRUE when a valid item was found, FALSE when at end of list or
1762 * something wrong.
1763 */
1764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001767 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001769 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001770 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001772 if (fi->fi_blob != NULL)
1773 {
1774 typval_T tv;
1775
1776 if (fi->fi_bi >= blob_len(fi->fi_blob))
1777 return FALSE;
1778 tv.v_type = VAR_NUMBER;
1779 tv.v_lock = VAR_FIXED;
1780 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1781 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001782 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001783 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001784 }
1785
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 item = fi->fi_lw.lw_item;
1787 if (item == NULL)
1788 result = FALSE;
1789 else
1790 {
1791 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001792 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001793 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 }
1795 return result;
1796}
1797
1798/*
1799 * Free the structure used to store info used by ":for".
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803{
Bram Moolenaar33570922005-01-25 22:26:29 +00001804 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001806 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001807 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001809 list_unref(fi->fi_list);
1810 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001811 if (fi != NULL && fi->fi_blob != NULL)
1812 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 vim_free(fi);
1814}
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817set_context_for_expression(
1818 expand_T *xp,
1819 char_u *arg,
1820 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001822 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001826 if (cmdidx == CMD_let || cmdidx == CMD_var
1827 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 {
1829 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001833 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 {
1835 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001836 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001837 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 break;
1839 }
1840 return;
1841 }
1842 }
1843 else
1844 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1845 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 while ((xp->xp_pattern = vim_strpbrk(arg,
1847 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1848 {
1849 c = *xp->xp_pattern;
1850 if (c == '&')
1851 {
1852 c = xp->xp_pattern[1];
1853 if (c == '&')
1854 {
1855 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001856 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001861 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1862 xp->xp_pattern += 2;
1863
1864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 else if (c == '$')
1867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 xp->xp_context = EXPAND_ENV_VARS;
1870 }
1871 else if (c == '=')
1872 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001873 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 xp->xp_context = EXPAND_EXPRESSION;
1875 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001876 else if (c == '#'
1877 && xp->xp_context == EXPAND_EXPRESSION)
1878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001879 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001880 break;
1881 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001882 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 && xp->xp_context == EXPAND_FUNCTIONS
1884 && vim_strchr(xp->xp_pattern, '(') == NULL)
1885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 break;
1888 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001891 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1894 if (c == '\\' && xp->xp_pattern[1] != NUL)
1895 ++xp->xp_pattern;
1896 xp->xp_context = EXPAND_NOTHING;
1897 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001900 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1902 /* skip */ ;
1903 xp->xp_context = EXPAND_NOTHING;
1904 }
1905 else if (c == '|')
1906 {
1907 if (xp->xp_pattern[1] == '|')
1908 {
1909 ++xp->xp_pattern;
1910 xp->xp_context = EXPAND_EXPRESSION;
1911 }
1912 else
1913 xp->xp_context = EXPAND_COMMANDS;
1914 }
1915 else
1916 xp->xp_context = EXPAND_EXPRESSION;
1917 }
1918 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Doesn't look like something valid, expand as an expression
1920 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 arg = xp->xp_pattern;
1923 if (*arg != NUL)
1924 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1925 /* skip */ ;
1926 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001927
1928 // ":exe one two" completes "two"
1929 if ((cmdidx == CMD_execute
1930 || cmdidx == CMD_echo
1931 || cmdidx == CMD_echon
1932 || cmdidx == CMD_echomsg)
1933 && xp->xp_context == EXPAND_EXPRESSION)
1934 {
1935 for (;;)
1936 {
1937 char_u *n = skiptowhite(arg);
1938
1939 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1940 break;
1941 arg = skipwhite(n);
1942 }
1943 }
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 xp->xp_pattern = arg;
1946}
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001949 * Return TRUE if "pat" matches "text".
1950 * Does not use 'cpo' and always uses 'magic'.
1951 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001952 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001953pattern_match(char_u *pat, char_u *text, int ic)
1954{
1955 int matches = FALSE;
1956 char_u *save_cpo;
1957 regmatch_T regmatch;
1958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001960 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001961 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001962 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1963 if (regmatch.regprog != NULL)
1964 {
1965 regmatch.rm_ic = ic;
1966 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1967 vim_regfree(regmatch.regprog);
1968 }
1969 p_cpo = save_cpo;
1970 return matches;
1971}
1972
1973/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 * Handle a name followed by "(". Both for just "name(arg)" and for
1975 * "expr->name(arg)".
1976 * Returns OK or FAIL.
1977 */
1978 static int
1979eval_func(
1980 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001981 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001982 char_u *name,
1983 int name_len,
1984 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001985 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001986 typval_T *basetv) // "expr" for "expr->name(arg)"
1987{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001988 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001989 char_u *s = name;
1990 int len = name_len;
1991 partial_T *partial;
1992 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001993 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994
1995 if (!evaluate)
1996 check_vars(s, len);
1997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001998 // If "s" is the name of a variable of type VAR_FUNC
1999 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002000 s = deref_func_name(s, &len, &partial,
2001 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 // Need to make a copy, in case evaluating the arguments makes
2004 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002006 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007 ret = FAIL;
2008 else
2009 {
2010 funcexe_T funcexe;
2011
2012 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002013 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 funcexe.firstline = curwin->w_cursor.lnum;
2015 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 funcexe.evaluate = evaluate;
2017 funcexe.partial = partial;
2018 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002019 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002020 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 }
2022 vim_free(s);
2023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002024 // If evaluate is FALSE rettv->v_type was not set in
2025 // get_func_tv, but it's needed in handle_subscript() to parse
2026 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002027 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2028 {
2029 rettv->vval.v_string = NULL;
2030 rettv->v_type = VAR_FUNC;
2031 }
2032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // Stop the expression evaluation when immediately
2034 // aborting on error, or when an interrupt occurred or
2035 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036 if (evaluate && aborting())
2037 {
2038 if (ret == OK)
2039 clear_tv(rettv);
2040 ret = FAIL;
2041 }
2042 return ret;
2043}
2044
2045/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002046 * Get the next line source line without advancing. But do skip over comment
2047 * lines.
2048 */
2049 static char_u *
2050getline_peek_skip_comments(evalarg_T *evalarg)
2051{
2052 for (;;)
2053 {
2054 char_u *next = getline_peek(evalarg->eval_getline,
2055 evalarg->eval_cookie);
2056 char_u *p;
2057
2058 if (next == NULL)
2059 break;
2060 p = skipwhite(next);
2061 if (*p != NUL && !vim9_comment_start(p))
2062 return next;
2063 (void)eval_next_line(evalarg);
2064 }
2065 return NULL;
2066}
2067
2068/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002069 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2070 * comment) and there is a next line, return the next line (skipping blanks)
2071 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2073 * "arg" must point somewhere inside a line, not at the start.
2074 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002075 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002076eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2077{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002078 char_u *p = skipwhite(arg);
2079
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002081 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002083 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002084 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002086 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002087
2088 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002089 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002090 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002091 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092
Bram Moolenaar9d489562020-07-30 20:08:50 +02002093 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094 {
2095 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002097 }
2098 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100}
2101
2102/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002103 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002104 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002105 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002106eval_next_line(evalarg_T *evalarg)
2107{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002108 garray_T *gap = &evalarg->eval_ga;
2109 char_u *line;
2110
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002111 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002112 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2113 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002114 else
2115 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002116 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002117 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2118 {
2119 // Going to concatenate the lines after parsing.
2120 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2121 ++gap->ga_len;
2122 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002123 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 {
2125 vim_free(evalarg->eval_tofree);
2126 evalarg->eval_tofree = line;
2127 }
2128 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002129}
2130
Bram Moolenaare6b53242020-07-01 17:28:33 +02002131/*
2132 * Call eval_next_non_blank() and get the next line if needed.
2133 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002134 char_u *
2135skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2136{
2137 int getnext;
2138 char_u *p = skipwhite(arg);
2139
Bram Moolenaar962d7212020-07-04 14:15:00 +02002140 if (evalarg == NULL)
2141 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002142 eval_next_non_blank(p, evalarg, &getnext);
2143 if (getnext)
2144 return eval_next_line(evalarg);
2145 return p;
2146}
2147
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002148/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002149 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002150 */
2151 void
2152clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2153{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002154 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002155 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002156 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002157 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002158 if (eap != NULL)
2159 {
2160 // We may need to keep the original command line, e.g. for
2161 // ":let" it has the variable names. But we may also need the
2162 // new one, "nextcmd" points into it. Keep both.
2163 vim_free(eap->cmdline_tofree);
2164 eap->cmdline_tofree = *eap->cmdlinep;
2165 *eap->cmdlinep = evalarg->eval_tofree;
2166 }
2167 else
2168 vim_free(evalarg->eval_tofree);
2169 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002170 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002171
2172 vim_free(evalarg->eval_tofree_lambda);
2173 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002174 }
2175}
2176
2177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2181 */
2182
2183/*
2184 * Handle zero level expression.
2185 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002187 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002188 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * Return OK or FAIL.
2190 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002191 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192eval0(
2193 char_u *arg,
2194 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002195 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002196 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 int ret;
2199 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002200 int did_emsg_before = did_emsg;
2201 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002202 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002206 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002207
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002208 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 /*
2213 * Report the invalid expression unless the expression evaluation has
2214 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002215 * exception, or we already gave a more specific error.
2216 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002218 if (!aborting()
2219 && did_emsg == did_emsg_before
2220 && called_emsg == called_emsg_before
2221 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002223
2224 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002225 // a next command to avoid more errors, unless "|" is following, which
2226 // could only be a command separator.
2227 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2228 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002229 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002231
2232 if (eap != NULL)
2233 eap->nextcmd = check_nextcmd(p);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return ret;
2236}
2237
2238/*
2239 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002240 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002241 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 *
2243 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002244 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002246 * Note: "rettv.v_lock" is not set.
2247 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 * Return OK or FAIL.
2249 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002250 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002253 char_u *p;
2254 int getnext;
2255
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002256 CLEAR_POINTER(rettv);
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /*
2259 * Get the first variable.
2260 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 return FAIL;
2263
Bram Moolenaar793648f2020-06-26 21:28:25 +02002264 p = eval_next_non_blank(*arg, evalarg, &getnext);
2265 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002267 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002268 int result;
2269 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002270 evalarg_T *evalarg_used = evalarg;
2271 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002273 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002274 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275
2276 if (evalarg == NULL)
2277 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002278 CLEAR_FIELD(local_evalarg);
2279 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002281 orig_flags = evalarg_used->eval_flags;
2282 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 if (getnext)
2285 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002286 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002287 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002288 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002289 {
2290 error_white_both(p, 1);
2291 clear_tv(rettv);
2292 return FAIL;
2293 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002294 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002295 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002300 int error = FALSE;
2301
Bram Moolenaar13106602020-10-04 16:06:05 +02002302 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002303 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002304 else if (vim9script)
2305 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002306 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002308 if (error || !op_falsy || !result)
2309 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002310 if (error)
2311 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
2314 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002315 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 if (op_falsy)
2318 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002319 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002320 {
2321 error_white_both(p, 1);
2322 clear_tv(rettv);
2323 return FAIL;
2324 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002325 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002326 evalarg_used->eval_flags = (op_falsy ? !result : result)
2327 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2328 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002329 {
2330 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002332 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002333 if (!op_falsy || !result)
2334 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002338 /*
2339 * Check for the ":".
2340 */
2341 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2342 if (*p != ':')
2343 {
2344 emsg(_(e_missing_colon));
2345 if (evaluate && result)
2346 clear_tv(rettv);
2347 evalarg_used->eval_flags = orig_flags;
2348 return FAIL;
2349 }
2350 if (getnext)
2351 *arg = eval_next_line(evalarg_used);
2352 else
2353 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002354 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002355 {
2356 error_white_both(p, 1);
2357 clear_tv(rettv);
2358 evalarg_used->eval_flags = orig_flags;
2359 return FAIL;
2360 }
2361 *arg = p;
2362 }
2363
2364 /*
2365 * Get the third variable. Recursive!
2366 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002367 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002368 {
2369 error_white_both(p, 1);
2370 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002371 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002372 return FAIL;
2373 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002374 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2375 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002377 if (eval1(arg, &var2, evalarg_used) == FAIL)
2378 {
2379 if (evaluate && result)
2380 clear_tv(rettv);
2381 evalarg_used->eval_flags = orig_flags;
2382 return FAIL;
2383 }
2384 if (evaluate && !result)
2385 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002387
2388 if (evalarg == NULL)
2389 clear_evalarg(&local_evalarg, NULL);
2390 else
2391 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393
2394 return OK;
2395}
2396
2397/*
2398 * Handle first level expression:
2399 * expr2 || expr2 || expr2 logical OR
2400 *
2401 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002402 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 *
2404 * Return OK or FAIL.
2405 */
2406 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002409 char_u *p;
2410 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 /*
2413 * Get the first variable.
2414 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return FAIL;
2417
2418 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002419 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002421 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002424 evalarg_T *evalarg_used = evalarg;
2425 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 int evaluate;
2427 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 long result = FALSE;
2429 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002430 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002431 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002432
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 if (evalarg == NULL)
2434 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 CLEAR_FIELD(local_evalarg);
2436 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 orig_flags = evalarg_used->eval_flags;
2439 evaluate = orig_flags & EVAL_EVALUATE;
2440 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002442 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002443 result = tv_get_bool_chk(rettv, &error);
2444 else if (tv_get_number_chk(rettv, &error) != 0)
2445 result = TRUE;
2446 clear_tv(rettv);
2447 if (error)
2448 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
2451 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 while (p[0] == '|' && p[1] == '|')
2455 {
2456 if (getnext)
2457 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002458 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002459 {
2460 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2461 {
2462 error_white_both(p, 2);
2463 clear_tv(rettv);
2464 return FAIL;
2465 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002466 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002467 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468
2469 /*
2470 * Get the second variable.
2471 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002472 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2473 {
2474 error_white_both(p, 2);
2475 clear_tv(rettv);
2476 return FAIL;
2477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2479 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002483
2484 /*
2485 * Compute the result.
2486 */
2487 if (evaluate && !result)
2488 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002489 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002490 result = tv_get_bool_chk(&var2, &error);
2491 else if (tv_get_number_chk(&var2, &error) != 0)
2492 result = TRUE;
2493 clear_tv(&var2);
2494 if (error)
2495 return FAIL;
2496 }
2497 if (evaluate)
2498 {
2499 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002500 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002501 rettv->v_type = VAR_BOOL;
2502 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002503 }
2504 else
2505 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002506 rettv->v_type = VAR_NUMBER;
2507 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002508 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002510
2511 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002513
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 if (evalarg == NULL)
2515 clear_evalarg(&local_evalarg, NULL);
2516 else
2517 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
2520 return OK;
2521}
2522
2523/*
2524 * Handle second level expression:
2525 * expr3 && expr3 && expr3 logical AND
2526 *
2527 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002528 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 *
2530 * Return OK or FAIL.
2531 */
2532 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002533eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002535 char_u *p;
2536 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
2538 /*
2539 * Get the first variable.
2540 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002541 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 return FAIL;
2543
2544 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002545 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002547 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002548 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002550 evalarg_T *evalarg_used = evalarg;
2551 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002552 int orig_flags;
2553 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554 long result = TRUE;
2555 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002557 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002558
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559 if (evalarg == NULL)
2560 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561 CLEAR_FIELD(local_evalarg);
2562 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002563 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 orig_flags = evalarg_used->eval_flags;
2565 evaluate = orig_flags & EVAL_EVALUATE;
2566 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002567 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002568 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002569 result = tv_get_bool_chk(rettv, &error);
2570 else if (tv_get_number_chk(rettv, &error) == 0)
2571 result = FALSE;
2572 clear_tv(rettv);
2573 if (error)
2574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576
2577 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 while (p[0] == '&' && p[1] == '&')
2581 {
2582 if (getnext)
2583 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002584 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002585 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002586 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002587 {
2588 error_white_both(p, 2);
2589 clear_tv(rettv);
2590 return FAIL;
2591 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002592 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002593 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594
2595 /*
2596 * Get the second variable.
2597 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002598 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2599 {
2600 error_white_both(p, 2);
2601 clear_tv(rettv);
2602 return FAIL;
2603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2605 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002607 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002609 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610
2611 /*
2612 * Compute the result.
2613 */
2614 if (evaluate && result)
2615 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002616 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002617 result = tv_get_bool_chk(&var2, &error);
2618 else if (tv_get_number_chk(&var2, &error) == 0)
2619 result = FALSE;
2620 clear_tv(&var2);
2621 if (error)
2622 return FAIL;
2623 }
2624 if (evaluate)
2625 {
2626 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002627 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002628 rettv->v_type = VAR_BOOL;
2629 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002630 }
2631 else
2632 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002633 rettv->v_type = VAR_NUMBER;
2634 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002635 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637
2638 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002640
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641 if (evalarg == NULL)
2642 clear_evalarg(&local_evalarg, NULL);
2643 else
2644 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646
2647 return OK;
2648}
2649
2650/*
2651 * Handle third level expression:
2652 * var1 == var2
2653 * var1 =~ var2
2654 * var1 != var2
2655 * var1 !~ var2
2656 * var1 > var2
2657 * var1 >= var2
2658 * var1 < var2
2659 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002660 * var1 is var2
2661 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 *
2663 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002664 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 *
2666 * Return OK or FAIL.
2667 */
2668 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002672 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002673 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002675 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677 /*
2678 * Get the first variable.
2679 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 return FAIL;
2682
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002683 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002684 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002687 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002689 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002691 typval_T var2;
2692 int ic;
2693 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002694 int evaluate = evalarg == NULL
2695 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002696
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002697 if (getnext)
2698 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002699 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2700 {
2701 error_white_both(p, len);
2702 clear_tv(rettv);
2703 return FAIL;
2704 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002705
Bram Moolenaar696ba232020-07-29 21:20:41 +02002706 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2707 {
2708 semsg(_(e_invexpr2), p);
2709 clear_tv(rettv);
2710 return FAIL;
2711 }
2712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002713 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (p[len] == '?')
2715 {
2716 ic = TRUE;
2717 ++len;
2718 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002719 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 else if (p[len] == '#')
2721 {
2722 ic = FALSE;
2723 ++len;
2724 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002725 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002727 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
2730 * Get the second variable.
2731 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002732 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2733 {
2734 error_white_both(p, 1);
2735 clear_tv(rettv);
2736 return FAIL;
2737 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002738 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 return FAIL;
2743 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002744 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002745 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002746 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002747
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002748 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002749 {
2750 ret = FAIL;
2751 clear_tv(rettv);
2752 }
2753 else
2754 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002755 clear_tv(&var2);
2756 return ret;
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759
2760 return OK;
2761}
2762
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002763/*
2764 * Make a copy of blob "tv1" and append blob "tv2".
2765 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 void
2767eval_addblob(typval_T *tv1, typval_T *tv2)
2768{
2769 blob_T *b1 = tv1->vval.v_blob;
2770 blob_T *b2 = tv2->vval.v_blob;
2771 blob_T *b = blob_alloc();
2772 int i;
2773
2774 if (b != NULL)
2775 {
2776 for (i = 0; i < blob_len(b1); i++)
2777 ga_append(&b->bv_ga, blob_get(b1, i));
2778 for (i = 0; i < blob_len(b2); i++)
2779 ga_append(&b->bv_ga, blob_get(b2, i));
2780
2781 clear_tv(tv1);
2782 rettv_blob_set(tv1, b);
2783 }
2784}
2785
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002786/*
2787 * Make a copy of list "tv1" and append list "tv2".
2788 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 int
2790eval_addlist(typval_T *tv1, typval_T *tv2)
2791{
2792 typval_T var3;
2793
2794 // concatenate Lists
2795 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2796 {
2797 clear_tv(tv1);
2798 clear_tv(tv2);
2799 return FAIL;
2800 }
2801 clear_tv(tv1);
2802 *tv1 = var3;
2803 return OK;
2804}
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806/*
2807 * Handle fourth level expression:
2808 * + number addition
2809 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002810 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002811 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 *
2813 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002814 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 *
2816 * Return OK or FAIL.
2817 */
2818 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 /*
2822 * Get the first variable.
2823 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002824 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 return FAIL;
2826
2827 /*
2828 * Repeat computing, until no '+', '-' or '.' is following.
2829 */
2830 for (;;)
2831 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002832 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002833 int getnext;
2834 char_u *p;
2835 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002836 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002837 int concat;
2838 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002839 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002841 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002842 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 p = eval_next_non_blank(*arg, evalarg, &getnext);
2844 op = *p;
2845 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002846 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002848
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002849 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002850 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002852 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002853 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002854 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002855 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002856 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002857 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002858 clear_tv(rettv);
2859 return FAIL;
2860 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002861 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002862 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002863 if ((op != '+' || (rettv->v_type != VAR_LIST
2864 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002865#ifdef FEAT_FLOAT
2866 && (op == '.' || rettv->v_type != VAR_FLOAT)
2867#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002868 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002869 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002870 int error = FALSE;
2871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 // For "list + ...", an illegal use of the first operand as
2873 // a number cannot be determined before evaluating the 2nd
2874 // operand: if this is also a list, all is ok.
2875 // For "something . ...", "something - ..." or "non-list + ...",
2876 // we know that the first operand needs to be a string or number
2877 // without evaluating the 2nd operand. So check before to avoid
2878 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002879 if (op != '.')
2880 tv_get_number_chk(rettv, &error);
2881 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002882 {
2883 clear_tv(rettv);
2884 return FAIL;
2885 }
2886 }
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 /*
2889 * Get the second variable.
2890 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002891 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002892 {
2893 error_white_both(p, oplen);
2894 clear_tv(rettv);
2895 return FAIL;
2896 }
2897 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002898 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 return FAIL;
2902 }
2903
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 /*
2907 * Compute the result.
2908 */
2909 if (op == '.')
2910 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002911 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2912 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002913 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914
Bram Moolenaar2e086612020-08-25 22:37:48 +02002915 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002916 || var2.v_type == VAR_CHANNEL
2917 || var2.v_type == VAR_JOB))
2918 emsg(_(e_inval_string));
2919#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002920 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002921 {
2922 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2923 var2.vval.v_float);
2924 s2 = buf2;
2925 }
2926#endif
2927 else
2928 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002929 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002930 {
2931 clear_tv(rettv);
2932 clear_tv(&var2);
2933 return FAIL;
2934 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 clear_tv(rettv);
2937 rettv->v_type = VAR_STRING;
2938 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002940 else if (op == '+' && rettv->v_type == VAR_BLOB
2941 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002943 else if (op == '+' && rettv->v_type == VAR_LIST
2944 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002945 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002947 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 else
2950 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 int error = FALSE;
2952 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002953#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002954 float_T f1 = 0, f2 = 0;
2955
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002956 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002958 f1 = rettv->vval.v_float;
2959 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002960 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002961 else
2962#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002964 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965 if (error)
2966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002967 // This can only happen for "list + non-list". For
2968 // "non-list + ..." or "something - ...", we returned
2969 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002970 clear_tv(rettv);
2971 return FAIL;
2972 }
2973#ifdef FEAT_FLOAT
2974 if (var2.v_type == VAR_FLOAT)
2975 f1 = n1;
2976#endif
2977 }
2978#ifdef FEAT_FLOAT
2979 if (var2.v_type == VAR_FLOAT)
2980 {
2981 f2 = var2.vval.v_float;
2982 n2 = 0;
2983 }
2984 else
2985#endif
2986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002987 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 if (error)
2989 {
2990 clear_tv(rettv);
2991 clear_tv(&var2);
2992 return FAIL;
2993 }
2994#ifdef FEAT_FLOAT
2995 if (rettv->v_type == VAR_FLOAT)
2996 f2 = n2;
2997#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000
3001#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003002 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3004 {
3005 if (op == '+')
3006 f1 = f1 + f2;
3007 else
3008 f1 = f1 - f2;
3009 rettv->v_type = VAR_FLOAT;
3010 rettv->vval.v_float = f1;
3011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013#endif
3014 {
3015 if (op == '+')
3016 n1 = n1 + n2;
3017 else
3018 n1 = n1 - n2;
3019 rettv->v_type = VAR_NUMBER;
3020 rettv->vval.v_number = n1;
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003023 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025 }
3026 return OK;
3027}
3028
3029/*
3030 * Handle fifth level expression:
3031 * * number multiplication
3032 * / number division
3033 * % number modulo
3034 *
3035 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003036 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 *
3038 * Return OK or FAIL.
3039 */
3040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003041eval6(
3042 char_u **arg,
3043 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003044 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003045 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003048 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051 /*
3052 * Get the first variable.
3053 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003054 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 return FAIL;
3056
3057 /*
3058 * Repeat computing, until no '*', '/' or '%' is following.
3059 */
3060 for (;;)
3061 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003062 int evaluate;
3063 int getnext;
3064 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003065 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003066 int op;
3067 varnumber_T n1, n2;
3068#ifdef FEAT_FLOAT
3069 float_T f1, f2;
3070#endif
3071 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072
Bram Moolenaar9d489562020-07-30 20:08:50 +02003073 p = eval_next_non_blank(*arg, evalarg, &getnext);
3074 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003075 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003077
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003078 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003080 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003081 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003082 {
3083 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3084 {
3085 error_white_both(p, 1);
3086 clear_tv(rettv);
3087 return FAIL;
3088 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003089 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003092#ifdef FEAT_FLOAT
3093 f1 = 0;
3094 f2 = 0;
3095#endif
3096 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003099#ifdef FEAT_FLOAT
3100 if (rettv->v_type == VAR_FLOAT)
3101 {
3102 f1 = rettv->vval.v_float;
3103 use_float = TRUE;
3104 n1 = 0;
3105 }
3106 else
3107#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003108 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003110 if (error)
3111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 else
3114 n1 = 0;
3115
3116 /*
3117 * Get the second variable.
3118 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003119 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3120 {
3121 error_white_both(p, 1);
3122 clear_tv(rettv);
3123 return FAIL;
3124 }
3125 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return FAIL;
3128
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#ifdef FEAT_FLOAT
3132 if (var2.v_type == VAR_FLOAT)
3133 {
3134 if (!use_float)
3135 {
3136 f1 = n1;
3137 use_float = TRUE;
3138 }
3139 f2 = var2.vval.v_float;
3140 n2 = 0;
3141 }
3142 else
3143#endif
3144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003145 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146 clear_tv(&var2);
3147 if (error)
3148 return FAIL;
3149#ifdef FEAT_FLOAT
3150 if (use_float)
3151 f2 = n2;
3152#endif
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /*
3156 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159#ifdef FEAT_FLOAT
3160 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 if (op == '*')
3163 f1 = f1 * f2;
3164 else if (op == '/')
3165 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003166# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003167 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003168 if (f2 == 0.0)
3169 {
3170 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003171 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003172 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003173 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003174 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003175 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003176 }
3177 else
3178 f1 = f1 / f2;
3179# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003180 // We rely on the floating point library to handle divide
3181 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003183# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188 return FAIL;
3189 }
3190 rettv->v_type = VAR_FLOAT;
3191 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 if (op == '*')
3197 n1 = n1 * n2;
3198 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003199 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003201 n1 = num_modulus(n1, n2);
3202
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203 rettv->v_type = VAR_NUMBER;
3204 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207 }
3208
3209 return OK;
3210}
3211
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003212 int
3213eval_leader(char_u **arg, int vim9)
3214{
3215 char_u *s = *arg;
3216 char_u *p = *arg;
3217
3218 while (*p == '!' || *p == '-' || *p == '+')
3219 {
3220 char_u *n = skipwhite(p + 1);
3221
3222 // ++, --, -+ and +- are not accepted in Vim9 script
3223 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3224 {
3225 semsg(_(e_invexpr2), s);
3226 return FAIL;
3227 }
3228 p = n;
3229 }
3230 *arg = p;
3231 return OK;
3232}
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234/*
3235 * Handle sixth level expression:
3236 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003237 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003238 * "string" string constant
3239 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * &option-name option value
3241 * @r register contents
3242 * identifier variable value
3243 * function() function call
3244 * $VAR environment variable
3245 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003246 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003248 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003249 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 *
3251 * Also handle:
3252 * ! in front logical NOT
3253 * - in front unary minus
3254 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003255 * trailing [] subscript in String or List
3256 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003257 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 *
3259 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003260 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 *
3262 * Return OK or FAIL.
3263 */
3264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003265eval7(
3266 char_u **arg,
3267 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003268 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003271 int evaluate = evalarg != NULL
3272 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int len;
3274 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 char_u *start_leader, *end_leader;
3276 int ret = OK;
3277 char_u *alias;
3278
3279 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003281 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003286 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003289 if (eval_leader(arg, in_vim9script()) == FAIL)
3290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 end_leader = *arg;
3292
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003293 if (**arg == '.' && (!isdigit(*(*arg + 1))
3294#ifdef FEAT_FLOAT
3295 || current_sctx.sc_version < 2
3296#endif
3297 ))
3298 {
3299 semsg(_(e_invexpr2), *arg);
3300 ++*arg;
3301 return FAIL;
3302 }
3303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 switch (**arg)
3305 {
3306 /*
3307 * Number constant.
3308 */
3309 case '0':
3310 case '1':
3311 case '2':
3312 case '3':
3313 case '4':
3314 case '5':
3315 case '6':
3316 case '7':
3317 case '8':
3318 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003319 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003320
3321 // Apply prefixed "-" and "+" now. Matters especially when
3322 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003323 if (ret == OK && evaluate && end_leader > start_leader
3324 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003325 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 break;
3327
3328 /*
3329 * String constant: "string".
3330 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003331 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 break;
3333
3334 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003337 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003338 break;
3339
3340 /*
3341 * List: [expr, expr]
3342 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003343 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 break;
3345
3346 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003347 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003348 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003349 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003350 {
3351 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003352 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003353 }
3354 else
3355 ret = NOTDONE;
3356 break;
3357
3358 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003359 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003360 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003362 case '{': if (in_vim9script())
3363 ret = NOTDONE;
3364 else
3365 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003366 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003367 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003368 break;
3369
3370 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003371 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003373 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 break;
3375
3376 /*
3377 * Environment variable: $VAR.
3378 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003379 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 break;
3381
3382 /*
3383 * Register contents: @r.
3384 */
3385 case '@': ++*arg;
3386 if (evaluate)
3387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003388 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003389 rettv->vval.v_string = get_reg_contents(**arg,
3390 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 if (**arg != NUL)
3393 ++*arg;
3394 break;
3395
3396 /*
3397 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003398 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003400 case '(': ret = NOTDONE;
3401 if (in_vim9script())
3402 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3403 if (ret == NOTDONE)
3404 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003405 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003406 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003407
Bram Moolenaar9215f012020-06-27 21:18:00 +02003408 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003409 if (**arg == ')')
3410 ++*arg;
3411 else if (ret == OK)
3412 {
3413 emsg(_(e_missing_close));
3414 clear_tv(rettv);
3415 ret = FAIL;
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 break;
3419
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 break;
3422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423
3424 if (ret == NOTDONE)
3425 {
3426 /*
3427 * Must be a variable or function name.
3428 * Can also be a curly-braces kind of name: {expr}.
3429 */
3430 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003431 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003432 if (alias != NULL)
3433 s = alias;
3434
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003435 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003436 ret = FAIL;
3437 else
3438 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003439 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3440
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003441 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3442 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003443 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003444 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003445 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003446 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003447 else if (flags & EVAL_CONSTANT)
3448 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003449 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003450 {
3451 // get the value of "true", "false" or a variable
3452 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3453 {
3454 rettv->v_type = VAR_BOOL;
3455 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003456 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003457 }
3458 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003459 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003460 {
3461 rettv->v_type = VAR_BOOL;
3462 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003463 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003464 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003465 else if (len == 4 && in_vim9script()
3466 && STRNCMP(s, "null", 4) == 0)
3467 {
3468 rettv->v_type = VAR_SPECIAL;
3469 rettv->vval.v_number = VVAL_NULL;
3470 ret = OK;
3471 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003472 else
3473 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003476 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003477 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003478 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003481 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003482 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483 }
3484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003485 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3486 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003487 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003488 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 /*
3491 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3492 */
3493 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003494 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003495 return ret;
3496}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003497
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003498/*
3499 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003500 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003501 * Adjusts "end_leaderp" until it is at "start_leader".
3502 */
3503 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003504eval7_leader(
3505 typval_T *rettv,
3506 int numeric_only,
3507 char_u *start_leader,
3508 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003509{
3510 char_u *end_leader = *end_leaderp;
3511 int ret = OK;
3512 int error = FALSE;
3513 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003514 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515#ifdef FEAT_FLOAT
3516 float_T f = 0.0;
3517
3518 if (rettv->v_type == VAR_FLOAT)
3519 f = rettv->vval.v_float;
3520 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003522 {
3523 while (VIM_ISWHITE(end_leader[-1]))
3524 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003525 if (in_vim9script() && end_leader[-1] == '!')
3526 val = tv2bool(rettv);
3527 else
3528 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003529 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003530 if (error)
3531 {
3532 clear_tv(rettv);
3533 ret = FAIL;
3534 }
3535 else
3536 {
3537 while (end_leader > start_leader)
3538 {
3539 --end_leader;
3540 if (*end_leader == '!')
3541 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003542 if (numeric_only)
3543 {
3544 ++end_leader;
3545 break;
3546 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003547#ifdef FEAT_FLOAT
3548 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003549 {
3550 if (in_vim9script())
3551 {
3552 rettv->v_type = VAR_BOOL;
3553 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3554 }
3555 else
3556 f = !f;
3557 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003558 else
3559#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003560 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003561 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003562 type = VAR_BOOL;
3563 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003564 }
3565 else if (*end_leader == '-')
3566 {
3567#ifdef FEAT_FLOAT
3568 if (rettv->v_type == VAR_FLOAT)
3569 f = -f;
3570 else
3571#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003572 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003573 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003574 type = VAR_NUMBER;
3575 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003576 }
3577 }
3578#ifdef FEAT_FLOAT
3579 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003582 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003585#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003587 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003588 if (in_vim9script())
3589 rettv->v_type = type;
3590 else
3591 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003592 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003595 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return ret;
3597}
3598
3599/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003600 * Call the function referred to in "rettv".
3601 */
3602 static int
3603call_func_rettv(
3604 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003605 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003606 typval_T *rettv,
3607 int evaluate,
3608 dict_T *selfdict,
3609 typval_T *basetv)
3610{
3611 partial_T *pt = NULL;
3612 funcexe_T funcexe;
3613 typval_T functv;
3614 char_u *s;
3615 int ret;
3616
3617 // need to copy the funcref so that we can clear rettv
3618 if (evaluate)
3619 {
3620 functv = *rettv;
3621 rettv->v_type = VAR_UNKNOWN;
3622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003623 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003624 if (functv.v_type == VAR_PARTIAL)
3625 {
3626 pt = functv.vval.v_partial;
3627 s = partial_name(pt);
3628 }
3629 else
3630 s = functv.vval.v_string;
3631 }
3632 else
3633 s = (char_u *)"";
3634
Bram Moolenaara80faa82020-04-12 19:37:17 +02003635 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003636 funcexe.firstline = curwin->w_cursor.lnum;
3637 funcexe.lastline = curwin->w_cursor.lnum;
3638 funcexe.evaluate = evaluate;
3639 funcexe.partial = pt;
3640 funcexe.selfdict = selfdict;
3641 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003642 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003644 // Clear the funcref afterwards, so that deleting it while
3645 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003646 if (evaluate)
3647 clear_tv(&functv);
3648
3649 return ret;
3650}
3651
3652/*
3653 * Evaluate "->method()".
3654 * "*arg" points to the '-'.
3655 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3656 */
3657 static int
3658eval_lambda(
3659 char_u **arg,
3660 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003661 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003662 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003663{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003664 int evaluate = evalarg != NULL
3665 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003666 typval_T base = *rettv;
3667 int ret;
3668
3669 // Skip over the ->.
3670 *arg += 2;
3671 rettv->v_type = VAR_UNKNOWN;
3672
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003673 if (**arg == '{')
3674 {
3675 // ->{lambda}()
3676 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3677 }
3678 else
3679 {
3680 // ->(lambda)()
3681 ++*arg;
3682 ret = eval1(arg, rettv, evalarg);
3683 *arg = skipwhite_and_linebreak(*arg, evalarg);
3684 if (**arg != ')')
3685 {
3686 emsg(_(e_missing_close));
3687 ret = FAIL;
3688 }
3689 ++*arg;
3690 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003691 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003692 return FAIL;
3693 else if (**arg != '(')
3694 {
3695 if (verbose)
3696 {
3697 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003698 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003699 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003701 }
3702 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003703 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003704 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003705 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003706 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003707
3708 // Clear the funcref afterwards, so that deleting it while
3709 // evaluating the arguments is possible (see test55).
3710 if (evaluate)
3711 clear_tv(&base);
3712
3713 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003714}
3715
3716/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003717 * Evaluate "->method()".
3718 * "*arg" points to the '-'.
3719 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3720 */
3721 static int
3722eval_method(
3723 char_u **arg,
3724 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003725 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003726 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003727{
3728 char_u *name;
3729 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003730 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003731 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003732 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003733 int evaluate = evalarg != NULL
3734 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003735
3736 // Skip over the ->.
3737 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003738 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003739
Bram Moolenaarac92e252019-08-03 21:58:38 +02003740 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003741 len = get_name_len(arg, &alias, evaluate, TRUE);
3742 if (alias != NULL)
3743 name = alias;
3744
3745 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003746 {
3747 if (verbose)
3748 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003749 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003750 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003751 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003752 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003753 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003754 if (**arg != '(')
3755 {
3756 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003758 ret = FAIL;
3759 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003760 else if (VIM_ISWHITE((*arg)[-1]))
3761 {
3762 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003763 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003764 ret = FAIL;
3765 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003766 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003767 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003768 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003769 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003770
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003771 // Clear the funcref afterwards, so that deleting it while
3772 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003773 if (evaluate)
3774 clear_tv(&base);
3775
Bram Moolenaarac92e252019-08-03 21:58:38 +02003776 return ret;
3777}
3778
3779/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003780 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3781 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003782 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3783 */
3784 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003785eval_index(
3786 char_u **arg,
3787 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003788 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003790{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003791 int evaluate = evalarg != NULL
3792 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003793 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003795 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003796 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003797 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003798 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003799
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003800 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3801 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003802
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003803 init_tv(&var1);
3804 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003805 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003807 /*
3808 * dict.name
3809 */
3810 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003811 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003812 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003813 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003815 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003816 }
3817 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003819 /*
3820 * something[idx]
3821 *
3822 * Get the (first) variable from inside the [].
3823 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003824 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003825 if (**arg == ':')
3826 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003827 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003829 else if (vim9 && **arg == ':')
3830 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003831 semsg(_(e_white_space_required_before_and_after_str_at_str),
3832 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003833 clear_tv(&var1);
3834 return FAIL;
3835 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003836 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003838 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003839 clear_tv(&var1);
3840 return FAIL;
3841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003842
3843 /*
3844 * Get the second variable from inside the [:].
3845 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003846 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003847 if (**arg == ':')
3848 {
3849 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003850 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003851 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003852 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003853 semsg(_(e_white_space_required_before_and_after_str_at_str),
3854 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003855 if (!empty1)
3856 clear_tv(&var1);
3857 return FAIL;
3858 }
3859 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003860 if (**arg == ']')
3861 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003862 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 if (!empty1)
3865 clear_tv(&var1);
3866 return FAIL;
3867 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003868 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 if (!empty1)
3872 clear_tv(&var1);
3873 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003874 return FAIL;
3875 }
3876 }
3877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003879 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003880 if (**arg != ']')
3881 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003882 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003883 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003884 clear_tv(&var1);
3885 if (range)
3886 clear_tv(&var2);
3887 return FAIL;
3888 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003889 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003890 }
3891
3892 if (evaluate)
3893 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003894 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003895 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003896 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003897
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003898 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003900 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003901 clear_tv(&var2);
3902 return res;
3903 }
3904 return OK;
3905}
3906
3907/*
3908 * Check if "rettv" can have an [index] or [sli:ce]
3909 */
3910 int
3911check_can_index(typval_T *rettv, int evaluate, int verbose)
3912{
3913 switch (rettv->v_type)
3914 {
3915 case VAR_FUNC:
3916 case VAR_PARTIAL:
3917 if (verbose)
3918 emsg(_("E695: Cannot index a Funcref"));
3919 return FAIL;
3920 case VAR_FLOAT:
3921#ifdef FEAT_FLOAT
3922 if (verbose)
3923 emsg(_(e_float_as_string));
3924 return FAIL;
3925#endif
3926 case VAR_BOOL:
3927 case VAR_SPECIAL:
3928 case VAR_JOB:
3929 case VAR_CHANNEL:
3930 if (verbose)
3931 emsg(_(e_cannot_index_special_variable));
3932 return FAIL;
3933 case VAR_UNKNOWN:
3934 case VAR_ANY:
3935 case VAR_VOID:
3936 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003937 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003938 emsg(_(e_cannot_index_special_variable));
3939 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003941 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003942
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003943 case VAR_STRING:
3944 case VAR_LIST:
3945 case VAR_DICT:
3946 case VAR_BLOB:
3947 break;
3948 case VAR_NUMBER:
3949 if (in_vim9script())
3950 emsg(_(e_cannot_index_number));
3951 break;
3952 }
3953 return OK;
3954}
3955
3956/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003957 * slice() function
3958 */
3959 void
3960f_slice(typval_T *argvars, typval_T *rettv)
3961{
3962 if (check_can_index(argvars, TRUE, FALSE) == OK)
3963 {
3964 copy_tv(argvars, rettv);
3965 eval_index_inner(rettv, TRUE, argvars + 1,
3966 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
3967 TRUE, NULL, 0, FALSE);
3968 }
3969}
3970
3971/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003972 * Apply index or range to "rettv".
3973 * "var1" is the first index, NULL for [:expr].
3974 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01003975 * "exclusive" is TRUE for slice(): second index is exclusive, use character
3976 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003977 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3978 */
3979 int
3980eval_index_inner(
3981 typval_T *rettv,
3982 int is_range,
3983 typval_T *var1,
3984 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003985 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003986 char_u *key,
3987 int keylen,
3988 int verbose)
3989{
Bram Moolenaar6601b622021-01-13 21:47:15 +01003990 varnumber_T n1, n2 = 0;
3991 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003992
3993 n1 = 0;
3994 if (var1 != NULL && rettv->v_type != VAR_DICT)
3995 n1 = tv_get_number(var1);
3996
3997 if (is_range)
3998 {
3999 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004001 if (verbose)
4002 emsg(_(e_cannot_slice_dictionary));
4003 return FAIL;
4004 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004005 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004006 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004007 else
4008 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004009 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004010
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004011 switch (rettv->v_type)
4012 {
4013 case VAR_UNKNOWN:
4014 case VAR_ANY:
4015 case VAR_VOID:
4016 case VAR_FUNC:
4017 case VAR_PARTIAL:
4018 case VAR_FLOAT:
4019 case VAR_BOOL:
4020 case VAR_SPECIAL:
4021 case VAR_JOB:
4022 case VAR_CHANNEL:
4023 break; // not evaluating, skipping over subscript
4024
4025 case VAR_NUMBER:
4026 case VAR_STRING:
4027 {
4028 char_u *s = tv_get_string(rettv);
4029
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004030 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004031 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004032 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004033 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004034 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004035 else
4036 s = char_from_string(s, n1);
4037 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004038 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // The resulting variable is a substring. If the indexes
4041 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042 if (n1 < 0)
4043 {
4044 n1 = len + n1;
4045 if (n1 < 0)
4046 n1 = 0;
4047 }
4048 if (n2 < 0)
4049 n2 = len + n2;
4050 else if (n2 >= len)
4051 n2 = len;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004052 if (exclusive)
4053 --n2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004054 if (n1 >= len || n2 < 0 || n1 > n2)
4055 s = NULL;
4056 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004057 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004058 }
4059 else
4060 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004061 // The resulting variable is a string of a single
4062 // character. If the index is too big or negative the
4063 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004064 if (n1 >= len || n1 < 0)
4065 s = NULL;
4066 else
4067 s = vim_strnsave(s + n1, 1);
4068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 clear_tv(rettv);
4070 rettv->v_type = VAR_STRING;
4071 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004072 }
4073 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004075 case VAR_BLOB:
4076 len = blob_len(rettv->vval.v_blob);
4077 if (is_range)
4078 {
4079 // The resulting variable is a sub-blob. If the indexes
4080 // are out of range the result is empty.
4081 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004082 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004083 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004084 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004085 n1 = 0;
4086 }
4087 if (n2 < 0)
4088 n2 = len + n2;
4089 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004090 n2 = len - (exclusive ? 0 : 1);
4091 if (exclusive)
4092 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 if (n1 >= len || n2 < 0 || n1 > n2)
4094 {
4095 clear_tv(rettv);
4096 rettv->v_type = VAR_BLOB;
4097 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004098 }
4099 else
4100 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 blob_T *blob = blob_alloc();
4102 long i;
4103
4104 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004105 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004107 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004108 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004109 return FAIL;
4110 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 blob->bv_ga.ga_len = n2 - n1 + 1;
4112 for (i = n1; i <= n2; i++)
4113 blob_set(blob, i - n1,
4114 blob_get(rettv->vval.v_blob, i));
4115
4116 clear_tv(rettv);
4117 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004119 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004120 }
4121 else
4122 {
4123 // The resulting variable is a byte value.
4124 // If the index is too big or negative that is an error.
4125 if (n1 < 0)
4126 n1 = len + n1;
4127 if (n1 < len && n1 >= 0)
4128 {
4129 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004130
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004131 clear_tv(rettv);
4132 rettv->v_type = VAR_NUMBER;
4133 rettv->vval.v_number = v;
4134 }
4135 else
4136 semsg(_(e_blobidx), n1);
4137 }
4138 break;
4139
4140 case VAR_LIST:
4141 if (var1 == NULL)
4142 n1 = 0;
4143 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004144 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004145 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004146 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004147 return FAIL;
4148 break;
4149
4150 case VAR_DICT:
4151 {
4152 dictitem_T *item;
4153 typval_T tmp;
4154
4155 if (key == NULL)
4156 {
4157 key = tv_get_string_chk(var1);
4158 if (key == NULL)
4159 return FAIL;
4160 }
4161
4162 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4163
4164 if (item == NULL && verbose)
4165 semsg(_(e_dictkey), key);
4166 if (item == NULL)
4167 return FAIL;
4168
4169 copy_tv(&item->di_tv, &tmp);
4170 clear_tv(rettv);
4171 *rettv = tmp;
4172 }
4173 break;
4174 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 return OK;
4176}
4177
4178/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004179 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004180 */
4181 char_u *
4182partial_name(partial_T *pt)
4183{
4184 if (pt->pt_name != NULL)
4185 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004186 if (pt->pt_func != NULL)
4187 return pt->pt_func->uf_name;
4188 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004189}
4190
Bram Moolenaarddecc252016-04-06 22:59:37 +02004191 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004192partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004193{
4194 int i;
4195
4196 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004197 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004198 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004199 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004200 if (pt->pt_name != NULL)
4201 {
4202 func_unref(pt->pt_name);
4203 vim_free(pt->pt_name);
4204 }
4205 else
4206 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004207
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004208 // Decrease the reference count for the context of a closure. If down
4209 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004210 if (pt->pt_funcstack != NULL)
4211 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004212 --pt->pt_funcstack->fs_refcount;
4213 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004214 }
4215
Bram Moolenaarddecc252016-04-06 22:59:37 +02004216 vim_free(pt);
4217}
4218
4219/*
4220 * Unreference a closure: decrement the reference count and free it when it
4221 * becomes zero.
4222 */
4223 void
4224partial_unref(partial_T *pt)
4225{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004226 if (pt != NULL)
4227 {
4228 if (--pt->pt_refcount <= 0)
4229 partial_free(pt);
4230
4231 // If the reference count goes down to one, the funcstack may be the
4232 // only reference and can be freed if no other partials reference it.
4233 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4234 funcstack_check_refcount(pt->pt_funcstack);
4235 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004236}
4237
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004238/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004239 * Return the next (unique) copy ID.
4240 * Used for serializing nested structures.
4241 */
4242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004244{
4245 current_copyID += COPYID_INC;
4246 return current_copyID;
4247}
4248
4249/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004250 * Garbage collection for lists and dictionaries.
4251 *
4252 * We use reference counts to be able to free most items right away when they
4253 * are no longer used. But for composite items it's possible that it becomes
4254 * unused while the reference count is > 0: When there is a recursive
4255 * reference. Example:
4256 * :let l = [1, 2, 3]
4257 * :let d = {9: l}
4258 * :let l[1] = d
4259 *
4260 * Since this is quite unusual we handle this with garbage collection: every
4261 * once in a while find out which lists and dicts are not referenced from any
4262 * variable.
4263 *
4264 * Here is a good reference text about garbage collection (refers to Python
4265 * but it applies to all reference-counting mechanisms):
4266 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004267 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004268
4269/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004270 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004271 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004272 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004273 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004274 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004275garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004276{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004277 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004279 buf_T *buf;
4280 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004281 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004283
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004284 if (!testing)
4285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004287 want_garbage_collect = FALSE;
4288 may_garbage_collect = FALSE;
4289 garbage_collect_at_exit = FALSE;
4290 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004291
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004292 // The execution stack can grow big, limit the size.
4293 if (exestack.ga_maxlen - exestack.ga_len > 500)
4294 {
4295 size_t new_len;
4296 char_u *pp;
4297 int n;
4298
4299 // Keep 150% of the current size, with a minimum of the growth size.
4300 n = exestack.ga_len / 2;
4301 if (n < exestack.ga_growsize)
4302 n = exestack.ga_growsize;
4303
4304 // Don't make it bigger though.
4305 if (exestack.ga_len + n < exestack.ga_maxlen)
4306 {
4307 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4308 pp = vim_realloc(exestack.ga_data, new_len);
4309 if (pp == NULL)
4310 return FAIL;
4311 exestack.ga_maxlen = exestack.ga_len + n;
4312 exestack.ga_data = pp;
4313 }
4314 }
4315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004316 // We advance by two because we add one for items referenced through
4317 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004318 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004319
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004320 /*
4321 * 1. Go through all accessible variables and mark all lists and dicts
4322 * with copyID.
4323 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004325 // Don't free variables in the previous_funccal list unless they are only
4326 // referenced through previous_funccal. This must be first, because if
4327 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004330 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004331 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004334 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004335 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4336 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004339 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004340 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4341 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004342 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4344 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004345#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004346 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004347 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4348 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004349 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004350 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004351 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4352 NULL, NULL);
4353#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004354
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004355 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004356 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4358 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004359 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004360 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004362 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004366 abort = abort || set_ref_in_functions(copyID);
4367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004371 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004372 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004373
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004374 // callbacks in buffers
4375 abort = abort || set_ref_in_buffers(copyID);
4376
Bram Moolenaar1dced572012-04-05 16:54:08 +02004377#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004378 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004379#endif
4380
Bram Moolenaardb913952012-06-29 12:54:53 +02004381#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004383#endif
4384
4385#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004386 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004387#endif
4388
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004389#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004390 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004391 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004392#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004393#ifdef FEAT_NETBEANS_INTG
4394 abort = abort || set_ref_in_nb_channel(copyID);
4395#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004396
Bram Moolenaare3188e22016-05-31 21:13:04 +02004397#ifdef FEAT_TIMERS
4398 abort = abort || set_ref_in_timer(copyID);
4399#endif
4400
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004401#ifdef FEAT_QUICKFIX
4402 abort = abort || set_ref_in_quickfix(copyID);
4403#endif
4404
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004405#ifdef FEAT_TERMINAL
4406 abort = abort || set_ref_in_term(copyID);
4407#endif
4408
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004409#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004410 abort = abort || set_ref_in_popups(copyID);
4411#endif
4412
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004413 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004414 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004415 /*
4416 * 2. Free lists and dictionaries that are not referenced.
4417 */
4418 did_free = free_unref_items(copyID);
4419
4420 /*
4421 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004423 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004425 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 else if (p_verbose > 0)
4427 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004428 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004429 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004430
4431 return did_free;
4432}
4433
4434/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004435 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004436 */
4437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004438free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004439{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004440 int did_free = FALSE;
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // Let all "free" functions know that we are here. This means no
4443 // dictionaries, lists, channels or jobs are to be freed, because we will
4444 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004445 in_free_unref_items = TRUE;
4446
4447 /*
4448 * PASS 1: free the contents of the items. We don't free the items
4449 * themselves yet, so that it is possible to decrement refcount counters
4450 */
4451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004453 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004456 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004457
4458#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // Go through the list of jobs and free items without the copyID. This
4460 // must happen before doing channels, because jobs refer to channels, but
4461 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004462 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004465 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4466#endif
4467
4468 /*
4469 * PASS 2: free the items themselves.
4470 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004471 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004472 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004473
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004474#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // Go through the list of jobs and free items without the copyID. This
4476 // must happen before doing channels, because jobs refer to channels, but
4477 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 free_unused_jobs(copyID, COPYID_MASK);
4479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004481 free_unused_channels(copyID, COPYID_MASK);
4482#endif
4483
4484 in_free_unref_items = FALSE;
4485
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004486 return did_free;
4487}
4488
4489/*
4490 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004491 * "list_stack" is used to add lists to be marked. Can be NULL.
4492 *
4493 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004494 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004496set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004497{
4498 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004499 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004500 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004501 hashtab_T *cur_ht;
4502 ht_stack_T *ht_stack = NULL;
4503 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004504
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004505 cur_ht = ht;
4506 for (;;)
4507 {
4508 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 // Mark each item in the hashtab. If the item contains a hashtab
4511 // it is added to ht_stack, if it contains a list it is added to
4512 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004513 todo = (int)cur_ht->ht_used;
4514 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4515 if (!HASHITEM_EMPTY(hi))
4516 {
4517 --todo;
4518 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4519 &ht_stack, list_stack);
4520 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004521 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522
4523 if (ht_stack == NULL)
4524 break;
4525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 cur_ht = ht_stack->ht;
4528 tempitem = ht_stack;
4529 ht_stack = ht_stack->prev;
4530 free(tempitem);
4531 }
4532
4533 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534}
4535
4536/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004537 * Mark a dict and its items with "copyID".
4538 * Returns TRUE if setting references failed somehow.
4539 */
4540 int
4541set_ref_in_dict(dict_T *d, int copyID)
4542{
4543 if (d != NULL && d->dv_copyID != copyID)
4544 {
4545 d->dv_copyID = copyID;
4546 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4547 }
4548 return FALSE;
4549}
4550
4551/*
4552 * Mark a list and its items with "copyID".
4553 * Returns TRUE if setting references failed somehow.
4554 */
4555 int
4556set_ref_in_list(list_T *ll, int copyID)
4557{
4558 if (ll != NULL && ll->lv_copyID != copyID)
4559 {
4560 ll->lv_copyID = copyID;
4561 return set_ref_in_list_items(ll, copyID, NULL);
4562 }
4563 return FALSE;
4564}
4565
4566/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004567 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004568 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4569 *
4570 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004571 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004573set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004574{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004575 listitem_T *li;
4576 int abort = FALSE;
4577 list_T *cur_l;
4578 list_stack_T *list_stack = NULL;
4579 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004581 cur_l = l;
4582 for (;;)
4583 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004584 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // Mark each item in the list. If the item contains a hashtab
4586 // it is added to ht_stack, if it contains a list it is added to
4587 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4589 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4590 ht_stack, &list_stack);
4591 if (list_stack == NULL)
4592 break;
4593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004595 cur_l = list_stack->list;
4596 tempitem = list_stack;
4597 list_stack = list_stack->prev;
4598 free(tempitem);
4599 }
4600
4601 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004602}
4603
4604/*
4605 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004606 * "list_stack" is used to add lists to be marked. Can be NULL.
4607 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4608 *
4609 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004610 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004612set_ref_in_item(
4613 typval_T *tv,
4614 int copyID,
4615 ht_stack_T **ht_stack,
4616 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004619
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004620 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004621 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004622 dict_T *dd = tv->vval.v_dict;
4623
Bram Moolenaara03f2332016-02-06 18:09:59 +01004624 if (dd != NULL && dd->dv_copyID != copyID)
4625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004626 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004627 dd->dv_copyID = copyID;
4628 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004629 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004630 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4631 }
4632 else
4633 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004634 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4635
Bram Moolenaara03f2332016-02-06 18:09:59 +01004636 if (newitem == NULL)
4637 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004638 else
4639 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004640 newitem->ht = &dd->dv_hashtab;
4641 newitem->prev = *ht_stack;
4642 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004643 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004644 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004645 }
4646 }
4647 else if (tv->v_type == VAR_LIST)
4648 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004649 list_T *ll = tv->vval.v_list;
4650
Bram Moolenaara03f2332016-02-06 18:09:59 +01004651 if (ll != NULL && ll->lv_copyID != copyID)
4652 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004653 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004654 ll->lv_copyID = copyID;
4655 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004656 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004657 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004658 }
4659 else
4660 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004661 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4662
Bram Moolenaara03f2332016-02-06 18:09:59 +01004663 if (newitem == NULL)
4664 abort = TRUE;
4665 else
4666 {
4667 newitem->list = ll;
4668 newitem->prev = *list_stack;
4669 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004671 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004672 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004673 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004674 else if (tv->v_type == VAR_FUNC)
4675 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004676 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004677 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004678 else if (tv->v_type == VAR_PARTIAL)
4679 {
4680 partial_T *pt = tv->vval.v_partial;
4681 int i;
4682
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004683 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004684 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004685 // Didn't see this partial yet.
4686 pt->pt_copyID = copyID;
4687
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004688 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004689
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004690 if (pt->pt_dict != NULL)
4691 {
4692 typval_T dtv;
4693
4694 dtv.v_type = VAR_DICT;
4695 dtv.vval.v_dict = pt->pt_dict;
4696 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4697 }
4698
4699 for (i = 0; i < pt->pt_argc; ++i)
4700 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4701 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004702 if (pt->pt_funcstack != NULL)
4703 {
4704 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4705
4706 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4707 abort = abort || set_ref_in_item(stack + i, copyID,
4708 ht_stack, list_stack);
4709 }
4710
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004711 }
4712 }
4713#ifdef FEAT_JOB_CHANNEL
4714 else if (tv->v_type == VAR_JOB)
4715 {
4716 job_T *job = tv->vval.v_job;
4717 typval_T dtv;
4718
4719 if (job != NULL && job->jv_copyID != copyID)
4720 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004721 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004722 if (job->jv_channel != NULL)
4723 {
4724 dtv.v_type = VAR_CHANNEL;
4725 dtv.vval.v_channel = job->jv_channel;
4726 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4727 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004728 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004729 {
4730 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004731 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4733 }
4734 }
4735 }
4736 else if (tv->v_type == VAR_CHANNEL)
4737 {
4738 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004739 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 typval_T dtv;
4741 jsonq_T *jq;
4742 cbq_T *cq;
4743
4744 if (ch != NULL && ch->ch_copyID != copyID)
4745 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004746 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004747 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004748 {
4749 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4750 jq = jq->jq_next)
4751 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4752 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4753 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004754 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004755 {
4756 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004757 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004758 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4759 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004760 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004761 {
4762 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004763 dtv.vval.v_partial =
4764 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004765 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4766 }
4767 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004768 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004769 {
4770 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004771 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004772 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4773 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004774 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 {
4776 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004777 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4779 }
4780 }
4781 }
4782#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004783 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004784}
4785
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 * Return a string with the string representation of a variable.
4788 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004789 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004790 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004791 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004792 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004793 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004794 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4795 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004796 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004798 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004799echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004800 typval_T *tv,
4801 char_u **tofree,
4802 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004803 int copyID,
4804 int echo_style,
4805 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004806 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004808 static int recurse = 0;
4809 char_u *r = NULL;
4810
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004812 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004813 if (!did_echo_string_emsg)
4814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // Only give this message once for a recursive call to avoid
4816 // flooding the user with errors. And stop iterating over lists
4817 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004818 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004820 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004821 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004822 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004823 }
4824 ++recurse;
4825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 switch (tv->v_type)
4827 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004828 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004829 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004830 {
4831 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004832 r = tv->vval.v_string;
4833 if (r == NULL)
4834 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004835 }
4836 else
4837 {
4838 *tofree = string_quote(tv->vval.v_string, FALSE);
4839 r = *tofree;
4840 }
4841 break;
4842
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004844 if (echo_style)
4845 {
4846 *tofree = NULL;
4847 r = tv->vval.v_string;
4848 }
4849 else
4850 {
4851 *tofree = string_quote(tv->vval.v_string, TRUE);
4852 r = *tofree;
4853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004855
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004856 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004857 {
4858 partial_T *pt = tv->vval.v_partial;
4859 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004860 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004861 garray_T ga;
4862 int i;
4863 char_u *tf;
4864
4865 ga_init2(&ga, 1, 100);
4866 ga_concat(&ga, (char_u *)"function(");
4867 if (fname != NULL)
4868 {
4869 ga_concat(&ga, fname);
4870 vim_free(fname);
4871 }
4872 if (pt != NULL && pt->pt_argc > 0)
4873 {
4874 ga_concat(&ga, (char_u *)", [");
4875 for (i = 0; i < pt->pt_argc; ++i)
4876 {
4877 if (i > 0)
4878 ga_concat(&ga, (char_u *)", ");
4879 ga_concat(&ga,
4880 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4881 vim_free(tf);
4882 }
4883 ga_concat(&ga, (char_u *)"]");
4884 }
4885 if (pt != NULL && pt->pt_dict != NULL)
4886 {
4887 typval_T dtv;
4888
4889 ga_concat(&ga, (char_u *)", ");
4890 dtv.v_type = VAR_DICT;
4891 dtv.vval.v_dict = pt->pt_dict;
4892 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4893 vim_free(tf);
4894 }
4895 ga_concat(&ga, (char_u *)")");
4896
4897 *tofree = ga.ga_data;
4898 r = *tofree;
4899 break;
4900 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004901
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004902 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004903 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004904 break;
4905
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004907 if (tv->vval.v_list == NULL)
4908 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004909 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004910 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004911 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004912 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004913 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4914 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004915 {
4916 *tofree = NULL;
4917 r = (char_u *)"[...]";
4918 }
4919 else
4920 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004921 int old_copyID = tv->vval.v_list->lv_copyID;
4922
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004923 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004924 *tofree = list2string(tv, copyID, restore_copyID);
4925 if (restore_copyID)
4926 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004927 r = *tofree;
4928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004930
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004932 if (tv->vval.v_dict == NULL)
4933 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004934 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004935 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004936 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004937 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004938 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4939 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004940 {
4941 *tofree = NULL;
4942 r = (char_u *)"{...}";
4943 }
4944 else
4945 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004946 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004947
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004948 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004949 *tofree = dict2string(tv, copyID, restore_copyID);
4950 if (restore_copyID)
4951 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 r = *tofree;
4953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004954 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004957 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004958 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004960 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004961 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004962 break;
4963
Bram Moolenaar835dc632016-02-07 14:27:38 +01004964 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004965 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004966 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004967 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004968 if (composite_val)
4969 {
4970 *tofree = string_quote(r, FALSE);
4971 r = *tofree;
4972 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004976#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 *tofree = NULL;
4978 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4979 r = numbuf;
4980 break;
4981#endif
4982
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004983 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004984 case VAR_SPECIAL:
4985 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004986 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004987 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004988 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004989
Bram Moolenaar8502c702014-06-17 12:51:16 +02004990 if (--recurse == 0)
4991 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004993}
4994
4995/*
4996 * Return a string with the string representation of a variable.
4997 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4998 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004999 * Does not put quotes around strings, as ":echo" displays values.
5000 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5001 * May return NULL.
5002 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005004echo_string(
5005 typval_T *tv,
5006 char_u **tofree,
5007 char_u *numbuf,
5008 int copyID)
5009{
5010 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5011}
5012
5013/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005014 * Return string "str" in ' quotes, doubling ' characters.
5015 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005018 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005019string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005020{
Bram Moolenaar33570922005-01-25 22:26:29 +00005021 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005022 char_u *p, *r, *s;
5023
Bram Moolenaar33570922005-01-25 22:26:29 +00005024 len = (function ? 13 : 3);
5025 if (str != NULL)
5026 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005027 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005028 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005029 if (*p == '\'')
5030 ++len;
5031 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 s = r = alloc(len);
5033 if (r != NULL)
5034 {
5035 if (function)
5036 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038 r += 10;
5039 }
5040 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 if (str != NULL)
5043 for (p = str; *p != NUL; )
5044 {
5045 if (*p == '\'')
5046 *r++ = '\'';
5047 MB_COPY_CHAR(p, r);
5048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005050 if (function)
5051 *r++ = ')';
5052 *r++ = NUL;
5053 }
5054 return s;
5055}
5056
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005057#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058/*
5059 * Convert the string "text" to a floating point number.
5060 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5061 * this always uses a decimal point.
5062 * Returns the length of the text that was consumed.
5063 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005065string2float(
5066 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005067 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068{
5069 char *s = (char *)text;
5070 float_T f;
5071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005073 if (STRNICMP(text, "inf", 3) == 0)
5074 {
5075 *value = INFINITY;
5076 return 3;
5077 }
5078 if (STRNICMP(text, "-inf", 3) == 0)
5079 {
5080 *value = -INFINITY;
5081 return 4;
5082 }
5083 if (STRNICMP(text, "nan", 3) == 0)
5084 {
5085 *value = NAN;
5086 return 3;
5087 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005088 f = strtod(s, &s);
5089 *value = f;
5090 return (int)((char_u *)s - text);
5091}
5092#endif
5093
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005095 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5096 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005097 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005098 */
5099 int
5100buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5101{
5102 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005103 char_u *t;
5104 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005105
5106 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5107 return -1;
5108
5109 if (lnum > buf->b_ml.ml_line_count)
5110 lnum = buf->b_ml.ml_line_count;
5111
5112 str = ml_get_buf(buf, lnum, FALSE);
5113 if (str == NULL)
5114 return -1;
5115
5116 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005117 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005118
Bram Moolenaar91458462021-01-13 20:08:38 +01005119 // count the number of characters
5120 t = str;
5121 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5122 t += mb_ptr2len(t);
5123
5124 // In insert mode, when the cursor is at the end of a non-empty line,
5125 // byteidx points to the NUL character immediately past the end of the
5126 // string. In this case, add one to the character count.
5127 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5128 count++;
5129
5130 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005131}
5132
5133/*
5134 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005135 * byte index. Works only for loaded buffers. Returns -1 on failure.
5136 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005137 */
5138 int
5139buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5140{
5141 char_u *str;
5142 char_u *t;
5143
5144 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5145 return -1;
5146
5147 if (lnum > buf->b_ml.ml_line_count)
5148 lnum = buf->b_ml.ml_line_count;
5149
5150 str = ml_get_buf(buf, lnum, FALSE);
5151 if (str == NULL)
5152 return -1;
5153
5154 // Convert the character offset to a byte offset
5155 t = str;
5156 while (*t != NUL && --charidx > 0)
5157 t += mb_ptr2len(t);
5158
Bram Moolenaar91458462021-01-13 20:08:38 +01005159 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005160}
5161
5162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005164 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005167var2fpos(
5168 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005170 int *fnum, // set to fnum for '0, 'A, etc.
5171 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005173 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005175 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005177 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005178 if (varp->v_type == VAR_LIST)
5179 {
5180 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005181 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005182 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005183 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005184
5185 l = varp->vval.v_list;
5186 if (l == NULL)
5187 return NULL;
5188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005189 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005190 pos.lnum = list_find_nr(l, 0L, &error);
5191 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005192 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005193 if (charcol)
5194 len = (long)mb_charlen(ml_get(pos.lnum));
5195 else
5196 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005197
Bram Moolenaarec65d772020-08-20 22:29:12 +02005198 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005200 li = list_find(l, 1L);
5201 if (li != NULL && li->li_tv.v_type == VAR_STRING
5202 && li->li_tv.vval.v_string != NULL
5203 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005204 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005205 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005206 }
5207 else
5208 {
5209 pos.col = list_find_nr(l, 1L, &error);
5210 if (error)
5211 return NULL;
5212 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005213
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005214 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005215 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005217 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005220 pos.coladd = list_find_nr(l, 2L, &error);
5221 if (error)
5222 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005223
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005224 return &pos;
5225 }
5226
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005227 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (name == NULL)
5229 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005231 {
5232 pos = curwin->w_cursor;
5233 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005234 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005235 return &pos;
5236 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005238 {
5239 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005240 pos = VIsual;
5241 else
5242 pos = curwin->w_cursor;
5243 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005244 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005245 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005246 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005249 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5251 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005252 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005253 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return pp;
5255 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005256
Bram Moolenaara5525202006-03-02 22:52:09 +00005257 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005258
Bram Moolenaar477933c2007-07-17 14:32:23 +00005259 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005260 {
5261 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005263 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005264 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 // In silent Ex mode topline is zero, but that's not a valid line
5266 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005267 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005268 return &pos;
5269 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005270 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005271 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005274 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005275 return &pos;
5276 }
5277 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005280 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 pos.lnum = curbuf->b_ml.ml_line_count;
5283 pos.col = 0;
5284 }
5285 else
5286 {
5287 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288 if (charcol)
5289 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5290 else
5291 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
5293 return &pos;
5294 }
5295 return NULL;
5296}
5297
5298/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005299 * Convert list in "arg" into a position and optional file number.
5300 * When "fnump" is NULL there is no file number, only 3 items.
5301 * Note that the column is passed on as-is, the caller may want to decrement
5302 * it to use 1 for the first column.
5303 * Return FAIL when conversion is not possible, doesn't check the position for
5304 * validity.
5305 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005307list2fpos(
5308 typval_T *arg,
5309 pos_T *posp,
5310 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005311 colnr_T *curswantp,
5312 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005313{
5314 list_T *l = arg->vval.v_list;
5315 long i = 0;
5316 long n;
5317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5319 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005320 if (arg->v_type != VAR_LIST
5321 || l == NULL
5322 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005323 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005324 return FAIL;
5325
5326 if (fnump != NULL)
5327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005329 if (n < 0)
5330 return FAIL;
5331 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005333 *fnump = n;
5334 }
5335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005337 if (n < 0)
5338 return FAIL;
5339 posp->lnum = n;
5340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005342 if (n < 0)
5343 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005344 // If character position is specified, then convert to byte position
5345 if (charcol)
5346 {
5347 buf_T *buf;
5348
5349 // Get the text for the specified line in a loaded buffer
5350 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5351 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5352 return FAIL;
5353
Bram Moolenaar91458462021-01-13 20:08:38 +01005354 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005355 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005356 posp->col = n;
5357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005358 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005359 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005360 posp->coladd = 0;
5361 else
5362 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005363
Bram Moolenaar493c1782014-05-28 14:34:46 +02005364 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005366
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367 return OK;
5368}
5369
5370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * Get the length of an environment variable name.
5372 * Advance "arg" to the first character after the name.
5373 * Return 0 for error.
5374 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005376get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 char_u *p;
5379 int len;
5380
5381 for (p = *arg; vim_isIDc(*p); ++p)
5382 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 return 0;
5385
5386 len = (int)(p - *arg);
5387 *arg = p;
5388 return len;
5389}
5390
5391/*
5392 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005393 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 * Return 0 if something is wrong.
5395 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005397get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 char_u *p;
5400 int len;
5401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005404 {
5405 if (*p == ':')
5406 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 // "s:" is start of "s:var", but "n:" is not and can be used in
5408 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005409 len = (int)(p - *arg);
5410 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5411 || len > 1)
5412 break;
5413 }
5414 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005415 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return 0;
5417
5418 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005419 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
5421 return len;
5422}
5423
5424/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005425 * Get the length of the name of a variable or function.
5426 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005428 * Return -1 if curly braces expansion failed.
5429 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 * If the name contains 'magic' {}'s, expand them and return the
5431 * expanded name in an allocated string via 'alias' - caller must free.
5432 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434get_name_len(
5435 char_u **arg,
5436 char_u **alias,
5437 int evaluate,
5438 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 char_u *p;
5442 char_u *expr_start;
5443 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
5447 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5448 && (*arg)[2] == (int)KE_SNR)
5449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 *arg += 3;
5452 return get_id_len(arg) + 3;
5453 }
5454 len = eval_fname_script(*arg);
5455 if (len > 0)
5456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 *arg += len;
5459 }
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005464 p = find_name_end(*arg, &expr_start, &expr_end,
5465 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (expr_start != NULL)
5467 {
5468 char_u *temp_string;
5469
5470 if (!evaluate)
5471 {
5472 len += (int)(p - *arg);
5473 *arg = skipwhite(p);
5474 return len;
5475 }
5476
5477 /*
5478 * Include any <SID> etc in the expanded string:
5479 * Thus the -len here.
5480 */
5481 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5482 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005483 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 *alias = temp_string;
5485 *arg = skipwhite(p);
5486 return (int)STRLEN(temp_string);
5487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005490 // Only give an error when there is something, otherwise it will be
5491 // reported at a higher level.
5492 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005493 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 return len;
5496}
5497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498/*
5499 * Find the end of a variable or function name, taking care of magic braces.
5500 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5501 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005502 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 * Return a pointer to just after the name. Equal to "arg" if there is no
5504 * valid name.
5505 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005506 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507find_name_end(
5508 char_u *arg,
5509 char_u **expr_start,
5510 char_u **expr_end,
5511 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 int mb_nest = 0;
5514 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005516 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005517 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 *expr_start = NULL;
5522 *expr_end = NULL;
5523 }
5524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005526 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5527 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005528 return arg;
5529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 for (p = arg; *p != NUL
5531 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005532 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005533 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005534 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005536 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005538 if (*p == '\'')
5539 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005541 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005542 ;
5543 if (*p == NUL)
5544 break;
5545 }
5546 else if (*p == '"')
5547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005549 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005550 if (*p == '\\' && p[1] != NUL)
5551 ++p;
5552 if (*p == NUL)
5553 break;
5554 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005555 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5556 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // "s:" is start of "s:var", but "n:" is not and can be used in
5558 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005559 len = (int)(p - arg);
5560 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005561 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005562 break;
5563 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 if (*p == '[')
5568 ++br_nest;
5569 else if (*p == ']')
5570 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005572
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005573 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 if (*p == '{')
5576 {
5577 mb_nest++;
5578 if (expr_start != NULL && *expr_start == NULL)
5579 *expr_start = p;
5580 }
5581 else if (*p == '}')
5582 {
5583 mb_nest--;
5584 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5585 *expr_end = p;
5586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589
5590 return p;
5591}
5592
5593/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 * Expands out the 'magic' {}'s in a variable/function name.
5595 * Note that this can call itself recursively, to deal with
5596 * constructs like foo{bar}{baz}{bam}
5597 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5598 * "in_start" ^
5599 * "expr_start" ^
5600 * "expr_end" ^
5601 * "in_end" ^
5602 *
5603 * Returns a new allocated string, which the caller must free.
5604 * Returns NULL for failure.
5605 */
5606 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005607make_expanded_name(
5608 char_u *in_start,
5609 char_u *expr_start,
5610 char_u *expr_end,
5611 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005612{
5613 char_u c1;
5614 char_u *retval = NULL;
5615 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005616
5617 if (expr_end == NULL || in_end == NULL)
5618 return NULL;
5619 *expr_start = NUL;
5620 *expr_end = NUL;
5621 c1 = *in_end;
5622 *in_end = NUL;
5623
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005624 temp_result = eval_to_string(expr_start + 1, FALSE);
5625 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005626 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005627 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5628 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629 if (retval != NULL)
5630 {
5631 STRCPY(retval, in_start);
5632 STRCAT(retval, temp_result);
5633 STRCAT(retval, expr_end + 1);
5634 }
5635 }
5636 vim_free(temp_result);
5637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005638 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005639 *expr_start = '{';
5640 *expr_end = '}';
5641
5642 if (retval != NULL)
5643 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005644 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005645 if (expr_start != NULL)
5646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005648 temp_result = make_expanded_name(retval, expr_start,
5649 expr_end, temp_result);
5650 vim_free(retval);
5651 retval = temp_result;
5652 }
5653 }
5654
5655 return retval;
5656}
5657
5658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005660 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005663eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005665 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005666}
5667
5668/*
5669 * Return TRUE if character "c" can be used as the first character in a
5670 * variable or function name (excluding '{' and '}').
5671 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005673eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005674{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005675 return ASCII_ISALPHA(c) || c == '_';
5676}
5677
5678/*
5679 * Return TRUE if character "c" can be used as the first character of a
5680 * dictionary key.
5681 */
5682 int
5683eval_isdictc(int c)
5684{
5685 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686}
5687
5688/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005689 * Handle:
5690 * - expr[expr], expr[expr:expr] subscript
5691 * - ".name" lookup
5692 * - function call with Funcref variable: func(expr)
5693 * - method call: var->method()
5694 *
5695 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005696 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005698handle_subscript(
5699 char_u **arg,
5700 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005701 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005702 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005703{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005704 int evaluate = evalarg != NULL
5705 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 int ret = OK;
5707 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005708 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005709 int getnext;
5710 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005711
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005712 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005713 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005714 // When at the end of the line and ".name" or "->{" or "->X" follows in
5715 // the next line then consume the line break.
5716 p = eval_next_non_blank(*arg, evalarg, &getnext);
5717 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005718 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005719 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005720 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005721 {
5722 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005723 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005724 check_white = FALSE;
5725 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005727 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5728 || rettv->v_type == VAR_PARTIAL))
5729 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005731 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5732 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005733
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005734 // Stop the expression evaluation when immediately aborting on
5735 // error, or when an interrupt occurred or an exception was thrown
5736 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005737 if (aborting())
5738 {
5739 if (ret == OK)
5740 clear_tv(rettv);
5741 ret = FAIL;
5742 }
5743 dict_unref(selfdict);
5744 selfdict = NULL;
5745 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005746 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005747 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005748 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005749 if (ret == OK)
5750 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005751 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5752 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005753 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005754 else
5755 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005756 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005757 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005758 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005759 // "." is ".name" lookup when we found a dict or when evaluating and
5760 // scriptversion is at least 2, where string concatenation is "..".
5761 else if (**arg == '['
5762 || (**arg == '.' && (rettv->v_type == VAR_DICT
5763 || (!evaluate
5764 && (*arg)[1] != '.'
5765 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005766 {
5767 dict_unref(selfdict);
5768 if (rettv->v_type == VAR_DICT)
5769 {
5770 selfdict = rettv->vval.v_dict;
5771 if (selfdict != NULL)
5772 ++selfdict->dv_refcount;
5773 }
5774 else
5775 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005776 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005777 {
5778 clear_tv(rettv);
5779 ret = FAIL;
5780 }
5781 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005782 else
5783 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005784 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005786 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5787 // Don't do this when "Func" is already a partial that was bound
5788 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005789 if (selfdict != NULL
5790 && (rettv->v_type == VAR_FUNC
5791 || (rettv->v_type == VAR_PARTIAL
5792 && (rettv->vval.v_partial->pt_auto
5793 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005795
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005796 dict_unref(selfdict);
5797 return ret;
5798}
5799
5800/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005801 * Make a copy of an item.
5802 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005803 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5804 * reference to an already copied list/dict can be used.
5805 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005806 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005808item_copy(
5809 typval_T *from,
5810 typval_T *to,
5811 int deep,
5812 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005813{
5814 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005815 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005816
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005820 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005821 }
5822 ++recurse;
5823
5824 switch (from->v_type)
5825 {
5826 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005827 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005828 case VAR_STRING:
5829 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005830 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005831 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005832 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005833 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005834 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005835 copy_tv(from, to);
5836 break;
5837 case VAR_LIST:
5838 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005839 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005840 if (from->vval.v_list == NULL)
5841 to->vval.v_list = NULL;
5842 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005844 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005845 to->vval.v_list = from->vval.v_list->lv_copylist;
5846 ++to->vval.v_list->lv_refcount;
5847 }
5848 else
5849 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5850 if (to->vval.v_list == NULL)
5851 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005853 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005855 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 case VAR_DICT:
5857 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005858 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005859 if (from->vval.v_dict == NULL)
5860 to->vval.v_dict = NULL;
5861 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005863 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005864 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5865 ++to->vval.v_dict->dv_refcount;
5866 }
5867 else
5868 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5869 if (to->vval.v_dict == NULL)
5870 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005871 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005872 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005873 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005875 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 }
5878 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880}
5881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882 void
5883echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5884{
5885 char_u *tofree;
5886 char_u numbuf[NUMBUFLEN];
5887 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5888
5889 if (*atstart)
5890 {
5891 *atstart = FALSE;
5892 // Call msg_start() after eval1(), evaluating the expression
5893 // may cause a message to appear.
5894 if (with_space)
5895 {
5896 // Mark the saved text as finishing the line, so that what
5897 // follows is displayed on a new line when scrolling back
5898 // at the more prompt.
5899 msg_sb_eol();
5900 msg_start();
5901 }
5902 }
5903 else if (with_space)
5904 msg_puts_attr(" ", echo_attr);
5905
5906 if (p != NULL)
5907 for ( ; *p != NUL && !got_int; ++p)
5908 {
5909 if (*p == '\n' || *p == '\r' || *p == TAB)
5910 {
5911 if (*p != TAB && *needclr)
5912 {
5913 // remove any text still there from the command
5914 msg_clr_eos();
5915 *needclr = FALSE;
5916 }
5917 msg_putchar_attr(*p, echo_attr);
5918 }
5919 else
5920 {
5921 if (has_mbyte)
5922 {
5923 int i = (*mb_ptr2len)(p);
5924
5925 (void)msg_outtrans_len_attr(p, i, echo_attr);
5926 p += i - 1;
5927 }
5928 else
5929 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5930 }
5931 }
5932 vim_free(tofree);
5933}
5934
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 * ":echo expr1 ..." print each argument separated with a space, add a
5937 * newline at the end.
5938 * ":echon expr1 ..." print each argument plain.
5939 */
5940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 char_u *p;
5946 int needclr = TRUE;
5947 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005948 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005949 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005950 evalarg_T evalarg;
5951
Bram Moolenaare707c882020-07-01 13:07:37 +02005952 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 if (eap->skip)
5955 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005956 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005958 // If eval1() causes an error message the text from the command may
5959 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005960 need_clr_eos = needclr;
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005963 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
5965 /*
5966 * Report the invalid expression unless the expression evaluation
5967 * has been cancelled due to an aborting error, an interrupt, or an
5968 * exception.
5969 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005970 if (!aborting() && did_emsg == did_emsg_before
5971 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005972 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005973 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 break;
5975 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 need_clr_eos = FALSE;
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 arg = skipwhite(arg);
5983 }
5984 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005985 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 if (eap->skip)
5988 --emsg_skip;
5989 else
5990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005991 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 if (needclr)
5993 msg_clr_eos();
5994 if (eap->cmdidx == CMD_echo)
5995 msg_end();
5996 }
5997}
5998
5999/*
6000 * ":echohl {name}".
6001 */
6002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006005 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006}
6007
6008/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006009 * Returns the :echo attribute
6010 */
6011 int
6012get_echo_attr(void)
6013{
6014 return echo_attr;
6015}
6016
6017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 * ":execute expr1 ..." execute the result of an expression.
6019 * ":echomsg expr1 ..." Print a message
6020 * ":echoerr expr1 ..." Print an error
6021 * Each gets spaces around each argument and a newline at the end for
6022 * echo commands
6023 */
6024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 int ret = OK;
6030 char_u *p;
6031 garray_T ga;
6032 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033
6034 ga_init2(&ga, 1, 80);
6035
6036 if (eap->skip)
6037 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006038 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006040 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006041 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
6044 if (!eap->skip)
6045 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006046 char_u buf[NUMBUFLEN];
6047
6048 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006049 {
6050 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6051 {
6052 emsg(_(e_inval_string));
6053 p = NULL;
6054 }
6055 else
6056 p = tv_get_string_buf(&rettv, buf);
6057 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006058 else
6059 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006060 if (p == NULL)
6061 {
6062 clear_tv(&rettv);
6063 ret = FAIL;
6064 break;
6065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 len = (int)STRLEN(p);
6067 if (ga_grow(&ga, len + 2) == FAIL)
6068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 ret = FAIL;
6071 break;
6072 }
6073 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 ga.ga_len += len;
6077 }
6078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 arg = skipwhite(arg);
6081 }
6082
6083 if (ret != FAIL && ga.ga_data != NULL)
6084 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006085 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006087 // Mark the already saved text as finishing the line, so that what
6088 // follows is displayed on a new line when scrolling back at the
6089 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006090 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006091 }
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006094 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006095 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006096 out_flush();
6097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 else if (eap->cmdidx == CMD_echoerr)
6099 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006100 int save_did_emsg = did_emsg;
6101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006102 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006103 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (!force_abort)
6105 did_emsg = save_did_emsg;
6106 }
6107 else if (eap->cmdidx == CMD_execute)
6108 do_cmdline((char_u *)ga.ga_data,
6109 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6110 }
6111
6112 ga_clear(&ga);
6113
6114 if (eap->skip)
6115 --emsg_skip;
6116
6117 eap->nextcmd = check_nextcmd(arg);
6118}
6119
6120/*
6121 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6122 * "arg" points to the "&" or '+' when called, to "option" when returning.
6123 * Returns NULL when no option name found. Otherwise pointer to the char
6124 * after the option name.
6125 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006126 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 char_u *p = *arg;
6130
6131 ++p;
6132 if (*p == 'g' && p[1] == ':')
6133 {
6134 *opt_flags = OPT_GLOBAL;
6135 p += 2;
6136 }
6137 else if (*p == 'l' && p[1] == ':')
6138 {
6139 *opt_flags = OPT_LOCAL;
6140 p += 2;
6141 }
6142 else
6143 *opt_flags = 0;
6144
6145 if (!ASCII_ISALPHA(*p))
6146 return NULL;
6147 *arg = p;
6148
6149 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006150 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 else
6152 while (ASCII_ISALPHA(*p))
6153 ++p;
6154 return p;
6155}
6156
6157/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006158 * Display script name where an item was last set.
6159 * Should only be invoked when 'verbose' is non-zero.
6160 */
6161 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006162last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006163{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006164 char_u *p;
6165
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006166 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006167 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006168 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006169 if (p != NULL)
6170 {
6171 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006172 msg_puts(_("\n\tLast set from "));
6173 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006174 if (script_ctx.sc_lnum > 0)
6175 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006176 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006177 msg_outnum((long)script_ctx.sc_lnum);
6178 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006179 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006180 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006181 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006182 }
6183}
6184
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006185#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187/*
6188 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006189 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 * "flags" can be "g" to do a global substitute.
6191 * Returns an allocated string, NULL for error.
6192 */
6193 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006194do_string_sub(
6195 char_u *str,
6196 char_u *pat,
6197 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006198 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 int sublen;
6202 regmatch_T regmatch;
6203 int i;
6204 int do_all;
6205 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006206 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 garray_T ga;
6208 char_u *ret;
6209 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006210 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006212 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006214 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 ga_init2(&ga, 1, 200);
6217
6218 do_all = (flags[0] == 'g');
6219
6220 regmatch.rm_ic = p_ic;
6221 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6222 if (regmatch.regprog != NULL)
6223 {
6224 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006225 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006228 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006229 if (regmatch.startp[0] == regmatch.endp[0])
6230 {
6231 if (zero_width == regmatch.startp[0])
6232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006234 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006235 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6236 (size_t)i);
6237 ga.ga_len += i;
6238 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006239 continue;
6240 }
6241 zero_width = regmatch.startp[0];
6242 }
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /*
6245 * Get some space for a temporary buffer to do the substitution
6246 * into. It will contain:
6247 * - The text up to where the match is.
6248 * - The substituted text.
6249 * - The text after the match.
6250 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006251 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006252 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6254 {
6255 ga_clear(&ga);
6256 break;
6257 }
6258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006259 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 i = (int)(regmatch.startp[0] - tail);
6261 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006263 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 + ga.ga_len + i, TRUE, TRUE, FALSE);
6265 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006266 tail = regmatch.endp[0];
6267 if (*tail == NUL)
6268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (!do_all)
6270 break;
6271 }
6272
6273 if (ga.ga_data != NULL)
6274 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6275
Bram Moolenaar473de612013-06-08 18:19:48 +02006276 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 }
6278
6279 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6280 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006281 if (p_cpo == empty_option)
6282 p_cpo = save_cpo;
6283 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006286 // If it's still empty it was changed and restored, need to restore in
6287 // the complicated way.
6288 if (*p_cpo == NUL)
6289 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006290 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293 return ret;
6294}