blob: 0dd2058872da266a93c77a755f0e28f522cde9c3 [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 Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020059static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000115
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200116#ifdef EBCDIC
117 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100118 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200119 */
120 sortFunctions();
121#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000122}
123
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124#if defined(EXITFREE) || defined(PROTO)
125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100126eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000127{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100129 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200130 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000131
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200132 // autoloaded script names
133 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000134
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200135 // unreferenced lists and dicts
136 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200137
138 // functions not garbage collected
139 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140}
141#endif
142
Bram Moolenaare6b53242020-07-01 17:28:33 +0200143 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200144fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
145{
146 CLEAR_FIELD(*evalarg);
147 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200148 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200150 evalarg->eval_cstack = eap->cstack;
151 if (getline_equal(eap->getline, eap->cookie, getsourceline))
152 {
153 evalarg->eval_getline = eap->getline;
154 evalarg->eval_cookie = eap->cookie;
155 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200156 }
157}
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Top level evaluation function, returning a boolean.
161 * Sets "error" to TRUE if there was an error.
162 * Return TRUE or FALSE.
163 */
164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100165eval_to_bool(
166 char_u *arg,
167 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200168 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaar33570922005-01-25 22:26:29 +0000171 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200172 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200173 evalarg_T evalarg;
174
Bram Moolenaar37c83712020-06-30 21:18:36 +0200175 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
177 if (skip)
178 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200179 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 else
182 {
183 *error = FALSE;
184 if (!skip)
185 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200186 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200187 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200188 else
189 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192 }
193 if (skip)
194 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200197 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200/*
201 * Call eval1() and give an error message if not done at a lower level.
202 */
203 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100206 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 int ret;
208 int did_emsg_before = did_emsg;
209 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200212 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
213
214 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215 if (ret == FAIL)
216 {
217 // Report the invalid expression unless the expression evaluation has
218 // been cancelled due to an aborting error, an interrupt, or an
219 // exception, or we already gave a more specific error.
220 // Also check called_emsg for when using assert_fails().
221 if (!aborting() && did_emsg == did_emsg_before
222 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200223 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100224 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200225 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100226 return ret;
227}
228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200230 * Return whether a typval is a valid expression to pass to eval_expr_typval()
231 * or eval_expr_to_bool(). An empty string returns FALSE;
232 */
233 int
234eval_expr_valid_arg(typval_T *tv)
235{
236 return tv->v_type != VAR_UNKNOWN
237 && (tv->v_type != VAR_STRING
238 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
239}
240
241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 * Evaluate an expression, which can be a function, partial or string.
243 * Pass arguments "argv[argc]".
244 * Return the result in "rettv" and OK or FAIL.
245 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200246 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100247eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
248{
249 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100250 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200251 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100252
253 if (expr->v_type == VAR_FUNC)
254 {
255 s = expr->vval.v_string;
256 if (s == NULL || *s == NUL)
257 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200258 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 funcexe.evaluate = TRUE;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100261 return FAIL;
262 }
263 else if (expr->v_type == VAR_PARTIAL)
264 {
265 partial_T *partial = expr->vval.v_partial;
266
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200267 if (partial == NULL)
268 return FAIL;
269
Bram Moolenaar822ba242020-05-24 23:00:18 +0200270 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200271 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200273 if (call_def_function(partial->pt_func, argc, argv,
274 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 return FAIL;
276 }
277 else
278 {
279 s = partial_name(partial);
280 if (s == NULL || *s == NUL)
281 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200282 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 funcexe.evaluate = TRUE;
284 funcexe.partial = partial;
285 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
286 return FAIL;
287 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200289 else if (expr->v_type == VAR_INSTR)
290 {
291 return exe_typval_instr(expr, rettv);
292 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 else
294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100295 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 if (s == NULL)
297 return FAIL;
298 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200299 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200301 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200303 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200304 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 return FAIL;
306 }
307 }
308 return OK;
309}
310
311/*
312 * Like eval_to_bool() but using a typval_T instead of a string.
313 * Works for string, funcref and partial.
314 */
315 int
316eval_expr_to_bool(typval_T *expr, int *error)
317{
318 typval_T rettv;
319 int res;
320
321 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
322 {
323 *error = TRUE;
324 return FALSE;
325 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200326 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100327 clear_tv(&rettv);
328 return res;
329}
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331/*
332 * Top level evaluation function, returning a string. If "skip" is TRUE,
333 * only parsing to "nextcmd" is done, without reporting errors. Return
334 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
335 */
336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100337eval_to_string_skip(
338 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200339 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100340 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
Bram Moolenaar33570922005-01-25 22:26:29 +0000342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar37c83712020-06-30 21:18:36 +0200346 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (skip)
348 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200349 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 retval = NULL;
351 else
352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100353 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356 if (skip)
357 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200358 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360 return retval;
361}
362
363/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364 * Skip over an expression at "*pp".
365 * Return FAIL for an error, OK otherwise.
366 */
367 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200368skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369{
Bram Moolenaar33570922005-01-25 22:26:29 +0000370 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371
372 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200373 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374}
375
376/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200377 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200378 * If in Vim9 script and line breaks are encountered, the lines are
379 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200380 * "arg" is advanced to just after the expression.
381 * "start" is set to the start of the expression, "end" to just after the end.
382 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200386skip_expr_concatenate(
387 char_u **arg,
388 char_u **start,
389 char_u **end,
390 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200391{
392 typval_T rettv;
393 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200394 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200395 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200396 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 int evaluate = evalarg == NULL
399 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200401 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 {
404 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200405 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200408 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200410 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411
412 // Don't evaluate the expression.
413 if (evalarg != NULL)
414 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200415 *arg = skipwhite(*arg);
416 res = eval1(arg, &rettv, evalarg);
417 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 if (evalarg != NULL)
419 evalarg->eval_flags = save_flags;
420
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200421 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 if (evalarg->eval_ga.ga_len == 1)
425 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200426 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 ga_clear(gap);
428 gap->ga_itemsize = 0;
429 }
430 else
431 {
432 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200433 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 // Line breaks encountered, concatenate all the lines.
436 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200437 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438
439 // free the lines only when using getsourceline()
440 if (evalarg->eval_cookie != NULL)
441 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200442 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200444 // Do not free the last line, "arg" points into it, free it
445 // later.
446 vim_free(evalarg->eval_tofree);
447 evalarg->eval_tofree =
448 ((char_u **)gap->ga_data)[gap->ga_len - 1];
449 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear_strings(gap);
451 }
452 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200453 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200454 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200455
456 // free lines that were explicitly marked for freeing
457 ga_clear_strings(freegap);
458 }
459
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200460 gap->ga_itemsize = 0;
461 if (p == NULL)
462 return FAIL;
463 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200464 vim_free(evalarg->eval_tofree_lambda);
465 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 // Compute "end" relative to the end.
467 *end = *start + STRLEN(*start) - endoff;
468 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200469 }
470
471 return res;
472}
473
474/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100475 * Convert "tv" to a string.
476 * When "convert" is TRUE convert a List into a sequence of lines and convert
477 * a Float to a String.
478 * Returns an allocated string (NULL when out of memory).
479 */
480 char_u *
481typval2string(typval_T *tv, int convert)
482{
483 garray_T ga;
484 char_u *retval;
485#ifdef FEAT_FLOAT
486 char_u numbuf[NUMBUFLEN];
487#endif
488
489 if (convert && tv->v_type == VAR_LIST)
490 {
491 ga_init2(&ga, (int)sizeof(char), 80);
492 if (tv->vval.v_list != NULL)
493 {
494 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
495 if (tv->vval.v_list->lv_len > 0)
496 ga_append(&ga, NL);
497 }
498 ga_append(&ga, NUL);
499 retval = (char_u *)ga.ga_data;
500 }
501#ifdef FEAT_FLOAT
502 else if (convert && tv->v_type == VAR_FLOAT)
503 {
504 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
505 retval = vim_strsave(numbuf);
506 }
507#endif
508 else
509 retval = vim_strsave(tv_get_string(tv));
510 return retval;
511}
512
513/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200514 * Top level evaluation function, returning a string. Does not handle line
515 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000516 * When "convert" is TRUE convert a List into a sequence of lines and convert
517 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * Return pointer to allocated memory, or NULL for failure.
519 */
520 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 int convert,
524 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
Bram Moolenaar33570922005-01-25 22:26:29 +0000526 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100528 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
531 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 retval = NULL;
533 else
534 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100535 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000536 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 return retval;
541}
542
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100543 char_u *
544eval_to_string(
545 char_u *arg,
546 int convert)
547{
548 return eval_to_string_eap(arg, convert, NULL);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000552 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200554 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 */
556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100557eval_to_string_safe(
558 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100559 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200563 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200570 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000571 if (use_sandbox)
572 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200573 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 return retval;
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579/*
580 * Top level evaluation function, returning a number.
581 * Evaluates "expr" silently.
582 * Returns -1 for an error.
583 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100585eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar33570922005-01-25 22:26:29 +0000587 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000589 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 ++emsg_off;
592
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200593 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 retval = -1;
595 else
596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100597 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600 --emsg_off;
601
602 return retval;
603}
604
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000606 * Top level evaluation function.
607 * Returns an allocated typval_T with the result.
608 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609 */
610 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200611eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000612{
613 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200614 evalarg_T evalarg;
615
616 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200618 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623 return tv;
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100627 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200628 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
629 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000630 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633call_vim_function(
634 char_u *func,
635 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200637 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200640 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200643 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200644 funcexe.firstline = curwin->w_cursor.lnum;
645 funcexe.lastline = curwin->w_cursor.lnum;
646 funcexe.evaluate = TRUE;
647 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000648 if (ret == FAIL)
649 clear_tv(rettv);
650
651 return ret;
652}
653
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100654/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100655 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200657 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
658 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100659 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200660 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100661call_func_retnr(
662 char_u *func,
663 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200664 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665{
666 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200667 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668
Bram Moolenaarded27a12018-08-01 19:06:03 +0200669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 return -1;
671
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100672 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673 clear_tv(&rettv);
674 return retval;
675}
676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000677/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100678 * Call Vim script function like call_func_retnr() and drop the result.
679 * Returns FAIL when calling the function fails.
680 */
681 int
682call_func_noret(
683 char_u *func,
684 int argc,
685 typval_T *argv)
686{
687 typval_T rettv;
688
689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
690 return FAIL;
691 clear_tv(&rettv);
692 return OK;
693}
694
695/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100696 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100697 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000698 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000699 */
700 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701call_func_retstr(
702 char_u *func,
703 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200704 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705{
706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708
Bram Moolenaarded27a12018-08-01 19:06:03 +0200709 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 return NULL;
711
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100712 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return retval;
715}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100718 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100719 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000720 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 */
722 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723call_func_retlist(
724 char_u *func,
725 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200726 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727{
728 typval_T rettv;
729
Bram Moolenaarded27a12018-08-01 19:06:03 +0200730 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 return NULL;
732
733 if (rettv.v_type != VAR_LIST)
734 {
735 clear_tv(&rettv);
736 return NULL;
737 }
738
739 return rettv.vval.v_list;
740}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#ifdef FEAT_FOLDING
743/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100744 * Evaluate "arg", which is 'foldexpr'.
745 * Note: caller must set "curwin" to match "arg".
746 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
747 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100750eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200753 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000755 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
756 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000759 if (use_sandbox)
760 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200761 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200763 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 retval = 0;
765 else
766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100767 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000768 if (tv.v_type == VAR_NUMBER)
769 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000770 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 retval = 0;
772 else
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // If the result is a string, check if there is a non-digit before
775 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (!VIM_ISDIGIT(*s) && *s != '-')
778 *cp = *s++;
779 retval = atol((char *)s);
780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000784 if (use_sandbox)
785 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200786 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200787 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791#endif
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 * Get an lval: variable, Dict item or List item that can be assigned a value
795 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
796 * "name.key", "name.key[expr]" etc.
797 * Indexing only works if "name" is an existing List or Dictionary.
798 * "name" points to the start of the name.
799 * If "rettv" is not NULL it points to the value to be assigned.
800 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
801 * wrong; must end in space or cmd separator.
802 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * flags:
804 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100805 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100806 * GLV_NO_AUTOLOAD: do not use script autoloading
807 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000809 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000811 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813get_lval(
814 char_u *name,
815 typval_T *rettv,
816 lval_T *lp,
817 int unlet,
818 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100819 int flags, // GLV_ values
820 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 char_u *expr_start, *expr_end;
824 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 dictitem_T *v;
826 typval_T var1;
827 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000829 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100831 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100832 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100833 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200836 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200842 lp->ll_name_end = find_name_end(name, NULL, NULL,
843 FNE_INCL_BR | fne_flags);
844 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 }
846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100847 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 if (expr_start != NULL)
851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100853 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 && *p != '[' && *p != '.')
855 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200856 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 return NULL;
858 }
859
860 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
861 if (lp->ll_exp_name == NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Report an invalid expression in braces, unless the
864 // expression evaluation has been cancelled due to an
865 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 if (!aborting() && !quiet)
867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000868 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 return NULL;
871 }
872 }
873 lp->ll_name = lp->ll_exp_name;
874 }
875 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 lp->ll_name = name;
878
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 // "a: type" is declaring variable "a" with a type, not "a:".
882 if (p == name + 2 && p[-1] == ':')
883 {
884 --p;
885 lp->ll_name_end = p;
886 }
887 if (*p == ':')
888 {
889 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
890 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100893 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
894 if (lp->ll_type == NULL && !quiet)
895 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200896 lp->ll_name_end = tp;
897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 }
899 }
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
903 return p;
904
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200905 if (in_vim9script() && lval_root != NULL)
906 {
907 // using local variable
908 lp->ll_tv = lval_root;
909 }
910 else
911 {
912 cc = *p;
913 *p = NUL;
914 // When we would write to the variable pass &ht and prevent autoload.
915 writing = !(flags & GLV_READ_ONLY);
916 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100917 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200918 if (v == NULL && !quiet)
919 semsg(_(e_undefined_variable_str), lp->ll_name);
920 *p = cc;
921 if (v == NULL)
922 return NULL;
923 lp->ll_tv = &v->di_tv;
924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100926 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927 {
928 if (!quiet)
929 semsg(_(e_variable_already_declared), lp->ll_name);
930 return NULL;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 /*
934 * Loop until no more [idx] or .key is following.
935 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100936 var1.v_type = VAR_UNKNOWN;
937 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200938 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200940 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
941 {
942 if (!quiet)
943 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
944 return NULL;
945 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200946 if (lp->ll_tv->v_type != VAR_LIST
947 && lp->ll_tv->v_type != VAR_DICT
948 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200954
955 // a NULL list/blob works like an empty list/blob, allocate one now.
956 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
957 rettv_list_alloc(lp->ll_tv);
958 else if (lp->ll_tv->v_type == VAR_BLOB
959 && lp->ll_tv->vval.v_blob == NULL)
960 rettv_blob_alloc(lp->ll_tv);
961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100965 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000967 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000968
Bram Moolenaar10c65862020-10-08 21:16:42 +0200969 if (in_vim9script() && lp->ll_valtype == NULL
970 && lp->ll_tv == &v->di_tv
971 && ht != NULL && ht == get_script_local_ht())
972 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200973 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200974
975 // Vim9 script local variable: get the type
976 if (sv != NULL)
977 lp->ll_valtype = sv->sv_type;
978 }
979
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 len = -1;
981 if (*p == '.')
982 {
983 key = p + 1;
984 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
985 ;
986 if (len == 0)
987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100989 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 }
992 p = key + len;
993 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000994 else
995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000997 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 if (*p == ':')
999 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001000 else
1001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001003 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001005 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001007 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 clear_tv(&var1);
1009 return NULL;
1010 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001011 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 }
1013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 if (*p == ':')
1016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001020 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001021 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001023 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001024 if (rettv != NULL
1025 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001026 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001027 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001028 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 }
1035 p = skipwhite(p + 1);
1036 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 else
1039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001041 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001042 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001047 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001049 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001050 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001051 clear_tv(&var2);
1052 return NULL;
1053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001059
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001063 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001064 clear_tv(&var1);
1065 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 }
1068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001069 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 ++p;
1071 }
1072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 {
1075 if (len == -1)
1076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001077 // "[key]": get key from "var1"
1078 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001079 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001082 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 }
1084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001085 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001086
1087 // a NULL dict is equivalent with an empty dict
1088 if (lp->ll_tv->vval.v_dict == NULL)
1089 {
1090 lp->ll_tv->vval.v_dict = dict_alloc();
1091 if (lp->ll_tv->vval.v_dict == NULL)
1092 {
1093 clear_tv(&var1);
1094 return NULL;
1095 }
1096 ++lp->ll_tv->vval.v_dict->dv_refcount;
1097 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001098 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001099
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001100 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // When assigning to a scope dictionary check that a function and
1103 // variable name is valid (only variable name unless it is l: or
1104 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001105 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001106 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 int prevval;
1108 int wrong;
1109
1110 if (len != -1)
1111 {
1112 prevval = key[len];
1113 key[len] = NUL;
1114 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001115 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001116 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001117 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1118 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001119 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001120 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001121 if (len != -1)
1122 key[len] = prevval;
1123 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001124 {
1125 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001126 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001127 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128 }
1129
Bram Moolenaar10c65862020-10-08 21:16:42 +02001130 if (lp->ll_valtype != NULL)
1131 // use the type of the member
1132 lp->ll_valtype = lp->ll_valtype->tt_member;
1133
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001136 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001137 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001138 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001140 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001141 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001142 return NULL;
1143 }
1144
Bram Moolenaar31b81602019-02-10 22:14:27 +01001145 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001149 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001150 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 }
1153 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001157 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001159 p = NULL;
1160 break;
1161 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001162 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001163 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001164 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1165 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001166 {
1167 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001168 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001169 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001170
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001171 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001174 else if (lp->ll_tv->v_type == VAR_BLOB)
1175 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001176 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1177
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001178 /*
1179 * Get the number and item for the only or first index of the List.
1180 */
1181 if (empty1)
1182 lp->ll_n1 = 0;
1183 else
1184 // is number or string
1185 lp->ll_n1 = (long)tv_get_number(&var1);
1186 clear_tv(&var1);
1187
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001188 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001190 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001191 return NULL;
1192 }
1193 if (lp->ll_range && !lp->ll_empty2)
1194 {
1195 lp->ll_n2 = (long)tv_get_number(&var2);
1196 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001197 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1198 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001199 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 }
1201 lp->ll_blob = lp->ll_tv->vval.v_blob;
1202 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001203 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 else
1206 {
1207 /*
1208 * Get the number and item for the only or first index of the List.
1209 */
1210 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001212 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001215 clear_tv(&var1);
1216
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001217 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001219 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001220 if (lp->ll_li == NULL)
1221 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001222 clear_tv(&var2);
1223 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001224 }
1225
Bram Moolenaar10c65862020-10-08 21:16:42 +02001226 if (lp->ll_valtype != NULL)
1227 // use the type of the member
1228 lp->ll_valtype = lp->ll_valtype->tt_member;
1229
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 /*
1231 * May need to find the item or absolute index for the second
1232 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 * When no index given: "lp->ll_empty2" is TRUE.
1234 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001238 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001241 if (check_range_index_two(lp->ll_list,
1242 &lp->ll_n1, lp->ll_li,
1243 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001245 }
1246
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 }
1250
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001251 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 return p;
1254}
1255
1256/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001257 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001260clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261{
1262 vim_free(lp->ll_exp_name);
1263 vim_free(lp->ll_newkey);
1264}
1265
1266/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001267 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001269 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1270 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001273set_var_lval(
1274 lval_T *lp,
1275 char_u *endp,
1276 typval_T *rettv,
1277 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001278 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1279 char_u *op,
1280 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281{
1282 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001283 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284
1285 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001287 cc = *endp;
1288 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001289 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1290 return;
1291
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 if (lp->ll_blob != NULL)
1293 {
1294 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 if (op != NULL && *op != '=')
1297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001298 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 return;
1300 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001301 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001302 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303
1304 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1305 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306 if (lp->ll_empty2)
1307 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1308
Bram Moolenaar68452172021-04-12 21:21:02 +02001309 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1310 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 }
1313 else
1314 {
1315 val = (int)tv_get_number_chk(rettv, &error);
1316 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001317 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 }
1319 }
1320 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001322 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001323
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001324 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1325 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001326 {
1327 emsg(_(e_cannot_mod));
1328 *endp = cc;
1329 return;
1330 }
1331
Bram Moolenaarff697e62019-02-12 22:28:33 +01001332 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001333 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001334 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001335 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001336 {
1337 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001338 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1339 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001340 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001341 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1342 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001343 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001346 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001347 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001348 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1349 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001350 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001351 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1352 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001353 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001356 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001357 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001358 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001359 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001360 else if (lp->ll_range)
1361 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001362 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1363 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001364 {
1365 emsg(_("E996: Cannot lock a range"));
1366 return;
1367 }
1368
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001369 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1370 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 }
1372 else
1373 {
1374 /*
1375 * Assign to a List or Dictionary item.
1376 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001377 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1378 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001379 {
1380 emsg(_("E996: Cannot lock a list or dict"));
1381 return;
1382 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001383
1384 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001385 && check_typval_arg_type(lp->ll_valtype, rettv,
1386 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001387 return;
1388
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 if (lp->ll_newkey != NULL)
1390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 if (op != NULL && *op != '=')
1392 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001393 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 return;
1395 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001396 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1397 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001398 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001400 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001401 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (di == NULL)
1403 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001404 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1405 {
1406 vim_free(di);
1407 return;
1408 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 else if (op != NULL && *op != '=')
1412 {
1413 tv_op(lp->ll_tv, rettv, op);
1414 return;
1415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001418
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 /*
1420 * Assign the value to the variable or list item.
1421 */
1422 if (copy)
1423 copy_tv(rettv, lp->ll_tv);
1424 else
1425 {
1426 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001427 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 }
1430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431}
1432
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001434 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1435 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 * Returns OK or FAIL.
1437 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001439tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001441 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 char_u numbuf[NUMBUFLEN];
1443 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001444 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001446 // Can't do anything with a Funcref or Dict on the right.
1447 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001448 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001449 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1450 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 {
1452 switch (tv1->v_type)
1453 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001454 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001455 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 case VAR_DICT:
1458 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001459 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001460 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001461 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001462 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001463 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001464 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 break;
1466
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 case VAR_BLOB:
1468 if (*op != '+' || tv2->v_type != VAR_BLOB)
1469 break;
1470 // BLOB += BLOB
1471 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1472 {
1473 blob_T *b1 = tv1->vval.v_blob;
1474 blob_T *b2 = tv2->vval.v_blob;
1475 int i, len = blob_len(b2);
1476 for (i = 0; i < len; i++)
1477 ga_append(&b1->bv_ga, blob_get(b2, i));
1478 }
1479 return OK;
1480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 case VAR_LIST:
1482 if (*op != '+' || tv2->v_type != VAR_LIST)
1483 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001484 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001485 if (tv2->vval.v_list != NULL)
1486 {
1487 if (tv1->vval.v_list == NULL)
1488 {
1489 tv1->vval.v_list = tv2->vval.v_list;
1490 ++tv1->vval.v_list->lv_refcount;
1491 }
1492 else
1493 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 return OK;
1496
1497 case VAR_NUMBER:
1498 case VAR_STRING:
1499 if (tv2->v_type == VAR_LIST)
1500 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001501 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001504 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001505#ifdef FEAT_FLOAT
1506 if (tv2->v_type == VAR_FLOAT)
1507 {
1508 float_T f = n;
1509
Bram Moolenaarff697e62019-02-12 22:28:33 +01001510 if (*op == '%')
1511 break;
1512 switch (*op)
1513 {
1514 case '+': f += tv2->vval.v_float; break;
1515 case '-': f -= tv2->vval.v_float; break;
1516 case '*': f *= tv2->vval.v_float; break;
1517 case '/': f /= tv2->vval.v_float; break;
1518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519 clear_tv(tv1);
1520 tv1->v_type = VAR_FLOAT;
1521 tv1->vval.v_float = f;
1522 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001524#endif
1525 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001526 switch (*op)
1527 {
1528 case '+': n += tv_get_number(tv2); break;
1529 case '-': n -= tv_get_number(tv2); break;
1530 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001531 case '/': n = num_divide(n, tv_get_number(tv2),
1532 &failed); break;
1533 case '%': n = num_modulus(n, tv_get_number(tv2),
1534 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001535 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001536 clear_tv(tv1);
1537 tv1->v_type = VAR_NUMBER;
1538 tv1->vval.v_number = n;
1539 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 }
1541 else
1542 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 if (tv2->v_type == VAR_FLOAT)
1544 break;
1545
Bram Moolenaarff697e62019-02-12 22:28:33 +01001546 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001547 s = tv_get_string(tv1);
1548 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 clear_tv(tv1);
1550 tv1->v_type = VAR_STRING;
1551 tv1->vval.v_string = s;
1552 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001553 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001556#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 {
1558 float_T f;
1559
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 if (*op == '%' || *op == '.'
1561 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 && tv2->v_type != VAR_NUMBER
1563 && tv2->v_type != VAR_STRING))
1564 break;
1565 if (tv2->v_type == VAR_FLOAT)
1566 f = tv2->vval.v_float;
1567 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001568 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001569 switch (*op)
1570 {
1571 case '+': tv1->vval.v_float += f; break;
1572 case '-': tv1->vval.v_float -= f; break;
1573 case '*': tv1->vval.v_float *= f; break;
1574 case '/': tv1->vval.v_float /= f; break;
1575 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001578 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 }
1580 }
1581
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001582 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001583 return FAIL;
1584}
1585
1586/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 * Evaluate the expression used in a ":for var in expr" command.
1588 * "arg" points to "var".
1589 * Set "*errp" to TRUE for an error, FALSE otherwise;
1590 * Return a pointer that holds the info. Null when there is an error.
1591 */
1592 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001593eval_for_line(
1594 char_u *arg,
1595 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001596 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001597 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598{
Bram Moolenaar33570922005-01-25 22:26:29 +00001599 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001600 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T tv;
1603 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001604 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001606 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001608 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609 if (fi == NULL)
1610 return NULL;
1611
Bram Moolenaar404557e2021-07-05 21:41:48 +02001612 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1613 &fi->fi_semicolon, FALSE);
1614 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615 return fi;
1616
Bram Moolenaar404557e2021-07-05 21:41:48 +02001617 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001618 if (expr[0] != 'i' || expr[1] != 'n'
1619 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001621 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1622 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1623 else
1624 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 return fi;
1626 }
1627
1628 if (skip)
1629 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001630 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1631 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 {
1633 *errp = FALSE;
1634 if (!skip)
1635 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001636 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001637 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001638 l = tv.vval.v_list;
1639 if (l == NULL)
1640 {
1641 // a null list is like an empty list: do nothing
1642 clear_tv(&tv);
1643 }
1644 else
1645 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001647 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001649 // No need to increment the refcount, it's already set for
1650 // the list being used in "tv".
1651 fi->fi_list = l;
1652 list_add_watch(l, &fi->fi_lw);
1653 fi->fi_lw.lw_item = l->lv_first;
1654 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001655 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001657 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001658 fi->fi_bi = 0;
1659 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001661 typval_T btv;
1662
1663 // Make a copy, so that the iteration still works when the
1664 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001666 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001668 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001669 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001670 else if (tv.v_type == VAR_STRING)
1671 {
1672 fi->fi_byte_idx = 0;
1673 fi->fi_string = tv.vval.v_string;
1674 tv.vval.v_string = NULL;
1675 if (fi->fi_string == NULL)
1676 fi->fi_string = vim_strsave((char_u *)"");
1677 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 else
1679 {
Sean Dewar80d73952021-08-04 19:25:54 +02001680 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 }
1683 }
1684 }
1685 if (skip)
1686 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001687 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
1689 return fi;
1690}
1691
1692/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001693 * Used when looping over a :for line, skip the "in expr" part.
1694 */
1695 void
1696skip_for_lines(void *fi_void, evalarg_T *evalarg)
1697{
1698 forinfo_T *fi = (forinfo_T *)fi_void;
1699 int i;
1700
1701 for (i = 0; i < fi->fi_break_count; ++i)
1702 eval_next_line(evalarg);
1703}
1704
1705/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 * Use the first item in a ":for" list. Advance to the next.
1707 * Assign the values to the variable (list). "arg" points to the first one.
1708 * Return TRUE when a valid item was found, FALSE when at end of list or
1709 * something wrong.
1710 */
1711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001714 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001716 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001717 ? (ASSIGN_FINAL
1718 // first round: error if variable exists
1719 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1720 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001721 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001722 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001723 int skip_assign = in_vim9script() && arg[0] == '_'
1724 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 if (fi->fi_blob != NULL)
1727 {
1728 typval_T tv;
1729
1730 if (fi->fi_bi >= blob_len(fi->fi_blob))
1731 return FALSE;
1732 tv.v_type = VAR_NUMBER;
1733 tv.v_lock = VAR_FIXED;
1734 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1735 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001736 if (skip_assign)
1737 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001738 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001739 fi->fi_varcount, flag, NULL) == OK;
1740 }
1741
1742 if (fi->fi_string != NULL)
1743 {
1744 typval_T tv;
1745 int len;
1746
1747 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1748 if (len == 0)
1749 return FALSE;
1750 tv.v_type = VAR_STRING;
1751 tv.v_lock = VAR_FIXED;
1752 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1753 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001754 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001755 if (skip_assign)
1756 result = TRUE;
1757 else
1758 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001759 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001760 vim_free(tv.vval.v_string);
1761 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001762 }
1763
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 item = fi->fi_lw.lw_item;
1765 if (item == NULL)
1766 result = FALSE;
1767 else
1768 {
1769 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001770 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001771 if (skip_assign)
1772 result = TRUE;
1773 else
1774 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 }
1777 return result;
1778}
1779
1780/*
1781 * Free the structure used to store info used by ":for".
1782 */
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001788 if (fi == NULL)
1789 return;
1790 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001791 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001793 list_unref(fi->fi_list);
1794 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001795 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001796 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001797 else
1798 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 vim_free(fi);
1800}
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803set_context_for_expression(
1804 expand_T *xp,
1805 char_u *arg,
1806 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001808 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001812 if (cmdidx == CMD_let || cmdidx == CMD_var
1813 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 {
1815 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001816 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001818 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001819 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 {
1821 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001822 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001823 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 break;
1825 }
1826 return;
1827 }
1828 }
1829 else
1830 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1831 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 while ((xp->xp_pattern = vim_strpbrk(arg,
1833 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1834 {
1835 c = *xp->xp_pattern;
1836 if (c == '&')
1837 {
1838 c = xp->xp_pattern[1];
1839 if (c == '&')
1840 {
1841 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001842 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001847 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1848 xp->xp_pattern += 2;
1849
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852 else if (c == '$')
1853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001854 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 xp->xp_context = EXPAND_ENV_VARS;
1856 }
1857 else if (c == '=')
1858 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001859 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 xp->xp_context = EXPAND_EXPRESSION;
1861 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001862 else if (c == '#'
1863 && xp->xp_context == EXPAND_EXPRESSION)
1864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001866 break;
1867 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001868 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && xp->xp_context == EXPAND_FUNCTIONS
1870 && vim_strchr(xp->xp_pattern, '(') == NULL)
1871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 break;
1874 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001875 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001877 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1880 if (c == '\\' && xp->xp_pattern[1] != NUL)
1881 ++xp->xp_pattern;
1882 xp->xp_context = EXPAND_NOTHING;
1883 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1888 /* skip */ ;
1889 xp->xp_context = EXPAND_NOTHING;
1890 }
1891 else if (c == '|')
1892 {
1893 if (xp->xp_pattern[1] == '|')
1894 {
1895 ++xp->xp_pattern;
1896 xp->xp_context = EXPAND_EXPRESSION;
1897 }
1898 else
1899 xp->xp_context = EXPAND_COMMANDS;
1900 }
1901 else
1902 xp->xp_context = EXPAND_EXPRESSION;
1903 }
1904 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // Doesn't look like something valid, expand as an expression
1906 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 arg = xp->xp_pattern;
1909 if (*arg != NUL)
1910 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1911 /* skip */ ;
1912 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001913
1914 // ":exe one two" completes "two"
1915 if ((cmdidx == CMD_execute
1916 || cmdidx == CMD_echo
1917 || cmdidx == CMD_echon
1918 || cmdidx == CMD_echomsg)
1919 && xp->xp_context == EXPAND_EXPRESSION)
1920 {
1921 for (;;)
1922 {
1923 char_u *n = skiptowhite(arg);
1924
1925 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1926 break;
1927 arg = skipwhite(n);
1928 }
1929 }
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 xp->xp_pattern = arg;
1932}
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001935 * Return TRUE if "pat" matches "text".
1936 * Does not use 'cpo' and always uses 'magic'.
1937 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001938 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001939pattern_match(char_u *pat, char_u *text, int ic)
1940{
1941 int matches = FALSE;
1942 char_u *save_cpo;
1943 regmatch_T regmatch;
1944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001945 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001946 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001947 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001948 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1949 if (regmatch.regprog != NULL)
1950 {
1951 regmatch.rm_ic = ic;
1952 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1953 vim_regfree(regmatch.regprog);
1954 }
1955 p_cpo = save_cpo;
1956 return matches;
1957}
1958
1959/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001960 * Handle a name followed by "(". Both for just "name(arg)" and for
1961 * "expr->name(arg)".
1962 * Returns OK or FAIL.
1963 */
1964 static int
1965eval_func(
1966 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001967 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001968 char_u *name,
1969 int name_len,
1970 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001971 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001972 typval_T *basetv) // "expr" for "expr->name(arg)"
1973{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001974 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001975 char_u *s = name;
1976 int len = name_len;
1977 partial_T *partial;
1978 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001979 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001980
1981 if (!evaluate)
1982 check_vars(s, len);
1983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001984 // If "s" is the name of a variable of type VAR_FUNC
1985 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001986 s = deref_func_name(s, &len, &partial,
1987 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001989 // Need to make a copy, in case evaluating the arguments makes
1990 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001991 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001992 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001993 ret = FAIL;
1994 else
1995 {
1996 funcexe_T funcexe;
1997
1998 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001999 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 funcexe.firstline = curwin->w_cursor.lnum;
2001 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 funcexe.evaluate = evaluate;
2003 funcexe.partial = partial;
2004 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002005 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002006 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007 }
2008 vim_free(s);
2009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // If evaluate is FALSE rettv->v_type was not set in
2011 // get_func_tv, but it's needed in handle_subscript() to parse
2012 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2014 {
2015 rettv->vval.v_string = NULL;
2016 rettv->v_type = VAR_FUNC;
2017 }
2018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002019 // Stop the expression evaluation when immediately
2020 // aborting on error, or when an interrupt occurred or
2021 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 if (evaluate && aborting())
2023 {
2024 if (ret == OK)
2025 clear_tv(rettv);
2026 ret = FAIL;
2027 }
2028 return ret;
2029}
2030
2031/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002032 * Get the next line source line without advancing. But do skip over comment
2033 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002034 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002035 */
2036 static char_u *
2037getline_peek_skip_comments(evalarg_T *evalarg)
2038{
2039 for (;;)
2040 {
2041 char_u *next = getline_peek(evalarg->eval_getline,
2042 evalarg->eval_cookie);
2043 char_u *p;
2044
2045 if (next == NULL)
2046 break;
2047 p = skipwhite(next);
2048 if (*p != NUL && !vim9_comment_start(p))
2049 return next;
2050 (void)eval_next_line(evalarg);
2051 }
2052 return NULL;
2053}
2054
2055/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002056 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2057 * comment) and there is a next line, return the next line (skipping blanks)
2058 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002059 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2060 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002061 * "arg" must point somewhere inside a line, not at the start.
2062 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002063 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002064eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2065{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002066 char_u *p = skipwhite(arg);
2067
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002068 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002069 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002070 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002071 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002072 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002073 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002074 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002075
2076 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002077 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002078 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002079 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080
Bram Moolenaar9d489562020-07-30 20:08:50 +02002081 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 {
2083 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002084 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085 }
2086 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002087 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088}
2089
2090/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002091 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002092 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002093 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002094 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002095eval_next_line(evalarg_T *evalarg)
2096{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002097 garray_T *gap = &evalarg->eval_ga;
2098 char_u *line;
2099
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002100 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002101 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2102 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002103 else
2104 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002105 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002106 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2107 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002108 char_u *p = skipwhite(line);
2109
2110 // Going to concatenate the lines after parsing. For an empty or
2111 // comment line use an empty string.
2112 if (*p == NUL || vim9_comment_start(p))
2113 {
2114 vim_free(line);
2115 line = vim_strsave((char_u *)"");
2116 }
2117
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002118 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2119 ++gap->ga_len;
2120 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002121 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002122 {
2123 vim_free(evalarg->eval_tofree);
2124 evalarg->eval_tofree = line;
2125 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002126
2127 // Advanced to the next line, "arg" no longer points into the previous
2128 // line.
2129 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2130
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002131 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002132}
2133
Bram Moolenaare6b53242020-07-01 17:28:33 +02002134/*
2135 * Call eval_next_non_blank() and get the next line if needed.
2136 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002137 char_u *
2138skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2139{
2140 int getnext;
2141 char_u *p = skipwhite(arg);
2142
Bram Moolenaar962d7212020-07-04 14:15:00 +02002143 if (evalarg == NULL)
2144 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002145 eval_next_non_blank(p, evalarg, &getnext);
2146 if (getnext)
2147 return eval_next_line(evalarg);
2148 return p;
2149}
2150
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002151/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002152 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002153 */
2154 void
2155clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2156{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002157 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002158 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002160 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002161 if (eap != NULL)
2162 {
2163 // We may need to keep the original command line, e.g. for
2164 // ":let" it has the variable names. But we may also need the
2165 // new one, "nextcmd" points into it. Keep both.
2166 vim_free(eap->cmdline_tofree);
2167 eap->cmdline_tofree = *eap->cmdlinep;
2168 *eap->cmdlinep = evalarg->eval_tofree;
2169 }
2170 else
2171 vim_free(evalarg->eval_tofree);
2172 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002175 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2176 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002177 }
2178}
2179
2180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2184 */
2185
2186/*
2187 * Handle zero level expression.
2188 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002190 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * Return OK or FAIL.
2193 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195eval0(
2196 char_u *arg,
2197 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002198 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int ret;
2202 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002203 int did_emsg_before = did_emsg;
2204 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002206 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002209 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002210 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002211
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002212 if (ret != FAIL)
2213 end_error = !ends_excmd2(arg, p);
2214 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
2216 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /*
2219 * Report the invalid expression unless the expression evaluation has
2220 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002221 * exception, or we already gave a more specific error.
2222 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002224 if (!aborting()
2225 && did_emsg == did_emsg_before
2226 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002227 && (flags & EVAL_CONSTANT) == 0
2228 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002229 {
2230 if (end_error)
2231 semsg(_(e_trailing_arg), p);
2232 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002233 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002234 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002235
2236 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002237 // a next command to avoid more errors, unless "|" is following, which
2238 // could only be a command separator.
2239 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2240 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002243
2244 if (eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002245 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 return ret;
2248}
2249
2250/*
2251 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002252 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002253 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 *
2255 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002256 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002258 * Note: "rettv.v_lock" is not set.
2259 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * Return OK or FAIL.
2261 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002262 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002265 char_u *p;
2266 int getnext;
2267
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002268 CLEAR_POINTER(rettv);
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /*
2271 * Get the first variable.
2272 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002273 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 return FAIL;
2275
Bram Moolenaar793648f2020-06-26 21:28:25 +02002276 p = eval_next_non_blank(*arg, evalarg, &getnext);
2277 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002279 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 int result;
2281 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002282 evalarg_T *evalarg_used = evalarg;
2283 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002285 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002286 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287
2288 if (evalarg == NULL)
2289 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002290 CLEAR_FIELD(local_evalarg);
2291 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002292 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002293 orig_flags = evalarg_used->eval_flags;
2294 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296 if (getnext)
2297 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002300 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002301 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002302 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002303 clear_tv(rettv);
2304 return FAIL;
2305 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002307 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 int error = FALSE;
2313
Bram Moolenaar13106602020-10-04 16:06:05 +02002314 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002315 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002316 else if (vim9script)
2317 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002318 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002320 if (error || !op_falsy || !result)
2321 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002322 if (error)
2323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325
2326 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002327 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002329 if (op_falsy)
2330 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002331 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002332 {
mityu4ac198c2021-05-28 17:52:40 +02002333 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002334 clear_tv(rettv);
2335 return FAIL;
2336 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002337 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002338 evalarg_used->eval_flags = (op_falsy ? !result : result)
2339 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2340 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002341 {
2342 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002344 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002345 if (!op_falsy || !result)
2346 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002348 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002350 /*
2351 * Check for the ":".
2352 */
2353 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2354 if (*p != ':')
2355 {
2356 emsg(_(e_missing_colon));
2357 if (evaluate && result)
2358 clear_tv(rettv);
2359 evalarg_used->eval_flags = orig_flags;
2360 return FAIL;
2361 }
2362 if (getnext)
2363 *arg = eval_next_line(evalarg_used);
2364 else
2365 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002366 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002367 {
2368 error_white_both(p, 1);
2369 clear_tv(rettv);
2370 evalarg_used->eval_flags = orig_flags;
2371 return FAIL;
2372 }
2373 *arg = p;
2374 }
2375
2376 /*
2377 * Get the third variable. Recursive!
2378 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002379 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002380 {
mityu4ac198c2021-05-28 17:52:40 +02002381 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002383 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002384 return FAIL;
2385 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2387 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002389 if (eval1(arg, &var2, evalarg_used) == FAIL)
2390 {
2391 if (evaluate && result)
2392 clear_tv(rettv);
2393 evalarg_used->eval_flags = orig_flags;
2394 return FAIL;
2395 }
2396 if (evaluate && !result)
2397 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002399
2400 if (evalarg == NULL)
2401 clear_evalarg(&local_evalarg, NULL);
2402 else
2403 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405
2406 return OK;
2407}
2408
2409/*
2410 * Handle first level expression:
2411 * expr2 || expr2 || expr2 logical OR
2412 *
2413 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002414 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 *
2416 * Return OK or FAIL.
2417 */
2418 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002421 char_u *p;
2422 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 /*
2425 * Get the first variable.
2426 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 return FAIL;
2429
2430 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002433 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 evalarg_T *evalarg_used = evalarg;
2437 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002438 int evaluate;
2439 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 long result = FALSE;
2441 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002442 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002443 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002444
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 if (evalarg == NULL)
2446 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447 CLEAR_FIELD(local_evalarg);
2448 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 orig_flags = evalarg_used->eval_flags;
2451 evaluate = orig_flags & EVAL_EVALUATE;
2452 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002454 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002455 result = tv_get_bool_chk(rettv, &error);
2456 else if (tv_get_number_chk(rettv, &error) != 0)
2457 result = TRUE;
2458 clear_tv(rettv);
2459 if (error)
2460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462
2463 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 while (p[0] == '|' && p[1] == '|')
2467 {
2468 if (getnext)
2469 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002470 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002471 {
2472 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2473 {
2474 error_white_both(p, 2);
2475 clear_tv(rettv);
2476 return FAIL;
2477 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002478 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002479 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002480
2481 /*
2482 * Get the second variable.
2483 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002484 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2485 {
mityu4ac198c2021-05-28 17:52:40 +02002486 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002487 clear_tv(rettv);
2488 return FAIL;
2489 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2491 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495
2496 /*
2497 * Compute the result.
2498 */
2499 if (evaluate && !result)
2500 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002501 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002502 result = tv_get_bool_chk(&var2, &error);
2503 else if (tv_get_number_chk(&var2, &error) != 0)
2504 result = TRUE;
2505 clear_tv(&var2);
2506 if (error)
2507 return FAIL;
2508 }
2509 if (evaluate)
2510 {
2511 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002512 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002513 rettv->v_type = VAR_BOOL;
2514 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002515 }
2516 else
2517 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002518 rettv->v_type = VAR_NUMBER;
2519 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002522
2523 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002525
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526 if (evalarg == NULL)
2527 clear_evalarg(&local_evalarg, NULL);
2528 else
2529 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
2531
2532 return OK;
2533}
2534
2535/*
2536 * Handle second level expression:
2537 * expr3 && expr3 && expr3 logical AND
2538 *
2539 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002540 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 *
2542 * Return OK or FAIL.
2543 */
2544 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002547 char_u *p;
2548 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 /*
2551 * Get the first variable.
2552 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 return FAIL;
2555
2556 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002559 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 evalarg_T *evalarg_used = evalarg;
2563 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564 int orig_flags;
2565 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 long result = TRUE;
2567 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002568 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002569 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002570
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 if (evalarg == NULL)
2572 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 CLEAR_FIELD(local_evalarg);
2574 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002575 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576 orig_flags = evalarg_used->eval_flags;
2577 evaluate = orig_flags & EVAL_EVALUATE;
2578 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002580 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002581 result = tv_get_bool_chk(rettv, &error);
2582 else if (tv_get_number_chk(rettv, &error) == 0)
2583 result = FALSE;
2584 clear_tv(rettv);
2585 if (error)
2586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588
2589 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 while (p[0] == '&' && p[1] == '&')
2593 {
2594 if (getnext)
2595 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002596 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002597 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002598 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002599 {
2600 error_white_both(p, 2);
2601 clear_tv(rettv);
2602 return FAIL;
2603 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002604 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606
2607 /*
2608 * Get the second variable.
2609 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002610 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2611 {
mityu4ac198c2021-05-28 17:52:40 +02002612 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 clear_tv(rettv);
2614 return FAIL;
2615 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2617 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002619 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622
2623 /*
2624 * Compute the result.
2625 */
2626 if (evaluate && result)
2627 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002628 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 result = tv_get_bool_chk(&var2, &error);
2630 else if (tv_get_number_chk(&var2, &error) == 0)
2631 result = FALSE;
2632 clear_tv(&var2);
2633 if (error)
2634 return FAIL;
2635 }
2636 if (evaluate)
2637 {
2638 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002639 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002640 rettv->v_type = VAR_BOOL;
2641 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002642 }
2643 else
2644 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 rettv->v_type = VAR_NUMBER;
2646 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002647 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002649
2650 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002652
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 if (evalarg == NULL)
2654 clear_evalarg(&local_evalarg, NULL);
2655 else
2656 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658
2659 return OK;
2660}
2661
2662/*
2663 * Handle third level expression:
2664 * var1 == var2
2665 * var1 =~ var2
2666 * var1 != var2
2667 * var1 !~ var2
2668 * var1 > var2
2669 * var1 >= var2
2670 * var1 < var2
2671 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002672 * var1 is var2
2673 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 *
2675 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002676 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 *
2678 * Return OK or FAIL.
2679 */
2680 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002684 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002685 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002687 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689 /*
2690 * Get the first variable.
2691 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002692 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 return FAIL;
2694
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002695 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002696 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002699 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002701 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002703 typval_T var2;
2704 int ic;
2705 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002706 int evaluate = evalarg == NULL
2707 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002708
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002709 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002710 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002711 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002712 p = *arg;
2713 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002714 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2715 {
mityu4ac198c2021-05-28 17:52:40 +02002716 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002717 clear_tv(rettv);
2718 return FAIL;
2719 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002720
Bram Moolenaar696ba232020-07-29 21:20:41 +02002721 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2722 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002723 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002724 clear_tv(rettv);
2725 return FAIL;
2726 }
2727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002728 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (p[len] == '?')
2730 {
2731 ic = TRUE;
2732 ++len;
2733 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 else if (p[len] == '#')
2736 {
2737 ic = FALSE;
2738 ++len;
2739 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002740 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002742 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /*
2745 * Get the second variable.
2746 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002747 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2748 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002749 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002750 clear_tv(rettv);
2751 return FAIL;
2752 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002753 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002756 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 return FAIL;
2758 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002759 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002760 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002761 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002762
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002763 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002764 {
2765 ret = FAIL;
2766 clear_tv(rettv);
2767 }
2768 else
2769 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002770 clear_tv(&var2);
2771 return ret;
2772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
2774
2775 return OK;
2776}
2777
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002778/*
2779 * Make a copy of blob "tv1" and append blob "tv2".
2780 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 void
2782eval_addblob(typval_T *tv1, typval_T *tv2)
2783{
2784 blob_T *b1 = tv1->vval.v_blob;
2785 blob_T *b2 = tv2->vval.v_blob;
2786 blob_T *b = blob_alloc();
2787 int i;
2788
2789 if (b != NULL)
2790 {
2791 for (i = 0; i < blob_len(b1); i++)
2792 ga_append(&b->bv_ga, blob_get(b1, i));
2793 for (i = 0; i < blob_len(b2); i++)
2794 ga_append(&b->bv_ga, blob_get(b2, i));
2795
2796 clear_tv(tv1);
2797 rettv_blob_set(tv1, b);
2798 }
2799}
2800
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002801/*
2802 * Make a copy of list "tv1" and append list "tv2".
2803 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 int
2805eval_addlist(typval_T *tv1, typval_T *tv2)
2806{
2807 typval_T var3;
2808
2809 // concatenate Lists
2810 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2811 {
2812 clear_tv(tv1);
2813 clear_tv(tv2);
2814 return FAIL;
2815 }
2816 clear_tv(tv1);
2817 *tv1 = var3;
2818 return OK;
2819}
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821/*
2822 * Handle fourth level expression:
2823 * + number addition
2824 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002825 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002826 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 *
2828 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002829 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *
2831 * Return OK or FAIL.
2832 */
2833 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 /*
2837 * Get the first variable.
2838 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002839 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 return FAIL;
2841
2842 /*
2843 * Repeat computing, until no '+', '-' or '.' is following.
2844 */
2845 for (;;)
2846 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002847 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002848 int getnext;
2849 char_u *p;
2850 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002851 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002852 int concat;
2853 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002854 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002856 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002857 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002858 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 p = eval_next_non_blank(*arg, evalarg, &getnext);
2860 op = *p;
2861 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002862 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2863 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002865 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2866 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002867
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002868 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002869 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002871 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002872 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002873 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002874 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002875 {
mityu4ac198c2021-05-28 17:52:40 +02002876 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002877 clear_tv(rettv);
2878 return FAIL;
2879 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002880 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002881 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002882 if ((op != '+' || (rettv->v_type != VAR_LIST
2883 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002884#ifdef FEAT_FLOAT
2885 && (op == '.' || rettv->v_type != VAR_FLOAT)
2886#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002887 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002889 int error = FALSE;
2890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 // For "list + ...", an illegal use of the first operand as
2892 // a number cannot be determined before evaluating the 2nd
2893 // operand: if this is also a list, all is ok.
2894 // For "something . ...", "something - ..." or "non-list + ...",
2895 // we know that the first operand needs to be a string or number
2896 // without evaluating the 2nd operand. So check before to avoid
2897 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002898 if (op != '.')
2899 tv_get_number_chk(rettv, &error);
2900 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002901 {
2902 clear_tv(rettv);
2903 return FAIL;
2904 }
2905 }
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 /*
2908 * Get the second variable.
2909 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002910 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002911 {
mityu89dcb4d2021-05-28 13:50:17 +02002912 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002913 clear_tv(rettv);
2914 return FAIL;
2915 }
2916 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002917 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 return FAIL;
2921 }
2922
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 /*
2926 * Compute the result.
2927 */
2928 if (op == '.')
2929 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2931 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002932 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933
Bram Moolenaar2e086612020-08-25 22:37:48 +02002934 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002935 || var2.v_type == VAR_CHANNEL
2936 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002937 semsg(_(e_using_invalid_value_as_string_str),
2938 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002939#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002940 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002941 {
2942 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2943 var2.vval.v_float);
2944 s2 = buf2;
2945 }
2946#endif
2947 else
2948 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002949 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002950 {
2951 clear_tv(rettv);
2952 clear_tv(&var2);
2953 return FAIL;
2954 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002956 clear_tv(rettv);
2957 rettv->v_type = VAR_STRING;
2958 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002960 else if (op == '+' && rettv->v_type == VAR_BLOB
2961 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002963 else if (op == '+' && rettv->v_type == VAR_LIST
2964 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002965 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002967 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 else
2970 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 int error = FALSE;
2972 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974 float_T f1 = 0, f2 = 0;
2975
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002978 f1 = rettv->vval.v_float;
2979 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002980 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981 else
2982#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002984 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 if (error)
2986 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002987 // This can only happen for "list + non-list" or
2988 // "blob + non-blob". For "non-list + ..." or
2989 // "something - ...", we returned before evaluating the
2990 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002992 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002993 return FAIL;
2994 }
2995#ifdef FEAT_FLOAT
2996 if (var2.v_type == VAR_FLOAT)
2997 f1 = n1;
2998#endif
2999 }
3000#ifdef FEAT_FLOAT
3001 if (var2.v_type == VAR_FLOAT)
3002 {
3003 f2 = var2.vval.v_float;
3004 n2 = 0;
3005 }
3006 else
3007#endif
3008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003009 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 if (error)
3011 {
3012 clear_tv(rettv);
3013 clear_tv(&var2);
3014 return FAIL;
3015 }
3016#ifdef FEAT_FLOAT
3017 if (rettv->v_type == VAR_FLOAT)
3018 f2 = n2;
3019#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003020 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003021 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003022
3023#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003024 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3026 {
3027 if (op == '+')
3028 f1 = f1 + f2;
3029 else
3030 f1 = f1 - f2;
3031 rettv->v_type = VAR_FLOAT;
3032 rettv->vval.v_float = f1;
3033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035#endif
3036 {
3037 if (op == '+')
3038 n1 = n1 + n2;
3039 else
3040 n1 = n1 - n2;
3041 rettv->v_type = VAR_NUMBER;
3042 rettv->vval.v_number = n1;
3043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 }
3048 return OK;
3049}
3050
3051/*
3052 * Handle fifth level expression:
3053 * * number multiplication
3054 * / number division
3055 * % number modulo
3056 *
3057 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003058 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 *
3060 * Return OK or FAIL.
3061 */
3062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063eval6(
3064 char_u **arg,
3065 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003066 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003069#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003070 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073 /*
3074 * Get the first variable.
3075 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003076 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 return FAIL;
3078
3079 /*
3080 * Repeat computing, until no '*', '/' or '%' is following.
3081 */
3082 for (;;)
3083 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003084 int evaluate;
3085 int getnext;
3086 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003087 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003088 int op;
3089 varnumber_T n1, n2;
3090#ifdef FEAT_FLOAT
3091 float_T f1, f2;
3092#endif
3093 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003095 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003096 p = eval_next_non_blank(*arg, evalarg, &getnext);
3097 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003098 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003100
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003101 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003102 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003103 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003104 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003105 {
3106 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3107 {
mityu4ac198c2021-05-28 17:52:40 +02003108 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003109 clear_tv(rettv);
3110 return FAIL;
3111 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003112 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003115#ifdef FEAT_FLOAT
3116 f1 = 0;
3117 f2 = 0;
3118#endif
3119 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#ifdef FEAT_FLOAT
3123 if (rettv->v_type == VAR_FLOAT)
3124 {
3125 f1 = rettv->vval.v_float;
3126 use_float = TRUE;
3127 n1 = 0;
3128 }
3129 else
3130#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003131 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003132 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003133 if (error)
3134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 else
3137 n1 = 0;
3138
3139 /*
3140 * Get the second variable.
3141 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003142 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3143 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003144 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003145 clear_tv(rettv);
3146 return FAIL;
3147 }
3148 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003149 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return FAIL;
3151
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003152 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154#ifdef FEAT_FLOAT
3155 if (var2.v_type == VAR_FLOAT)
3156 {
3157 if (!use_float)
3158 {
3159 f1 = n1;
3160 use_float = TRUE;
3161 }
3162 f2 = var2.vval.v_float;
3163 n2 = 0;
3164 }
3165 else
3166#endif
3167 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003168 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 clear_tv(&var2);
3170 if (error)
3171 return FAIL;
3172#ifdef FEAT_FLOAT
3173 if (use_float)
3174 f2 = n2;
3175#endif
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 /*
3179 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182#ifdef FEAT_FLOAT
3183 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003185 if (op == '*')
3186 f1 = f1 * f2;
3187 else if (op == '/')
3188 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003189# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003190 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003191 if (f2 == 0.0)
3192 {
3193 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003194 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003195 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003196 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003197 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003198 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003199 }
3200 else
3201 f1 = f1 / f2;
3202# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003203 // We rely on the floating point library to handle divide
3204 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003205 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003206# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211 return FAIL;
3212 }
3213 rettv->v_type = VAR_FLOAT;
3214 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 int failed = FALSE;
3220
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003221 if (op == '*')
3222 n1 = n1 * n2;
3223 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003224 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003226 n1 = num_modulus(n1, n2, &failed);
3227 if (failed)
3228 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230 rettv->v_type = VAR_NUMBER;
3231 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 }
3235
3236 return OK;
3237}
3238
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003239/*
3240 * Handle a type cast before a base level expression.
3241 * "arg" must point to the first non-white of the expression.
3242 * "arg" is advanced to just after the recognized expression.
3243 * Return OK or FAIL.
3244 */
3245 static int
3246eval7t(
3247 char_u **arg,
3248 typval_T *rettv,
3249 evalarg_T *evalarg,
3250 int want_string) // after "." operator
3251{
3252 type_T *want_type = NULL;
3253 garray_T type_list; // list of pointers to allocated types
3254 int res;
3255 int evaluate = evalarg == NULL ? 0
3256 : (evalarg->eval_flags & EVAL_EVALUATE);
3257
3258 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003259 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3260 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003261 {
3262 ++*arg;
3263 ga_init2(&type_list, sizeof(type_T *), 10);
3264 want_type = parse_type(arg, &type_list, TRUE);
3265 if (want_type == NULL && (evaluate || **arg != '>'))
3266 {
3267 clear_type_list(&type_list);
3268 return FAIL;
3269 }
3270
3271 if (**arg != '>')
3272 {
3273 if (*skipwhite(*arg) == '>')
3274 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3275 else
3276 emsg(_(e_missing_gt));
3277 clear_type_list(&type_list);
3278 return FAIL;
3279 }
3280 ++*arg;
3281 *arg = skipwhite_and_linebreak(*arg, evalarg);
3282 }
3283
3284 res = eval7(arg, rettv, evalarg, want_string);
3285
3286 if (want_type != NULL && evaluate)
3287 {
3288 if (res == OK)
3289 {
3290 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3291
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003292 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003293 {
3294 if (want_type == &t_bool && actual != &t_bool
3295 && (actual->tt_flags & TTFLAG_BOOL_OK))
3296 {
3297 int n = tv2bool(rettv);
3298
3299 // can use "0" and "1" for boolean in some places
3300 clear_tv(rettv);
3301 rettv->v_type = VAR_BOOL;
3302 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3303 }
3304 else
3305 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003306 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003307
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003308 where.wt_variable = TRUE;
3309 res = check_type(want_type, actual, TRUE, where);
3310 }
3311 }
3312 }
3313 clear_type_list(&type_list);
3314 }
3315
3316 return res;
3317}
3318
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003319 int
3320eval_leader(char_u **arg, int vim9)
3321{
3322 char_u *s = *arg;
3323 char_u *p = *arg;
3324
3325 while (*p == '!' || *p == '-' || *p == '+')
3326 {
3327 char_u *n = skipwhite(p + 1);
3328
3329 // ++, --, -+ and +- are not accepted in Vim9 script
3330 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3331 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003332 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003333 return FAIL;
3334 }
3335 p = n;
3336 }
3337 *arg = p;
3338 return OK;
3339}
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341/*
3342 * Handle sixth level expression:
3343 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003344 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003345 * "string" string constant
3346 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * &option-name option value
3348 * @r register contents
3349 * identifier variable value
3350 * function() function call
3351 * $VAR environment variable
3352 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003353 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003355 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003356 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 *
3358 * Also handle:
3359 * ! in front logical NOT
3360 * - in front unary minus
3361 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 * trailing [] subscript in String or List
3363 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003364 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 *
3366 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003367 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 *
3369 * Return OK or FAIL.
3370 */
3371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003372eval7(
3373 char_u **arg,
3374 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003375 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003378 int evaluate = evalarg != NULL
3379 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len;
3381 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 char_u *start_leader, *end_leader;
3383 int ret = OK;
3384 char_u *alias;
3385
3386 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003388 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003393 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
3395 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003396 if (eval_leader(arg, in_vim9script()) == FAIL)
3397 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 end_leader = *arg;
3399
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003400 if (**arg == '.' && (!isdigit(*(*arg + 1))
3401#ifdef FEAT_FLOAT
3402 || current_sctx.sc_version < 2
3403#endif
3404 ))
3405 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003406 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003407 ++*arg;
3408 return FAIL;
3409 }
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 switch (**arg)
3412 {
3413 /*
3414 * Number constant.
3415 */
3416 case '0':
3417 case '1':
3418 case '2':
3419 case '3':
3420 case '4':
3421 case '5':
3422 case '6':
3423 case '7':
3424 case '8':
3425 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003426 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003427
3428 // Apply prefixed "-" and "+" now. Matters especially when
3429 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003430 if (ret == OK && evaluate && end_leader > start_leader
3431 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003432 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 break;
3434
3435 /*
3436 * String constant: "string".
3437 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003438 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 break;
3440
3441 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003442 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003444 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003445 break;
3446
3447 /*
3448 * List: [expr, expr]
3449 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003450 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 break;
3452
3453 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003454 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003455 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003456 case '#': if (in_vim9script())
3457 {
3458 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3459 }
3460 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003461 {
3462 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003463 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003464 }
3465 else
3466 ret = NOTDONE;
3467 break;
3468
3469 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003470 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003471 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003473 case '{': if (in_vim9script())
3474 ret = NOTDONE;
3475 else
3476 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003477 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003478 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003479 break;
3480
3481 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003482 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003484 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 break;
3486
3487 /*
3488 * Environment variable: $VAR.
3489 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003490 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 break;
3492
3493 /*
3494 * Register contents: @r.
3495 */
3496 case '@': ++*arg;
3497 if (evaluate)
3498 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003499 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3500 semsg(_(e_syntax_error_at_str), *arg);
3501 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3502 emsg_invreg(**arg);
3503 else
3504 {
3505 rettv->v_type = VAR_STRING;
3506 rettv->vval.v_string = get_reg_contents(**arg,
3507 GREG_EXPR_SRC);
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 if (**arg != NUL)
3511 ++*arg;
3512 break;
3513
3514 /*
3515 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003516 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003518 case '(': ret = NOTDONE;
3519 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003520 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003521 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003522 if (ret == OK && evaluate)
3523 {
3524 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3525
Bram Moolenaara9931532021-06-12 15:58:16 +02003526 // Compile it here to get the return type. The return
3527 // type is optional, when it's missing use t_unknown.
3528 // This is recognized in compile_return().
3529 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3530 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003531 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003532 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003533 {
3534 clear_tv(rettv);
3535 ret = FAIL;
3536 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003537 }
3538 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003539 if (ret == NOTDONE)
3540 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003541 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003542 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003543
Bram Moolenaar9215f012020-06-27 21:18:00 +02003544 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003545 if (**arg == ')')
3546 ++*arg;
3547 else if (ret == OK)
3548 {
3549 emsg(_(e_missing_close));
3550 clear_tv(rettv);
3551 ret = FAIL;
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554 break;
3555
Bram Moolenaar8c711452005-01-14 21:53:12 +00003556 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559
3560 if (ret == NOTDONE)
3561 {
3562 /*
3563 * Must be a variable or function name.
3564 * Can also be a curly-braces kind of name: {expr}.
3565 */
3566 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003567 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 if (alias != NULL)
3569 s = alias;
3570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003571 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003572 ret = FAIL;
3573 else
3574 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003575 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3576
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003577 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003578 {
3579 emsg(_(e_cannot_use_underscore_here));
3580 ret = FAIL;
3581 }
3582 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003583 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003584 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003585 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003586 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003587 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003588 else if (flags & EVAL_CONSTANT)
3589 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003591 {
3592 // get the value of "true", "false" or a variable
3593 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3594 {
3595 rettv->v_type = VAR_BOOL;
3596 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003597 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003598 }
3599 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003600 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003601 {
3602 rettv->v_type = VAR_BOOL;
3603 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003604 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003605 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003606 else if (len == 4 && in_vim9script()
3607 && STRNCMP(s, "null", 4) == 0)
3608 {
3609 rettv->v_type = VAR_SPECIAL;
3610 rettv->vval.v_number = VVAL_NULL;
3611 ret = OK;
3612 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003613 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003614 ret = eval_variable(s, len, rettv, NULL,
3615 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003617 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003618 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003619 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003620 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003621 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003624 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 }
3626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003627 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3628 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003629 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003630 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 /*
3633 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3634 */
3635 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003636 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003637 return ret;
3638}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003639
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640/*
3641 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003642 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003643 * Adjusts "end_leaderp" until it is at "start_leader".
3644 */
3645 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003646eval7_leader(
3647 typval_T *rettv,
3648 int numeric_only,
3649 char_u *start_leader,
3650 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003651{
3652 char_u *end_leader = *end_leaderp;
3653 int ret = OK;
3654 int error = FALSE;
3655 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003656 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003657#ifdef FEAT_FLOAT
3658 float_T f = 0.0;
3659
3660 if (rettv->v_type == VAR_FLOAT)
3661 f = rettv->vval.v_float;
3662 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003663#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003664 {
3665 while (VIM_ISWHITE(end_leader[-1]))
3666 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003667 if (in_vim9script() && end_leader[-1] == '!')
3668 val = tv2bool(rettv);
3669 else
3670 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003671 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003672 if (error)
3673 {
3674 clear_tv(rettv);
3675 ret = FAIL;
3676 }
3677 else
3678 {
3679 while (end_leader > start_leader)
3680 {
3681 --end_leader;
3682 if (*end_leader == '!')
3683 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003684 if (numeric_only)
3685 {
3686 ++end_leader;
3687 break;
3688 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003689#ifdef FEAT_FLOAT
3690 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003691 {
3692 if (in_vim9script())
3693 {
3694 rettv->v_type = VAR_BOOL;
3695 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3696 }
3697 else
3698 f = !f;
3699 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003700 else
3701#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003702 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003703 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003704 type = VAR_BOOL;
3705 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706 }
3707 else if (*end_leader == '-')
3708 {
3709#ifdef FEAT_FLOAT
3710 if (rettv->v_type == VAR_FLOAT)
3711 f = -f;
3712 else
3713#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003714 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003715 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003716 type = VAR_NUMBER;
3717 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003718 }
3719 }
3720#ifdef FEAT_FLOAT
3721 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003723 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003724 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003728 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003729 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003730 if (in_vim9script())
3731 rettv->v_type = type;
3732 else
3733 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003734 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 return ret;
3739}
3740
3741/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003742 * Call the function referred to in "rettv".
3743 */
3744 static int
3745call_func_rettv(
3746 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003747 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003748 typval_T *rettv,
3749 int evaluate,
3750 dict_T *selfdict,
3751 typval_T *basetv)
3752{
3753 partial_T *pt = NULL;
3754 funcexe_T funcexe;
3755 typval_T functv;
3756 char_u *s;
3757 int ret;
3758
3759 // need to copy the funcref so that we can clear rettv
3760 if (evaluate)
3761 {
3762 functv = *rettv;
3763 rettv->v_type = VAR_UNKNOWN;
3764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003765 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003766 if (functv.v_type == VAR_PARTIAL)
3767 {
3768 pt = functv.vval.v_partial;
3769 s = partial_name(pt);
3770 }
3771 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003772 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003773 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003774 if (s == NULL || *s == NUL)
3775 {
3776 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003777 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003778 goto theend;
3779 }
3780 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003781 }
3782 else
3783 s = (char_u *)"";
3784
Bram Moolenaara80faa82020-04-12 19:37:17 +02003785 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003786 funcexe.firstline = curwin->w_cursor.lnum;
3787 funcexe.lastline = curwin->w_cursor.lnum;
3788 funcexe.evaluate = evaluate;
3789 funcexe.partial = pt;
3790 funcexe.selfdict = selfdict;
3791 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003792 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003793
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003794theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003795 // Clear the funcref afterwards, so that deleting it while
3796 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003797 if (evaluate)
3798 clear_tv(&functv);
3799
3800 return ret;
3801}
3802
3803/*
3804 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003805 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003806 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3807 */
3808 static int
3809eval_lambda(
3810 char_u **arg,
3811 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003812 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003813 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003815 int evaluate = evalarg != NULL
3816 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003817 typval_T base = *rettv;
3818 int ret;
3819
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 rettv->v_type = VAR_UNKNOWN;
3821
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003822 if (**arg == '{')
3823 {
3824 // ->{lambda}()
3825 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3826 }
3827 else
3828 {
3829 // ->(lambda)()
3830 ++*arg;
3831 ret = eval1(arg, rettv, evalarg);
3832 *arg = skipwhite_and_linebreak(*arg, evalarg);
3833 if (**arg != ')')
3834 {
3835 emsg(_(e_missing_close));
3836 ret = FAIL;
3837 }
3838 ++*arg;
3839 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003840 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003841 return FAIL;
3842 else if (**arg != '(')
3843 {
3844 if (verbose)
3845 {
3846 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003847 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003848 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003850 }
3851 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003852 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003853 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003854 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003855 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003856
3857 // Clear the funcref afterwards, so that deleting it while
3858 // evaluating the arguments is possible (see test55).
3859 if (evaluate)
3860 clear_tv(&base);
3861
3862 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003863}
3864
3865/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003866 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003867 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003868 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3869 */
3870 static int
3871eval_method(
3872 char_u **arg,
3873 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003874 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003875 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003876{
3877 char_u *name;
3878 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003879 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003880 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003881 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003882 int evaluate = evalarg != NULL
3883 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003884
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003885 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003886
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003888 len = get_name_len(arg, &alias, evaluate, TRUE);
3889 if (alias != NULL)
3890 name = alias;
3891
3892 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003893 {
3894 if (verbose)
3895 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003896 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003898 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003899 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003900 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003901 if (**arg != '(')
3902 {
3903 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003905 ret = FAIL;
3906 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003907 else if (VIM_ISWHITE((*arg)[-1]))
3908 {
3909 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003910 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003911 ret = FAIL;
3912 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003913 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003914 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003915 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003916 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003917
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003918 // Clear the funcref afterwards, so that deleting it while
3919 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920 if (evaluate)
3921 clear_tv(&base);
3922
Bram Moolenaarac92e252019-08-03 21:58:38 +02003923 return ret;
3924}
3925
3926/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003927 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3928 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003929 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3930 */
3931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932eval_index(
3933 char_u **arg,
3934 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003935 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003936 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003937{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003938 int evaluate = evalarg != NULL
3939 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003942 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003943 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003944 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003945 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003946
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003947 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3948 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003949
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003950 init_tv(&var1);
3951 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003952 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003954 /*
3955 * dict.name
3956 */
3957 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003958 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003960 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003962 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003963 }
3964 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003966 /*
3967 * something[idx]
3968 *
3969 * Get the (first) variable from inside the [].
3970 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003971 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003972 if (**arg == ':')
3973 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003974 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003975 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003976 else if (vim9 && **arg == ':')
3977 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003978 semsg(_(e_white_space_required_before_and_after_str_at_str),
3979 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003980 clear_tv(&var1);
3981 return FAIL;
3982 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003983 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003985#ifdef FEAT_FLOAT
3986 // allow for indexing with float
3987 if (vim9 && rettv->v_type == VAR_DICT
3988 && var1.v_type == VAR_FLOAT)
3989 {
3990 var1.vval.v_string = typval_tostring(&var1, TRUE);
3991 var1.v_type = VAR_STRING;
3992 }
3993#endif
3994 if (tv_get_string_chk(&var1) == NULL)
3995 {
3996 // not a number or string
3997 clear_tv(&var1);
3998 return FAIL;
3999 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004000 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004001
4002 /*
4003 * Get the second variable from inside the [:].
4004 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004005 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004006 if (**arg == ':')
4007 {
4008 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004009 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004010 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004011 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004012 semsg(_(e_white_space_required_before_and_after_str_at_str),
4013 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004014 if (!empty1)
4015 clear_tv(&var1);
4016 return FAIL;
4017 }
4018 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019 if (**arg == ']')
4020 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004021 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004023 if (!empty1)
4024 clear_tv(&var1);
4025 return FAIL;
4026 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004027 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004028 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004030 if (!empty1)
4031 clear_tv(&var1);
4032 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 return FAIL;
4034 }
4035 }
4036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004038 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004039 if (**arg != ']')
4040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004041 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004042 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 clear_tv(&var1);
4044 if (range)
4045 clear_tv(&var2);
4046 return FAIL;
4047 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004048 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004049 }
4050
4051 if (evaluate)
4052 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004054 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004055 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004056
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004057 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004059 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004060 clear_tv(&var2);
4061 return res;
4062 }
4063 return OK;
4064}
4065
4066/*
4067 * Check if "rettv" can have an [index] or [sli:ce]
4068 */
4069 int
4070check_can_index(typval_T *rettv, int evaluate, int verbose)
4071{
4072 switch (rettv->v_type)
4073 {
4074 case VAR_FUNC:
4075 case VAR_PARTIAL:
4076 if (verbose)
4077 emsg(_("E695: Cannot index a Funcref"));
4078 return FAIL;
4079 case VAR_FLOAT:
4080#ifdef FEAT_FLOAT
4081 if (verbose)
4082 emsg(_(e_float_as_string));
4083 return FAIL;
4084#endif
4085 case VAR_BOOL:
4086 case VAR_SPECIAL:
4087 case VAR_JOB:
4088 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004089 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004090 if (verbose)
4091 emsg(_(e_cannot_index_special_variable));
4092 return FAIL;
4093 case VAR_UNKNOWN:
4094 case VAR_ANY:
4095 case VAR_VOID:
4096 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004098 emsg(_(e_cannot_index_special_variable));
4099 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004102
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004103 case VAR_STRING:
4104 case VAR_LIST:
4105 case VAR_DICT:
4106 case VAR_BLOB:
4107 break;
4108 case VAR_NUMBER:
4109 if (in_vim9script())
4110 emsg(_(e_cannot_index_number));
4111 break;
4112 }
4113 return OK;
4114}
4115
4116/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004117 * slice() function
4118 */
4119 void
4120f_slice(typval_T *argvars, typval_T *rettv)
4121{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004122 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004123 && ((argvars[0].v_type != VAR_STRING
4124 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004125 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004126 && check_for_list_arg(argvars, 0) == FAIL)
4127 || check_for_number_arg(argvars, 1) == FAIL
4128 || check_for_opt_number_arg(argvars, 2) == FAIL))
4129 return;
4130
Bram Moolenaar6601b622021-01-13 21:47:15 +01004131 if (check_can_index(argvars, TRUE, FALSE) == OK)
4132 {
4133 copy_tv(argvars, rettv);
4134 eval_index_inner(rettv, TRUE, argvars + 1,
4135 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4136 TRUE, NULL, 0, FALSE);
4137 }
4138}
4139
4140/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004141 * Apply index or range to "rettv".
4142 * "var1" is the first index, NULL for [:expr].
4143 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004144 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4145 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4147 */
4148 int
4149eval_index_inner(
4150 typval_T *rettv,
4151 int is_range,
4152 typval_T *var1,
4153 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004154 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 char_u *key,
4156 int keylen,
4157 int verbose)
4158{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004159 varnumber_T n1, n2 = 0;
4160 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004161
4162 n1 = 0;
4163 if (var1 != NULL && rettv->v_type != VAR_DICT)
4164 n1 = tv_get_number(var1);
4165
4166 if (is_range)
4167 {
4168 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 if (verbose)
4171 emsg(_(e_cannot_slice_dictionary));
4172 return FAIL;
4173 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004174 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004176 else
4177 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004179
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004180 switch (rettv->v_type)
4181 {
4182 case VAR_UNKNOWN:
4183 case VAR_ANY:
4184 case VAR_VOID:
4185 case VAR_FUNC:
4186 case VAR_PARTIAL:
4187 case VAR_FLOAT:
4188 case VAR_BOOL:
4189 case VAR_SPECIAL:
4190 case VAR_JOB:
4191 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004192 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004193 break; // not evaluating, skipping over subscript
4194
4195 case VAR_NUMBER:
4196 case VAR_STRING:
4197 {
4198 char_u *s = tv_get_string(rettv);
4199
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004200 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004201 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004202 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004203 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004204 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004205 else
4206 s = char_from_string(s, n1);
4207 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004208 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004210 // The resulting variable is a substring. If the indexes
4211 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 if (n1 < 0)
4213 {
4214 n1 = len + n1;
4215 if (n1 < 0)
4216 n1 = 0;
4217 }
4218 if (n2 < 0)
4219 n2 = len + n2;
4220 else if (n2 >= len)
4221 n2 = len;
4222 if (n1 >= len || n2 < 0 || n1 > n2)
4223 s = NULL;
4224 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004225 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004226 }
4227 else
4228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004229 // The resulting variable is a string of a single
4230 // character. If the index is too big or negative the
4231 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 if (n1 >= len || n1 < 0)
4233 s = NULL;
4234 else
4235 s = vim_strnsave(s + n1, 1);
4236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 clear_tv(rettv);
4238 rettv->v_type = VAR_STRING;
4239 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004240 }
4241 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004242
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004243 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004244 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4245 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004246 break;
4247
4248 case VAR_LIST:
4249 if (var1 == NULL)
4250 n1 = 0;
4251 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004252 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004254 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 return FAIL;
4256 break;
4257
4258 case VAR_DICT:
4259 {
4260 dictitem_T *item;
4261 typval_T tmp;
4262
4263 if (key == NULL)
4264 {
4265 key = tv_get_string_chk(var1);
4266 if (key == NULL)
4267 return FAIL;
4268 }
4269
4270 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4271
4272 if (item == NULL && verbose)
4273 semsg(_(e_dictkey), key);
4274 if (item == NULL)
4275 return FAIL;
4276
4277 copy_tv(&item->di_tv, &tmp);
4278 clear_tv(rettv);
4279 *rettv = tmp;
4280 }
4281 break;
4282 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283 return OK;
4284}
4285
4286/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004287 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 */
4289 char_u *
4290partial_name(partial_T *pt)
4291{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004292 if (pt != NULL)
4293 {
4294 if (pt->pt_name != NULL)
4295 return pt->pt_name;
4296 if (pt->pt_func != NULL)
4297 return pt->pt_func->uf_name;
4298 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004299 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300}
4301
Bram Moolenaarddecc252016-04-06 22:59:37 +02004302 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004303partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004304{
4305 int i;
4306
4307 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004308 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004309 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004310 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 if (pt->pt_name != NULL)
4312 {
4313 func_unref(pt->pt_name);
4314 vim_free(pt->pt_name);
4315 }
4316 else
4317 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004318
Bram Moolenaar54656012021-06-09 20:50:46 +02004319 // "out_up" is no longer used, decrement refcount on partial that owns it.
4320 partial_unref(pt->pt_outer.out_up_partial);
4321
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004322 // Decrease the reference count for the context of a closure. If down
4323 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004324 if (pt->pt_funcstack != NULL)
4325 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004326 --pt->pt_funcstack->fs_refcount;
4327 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004328 }
4329
Bram Moolenaarddecc252016-04-06 22:59:37 +02004330 vim_free(pt);
4331}
4332
4333/*
4334 * Unreference a closure: decrement the reference count and free it when it
4335 * becomes zero.
4336 */
4337 void
4338partial_unref(partial_T *pt)
4339{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004340 if (pt != NULL)
4341 {
4342 if (--pt->pt_refcount <= 0)
4343 partial_free(pt);
4344
4345 // If the reference count goes down to one, the funcstack may be the
4346 // only reference and can be freed if no other partials reference it.
4347 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4348 funcstack_check_refcount(pt->pt_funcstack);
4349 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004350}
4351
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004352/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004353 * Return the next (unique) copy ID.
4354 * Used for serializing nested structures.
4355 */
4356 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004357get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004358{
4359 current_copyID += COPYID_INC;
4360 return current_copyID;
4361}
4362
4363/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004364 * Garbage collection for lists and dictionaries.
4365 *
4366 * We use reference counts to be able to free most items right away when they
4367 * are no longer used. But for composite items it's possible that it becomes
4368 * unused while the reference count is > 0: When there is a recursive
4369 * reference. Example:
4370 * :let l = [1, 2, 3]
4371 * :let d = {9: l}
4372 * :let l[1] = d
4373 *
4374 * Since this is quite unusual we handle this with garbage collection: every
4375 * once in a while find out which lists and dicts are not referenced from any
4376 * variable.
4377 *
4378 * Here is a good reference text about garbage collection (refers to Python
4379 * but it applies to all reference-counting mechanisms):
4380 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004381 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004382
4383/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004384 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004385 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004386 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004387 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004388 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004389garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004391 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004392 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004393 buf_T *buf;
4394 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004395 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004396 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004397
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004398 if (!testing)
4399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004400 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004401 want_garbage_collect = FALSE;
4402 may_garbage_collect = FALSE;
4403 garbage_collect_at_exit = FALSE;
4404 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004405
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004406 // The execution stack can grow big, limit the size.
4407 if (exestack.ga_maxlen - exestack.ga_len > 500)
4408 {
4409 size_t new_len;
4410 char_u *pp;
4411 int n;
4412
4413 // Keep 150% of the current size, with a minimum of the growth size.
4414 n = exestack.ga_len / 2;
4415 if (n < exestack.ga_growsize)
4416 n = exestack.ga_growsize;
4417
4418 // Don't make it bigger though.
4419 if (exestack.ga_len + n < exestack.ga_maxlen)
4420 {
4421 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4422 pp = vim_realloc(exestack.ga_data, new_len);
4423 if (pp == NULL)
4424 return FAIL;
4425 exestack.ga_maxlen = exestack.ga_len + n;
4426 exestack.ga_data = pp;
4427 }
4428 }
4429
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004430 // We advance by two because we add one for items referenced through
4431 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004432 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004433
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004434 /*
4435 * 1. Go through all accessible variables and mark all lists and dicts
4436 * with copyID.
4437 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // Don't free variables in the previous_funccal list unless they are only
4440 // referenced through previous_funccal. This must be first, because if
4441 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004445 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004448 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4450 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004453 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004454 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4455 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004456 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4458 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004459#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004460 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004461 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4462 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004463 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004464 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004465 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4466 NULL, NULL);
4467#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004470 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4472 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004474 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004480 abort = abort || set_ref_in_functions(copyID);
4481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004486 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004487
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004488 // callbacks in buffers
4489 abort = abort || set_ref_in_buffers(copyID);
4490
Bram Moolenaar1dced572012-04-05 16:54:08 +02004491#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004492 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004493#endif
4494
Bram Moolenaardb913952012-06-29 12:54:53 +02004495#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004496 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004497#endif
4498
4499#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004501#endif
4502
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004504 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004505 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004506#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004507#ifdef FEAT_NETBEANS_INTG
4508 abort = abort || set_ref_in_nb_channel(copyID);
4509#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004510
Bram Moolenaare3188e22016-05-31 21:13:04 +02004511#ifdef FEAT_TIMERS
4512 abort = abort || set_ref_in_timer(copyID);
4513#endif
4514
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004515#ifdef FEAT_QUICKFIX
4516 abort = abort || set_ref_in_quickfix(copyID);
4517#endif
4518
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004519#ifdef FEAT_TERMINAL
4520 abort = abort || set_ref_in_term(copyID);
4521#endif
4522
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004523#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004524 abort = abort || set_ref_in_popups(copyID);
4525#endif
4526
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004528 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 /*
4530 * 2. Free lists and dictionaries that are not referenced.
4531 */
4532 did_free = free_unref_items(copyID);
4533
4534 /*
4535 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004536 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004539 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 else if (p_verbose > 0)
4541 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004544
4545 return did_free;
4546}
4547
4548/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004549 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004550 */
4551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004552free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004553{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004554 int did_free = FALSE;
4555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // Let all "free" functions know that we are here. This means no
4557 // dictionaries, lists, channels or jobs are to be freed, because we will
4558 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004559 in_free_unref_items = TRUE;
4560
4561 /*
4562 * PASS 1: free the contents of the items. We don't free the items
4563 * themselves yet, so that it is possible to decrement refcount counters
4564 */
4565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004567 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004570 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571
4572#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004573 // Go through the list of jobs and free items without the copyID. This
4574 // must happen before doing channels, because jobs refer to channels, but
4575 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4580#endif
4581
4582 /*
4583 * PASS 2: free the items themselves.
4584 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004585 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004586 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004587
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 // Go through the list of jobs and free items without the copyID. This
4590 // must happen before doing channels, because jobs refer to channels, but
4591 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004592 free_unused_jobs(copyID, COPYID_MASK);
4593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004595 free_unused_channels(copyID, COPYID_MASK);
4596#endif
4597
4598 in_free_unref_items = FALSE;
4599
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600 return did_free;
4601}
4602
4603/*
4604 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 * "list_stack" is used to add lists to be marked. Can be NULL.
4606 *
4607 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004610set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611{
4612 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004613 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 hashtab_T *cur_ht;
4616 ht_stack_T *ht_stack = NULL;
4617 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004618
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 cur_ht = ht;
4620 for (;;)
4621 {
4622 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // Mark each item in the hashtab. If the item contains a hashtab
4625 // it is added to ht_stack, if it contains a list it is added to
4626 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 todo = (int)cur_ht->ht_used;
4628 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4629 if (!HASHITEM_EMPTY(hi))
4630 {
4631 --todo;
4632 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4633 &ht_stack, list_stack);
4634 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004635 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004636
4637 if (ht_stack == NULL)
4638 break;
4639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004641 cur_ht = ht_stack->ht;
4642 tempitem = ht_stack;
4643 ht_stack = ht_stack->prev;
4644 free(tempitem);
4645 }
4646
4647 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648}
4649
4650/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004651 * Mark a dict and its items with "copyID".
4652 * Returns TRUE if setting references failed somehow.
4653 */
4654 int
4655set_ref_in_dict(dict_T *d, int copyID)
4656{
4657 if (d != NULL && d->dv_copyID != copyID)
4658 {
4659 d->dv_copyID = copyID;
4660 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4661 }
4662 return FALSE;
4663}
4664
4665/*
4666 * Mark a list and its items with "copyID".
4667 * Returns TRUE if setting references failed somehow.
4668 */
4669 int
4670set_ref_in_list(list_T *ll, int copyID)
4671{
4672 if (ll != NULL && ll->lv_copyID != copyID)
4673 {
4674 ll->lv_copyID = copyID;
4675 return set_ref_in_list_items(ll, copyID, NULL);
4676 }
4677 return FALSE;
4678}
4679
4680/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004681 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004682 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4683 *
4684 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004685 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004686 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004687set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 listitem_T *li;
4690 int abort = FALSE;
4691 list_T *cur_l;
4692 list_stack_T *list_stack = NULL;
4693 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004694
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004695 cur_l = l;
4696 for (;;)
4697 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004698 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004699 // Mark each item in the list. If the item contains a hashtab
4700 // it is added to ht_stack, if it contains a list it is added to
4701 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004702 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4703 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4704 ht_stack, &list_stack);
4705 if (list_stack == NULL)
4706 break;
4707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004709 cur_l = list_stack->list;
4710 tempitem = list_stack;
4711 list_stack = list_stack->prev;
4712 free(tempitem);
4713 }
4714
4715 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716}
4717
4718/*
4719 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004720 * "list_stack" is used to add lists to be marked. Can be NULL.
4721 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4722 *
4723 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004724 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726set_ref_in_item(
4727 typval_T *tv,
4728 int copyID,
4729 ht_stack_T **ht_stack,
4730 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004731{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004732 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004733
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004734 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004735 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004736 dict_T *dd = tv->vval.v_dict;
4737
Bram Moolenaara03f2332016-02-06 18:09:59 +01004738 if (dd != NULL && dd->dv_copyID != copyID)
4739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004741 dd->dv_copyID = copyID;
4742 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004743 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4745 }
4746 else
4747 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004748 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4749
Bram Moolenaara03f2332016-02-06 18:09:59 +01004750 if (newitem == NULL)
4751 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004752 else
4753 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004754 newitem->ht = &dd->dv_hashtab;
4755 newitem->prev = *ht_stack;
4756 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004757 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004758 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004759 }
4760 }
4761 else if (tv->v_type == VAR_LIST)
4762 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004763 list_T *ll = tv->vval.v_list;
4764
Bram Moolenaara03f2332016-02-06 18:09:59 +01004765 if (ll != NULL && ll->lv_copyID != copyID)
4766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 ll->lv_copyID = copyID;
4769 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004770 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004771 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004772 }
4773 else
4774 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004775 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4776
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 if (newitem == NULL)
4778 abort = TRUE;
4779 else
4780 {
4781 newitem->list = ll;
4782 newitem->prev = *list_stack;
4783 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004784 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004785 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004786 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004787 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004788 else if (tv->v_type == VAR_FUNC)
4789 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004790 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004791 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 else if (tv->v_type == VAR_PARTIAL)
4793 {
4794 partial_T *pt = tv->vval.v_partial;
4795 int i;
4796
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004797 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004799 // Didn't see this partial yet.
4800 pt->pt_copyID = copyID;
4801
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004802 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004803
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 if (pt->pt_dict != NULL)
4805 {
4806 typval_T dtv;
4807
4808 dtv.v_type = VAR_DICT;
4809 dtv.vval.v_dict = pt->pt_dict;
4810 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4811 }
4812
4813 for (i = 0; i < pt->pt_argc; ++i)
4814 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4815 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004816 if (pt->pt_funcstack != NULL)
4817 {
4818 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4819
4820 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4821 abort = abort || set_ref_in_item(stack + i, copyID,
4822 ht_stack, list_stack);
4823 }
4824
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004825 }
4826 }
4827#ifdef FEAT_JOB_CHANNEL
4828 else if (tv->v_type == VAR_JOB)
4829 {
4830 job_T *job = tv->vval.v_job;
4831 typval_T dtv;
4832
4833 if (job != NULL && job->jv_copyID != copyID)
4834 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004835 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004836 if (job->jv_channel != NULL)
4837 {
4838 dtv.v_type = VAR_CHANNEL;
4839 dtv.vval.v_channel = job->jv_channel;
4840 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4841 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004842 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 {
4844 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004845 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4847 }
4848 }
4849 }
4850 else if (tv->v_type == VAR_CHANNEL)
4851 {
4852 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004853 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 typval_T dtv;
4855 jsonq_T *jq;
4856 cbq_T *cq;
4857
4858 if (ch != NULL && ch->ch_copyID != copyID)
4859 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004860 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004861 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004862 {
4863 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4864 jq = jq->jq_next)
4865 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4866 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4867 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004868 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 {
4870 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004872 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4873 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 {
4876 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 dtv.vval.v_partial =
4878 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004879 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4880 }
4881 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004882 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004883 {
4884 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4887 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 {
4890 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4893 }
4894 }
4895 }
4896#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004897 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004898}
4899
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 * Return a string with the string representation of a variable.
4902 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004903 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004904 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004905 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004906 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004907 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004908 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4909 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004910 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004912 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004913echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914 typval_T *tv,
4915 char_u **tofree,
4916 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004917 int copyID,
4918 int echo_style,
4919 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004920 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004922 static int recurse = 0;
4923 char_u *r = NULL;
4924
Bram Moolenaar33570922005-01-25 22:26:29 +00004925 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004926 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004927 if (!did_echo_string_emsg)
4928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 // Only give this message once for a recursive call to avoid
4930 // flooding the user with errors. And stop iterating over lists
4931 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004932 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004933 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004936 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004937 }
4938 ++recurse;
4939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 switch (tv->v_type)
4941 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004942 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004943 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004944 {
4945 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004946 r = tv->vval.v_string;
4947 if (r == NULL)
4948 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004949 }
4950 else
4951 {
4952 *tofree = string_quote(tv->vval.v_string, FALSE);
4953 r = *tofree;
4954 }
4955 break;
4956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 if (echo_style)
4959 {
4960 *tofree = NULL;
4961 r = tv->vval.v_string;
4962 }
4963 else
4964 {
4965 *tofree = string_quote(tv->vval.v_string, TRUE);
4966 r = *tofree;
4967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004968 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004970 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004971 {
4972 partial_T *pt = tv->vval.v_partial;
4973 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004974 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004975 garray_T ga;
4976 int i;
4977 char_u *tf;
4978
4979 ga_init2(&ga, 1, 100);
4980 ga_concat(&ga, (char_u *)"function(");
4981 if (fname != NULL)
4982 {
4983 ga_concat(&ga, fname);
4984 vim_free(fname);
4985 }
4986 if (pt != NULL && pt->pt_argc > 0)
4987 {
4988 ga_concat(&ga, (char_u *)", [");
4989 for (i = 0; i < pt->pt_argc; ++i)
4990 {
4991 if (i > 0)
4992 ga_concat(&ga, (char_u *)", ");
4993 ga_concat(&ga,
4994 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4995 vim_free(tf);
4996 }
4997 ga_concat(&ga, (char_u *)"]");
4998 }
4999 if (pt != NULL && pt->pt_dict != NULL)
5000 {
5001 typval_T dtv;
5002
5003 ga_concat(&ga, (char_u *)", ");
5004 dtv.v_type = VAR_DICT;
5005 dtv.vval.v_dict = pt->pt_dict;
5006 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5007 vim_free(tf);
5008 }
5009 ga_concat(&ga, (char_u *)")");
5010
5011 *tofree = ga.ga_data;
5012 r = *tofree;
5013 break;
5014 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005015
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005016 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005017 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005018 break;
5019
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005021 if (tv->vval.v_list == NULL)
5022 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005023 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005024 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005025 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005026 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005027 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5028 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 {
5030 *tofree = NULL;
5031 r = (char_u *)"[...]";
5032 }
5033 else
5034 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005035 int old_copyID = tv->vval.v_list->lv_copyID;
5036
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005037 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 *tofree = list2string(tv, copyID, restore_copyID);
5039 if (restore_copyID)
5040 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041 r = *tofree;
5042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005043 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005044
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005046 if (tv->vval.v_dict == NULL)
5047 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005048 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005050 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005051 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005052 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5053 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 {
5055 *tofree = NULL;
5056 r = (char_u *)"{...}";
5057 }
5058 else
5059 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005060 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005062 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005063 *tofree = dict2string(tv, copyID, restore_copyID);
5064 if (restore_copyID)
5065 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005066 r = *tofree;
5067 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005068 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005071 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005072 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005074 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005075 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005076 break;
5077
Bram Moolenaar835dc632016-02-07 14:27:38 +01005078 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005079 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005080#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005082 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5083 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005084 if (composite_val)
5085 {
5086 *tofree = string_quote(r, FALSE);
5087 r = *tofree;
5088 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005089#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005091
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005092 case VAR_INSTR:
5093 *tofree = NULL;
5094 r = (char_u *)"instructions";
5095 break;
5096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005098#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 *tofree = NULL;
5100 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5101 r = numbuf;
5102 break;
5103#endif
5104
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005105 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005106 case VAR_SPECIAL:
5107 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005108 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005109 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111
Bram Moolenaar8502c702014-06-17 12:51:16 +02005112 if (--recurse == 0)
5113 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115}
5116
5117/*
5118 * Return a string with the string representation of a variable.
5119 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5120 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005121 * Does not put quotes around strings, as ":echo" displays values.
5122 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5123 * May return NULL.
5124 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005125 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005126echo_string(
5127 typval_T *tv,
5128 char_u **tofree,
5129 char_u *numbuf,
5130 int copyID)
5131{
5132 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5133}
5134
5135/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005136 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5137 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005138 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005139 */
5140 int
5141buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5142{
5143 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005144 char_u *t;
5145 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005146
5147 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5148 return -1;
5149
5150 if (lnum > buf->b_ml.ml_line_count)
5151 lnum = buf->b_ml.ml_line_count;
5152
5153 str = ml_get_buf(buf, lnum, FALSE);
5154 if (str == NULL)
5155 return -1;
5156
5157 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005158 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005159
Bram Moolenaar91458462021-01-13 20:08:38 +01005160 // count the number of characters
5161 t = str;
5162 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5163 t += mb_ptr2len(t);
5164
5165 // In insert mode, when the cursor is at the end of a non-empty line,
5166 // byteidx points to the NUL character immediately past the end of the
5167 // string. In this case, add one to the character count.
5168 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5169 count++;
5170
5171 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005172}
5173
5174/*
5175 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005176 * byte index. Works only for loaded buffers. Returns -1 on failure.
5177 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005178 */
5179 int
5180buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5181{
5182 char_u *str;
5183 char_u *t;
5184
5185 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5186 return -1;
5187
5188 if (lnum > buf->b_ml.ml_line_count)
5189 lnum = buf->b_ml.ml_line_count;
5190
5191 str = ml_get_buf(buf, lnum, FALSE);
5192 if (str == NULL)
5193 return -1;
5194
5195 // Convert the character offset to a byte offset
5196 t = str;
5197 while (*t != NUL && --charidx > 0)
5198 t += mb_ptr2len(t);
5199
Bram Moolenaar91458462021-01-13 20:08:38 +01005200 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005201}
5202
5203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005205 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005207 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005208var2fpos(
5209 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005211 int *fnum, // set to fnum for '0, 'A, etc.
5212 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005214 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005216 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005219 if (varp->v_type == VAR_LIST)
5220 {
5221 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005222 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005223 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005224 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005225
5226 l = varp->vval.v_list;
5227 if (l == NULL)
5228 return NULL;
5229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005231 pos.lnum = list_find_nr(l, 0L, &error);
5232 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005234 if (charcol)
5235 len = (long)mb_charlen(ml_get(pos.lnum));
5236 else
5237 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005238
Bram Moolenaarec65d772020-08-20 22:29:12 +02005239 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005241 li = list_find(l, 1L);
5242 if (li != NULL && li->li_tv.v_type == VAR_STRING
5243 && li->li_tv.vval.v_string != NULL
5244 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005245 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005246 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005247 }
5248 else
5249 {
5250 pos.col = list_find_nr(l, 1L, &error);
5251 if (error)
5252 return NULL;
5253 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005256 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005258 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005261 pos.coladd = list_find_nr(l, 2L, &error);
5262 if (error)
5263 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005264
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005265 return &pos;
5266 }
5267
Bram Moolenaarc5809432021-03-27 21:23:30 +01005268 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5269 return NULL;
5270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005271 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005272 if (name == NULL)
5273 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005274 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005275 {
5276 pos = curwin->w_cursor;
5277 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005279 return &pos;
5280 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005282 {
5283 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005284 pos = VIsual;
5285 else
5286 pos = curwin->w_cursor;
5287 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005288 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005290 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005293 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5295 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005296 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005297 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 return pp;
5299 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005300
Bram Moolenaara5525202006-03-02 22:52:09 +00005301 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005302
Bram Moolenaar477933c2007-07-17 14:32:23 +00005303 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005304 {
5305 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005306 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005307 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005308 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // In silent Ex mode topline is zero, but that's not a valid line
5310 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005311 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005312 return &pos;
5313 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005315 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005318 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005319 return &pos;
5320 }
5321 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005324 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 pos.lnum = curbuf->b_ml.ml_line_count;
5327 pos.col = 0;
5328 }
5329 else
5330 {
5331 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005332 if (charcol)
5333 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5334 else
5335 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 return &pos;
5338 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005339 if (in_vim9script())
5340 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 return NULL;
5342}
5343
5344/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005345 * Convert list in "arg" into a position and optional file number.
5346 * When "fnump" is NULL there is no file number, only 3 items.
5347 * Note that the column is passed on as-is, the caller may want to decrement
5348 * it to use 1 for the first column.
5349 * Return FAIL when conversion is not possible, doesn't check the position for
5350 * validity.
5351 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005353list2fpos(
5354 typval_T *arg,
5355 pos_T *posp,
5356 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 colnr_T *curswantp,
5358 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005359{
5360 list_T *l = arg->vval.v_list;
5361 long i = 0;
5362 long n;
5363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5365 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005366 if (arg->v_type != VAR_LIST
5367 || l == NULL
5368 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005369 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005370 return FAIL;
5371
5372 if (fnump != NULL)
5373 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 if (n < 0)
5376 return FAIL;
5377 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 *fnump = n;
5380 }
5381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383 if (n < 0)
5384 return FAIL;
5385 posp->lnum = n;
5386
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005388 if (n < 0)
5389 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005390 // If character position is specified, then convert to byte position
5391 if (charcol)
5392 {
5393 buf_T *buf;
5394
5395 // Get the text for the specified line in a loaded buffer
5396 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5397 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5398 return FAIL;
5399
Bram Moolenaar91458462021-01-13 20:08:38 +01005400 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005401 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005402 posp->col = n;
5403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005406 posp->coladd = 0;
5407 else
5408 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005409
Bram Moolenaar493c1782014-05-28 14:34:46 +02005410 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005411 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005412
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005413 return OK;
5414}
5415
5416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 * Get the length of an environment variable name.
5418 * Advance "arg" to the first character after the name.
5419 * Return 0 for error.
5420 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 char_u *p;
5425 int len;
5426
5427 for (p = *arg; vim_isIDc(*p); ++p)
5428 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return 0;
5431
5432 len = (int)(p - *arg);
5433 *arg = p;
5434 return len;
5435}
5436
5437/*
5438 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005439 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 * Return 0 if something is wrong.
5441 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005443get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 char_u *p;
5446 int len;
5447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005448 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005450 {
5451 if (*p == ':')
5452 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // "s:" is start of "s:var", but "n:" is not and can be used in
5454 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005455 len = (int)(p - *arg);
5456 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5457 || len > 1)
5458 break;
5459 }
5460 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return 0;
5463
5464 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005465 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 return len;
5468}
5469
5470/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005471 * Get the length of the name of a variable or function.
5472 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474 * Return -1 if curly braces expansion failed.
5475 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * If the name contains 'magic' {}'s, expand them and return the
5477 * expanded name in an allocated string via 'alias' - caller must free.
5478 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480get_name_len(
5481 char_u **arg,
5482 char_u **alias,
5483 int evaluate,
5484 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 char_u *p;
5488 char_u *expr_start;
5489 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5494 && (*arg)[2] == (int)KE_SNR)
5495 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005496 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 *arg += 3;
5498 return get_id_len(arg) + 3;
5499 }
5500 len = eval_fname_script(*arg);
5501 if (len > 0)
5502 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005503 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 *arg += len;
5505 }
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005510 p = find_name_end(*arg, &expr_start, &expr_end,
5511 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (expr_start != NULL)
5513 {
5514 char_u *temp_string;
5515
5516 if (!evaluate)
5517 {
5518 len += (int)(p - *arg);
5519 *arg = skipwhite(p);
5520 return len;
5521 }
5522
5523 /*
5524 * Include any <SID> etc in the expanded string:
5525 * Thus the -len here.
5526 */
5527 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5528 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005529 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 *alias = temp_string;
5531 *arg = skipwhite(p);
5532 return (int)STRLEN(temp_string);
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005536 // Only give an error when there is something, otherwise it will be
5537 // reported at a higher level.
5538 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005539 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
5541 return len;
5542}
5543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544/*
5545 * Find the end of a variable or function name, taking care of magic braces.
5546 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5547 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005548 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 * Return a pointer to just after the name. Equal to "arg" if there is no
5550 * valid name.
5551 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553find_name_end(
5554 char_u *arg,
5555 char_u **expr_start,
5556 char_u **expr_end,
5557 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 int mb_nest = 0;
5560 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005562 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005563 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 *expr_start = NULL;
5568 *expr_end = NULL;
5569 }
5570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005572 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5573 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005574 return arg;
5575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 for (p = arg; *p != NUL
5577 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005578 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005579 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005580 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005582 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584 if (*p == '\'')
5585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005587 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005588 ;
5589 if (*p == NUL)
5590 break;
5591 }
5592 else if (*p == '"')
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005595 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005596 if (*p == '\\' && p[1] != NUL)
5597 ++p;
5598 if (*p == NUL)
5599 break;
5600 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005601 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5602 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005603 // "s:" is start of "s:var", but "n:" is not and can be used in
5604 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005605 len = (int)(p - arg);
5606 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005607 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005608 break;
5609 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005610
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 if (*p == '[')
5614 ++br_nest;
5615 else if (*p == ']')
5616 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005618
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005619 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 if (*p == '{')
5622 {
5623 mb_nest++;
5624 if (expr_start != NULL && *expr_start == NULL)
5625 *expr_start = p;
5626 }
5627 else if (*p == '}')
5628 {
5629 mb_nest--;
5630 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5631 *expr_end = p;
5632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 return p;
5637}
5638
5639/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005640 * Expands out the 'magic' {}'s in a variable/function name.
5641 * Note that this can call itself recursively, to deal with
5642 * constructs like foo{bar}{baz}{bam}
5643 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5644 * "in_start" ^
5645 * "expr_start" ^
5646 * "expr_end" ^
5647 * "in_end" ^
5648 *
5649 * Returns a new allocated string, which the caller must free.
5650 * Returns NULL for failure.
5651 */
5652 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005653make_expanded_name(
5654 char_u *in_start,
5655 char_u *expr_start,
5656 char_u *expr_end,
5657 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005658{
5659 char_u c1;
5660 char_u *retval = NULL;
5661 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005662
5663 if (expr_end == NULL || in_end == NULL)
5664 return NULL;
5665 *expr_start = NUL;
5666 *expr_end = NUL;
5667 c1 = *in_end;
5668 *in_end = NUL;
5669
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005670 temp_result = eval_to_string(expr_start + 1, FALSE);
5671 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005672 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005673 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5674 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005675 if (retval != NULL)
5676 {
5677 STRCPY(retval, in_start);
5678 STRCAT(retval, temp_result);
5679 STRCAT(retval, expr_end + 1);
5680 }
5681 }
5682 vim_free(temp_result);
5683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 *expr_start = '{';
5686 *expr_end = '}';
5687
5688 if (retval != NULL)
5689 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005690 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005691 if (expr_start != NULL)
5692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005693 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005694 temp_result = make_expanded_name(retval, expr_start,
5695 expr_end, temp_result);
5696 vim_free(retval);
5697 retval = temp_result;
5698 }
5699 }
5700
5701 return retval;
5702}
5703
5704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005706 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005709eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005711 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005712}
5713
5714/*
5715 * Return TRUE if character "c" can be used as the first character in a
5716 * variable or function name (excluding '{' and '}').
5717 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005720{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005721 return ASCII_ISALPHA(c) || c == '_';
5722}
5723
5724/*
5725 * Return TRUE if character "c" can be used as the first character of a
5726 * dictionary key.
5727 */
5728 int
5729eval_isdictc(int c)
5730{
5731 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732}
5733
5734/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005735 * Handle:
5736 * - expr[expr], expr[expr:expr] subscript
5737 * - ".name" lookup
5738 * - function call with Funcref variable: func(expr)
5739 * - method call: var->method()
5740 *
5741 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005742 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005744handle_subscript(
5745 char_u **arg,
5746 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005747 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005748 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005749{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005750 int evaluate = evalarg != NULL
5751 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005752 int ret = OK;
5753 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005754 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005755 int getnext;
5756 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005757
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005758 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005759 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005760 // When at the end of the line and ".name" or "->{" or "->X" follows in
5761 // the next line then consume the line break.
5762 p = eval_next_non_blank(*arg, evalarg, &getnext);
5763 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005764 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005765 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5766 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5767 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005768 {
5769 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005770 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005771 check_white = FALSE;
5772 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005773
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005774 if (rettv->v_type == VAR_ANY)
5775 {
5776 char_u *exp_name;
5777 int cc;
5778 int idx;
5779 ufunc_T *ufunc;
5780 type_T *type;
5781
5782 // Found script from "import * as {name}", script item name must
5783 // follow.
5784 if (**arg != '.')
5785 {
5786 if (verbose)
5787 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5788 ret = FAIL;
5789 break;
5790 }
5791 ++*arg;
5792 if (IS_WHITE_OR_NUL(**arg))
5793 {
5794 if (verbose)
5795 emsg(_(e_no_white_space_allowed_after_dot));
5796 ret = FAIL;
5797 break;
5798 }
5799
5800 // isolate the name
5801 exp_name = *arg;
5802 while (eval_isnamec(**arg))
5803 ++*arg;
5804 cc = **arg;
5805 **arg = NUL;
5806
5807 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5808 evalarg->eval_cctx, verbose);
5809 **arg = cc;
5810 *arg = skipwhite(*arg);
5811
5812 if (idx < 0 && ufunc == NULL)
5813 {
5814 ret = FAIL;
5815 break;
5816 }
5817 if (idx >= 0)
5818 {
5819 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5820 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5821
5822 copy_tv(sv->sv_tv, rettv);
5823 }
5824 else
5825 {
5826 rettv->v_type = VAR_FUNC;
5827 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5828 }
5829 }
5830
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005831 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5832 || rettv->v_type == VAR_PARTIAL))
5833 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005834 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005835 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5836 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005837
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005838 // Stop the expression evaluation when immediately aborting on
5839 // error, or when an interrupt occurred or an exception was thrown
5840 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005841 if (aborting())
5842 {
5843 if (ret == OK)
5844 clear_tv(rettv);
5845 ret = FAIL;
5846 }
5847 dict_unref(selfdict);
5848 selfdict = NULL;
5849 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005850 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005851 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005852 if (in_vim9script())
5853 *arg = skipwhite(p + 2);
5854 else
5855 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005856 if (ret == OK)
5857 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005858 if (VIM_ISWHITE(**arg))
5859 {
5860 emsg(_(e_nowhitespace));
5861 ret = FAIL;
5862 }
5863 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005864 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005865 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005866 else
5867 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005868 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005869 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005870 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005871 // "." is ".name" lookup when we found a dict or when evaluating and
5872 // scriptversion is at least 2, where string concatenation is "..".
5873 else if (**arg == '['
5874 || (**arg == '.' && (rettv->v_type == VAR_DICT
5875 || (!evaluate
5876 && (*arg)[1] != '.'
5877 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005878 {
5879 dict_unref(selfdict);
5880 if (rettv->v_type == VAR_DICT)
5881 {
5882 selfdict = rettv->vval.v_dict;
5883 if (selfdict != NULL)
5884 ++selfdict->dv_refcount;
5885 }
5886 else
5887 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005888 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005889 {
5890 clear_tv(rettv);
5891 ret = FAIL;
5892 }
5893 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005894 else
5895 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005896 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5899 // Don't do this when "Func" is already a partial that was bound
5900 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005901 if (selfdict != NULL
5902 && (rettv->v_type == VAR_FUNC
5903 || (rettv->v_type == VAR_PARTIAL
5904 && (rettv->vval.v_partial->pt_auto
5905 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005906 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005907
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005908 dict_unref(selfdict);
5909 return ret;
5910}
5911
5912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 * Make a copy of an item.
5914 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5916 * reference to an already copied list/dict can be used.
5917 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005919 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920item_copy(
5921 typval_T *from,
5922 typval_T *to,
5923 int deep,
5924 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925{
5926 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005931 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005932 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 }
5934 ++recurse;
5935
5936 switch (from->v_type)
5937 {
5938 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005939 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 case VAR_STRING:
5941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005942 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005943 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005946 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005947 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 copy_tv(from, to);
5949 break;
5950 case VAR_LIST:
5951 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005952 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005953 if (from->vval.v_list == NULL)
5954 to->vval.v_list = NULL;
5955 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 to->vval.v_list = from->vval.v_list->lv_copylist;
5959 ++to->vval.v_list->lv_refcount;
5960 }
5961 else
5962 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5963 if (to->vval.v_list == NULL)
5964 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005966 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005968 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 case VAR_DICT:
5970 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005971 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005972 if (from->vval.v_dict == NULL)
5973 to->vval.v_dict = NULL;
5974 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005977 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5978 ++to->vval.v_dict->dv_refcount;
5979 }
5980 else
5981 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5982 if (to->vval.v_dict == NULL)
5983 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005985 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005986 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005988 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 }
5991 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993}
5994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 void
5996echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5997{
5998 char_u *tofree;
5999 char_u numbuf[NUMBUFLEN];
6000 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6001
6002 if (*atstart)
6003 {
6004 *atstart = FALSE;
6005 // Call msg_start() after eval1(), evaluating the expression
6006 // may cause a message to appear.
6007 if (with_space)
6008 {
6009 // Mark the saved text as finishing the line, so that what
6010 // follows is displayed on a new line when scrolling back
6011 // at the more prompt.
6012 msg_sb_eol();
6013 msg_start();
6014 }
6015 }
6016 else if (with_space)
6017 msg_puts_attr(" ", echo_attr);
6018
6019 if (p != NULL)
6020 for ( ; *p != NUL && !got_int; ++p)
6021 {
6022 if (*p == '\n' || *p == '\r' || *p == TAB)
6023 {
6024 if (*p != TAB && *needclr)
6025 {
6026 // remove any text still there from the command
6027 msg_clr_eos();
6028 *needclr = FALSE;
6029 }
6030 msg_putchar_attr(*p, echo_attr);
6031 }
6032 else
6033 {
6034 if (has_mbyte)
6035 {
6036 int i = (*mb_ptr2len)(p);
6037
6038 (void)msg_outtrans_len_attr(p, i, echo_attr);
6039 p += i - 1;
6040 }
6041 else
6042 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6043 }
6044 }
6045 vim_free(tofree);
6046}
6047
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 * ":echo expr1 ..." print each argument separated with a space, add a
6050 * newline at the end.
6051 * ":echon expr1 ..." print each argument plain.
6052 */
6053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006058 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int needclr = TRUE;
6060 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006061 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006062 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006063 evalarg_T evalarg;
6064
Bram Moolenaare707c882020-07-01 13:07:37 +02006065 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 if (eap->skip)
6068 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006069 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006071 // If eval1() causes an error message the text from the command may
6072 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006073 need_clr_eos = needclr;
6074
Bram Moolenaar68db9962021-05-09 23:19:22 +02006075 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006076 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
6078 /*
6079 * Report the invalid expression unless the expression evaluation
6080 * has been cancelled due to an aborting error, an interrupt, or an
6081 * exception.
6082 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006083 if (!aborting() && did_emsg == did_emsg_before
6084 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006085 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006086 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 break;
6088 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 need_clr_eos = FALSE;
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006092 {
6093 if (rettv.v_type == VAR_VOID)
6094 {
6095 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6096 break;
6097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006099 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 arg = skipwhite(arg);
6103 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006104 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006105 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 if (eap->skip)
6108 --emsg_skip;
6109 else
6110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 if (needclr)
6113 msg_clr_eos();
6114 if (eap->cmdidx == CMD_echo)
6115 msg_end();
6116 }
6117}
6118
6119/*
6120 * ":echohl {name}".
6121 */
6122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006123ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006125 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126}
6127
6128/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006129 * Returns the :echo attribute
6130 */
6131 int
6132get_echo_attr(void)
6133{
6134 return echo_attr;
6135}
6136
6137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 * ":execute expr1 ..." execute the result of an expression.
6139 * ":echomsg expr1 ..." Print a message
6140 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006141 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 * Each gets spaces around each argument and a newline at the end for
6143 * echo commands
6144 */
6145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 int ret = OK;
6151 char_u *p;
6152 garray_T ga;
6153 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006154 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 ga_init2(&ga, 1, 80);
6157
6158 if (eap->skip)
6159 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006160 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006162 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006163 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 if (!eap->skip)
6167 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006168 char_u buf[NUMBUFLEN];
6169
6170 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006171 {
6172 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6173 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006174 semsg(_(e_using_invalid_value_as_string_str),
6175 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006176 p = NULL;
6177 }
6178 else
6179 p = tv_get_string_buf(&rettv, buf);
6180 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006181 else
6182 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006183 if (p == NULL)
6184 {
6185 clear_tv(&rettv);
6186 ret = FAIL;
6187 break;
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 len = (int)STRLEN(p);
6190 if (ga_grow(&ga, len + 2) == FAIL)
6191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 ret = FAIL;
6194 break;
6195 }
6196 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 ga.ga_len += len;
6200 }
6201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 arg = skipwhite(arg);
6204 }
6205
6206 if (ret != FAIL && ga.ga_data != NULL)
6207 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006208 // use the first line of continuation lines for messages
6209 SOURCING_LNUM = start_lnum;
6210
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006211 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006213 // Mark the already saved text as finishing the line, so that what
6214 // follows is displayed on a new line when scrolling back at the
6215 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006216 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006217 }
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006220 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006221 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006222 out_flush();
6223 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006224 else if (eap->cmdidx == CMD_echoconsole)
6225 {
6226 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6227 ui_write((char_u *)"\r\n", 2, TRUE);
6228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 else if (eap->cmdidx == CMD_echoerr)
6230 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006231 int save_did_emsg = did_emsg;
6232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006234 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (!force_abort)
6236 did_emsg = save_did_emsg;
6237 }
6238 else if (eap->cmdidx == CMD_execute)
6239 do_cmdline((char_u *)ga.ga_data,
6240 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6241 }
6242
6243 ga_clear(&ga);
6244
6245 if (eap->skip)
6246 --emsg_skip;
6247
Bram Moolenaar63b91732021-08-05 20:40:03 +02006248 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249}
6250
6251/*
6252 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6253 * "arg" points to the "&" or '+' when called, to "option" when returning.
6254 * Returns NULL when no option name found. Otherwise pointer to the char
6255 * after the option name.
6256 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006257 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 char_u *p = *arg;
6261
6262 ++p;
6263 if (*p == 'g' && p[1] == ':')
6264 {
6265 *opt_flags = OPT_GLOBAL;
6266 p += 2;
6267 }
6268 else if (*p == 'l' && p[1] == ':')
6269 {
6270 *opt_flags = OPT_LOCAL;
6271 p += 2;
6272 }
6273 else
6274 *opt_flags = 0;
6275
6276 if (!ASCII_ISALPHA(*p))
6277 return NULL;
6278 *arg = p;
6279
6280 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006281 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
6283 while (ASCII_ISALPHA(*p))
6284 ++p;
6285 return p;
6286}
6287
6288/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006289 * Display script name where an item was last set.
6290 * Should only be invoked when 'verbose' is non-zero.
6291 */
6292 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006293last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006294{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006295 char_u *p;
6296
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006297 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006298 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006299 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006300 if (p != NULL)
6301 {
6302 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006303 msg_puts(_("\n\tLast set from "));
6304 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006305 if (script_ctx.sc_lnum > 0)
6306 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006307 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006308 msg_outnum((long)script_ctx.sc_lnum);
6309 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006310 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006311 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006312 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006313 }
6314}
6315
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006316#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
6318/*
6319 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006320 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * "flags" can be "g" to do a global substitute.
6322 * Returns an allocated string, NULL for error.
6323 */
6324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325do_string_sub(
6326 char_u *str,
6327 char_u *pat,
6328 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006329 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 int sublen;
6333 regmatch_T regmatch;
6334 int i;
6335 int do_all;
6336 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006337 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 garray_T ga;
6339 char_u *ret;
6340 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006341 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006343 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006345 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 ga_init2(&ga, 1, 200);
6348
6349 do_all = (flags[0] == 'g');
6350
6351 regmatch.rm_ic = p_ic;
6352 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6353 if (regmatch.regprog != NULL)
6354 {
6355 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006356 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006359 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006360 if (regmatch.startp[0] == regmatch.endp[0])
6361 {
6362 if (zero_width == regmatch.startp[0])
6363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006364 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006365 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006366 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6367 (size_t)i);
6368 ga.ga_len += i;
6369 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006370 continue;
6371 }
6372 zero_width = regmatch.startp[0];
6373 }
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 /*
6376 * Get some space for a temporary buffer to do the substitution
6377 * into. It will contain:
6378 * - The text up to where the match is.
6379 * - The substituted text.
6380 * - The text after the match.
6381 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006382 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006383 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6385 {
6386 ga_clear(&ga);
6387 break;
6388 }
6389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006390 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 i = (int)(regmatch.startp[0] - tail);
6392 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006394 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 + ga.ga_len + i, TRUE, TRUE, FALSE);
6396 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006397 tail = regmatch.endp[0];
6398 if (*tail == NUL)
6399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (!do_all)
6401 break;
6402 }
6403
6404 if (ga.ga_data != NULL)
6405 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6406
Bram Moolenaar473de612013-06-08 18:19:48 +02006407 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409
6410 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6411 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006412 if (p_cpo == empty_option)
6413 p_cpo = save_cpo;
6414 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006416 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006417 // If it's still empty it was changed and restored, need to restore in
6418 // the complicated way.
6419 if (*p_cpo == NUL)
6420 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006421 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424 return ret;
6425}