blob: edef8785d5f04b0771ceb33bf4c53b58a8d47fda [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
905 cc = *p;
906 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100907 // When we would write to the variable pass &ht and prevent autoload.
908 writing = !(flags & GLV_READ_ONLY);
909 v = find_var(lp->ll_name, writing ? &ht : NULL,
910 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200912 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000914 if (v == NULL)
915 return NULL;
916
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100917 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
918 {
919 if (!quiet)
920 semsg(_(e_variable_already_declared), lp->ll_name);
921 return NULL;
922 }
923
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 /*
925 * Loop until no more [idx] or .key is following.
926 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100928 var1.v_type = VAR_UNKNOWN;
929 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200930 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000931 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200932 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
933 {
934 if (!quiet)
935 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
936 return NULL;
937 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200938 if (lp->ll_tv->v_type != VAR_LIST
939 && lp->ll_tv->v_type != VAR_DICT
940 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100943 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200946
947 // a NULL list/blob works like an empty list/blob, allocate one now.
948 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
949 rettv_list_alloc(lp->ll_tv);
950 else if (lp->ll_tv->v_type == VAR_BLOB
951 && lp->ll_tv->vval.v_blob == NULL)
952 rettv_blob_alloc(lp->ll_tv);
953
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100957 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000960
Bram Moolenaar10c65862020-10-08 21:16:42 +0200961 if (in_vim9script() && lp->ll_valtype == NULL
962 && lp->ll_tv == &v->di_tv
963 && ht != NULL && ht == get_script_local_ht())
964 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200965 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200966
967 // Vim9 script local variable: get the type
968 if (sv != NULL)
969 lp->ll_valtype = sv->sv_type;
970 }
971
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 len = -1;
973 if (*p == '.')
974 {
975 key = p + 1;
976 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
977 ;
978 if (len == 0)
979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100981 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 }
984 p = key + len;
985 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000986 else
987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000989 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 if (*p == ':')
991 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000992 else
993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200995 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100997 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001000 clear_tv(&var1);
1001 return NULL;
1002 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001003 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 }
1005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 if (*p == ':')
1008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001012 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001013 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001015 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 if (rettv != NULL
1017 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001019 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001020 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001023 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001024 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 }
1027 p = skipwhite(p + 1);
1028 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001030 else
1031 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001033 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001034 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001036 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001039 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001041 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001042 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001043 clear_tv(&var2);
1044 return NULL;
1045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001053 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001055 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001056 clear_tv(&var1);
1057 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 }
1060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001061 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 ++p;
1063 }
1064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001066 {
1067 if (len == -1)
1068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001069 // "[key]": get key from "var1"
1070 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001071 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 }
1076 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001077 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001078
1079 // a NULL dict is equivalent with an empty dict
1080 if (lp->ll_tv->vval.v_dict == NULL)
1081 {
1082 lp->ll_tv->vval.v_dict = dict_alloc();
1083 if (lp->ll_tv->vval.v_dict == NULL)
1084 {
1085 clear_tv(&var1);
1086 return NULL;
1087 }
1088 ++lp->ll_tv->vval.v_dict->dv_refcount;
1089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001090 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001091
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001092 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001094 // When assigning to a scope dictionary check that a function and
1095 // variable name is valid (only variable name unless it is l: or
1096 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001098 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001099 int prevval;
1100 int wrong;
1101
1102 if (len != -1)
1103 {
1104 prevval = key[len];
1105 key[len] = NUL;
1106 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001107 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001108 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1110 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001111 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001112 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001113 if (len != -1)
1114 key[len] = prevval;
1115 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001116 {
1117 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001119 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001120 }
1121
Bram Moolenaar10c65862020-10-08 21:16:42 +02001122 if (lp->ll_valtype != NULL)
1123 // use the type of the member
1124 lp->ll_valtype = lp->ll_valtype->tt_member;
1125
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001127 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001129 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001130 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001132 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001133 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001134 return NULL;
1135 }
1136
Bram Moolenaar31b81602019-02-10 22:14:27 +01001137 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001141 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001142 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 }
1145 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001149 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 p = NULL;
1152 break;
1153 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001155 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001156 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1157 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001158 {
1159 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001161 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001162
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001163 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 else if (lp->ll_tv->v_type == VAR_BLOB)
1167 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001168 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1169
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 /*
1171 * Get the number and item for the only or first index of the List.
1172 */
1173 if (empty1)
1174 lp->ll_n1 = 0;
1175 else
1176 // is number or string
1177 lp->ll_n1 = (long)tv_get_number(&var1);
1178 clear_tv(&var1);
1179
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001180 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return NULL;
1184 }
1185 if (lp->ll_range && !lp->ll_empty2)
1186 {
1187 lp->ll_n2 = (long)tv_get_number(&var2);
1188 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001189 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1190 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001191 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 }
1193 lp->ll_blob = lp->ll_tv->vval.v_blob;
1194 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001195 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001196 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001197 else
1198 {
1199 /*
1200 * Get the number and item for the only or first index of the List.
1201 */
1202 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001205 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001206 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001207 clear_tv(&var1);
1208
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001209 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001211 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001212 if (lp->ll_li == NULL)
1213 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001214 clear_tv(&var2);
1215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 }
1217
Bram Moolenaar10c65862020-10-08 21:16:42 +02001218 if (lp->ll_valtype != NULL)
1219 // use the type of the member
1220 lp->ll_valtype = lp->ll_valtype->tt_member;
1221
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 /*
1223 * May need to find the item or absolute index for the second
1224 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 * When no index given: "lp->ll_empty2" is TRUE.
1226 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001230 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001233 if (check_range_index_two(lp->ll_list,
1234 &lp->ll_n1, lp->ll_li,
1235 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237 }
1238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 }
1242
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001243 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 return p;
1246}
1247
1248/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001249 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001252clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253{
1254 vim_free(lp->ll_exp_name);
1255 vim_free(lp->ll_newkey);
1256}
1257
1258/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001259 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001261 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1262 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265set_var_lval(
1266 lval_T *lp,
1267 char_u *endp,
1268 typval_T *rettv,
1269 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001270 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1271 char_u *op,
1272 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001273{
1274 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276
1277 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001279 cc = *endp;
1280 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001281 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1282 return;
1283
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 if (lp->ll_blob != NULL)
1285 {
1286 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001287
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 if (op != NULL && *op != '=')
1289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001290 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 return;
1292 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001293 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001294 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295
1296 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1297 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 if (lp->ll_empty2)
1299 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1300
Bram Moolenaar68452172021-04-12 21:21:02 +02001301 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1302 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 else
1306 {
1307 val = (int)tv_get_number_chk(rettv, &error);
1308 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001309 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 }
1311 }
1312 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001314 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001315
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001316 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1317 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001318 {
1319 emsg(_(e_cannot_mod));
1320 *endp = cc;
1321 return;
1322 }
1323
Bram Moolenaarff697e62019-02-12 22:28:33 +01001324 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001325 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001326 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001327 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001328 {
1329 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001330 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1331 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001332 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001333 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1334 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001335 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001338 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001339 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001340 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1341 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001342 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001343 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1344 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001345 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001346 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001348 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001349 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001350 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001351 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 else if (lp->ll_range)
1353 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001354 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1355 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001356 {
1357 emsg(_("E996: Cannot lock a range"));
1358 return;
1359 }
1360
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001361 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1362 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 }
1364 else
1365 {
1366 /*
1367 * Assign to a List or Dictionary item.
1368 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001369 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1370 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001371 {
1372 emsg(_("E996: Cannot lock a list or dict"));
1373 return;
1374 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001375
1376 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001377 && check_typval_arg_type(lp->ll_valtype, rettv,
1378 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001379 return;
1380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 if (lp->ll_newkey != NULL)
1382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 if (op != NULL && *op != '=')
1384 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001385 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001386 return;
1387 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001388 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1389 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001390 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001392 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001393 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 if (di == NULL)
1395 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001396 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1397 {
1398 vim_free(di);
1399 return;
1400 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 else if (op != NULL && *op != '=')
1404 {
1405 tv_op(lp->ll_tv, rettv, op);
1406 return;
1407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001410
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 /*
1412 * Assign the value to the variable or list item.
1413 */
1414 if (copy)
1415 copy_tv(rettv, lp->ll_tv);
1416 else
1417 {
1418 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001419 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 }
1422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423}
1424
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001426 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1427 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428 * Returns OK or FAIL.
1429 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001431tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001432{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001433 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 char_u numbuf[NUMBUFLEN];
1435 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001436 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001437
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001438 // Can't do anything with a Funcref or Dict on the right.
1439 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001440 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001441 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1442 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001443 {
1444 switch (tv1->v_type)
1445 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001446 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001447 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 case VAR_DICT:
1450 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001451 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001452 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001453 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001454 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001455 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001456 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 break;
1458
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 case VAR_BLOB:
1460 if (*op != '+' || tv2->v_type != VAR_BLOB)
1461 break;
1462 // BLOB += BLOB
1463 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1464 {
1465 blob_T *b1 = tv1->vval.v_blob;
1466 blob_T *b2 = tv2->vval.v_blob;
1467 int i, len = blob_len(b2);
1468 for (i = 0; i < len; i++)
1469 ga_append(&b1->bv_ga, blob_get(b2, i));
1470 }
1471 return OK;
1472
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 case VAR_LIST:
1474 if (*op != '+' || tv2->v_type != VAR_LIST)
1475 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001476 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001477 if (tv2->vval.v_list != NULL)
1478 {
1479 if (tv1->vval.v_list == NULL)
1480 {
1481 tv1->vval.v_list = tv2->vval.v_list;
1482 ++tv1->vval.v_list->lv_refcount;
1483 }
1484 else
1485 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1486 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487 return OK;
1488
1489 case VAR_NUMBER:
1490 case VAR_STRING:
1491 if (tv2->v_type == VAR_LIST)
1492 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001493 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001496 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001497#ifdef FEAT_FLOAT
1498 if (tv2->v_type == VAR_FLOAT)
1499 {
1500 float_T f = n;
1501
Bram Moolenaarff697e62019-02-12 22:28:33 +01001502 if (*op == '%')
1503 break;
1504 switch (*op)
1505 {
1506 case '+': f += tv2->vval.v_float; break;
1507 case '-': f -= tv2->vval.v_float; break;
1508 case '*': f *= tv2->vval.v_float; break;
1509 case '/': f /= tv2->vval.v_float; break;
1510 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001511 clear_tv(tv1);
1512 tv1->v_type = VAR_FLOAT;
1513 tv1->vval.v_float = f;
1514 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001516#endif
1517 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 switch (*op)
1519 {
1520 case '+': n += tv_get_number(tv2); break;
1521 case '-': n -= tv_get_number(tv2); break;
1522 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001523 case '/': n = num_divide(n, tv_get_number(tv2),
1524 &failed); break;
1525 case '%': n = num_modulus(n, tv_get_number(tv2),
1526 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001528 clear_tv(tv1);
1529 tv1->v_type = VAR_NUMBER;
1530 tv1->vval.v_number = n;
1531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 }
1533 else
1534 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535 if (tv2->v_type == VAR_FLOAT)
1536 break;
1537
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001539 s = tv_get_string(tv1);
1540 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 clear_tv(tv1);
1542 tv1->v_type = VAR_STRING;
1543 tv1->vval.v_string = s;
1544 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001545 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001546
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001547 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001548#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001549 {
1550 float_T f;
1551
Bram Moolenaarff697e62019-02-12 22:28:33 +01001552 if (*op == '%' || *op == '.'
1553 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 && tv2->v_type != VAR_NUMBER
1555 && tv2->v_type != VAR_STRING))
1556 break;
1557 if (tv2->v_type == VAR_FLOAT)
1558 f = tv2->vval.v_float;
1559 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001560 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001561 switch (*op)
1562 {
1563 case '+': tv1->vval.v_float += f; break;
1564 case '-': tv1->vval.v_float -= f; break;
1565 case '*': tv1->vval.v_float *= f; break;
1566 case '/': tv1->vval.v_float /= f; break;
1567 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001570 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 }
1572 }
1573
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001574 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001575 return FAIL;
1576}
1577
1578/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 * Evaluate the expression used in a ":for var in expr" command.
1580 * "arg" points to "var".
1581 * Set "*errp" to TRUE for an error, FALSE otherwise;
1582 * Return a pointer that holds the info. Null when there is an error.
1583 */
1584 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001585eval_for_line(
1586 char_u *arg,
1587 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001588 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001589 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001590{
Bram Moolenaar33570922005-01-25 22:26:29 +00001591 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001592 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001594 typval_T tv;
1595 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001596 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001598 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001600 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601 if (fi == NULL)
1602 return NULL;
1603
Bram Moolenaar404557e2021-07-05 21:41:48 +02001604 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1605 &fi->fi_semicolon, FALSE);
1606 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607 return fi;
1608
Bram Moolenaar404557e2021-07-05 21:41:48 +02001609 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001610 if (expr[0] != 'i' || expr[1] != 'n'
1611 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001613 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1614 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1615 else
1616 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 return fi;
1618 }
1619
1620 if (skip)
1621 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001622 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1623 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624 {
1625 *errp = FALSE;
1626 if (!skip)
1627 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001628 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001629 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630 l = tv.vval.v_list;
1631 if (l == NULL)
1632 {
1633 // a null list is like an empty list: do nothing
1634 clear_tv(&tv);
1635 }
1636 else
1637 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001639 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001641 // No need to increment the refcount, it's already set for
1642 // the list being used in "tv".
1643 fi->fi_list = l;
1644 list_add_watch(l, &fi->fi_lw);
1645 fi->fi_lw.lw_item = l->lv_first;
1646 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001647 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001648 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001649 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001650 fi->fi_bi = 0;
1651 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001652 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001653 typval_T btv;
1654
1655 // Make a copy, so that the iteration still works when the
1656 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001658 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001659 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001660 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001661 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001662 else if (tv.v_type == VAR_STRING)
1663 {
1664 fi->fi_byte_idx = 0;
1665 fi->fi_string = tv.vval.v_string;
1666 tv.vval.v_string = NULL;
1667 if (fi->fi_string == NULL)
1668 fi->fi_string = vim_strsave((char_u *)"");
1669 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 else
1671 {
Sean Dewar80d73952021-08-04 19:25:54 +02001672 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001673 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 }
1675 }
1676 }
1677 if (skip)
1678 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680
1681 return fi;
1682}
1683
1684/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001685 * Used when looping over a :for line, skip the "in expr" part.
1686 */
1687 void
1688skip_for_lines(void *fi_void, evalarg_T *evalarg)
1689{
1690 forinfo_T *fi = (forinfo_T *)fi_void;
1691 int i;
1692
1693 for (i = 0; i < fi->fi_break_count; ++i)
1694 eval_next_line(evalarg);
1695}
1696
1697/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 * Use the first item in a ":for" list. Advance to the next.
1699 * Assign the values to the variable (list). "arg" points to the first one.
1700 * Return TRUE when a valid item was found, FALSE when at end of list or
1701 * something wrong.
1702 */
1703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001706 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001708 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001709 ? (ASSIGN_FINAL
1710 // first round: error if variable exists
1711 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1712 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001713 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001714 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001715 int skip_assign = in_vim9script() && arg[0] == '_'
1716 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 if (fi->fi_blob != NULL)
1719 {
1720 typval_T tv;
1721
1722 if (fi->fi_bi >= blob_len(fi->fi_blob))
1723 return FALSE;
1724 tv.v_type = VAR_NUMBER;
1725 tv.v_lock = VAR_FIXED;
1726 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1727 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001728 if (skip_assign)
1729 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001730 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001731 fi->fi_varcount, flag, NULL) == OK;
1732 }
1733
1734 if (fi->fi_string != NULL)
1735 {
1736 typval_T tv;
1737 int len;
1738
1739 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1740 if (len == 0)
1741 return FALSE;
1742 tv.v_type = VAR_STRING;
1743 tv.v_lock = VAR_FIXED;
1744 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1745 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001746 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001747 if (skip_assign)
1748 result = TRUE;
1749 else
1750 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001751 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001752 vim_free(tv.vval.v_string);
1753 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 }
1755
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 item = fi->fi_lw.lw_item;
1757 if (item == NULL)
1758 result = FALSE;
1759 else
1760 {
1761 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001762 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001763 if (skip_assign)
1764 result = TRUE;
1765 else
1766 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001767 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 }
1769 return result;
1770}
1771
1772/*
1773 * Free the structure used to store info used by ":for".
1774 */
1775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001780 if (fi == NULL)
1781 return;
1782 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001783 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001785 list_unref(fi->fi_list);
1786 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001787 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001788 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001789 else
1790 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 vim_free(fi);
1792}
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795set_context_for_expression(
1796 expand_T *xp,
1797 char_u *arg,
1798 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001800 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001804 if (cmdidx == CMD_let || cmdidx == CMD_var
1805 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 {
1807 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001808 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001810 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001811 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 {
1813 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001814 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001815 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 break;
1817 }
1818 return;
1819 }
1820 }
1821 else
1822 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1823 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 while ((xp->xp_pattern = vim_strpbrk(arg,
1825 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1826 {
1827 c = *xp->xp_pattern;
1828 if (c == '&')
1829 {
1830 c = xp->xp_pattern[1];
1831 if (c == '&')
1832 {
1833 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001834 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001839 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1840 xp->xp_pattern += 2;
1841
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 else if (c == '$')
1845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001846 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 xp->xp_context = EXPAND_ENV_VARS;
1848 }
1849 else if (c == '=')
1850 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001851 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 xp->xp_context = EXPAND_EXPRESSION;
1853 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001854 else if (c == '#'
1855 && xp->xp_context == EXPAND_EXPRESSION)
1856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001857 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001858 break;
1859 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001860 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 && xp->xp_context == EXPAND_FUNCTIONS
1862 && vim_strchr(xp->xp_pattern, '(') == NULL)
1863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001864 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 break;
1866 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001867 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001869 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
1871 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1872 if (c == '\\' && xp->xp_pattern[1] != NUL)
1873 ++xp->xp_pattern;
1874 xp->xp_context = EXPAND_NOTHING;
1875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1880 /* skip */ ;
1881 xp->xp_context = EXPAND_NOTHING;
1882 }
1883 else if (c == '|')
1884 {
1885 if (xp->xp_pattern[1] == '|')
1886 {
1887 ++xp->xp_pattern;
1888 xp->xp_context = EXPAND_EXPRESSION;
1889 }
1890 else
1891 xp->xp_context = EXPAND_COMMANDS;
1892 }
1893 else
1894 xp->xp_context = EXPAND_EXPRESSION;
1895 }
1896 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // Doesn't look like something valid, expand as an expression
1898 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 arg = xp->xp_pattern;
1901 if (*arg != NUL)
1902 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1903 /* skip */ ;
1904 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001905
1906 // ":exe one two" completes "two"
1907 if ((cmdidx == CMD_execute
1908 || cmdidx == CMD_echo
1909 || cmdidx == CMD_echon
1910 || cmdidx == CMD_echomsg)
1911 && xp->xp_context == EXPAND_EXPRESSION)
1912 {
1913 for (;;)
1914 {
1915 char_u *n = skiptowhite(arg);
1916
1917 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1918 break;
1919 arg = skipwhite(n);
1920 }
1921 }
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 xp->xp_pattern = arg;
1924}
1925
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001927 * Return TRUE if "pat" matches "text".
1928 * Does not use 'cpo' and always uses 'magic'.
1929 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001930 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001931pattern_match(char_u *pat, char_u *text, int ic)
1932{
1933 int matches = FALSE;
1934 char_u *save_cpo;
1935 regmatch_T regmatch;
1936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001938 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001939 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001940 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1941 if (regmatch.regprog != NULL)
1942 {
1943 regmatch.rm_ic = ic;
1944 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1945 vim_regfree(regmatch.regprog);
1946 }
1947 p_cpo = save_cpo;
1948 return matches;
1949}
1950
1951/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001952 * Handle a name followed by "(". Both for just "name(arg)" and for
1953 * "expr->name(arg)".
1954 * Returns OK or FAIL.
1955 */
1956 static int
1957eval_func(
1958 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001959 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001960 char_u *name,
1961 int name_len,
1962 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001963 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001964 typval_T *basetv) // "expr" for "expr->name(arg)"
1965{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001966 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001967 char_u *s = name;
1968 int len = name_len;
1969 partial_T *partial;
1970 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001971 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001972
1973 if (!evaluate)
1974 check_vars(s, len);
1975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 // If "s" is the name of a variable of type VAR_FUNC
1977 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001978 s = deref_func_name(s, &len, &partial,
1979 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // Need to make a copy, in case evaluating the arguments makes
1982 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001983 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001984 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001985 ret = FAIL;
1986 else
1987 {
1988 funcexe_T funcexe;
1989
1990 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001991 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 funcexe.firstline = curwin->w_cursor.lnum;
1993 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994 funcexe.evaluate = evaluate;
1995 funcexe.partial = partial;
1996 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001997 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001998 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 }
2000 vim_free(s);
2001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002002 // If evaluate is FALSE rettv->v_type was not set in
2003 // get_func_tv, but it's needed in handle_subscript() to parse
2004 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2006 {
2007 rettv->vval.v_string = NULL;
2008 rettv->v_type = VAR_FUNC;
2009 }
2010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002011 // Stop the expression evaluation when immediately
2012 // aborting on error, or when an interrupt occurred or
2013 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 if (evaluate && aborting())
2015 {
2016 if (ret == OK)
2017 clear_tv(rettv);
2018 ret = FAIL;
2019 }
2020 return ret;
2021}
2022
2023/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002024 * Get the next line source line without advancing. But do skip over comment
2025 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002026 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002027 */
2028 static char_u *
2029getline_peek_skip_comments(evalarg_T *evalarg)
2030{
2031 for (;;)
2032 {
2033 char_u *next = getline_peek(evalarg->eval_getline,
2034 evalarg->eval_cookie);
2035 char_u *p;
2036
2037 if (next == NULL)
2038 break;
2039 p = skipwhite(next);
2040 if (*p != NUL && !vim9_comment_start(p))
2041 return next;
2042 (void)eval_next_line(evalarg);
2043 }
2044 return NULL;
2045}
2046
2047/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002048 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2049 * comment) and there is a next line, return the next line (skipping blanks)
2050 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002051 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2052 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 * "arg" must point somewhere inside a line, not at the start.
2054 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002055 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002056eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2057{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002058 char_u *p = skipwhite(arg);
2059
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002060 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002061 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002063 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002064 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002065 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002066 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002067
2068 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002069 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002070 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002071 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072
Bram Moolenaar9d489562020-07-30 20:08:50 +02002073 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074 {
2075 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002076 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002077 }
2078 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002079 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080}
2081
2082/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002083 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002084 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002085 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002086 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002087eval_next_line(evalarg_T *evalarg)
2088{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002089 garray_T *gap = &evalarg->eval_ga;
2090 char_u *line;
2091
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002092 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002093 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2094 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002095 else
2096 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002097 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002098 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2099 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002100 char_u *p = skipwhite(line);
2101
2102 // Going to concatenate the lines after parsing. For an empty or
2103 // comment line use an empty string.
2104 if (*p == NUL || vim9_comment_start(p))
2105 {
2106 vim_free(line);
2107 line = vim_strsave((char_u *)"");
2108 }
2109
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002110 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2111 ++gap->ga_len;
2112 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002113 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002114 {
2115 vim_free(evalarg->eval_tofree);
2116 evalarg->eval_tofree = line;
2117 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002118
2119 // Advanced to the next line, "arg" no longer points into the previous
2120 // line.
2121 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2122
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002123 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002124}
2125
Bram Moolenaare6b53242020-07-01 17:28:33 +02002126/*
2127 * Call eval_next_non_blank() and get the next line if needed.
2128 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002129 char_u *
2130skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2131{
2132 int getnext;
2133 char_u *p = skipwhite(arg);
2134
Bram Moolenaar962d7212020-07-04 14:15:00 +02002135 if (evalarg == NULL)
2136 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002137 eval_next_non_blank(p, evalarg, &getnext);
2138 if (getnext)
2139 return eval_next_line(evalarg);
2140 return p;
2141}
2142
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002143/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002144 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002145 */
2146 void
2147clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2148{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002149 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002150 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002151 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002152 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002153 if (eap != NULL)
2154 {
2155 // We may need to keep the original command line, e.g. for
2156 // ":let" it has the variable names. But we may also need the
2157 // new one, "nextcmd" points into it. Keep both.
2158 vim_free(eap->cmdline_tofree);
2159 eap->cmdline_tofree = *eap->cmdlinep;
2160 *eap->cmdlinep = evalarg->eval_tofree;
2161 }
2162 else
2163 vim_free(evalarg->eval_tofree);
2164 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002165 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002166
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002167 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2168 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002169 }
2170}
2171
2172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2176 */
2177
2178/*
2179 * Handle zero level expression.
2180 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002182 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * Return OK or FAIL.
2185 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002187eval0(
2188 char_u *arg,
2189 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002191 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 int ret;
2194 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002195 int did_emsg_before = did_emsg;
2196 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002197 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002198 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002202 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002203
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002204 if (ret != FAIL)
2205 end_error = !ends_excmd2(arg, p);
2206 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /*
2211 * Report the invalid expression unless the expression evaluation has
2212 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002213 * exception, or we already gave a more specific error.
2214 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002216 if (!aborting()
2217 && did_emsg == did_emsg_before
2218 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002219 && (flags & EVAL_CONSTANT) == 0
2220 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002221 {
2222 if (end_error)
2223 semsg(_(e_trailing_arg), p);
2224 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002225 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002226 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002227
2228 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002229 // a next command to avoid more errors, unless "|" is following, which
2230 // could only be a command separator.
2231 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2232 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002235
2236 if (eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002237 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return ret;
2240}
2241
2242/*
2243 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002244 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002245 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 *
2247 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002248 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002250 * Note: "rettv.v_lock" is not set.
2251 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * Return OK or FAIL.
2253 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002254 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002255eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002257 char_u *p;
2258 int getnext;
2259
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002260 CLEAR_POINTER(rettv);
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 /*
2263 * Get the first variable.
2264 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 return FAIL;
2267
Bram Moolenaar793648f2020-06-26 21:28:25 +02002268 p = eval_next_non_blank(*arg, evalarg, &getnext);
2269 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002271 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 int result;
2273 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002274 evalarg_T *evalarg_used = evalarg;
2275 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002276 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002277 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002278 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279
2280 if (evalarg == NULL)
2281 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002282 CLEAR_FIELD(local_evalarg);
2283 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002285 orig_flags = evalarg_used->eval_flags;
2286 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002288 if (getnext)
2289 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002290 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002291 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002292 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002293 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002294 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002295 clear_tv(rettv);
2296 return FAIL;
2297 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002304 int error = FALSE;
2305
Bram Moolenaar13106602020-10-04 16:06:05 +02002306 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002307 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002308 else if (vim9script)
2309 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002310 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002312 if (error || !op_falsy || !result)
2313 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 if (error)
2315 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317
2318 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002319 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002321 if (op_falsy)
2322 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002323 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002324 {
mityu4ac198c2021-05-28 17:52:40 +02002325 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002326 clear_tv(rettv);
2327 return FAIL;
2328 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002329 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002330 evalarg_used->eval_flags = (op_falsy ? !result : result)
2331 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2332 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002333 {
2334 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002336 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002337 if (!op_falsy || !result)
2338 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002342 /*
2343 * Check for the ":".
2344 */
2345 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2346 if (*p != ':')
2347 {
2348 emsg(_(e_missing_colon));
2349 if (evaluate && result)
2350 clear_tv(rettv);
2351 evalarg_used->eval_flags = orig_flags;
2352 return FAIL;
2353 }
2354 if (getnext)
2355 *arg = eval_next_line(evalarg_used);
2356 else
2357 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002358 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002359 {
2360 error_white_both(p, 1);
2361 clear_tv(rettv);
2362 evalarg_used->eval_flags = orig_flags;
2363 return FAIL;
2364 }
2365 *arg = p;
2366 }
2367
2368 /*
2369 * Get the third variable. Recursive!
2370 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002371 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002372 {
mityu4ac198c2021-05-28 17:52:40 +02002373 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002374 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002375 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002376 return FAIL;
2377 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002378 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2379 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002380 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002381 if (eval1(arg, &var2, evalarg_used) == FAIL)
2382 {
2383 if (evaluate && result)
2384 clear_tv(rettv);
2385 evalarg_used->eval_flags = orig_flags;
2386 return FAIL;
2387 }
2388 if (evaluate && !result)
2389 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002391
2392 if (evalarg == NULL)
2393 clear_evalarg(&local_evalarg, NULL);
2394 else
2395 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397
2398 return OK;
2399}
2400
2401/*
2402 * Handle first level expression:
2403 * expr2 || expr2 || expr2 logical OR
2404 *
2405 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002406 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 *
2408 * Return OK or FAIL.
2409 */
2410 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002413 char_u *p;
2414 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /*
2417 * Get the first variable.
2418 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return FAIL;
2421
2422 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002423 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002425 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 evalarg_T *evalarg_used = evalarg;
2429 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002430 int evaluate;
2431 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 long result = FALSE;
2433 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002434 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002435 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002436
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 if (evalarg == NULL)
2438 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 CLEAR_FIELD(local_evalarg);
2440 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 orig_flags = evalarg_used->eval_flags;
2443 evaluate = orig_flags & EVAL_EVALUATE;
2444 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002446 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002447 result = tv_get_bool_chk(rettv, &error);
2448 else if (tv_get_number_chk(rettv, &error) != 0)
2449 result = TRUE;
2450 clear_tv(rettv);
2451 if (error)
2452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
2454
2455 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002458 while (p[0] == '|' && p[1] == '|')
2459 {
2460 if (getnext)
2461 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002462 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002463 {
2464 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2465 {
2466 error_white_both(p, 2);
2467 clear_tv(rettv);
2468 return FAIL;
2469 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002470 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002471 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472
2473 /*
2474 * Get the second variable.
2475 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002476 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2477 {
mityu4ac198c2021-05-28 17:52:40 +02002478 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002479 clear_tv(rettv);
2480 return FAIL;
2481 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2483 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002486 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002487
2488 /*
2489 * Compute the result.
2490 */
2491 if (evaluate && !result)
2492 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002493 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002494 result = tv_get_bool_chk(&var2, &error);
2495 else if (tv_get_number_chk(&var2, &error) != 0)
2496 result = TRUE;
2497 clear_tv(&var2);
2498 if (error)
2499 return FAIL;
2500 }
2501 if (evaluate)
2502 {
2503 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002504 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002505 rettv->v_type = VAR_BOOL;
2506 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002507 }
2508 else
2509 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002510 rettv->v_type = VAR_NUMBER;
2511 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514
2515 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002517
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 if (evalarg == NULL)
2519 clear_evalarg(&local_evalarg, NULL);
2520 else
2521 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523
2524 return OK;
2525}
2526
2527/*
2528 * Handle second level expression:
2529 * expr3 && expr3 && expr3 logical AND
2530 *
2531 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002532 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 *
2534 * Return OK or FAIL.
2535 */
2536 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002537eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002539 char_u *p;
2540 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 /*
2543 * Get the first variable.
2544 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 return FAIL;
2547
2548 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002551 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554 evalarg_T *evalarg_used = evalarg;
2555 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002556 int orig_flags;
2557 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002558 long result = TRUE;
2559 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002560 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002561 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002562
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002563 if (evalarg == NULL)
2564 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 CLEAR_FIELD(local_evalarg);
2566 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002567 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 orig_flags = evalarg_used->eval_flags;
2569 evaluate = orig_flags & EVAL_EVALUATE;
2570 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002572 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002573 result = tv_get_bool_chk(rettv, &error);
2574 else if (tv_get_number_chk(rettv, &error) == 0)
2575 result = FALSE;
2576 clear_tv(rettv);
2577 if (error)
2578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
2581 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 while (p[0] == '&' && p[1] == '&')
2585 {
2586 if (getnext)
2587 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002588 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002589 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002590 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002591 {
2592 error_white_both(p, 2);
2593 clear_tv(rettv);
2594 return FAIL;
2595 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002596 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002597 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598
2599 /*
2600 * Get the second variable.
2601 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002602 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2603 {
mityu4ac198c2021-05-28 17:52:40 +02002604 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 clear_tv(rettv);
2606 return FAIL;
2607 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2609 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002611 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002613 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614
2615 /*
2616 * Compute the result.
2617 */
2618 if (evaluate && result)
2619 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002620 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002621 result = tv_get_bool_chk(&var2, &error);
2622 else if (tv_get_number_chk(&var2, &error) == 0)
2623 result = FALSE;
2624 clear_tv(&var2);
2625 if (error)
2626 return FAIL;
2627 }
2628 if (evaluate)
2629 {
2630 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002631 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002632 rettv->v_type = VAR_BOOL;
2633 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002634 }
2635 else
2636 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002637 rettv->v_type = VAR_NUMBER;
2638 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641
2642 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002644
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 if (evalarg == NULL)
2646 clear_evalarg(&local_evalarg, NULL);
2647 else
2648 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650
2651 return OK;
2652}
2653
2654/*
2655 * Handle third level expression:
2656 * var1 == var2
2657 * var1 =~ var2
2658 * var1 != var2
2659 * var1 !~ var2
2660 * var1 > var2
2661 * var1 >= var2
2662 * var1 < var2
2663 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002664 * var1 is var2
2665 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 *
2667 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002668 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 *
2670 * Return OK or FAIL.
2671 */
2672 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002676 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002677 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002679 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 /*
2682 * Get the first variable.
2683 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return FAIL;
2686
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002687 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002688 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002691 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002693 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002695 typval_T var2;
2696 int ic;
2697 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002698 int evaluate = evalarg == NULL
2699 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002700
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002701 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002702 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002703 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002704 p = *arg;
2705 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002706 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2707 {
mityu4ac198c2021-05-28 17:52:40 +02002708 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002709 clear_tv(rettv);
2710 return FAIL;
2711 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002712
Bram Moolenaar696ba232020-07-29 21:20:41 +02002713 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2714 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002715 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002716 clear_tv(rettv);
2717 return FAIL;
2718 }
2719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002720 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (p[len] == '?')
2722 {
2723 ic = TRUE;
2724 ++len;
2725 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002726 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 else if (p[len] == '#')
2728 {
2729 ic = FALSE;
2730 ++len;
2731 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002732 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002734 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * Get the second variable.
2738 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002739 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2740 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002741 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 clear_tv(rettv);
2743 return FAIL;
2744 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002745 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002748 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 return FAIL;
2750 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002751 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002752 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002753 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002754
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002755 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002756 {
2757 ret = FAIL;
2758 clear_tv(rettv);
2759 }
2760 else
2761 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002762 clear_tv(&var2);
2763 return ret;
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766
2767 return OK;
2768}
2769
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002770/*
2771 * Make a copy of blob "tv1" and append blob "tv2".
2772 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 void
2774eval_addblob(typval_T *tv1, typval_T *tv2)
2775{
2776 blob_T *b1 = tv1->vval.v_blob;
2777 blob_T *b2 = tv2->vval.v_blob;
2778 blob_T *b = blob_alloc();
2779 int i;
2780
2781 if (b != NULL)
2782 {
2783 for (i = 0; i < blob_len(b1); i++)
2784 ga_append(&b->bv_ga, blob_get(b1, i));
2785 for (i = 0; i < blob_len(b2); i++)
2786 ga_append(&b->bv_ga, blob_get(b2, i));
2787
2788 clear_tv(tv1);
2789 rettv_blob_set(tv1, b);
2790 }
2791}
2792
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002793/*
2794 * Make a copy of list "tv1" and append list "tv2".
2795 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 int
2797eval_addlist(typval_T *tv1, typval_T *tv2)
2798{
2799 typval_T var3;
2800
2801 // concatenate Lists
2802 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2803 {
2804 clear_tv(tv1);
2805 clear_tv(tv2);
2806 return FAIL;
2807 }
2808 clear_tv(tv1);
2809 *tv1 = var3;
2810 return OK;
2811}
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813/*
2814 * Handle fourth level expression:
2815 * + number addition
2816 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002817 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002818 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 *
2820 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002821 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 *
2823 * Return OK or FAIL.
2824 */
2825 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /*
2829 * Get the first variable.
2830 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002831 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 return FAIL;
2833
2834 /*
2835 * Repeat computing, until no '+', '-' or '.' is following.
2836 */
2837 for (;;)
2838 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002839 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 int getnext;
2841 char_u *p;
2842 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002843 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 int concat;
2845 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002846 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002848 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002849 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002850 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 p = eval_next_non_blank(*arg, evalarg, &getnext);
2852 op = *p;
2853 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002854 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2855 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002857 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2858 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002859
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002860 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002861 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002863 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002864 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002865 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002866 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002867 {
mityu4ac198c2021-05-28 17:52:40 +02002868 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002869 clear_tv(rettv);
2870 return FAIL;
2871 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002872 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002873 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002874 if ((op != '+' || (rettv->v_type != VAR_LIST
2875 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002876#ifdef FEAT_FLOAT
2877 && (op == '.' || rettv->v_type != VAR_FLOAT)
2878#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002879 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002880 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002881 int error = FALSE;
2882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002883 // For "list + ...", an illegal use of the first operand as
2884 // a number cannot be determined before evaluating the 2nd
2885 // operand: if this is also a list, all is ok.
2886 // For "something . ...", "something - ..." or "non-list + ...",
2887 // we know that the first operand needs to be a string or number
2888 // without evaluating the 2nd operand. So check before to avoid
2889 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002890 if (op != '.')
2891 tv_get_number_chk(rettv, &error);
2892 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002893 {
2894 clear_tv(rettv);
2895 return FAIL;
2896 }
2897 }
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /*
2900 * Get the second variable.
2901 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002902 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002903 {
mityu89dcb4d2021-05-28 13:50:17 +02002904 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002905 clear_tv(rettv);
2906 return FAIL;
2907 }
2908 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002909 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 return FAIL;
2913 }
2914
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 /*
2918 * Compute the result.
2919 */
2920 if (op == '.')
2921 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2923 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002924 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925
Bram Moolenaar2e086612020-08-25 22:37:48 +02002926 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002927 || var2.v_type == VAR_CHANNEL
2928 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002929 semsg(_(e_using_invalid_value_as_string_str),
2930 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002932 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002933 {
2934 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2935 var2.vval.v_float);
2936 s2 = buf2;
2937 }
2938#endif
2939 else
2940 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002941 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
2943 clear_tv(rettv);
2944 clear_tv(&var2);
2945 return FAIL;
2946 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 clear_tv(rettv);
2949 rettv->v_type = VAR_STRING;
2950 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002952 else if (op == '+' && rettv->v_type == VAR_BLOB
2953 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002955 else if (op == '+' && rettv->v_type == VAR_LIST
2956 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002957 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002959 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else
2962 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963 int error = FALSE;
2964 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966 float_T f1 = 0, f2 = 0;
2967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002968 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002970 f1 = rettv->vval.v_float;
2971 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002972 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 else
2974#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 if (error)
2978 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002979 // This can only happen for "list + non-list" or
2980 // "blob + non-blob". For "non-list + ..." or
2981 // "something - ...", we returned before evaluating the
2982 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002984 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 return FAIL;
2986 }
2987#ifdef FEAT_FLOAT
2988 if (var2.v_type == VAR_FLOAT)
2989 f1 = n1;
2990#endif
2991 }
2992#ifdef FEAT_FLOAT
2993 if (var2.v_type == VAR_FLOAT)
2994 {
2995 f2 = var2.vval.v_float;
2996 n2 = 0;
2997 }
2998 else
2999#endif
3000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003001 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 if (error)
3003 {
3004 clear_tv(rettv);
3005 clear_tv(&var2);
3006 return FAIL;
3007 }
3008#ifdef FEAT_FLOAT
3009 if (rettv->v_type == VAR_FLOAT)
3010 f2 = n2;
3011#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003013 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014
3015#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003016 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3018 {
3019 if (op == '+')
3020 f1 = f1 + f2;
3021 else
3022 f1 = f1 - f2;
3023 rettv->v_type = VAR_FLOAT;
3024 rettv->vval.v_float = f1;
3025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027#endif
3028 {
3029 if (op == '+')
3030 n1 = n1 + n2;
3031 else
3032 n1 = n1 - n2;
3033 rettv->v_type = VAR_NUMBER;
3034 rettv->vval.v_number = n1;
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003037 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 }
3040 return OK;
3041}
3042
3043/*
3044 * Handle fifth level expression:
3045 * * number multiplication
3046 * / number division
3047 * % number modulo
3048 *
3049 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003050 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 *
3052 * Return OK or FAIL.
3053 */
3054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055eval6(
3056 char_u **arg,
3057 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003059 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003062 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 /*
3066 * Get the first variable.
3067 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003068 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return FAIL;
3070
3071 /*
3072 * Repeat computing, until no '*', '/' or '%' is following.
3073 */
3074 for (;;)
3075 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003076 int evaluate;
3077 int getnext;
3078 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003079 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003080 int op;
3081 varnumber_T n1, n2;
3082#ifdef FEAT_FLOAT
3083 float_T f1, f2;
3084#endif
3085 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003086
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003087 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003088 p = eval_next_non_blank(*arg, evalarg, &getnext);
3089 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003090 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003092
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003093 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003095 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003096 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003097 {
3098 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3099 {
mityu4ac198c2021-05-28 17:52:40 +02003100 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003101 clear_tv(rettv);
3102 return FAIL;
3103 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003104 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003107#ifdef FEAT_FLOAT
3108 f1 = 0;
3109 f2 = 0;
3110#endif
3111 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003112 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114#ifdef FEAT_FLOAT
3115 if (rettv->v_type == VAR_FLOAT)
3116 {
3117 f1 = rettv->vval.v_float;
3118 use_float = TRUE;
3119 n1 = 0;
3120 }
3121 else
3122#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003124 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003125 if (error)
3126 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
3128 else
3129 n1 = 0;
3130
3131 /*
3132 * Get the second variable.
3133 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003134 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3135 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003136 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 clear_tv(rettv);
3138 return FAIL;
3139 }
3140 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003141 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return FAIL;
3143
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146#ifdef FEAT_FLOAT
3147 if (var2.v_type == VAR_FLOAT)
3148 {
3149 if (!use_float)
3150 {
3151 f1 = n1;
3152 use_float = TRUE;
3153 }
3154 f2 = var2.vval.v_float;
3155 n2 = 0;
3156 }
3157 else
3158#endif
3159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003160 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 clear_tv(&var2);
3162 if (error)
3163 return FAIL;
3164#ifdef FEAT_FLOAT
3165 if (use_float)
3166 f2 = n2;
3167#endif
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /*
3171 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174#ifdef FEAT_FLOAT
3175 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (op == '*')
3178 f1 = f1 * f2;
3179 else if (op == '/')
3180 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003181# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003183 if (f2 == 0.0)
3184 {
3185 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003186 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003187 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003188 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003189 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003190 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003191 }
3192 else
3193 f1 = f1 / f2;
3194# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 // We rely on the floating point library to handle divide
3196 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003198# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203 return FAIL;
3204 }
3205 rettv->v_type = VAR_FLOAT;
3206 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003211 int failed = FALSE;
3212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213 if (op == '*')
3214 n1 = n1 * n2;
3215 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003216 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003218 n1 = num_modulus(n1, n2, &failed);
3219 if (failed)
3220 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003221
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222 rettv->v_type = VAR_NUMBER;
3223 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226 }
3227
3228 return OK;
3229}
3230
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003231/*
3232 * Handle a type cast before a base level expression.
3233 * "arg" must point to the first non-white of the expression.
3234 * "arg" is advanced to just after the recognized expression.
3235 * Return OK or FAIL.
3236 */
3237 static int
3238eval7t(
3239 char_u **arg,
3240 typval_T *rettv,
3241 evalarg_T *evalarg,
3242 int want_string) // after "." operator
3243{
3244 type_T *want_type = NULL;
3245 garray_T type_list; // list of pointers to allocated types
3246 int res;
3247 int evaluate = evalarg == NULL ? 0
3248 : (evalarg->eval_flags & EVAL_EVALUATE);
3249
3250 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003251 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3252 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003253 {
3254 ++*arg;
3255 ga_init2(&type_list, sizeof(type_T *), 10);
3256 want_type = parse_type(arg, &type_list, TRUE);
3257 if (want_type == NULL && (evaluate || **arg != '>'))
3258 {
3259 clear_type_list(&type_list);
3260 return FAIL;
3261 }
3262
3263 if (**arg != '>')
3264 {
3265 if (*skipwhite(*arg) == '>')
3266 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3267 else
3268 emsg(_(e_missing_gt));
3269 clear_type_list(&type_list);
3270 return FAIL;
3271 }
3272 ++*arg;
3273 *arg = skipwhite_and_linebreak(*arg, evalarg);
3274 }
3275
3276 res = eval7(arg, rettv, evalarg, want_string);
3277
3278 if (want_type != NULL && evaluate)
3279 {
3280 if (res == OK)
3281 {
3282 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3283
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003284 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003285 {
3286 if (want_type == &t_bool && actual != &t_bool
3287 && (actual->tt_flags & TTFLAG_BOOL_OK))
3288 {
3289 int n = tv2bool(rettv);
3290
3291 // can use "0" and "1" for boolean in some places
3292 clear_tv(rettv);
3293 rettv->v_type = VAR_BOOL;
3294 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3295 }
3296 else
3297 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003298 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003299
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003300 where.wt_variable = TRUE;
3301 res = check_type(want_type, actual, TRUE, where);
3302 }
3303 }
3304 }
3305 clear_type_list(&type_list);
3306 }
3307
3308 return res;
3309}
3310
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003311 int
3312eval_leader(char_u **arg, int vim9)
3313{
3314 char_u *s = *arg;
3315 char_u *p = *arg;
3316
3317 while (*p == '!' || *p == '-' || *p == '+')
3318 {
3319 char_u *n = skipwhite(p + 1);
3320
3321 // ++, --, -+ and +- are not accepted in Vim9 script
3322 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3323 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003324 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003325 return FAIL;
3326 }
3327 p = n;
3328 }
3329 *arg = p;
3330 return OK;
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Handle sixth level expression:
3335 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003336 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003337 * "string" string constant
3338 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * &option-name option value
3340 * @r register contents
3341 * identifier variable value
3342 * function() function call
3343 * $VAR environment variable
3344 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003345 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003347 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003348 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 *
3350 * Also handle:
3351 * ! in front logical NOT
3352 * - in front unary minus
3353 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 * trailing [] subscript in String or List
3355 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003356 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 *
3358 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003359 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 *
3361 * Return OK or FAIL.
3362 */
3363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364eval7(
3365 char_u **arg,
3366 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003367 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003370 int evaluate = evalarg != NULL
3371 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int len;
3373 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 char_u *start_leader, *end_leader;
3375 int ret = OK;
3376 char_u *alias;
3377
3378 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003380 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003385 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
3387 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003388 if (eval_leader(arg, in_vim9script()) == FAIL)
3389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 end_leader = *arg;
3391
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003392 if (**arg == '.' && (!isdigit(*(*arg + 1))
3393#ifdef FEAT_FLOAT
3394 || current_sctx.sc_version < 2
3395#endif
3396 ))
3397 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003398 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003399 ++*arg;
3400 return FAIL;
3401 }
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 switch (**arg)
3404 {
3405 /*
3406 * Number constant.
3407 */
3408 case '0':
3409 case '1':
3410 case '2':
3411 case '3':
3412 case '4':
3413 case '5':
3414 case '6':
3415 case '7':
3416 case '8':
3417 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003418 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003419
3420 // Apply prefixed "-" and "+" now. Matters especially when
3421 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003422 if (ret == OK && evaluate && end_leader > start_leader
3423 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003424 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 break;
3426
3427 /*
3428 * String constant: "string".
3429 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003430 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 break;
3432
3433 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003434 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003436 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003437 break;
3438
3439 /*
3440 * List: [expr, expr]
3441 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003442 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 break;
3444
3445 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003446 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003447 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003448 case '#': if (in_vim9script())
3449 {
3450 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3451 }
3452 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003453 {
3454 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003455 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003456 }
3457 else
3458 ret = NOTDONE;
3459 break;
3460
3461 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003462 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003463 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003465 case '{': if (in_vim9script())
3466 ret = NOTDONE;
3467 else
3468 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003469 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003470 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003471 break;
3472
3473 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003474 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003476 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 break;
3478
3479 /*
3480 * Environment variable: $VAR.
3481 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003482 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 break;
3484
3485 /*
3486 * Register contents: @r.
3487 */
3488 case '@': ++*arg;
3489 if (evaluate)
3490 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003491 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3492 semsg(_(e_syntax_error_at_str), *arg);
3493 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3494 emsg_invreg(**arg);
3495 else
3496 {
3497 rettv->v_type = VAR_STRING;
3498 rettv->vval.v_string = get_reg_contents(**arg,
3499 GREG_EXPR_SRC);
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 if (**arg != NUL)
3503 ++*arg;
3504 break;
3505
3506 /*
3507 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003508 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003510 case '(': ret = NOTDONE;
3511 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003512 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003513 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003514 if (ret == OK && evaluate)
3515 {
3516 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3517
Bram Moolenaara9931532021-06-12 15:58:16 +02003518 // Compile it here to get the return type. The return
3519 // type is optional, when it's missing use t_unknown.
3520 // This is recognized in compile_return().
3521 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3522 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003523 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003524 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003525 {
3526 clear_tv(rettv);
3527 ret = FAIL;
3528 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003529 }
3530 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003531 if (ret == NOTDONE)
3532 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003533 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003534 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003535
Bram Moolenaar9215f012020-06-27 21:18:00 +02003536 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003537 if (**arg == ')')
3538 ++*arg;
3539 else if (ret == OK)
3540 {
3541 emsg(_(e_missing_close));
3542 clear_tv(rettv);
3543 ret = FAIL;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546 break;
3547
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551
3552 if (ret == NOTDONE)
3553 {
3554 /*
3555 * Must be a variable or function name.
3556 * Can also be a curly-braces kind of name: {expr}.
3557 */
3558 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003559 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560 if (alias != NULL)
3561 s = alias;
3562
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003563 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 ret = FAIL;
3565 else
3566 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003567 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3568
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003569 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003570 {
3571 emsg(_(e_cannot_use_underscore_here));
3572 ret = FAIL;
3573 }
3574 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003575 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003576 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003577 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003578 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003579 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003580 else if (flags & EVAL_CONSTANT)
3581 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003583 {
3584 // get the value of "true", "false" or a variable
3585 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3586 {
3587 rettv->v_type = VAR_BOOL;
3588 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003589 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003590 }
3591 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003592 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003593 {
3594 rettv->v_type = VAR_BOOL;
3595 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003596 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003597 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003598 else if (len == 4 && in_vim9script()
3599 && STRNCMP(s, "null", 4) == 0)
3600 {
3601 rettv->v_type = VAR_SPECIAL;
3602 rettv->vval.v_number = VVAL_NULL;
3603 ret = OK;
3604 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003605 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003606 ret = eval_variable(s, len, rettv, NULL,
3607 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003608 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003609 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003610 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003611 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003612 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003613 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003616 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 }
3618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3620 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003621 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003622 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 /*
3625 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3626 */
3627 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003628 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003629 return ret;
3630}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003631
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003632/*
3633 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003634 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003635 * Adjusts "end_leaderp" until it is at "start_leader".
3636 */
3637 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003638eval7_leader(
3639 typval_T *rettv,
3640 int numeric_only,
3641 char_u *start_leader,
3642 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003643{
3644 char_u *end_leader = *end_leaderp;
3645 int ret = OK;
3646 int error = FALSE;
3647 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003648 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003649#ifdef FEAT_FLOAT
3650 float_T f = 0.0;
3651
3652 if (rettv->v_type == VAR_FLOAT)
3653 f = rettv->vval.v_float;
3654 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003655#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003656 {
3657 while (VIM_ISWHITE(end_leader[-1]))
3658 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003659 if (in_vim9script() && end_leader[-1] == '!')
3660 val = tv2bool(rettv);
3661 else
3662 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003663 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664 if (error)
3665 {
3666 clear_tv(rettv);
3667 ret = FAIL;
3668 }
3669 else
3670 {
3671 while (end_leader > start_leader)
3672 {
3673 --end_leader;
3674 if (*end_leader == '!')
3675 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003676 if (numeric_only)
3677 {
3678 ++end_leader;
3679 break;
3680 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003681#ifdef FEAT_FLOAT
3682 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003683 {
3684 if (in_vim9script())
3685 {
3686 rettv->v_type = VAR_BOOL;
3687 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3688 }
3689 else
3690 f = !f;
3691 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003692 else
3693#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003694 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003695 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003696 type = VAR_BOOL;
3697 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003698 }
3699 else if (*end_leader == '-')
3700 {
3701#ifdef FEAT_FLOAT
3702 if (rettv->v_type == VAR_FLOAT)
3703 f = -f;
3704 else
3705#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003706 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003707 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003708 type = VAR_NUMBER;
3709 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003710 }
3711 }
3712#ifdef FEAT_FLOAT
3713 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003715 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003716 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003720 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003722 if (in_vim9script())
3723 rettv->v_type = type;
3724 else
3725 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003726 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003729 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return ret;
3731}
3732
3733/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003734 * Call the function referred to in "rettv".
3735 */
3736 static int
3737call_func_rettv(
3738 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003739 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003740 typval_T *rettv,
3741 int evaluate,
3742 dict_T *selfdict,
3743 typval_T *basetv)
3744{
3745 partial_T *pt = NULL;
3746 funcexe_T funcexe;
3747 typval_T functv;
3748 char_u *s;
3749 int ret;
3750
3751 // need to copy the funcref so that we can clear rettv
3752 if (evaluate)
3753 {
3754 functv = *rettv;
3755 rettv->v_type = VAR_UNKNOWN;
3756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003757 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003758 if (functv.v_type == VAR_PARTIAL)
3759 {
3760 pt = functv.vval.v_partial;
3761 s = partial_name(pt);
3762 }
3763 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003764 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003765 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003766 if (s == NULL || *s == NUL)
3767 {
3768 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003769 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003770 goto theend;
3771 }
3772 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003773 }
3774 else
3775 s = (char_u *)"";
3776
Bram Moolenaara80faa82020-04-12 19:37:17 +02003777 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003778 funcexe.firstline = curwin->w_cursor.lnum;
3779 funcexe.lastline = curwin->w_cursor.lnum;
3780 funcexe.evaluate = evaluate;
3781 funcexe.partial = pt;
3782 funcexe.selfdict = selfdict;
3783 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003784 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003785
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003786theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003787 // Clear the funcref afterwards, so that deleting it while
3788 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003789 if (evaluate)
3790 clear_tv(&functv);
3791
3792 return ret;
3793}
3794
3795/*
3796 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003797 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003798 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3799 */
3800 static int
3801eval_lambda(
3802 char_u **arg,
3803 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003804 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003806{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003807 int evaluate = evalarg != NULL
3808 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809 typval_T base = *rettv;
3810 int ret;
3811
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812 rettv->v_type = VAR_UNKNOWN;
3813
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003814 if (**arg == '{')
3815 {
3816 // ->{lambda}()
3817 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3818 }
3819 else
3820 {
3821 // ->(lambda)()
3822 ++*arg;
3823 ret = eval1(arg, rettv, evalarg);
3824 *arg = skipwhite_and_linebreak(*arg, evalarg);
3825 if (**arg != ')')
3826 {
3827 emsg(_(e_missing_close));
3828 ret = FAIL;
3829 }
3830 ++*arg;
3831 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003832 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003833 return FAIL;
3834 else if (**arg != '(')
3835 {
3836 if (verbose)
3837 {
3838 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003839 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003840 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003842 }
3843 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003844 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003846 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003847 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003848
3849 // Clear the funcref afterwards, so that deleting it while
3850 // evaluating the arguments is possible (see test55).
3851 if (evaluate)
3852 clear_tv(&base);
3853
3854 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003855}
3856
3857/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003858 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003859 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003860 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3861 */
3862 static int
3863eval_method(
3864 char_u **arg,
3865 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003866 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003868{
3869 char_u *name;
3870 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003871 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003872 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003873 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003874 int evaluate = evalarg != NULL
3875 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003876
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003877 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003878
Bram Moolenaarac92e252019-08-03 21:58:38 +02003879 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003880 len = get_name_len(arg, &alias, evaluate, TRUE);
3881 if (alias != NULL)
3882 name = alias;
3883
3884 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003885 {
3886 if (verbose)
3887 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003888 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003889 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003890 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003891 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003892 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003893 if (**arg != '(')
3894 {
3895 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003897 ret = FAIL;
3898 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003899 else if (VIM_ISWHITE((*arg)[-1]))
3900 {
3901 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003902 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003903 ret = FAIL;
3904 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003905 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003906 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003907 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003908 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003909
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003910 // Clear the funcref afterwards, so that deleting it while
3911 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003912 if (evaluate)
3913 clear_tv(&base);
3914
Bram Moolenaarac92e252019-08-03 21:58:38 +02003915 return ret;
3916}
3917
3918/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003919 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3920 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003921 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3922 */
3923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924eval_index(
3925 char_u **arg,
3926 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003927 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003928 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003929{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003930 int evaluate = evalarg != NULL
3931 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003932 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003934 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003935 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003936 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003937 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003938
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003939 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3940 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003942 init_tv(&var1);
3943 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003944 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003946 /*
3947 * dict.name
3948 */
3949 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003951 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003952 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003953 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003954 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003955 }
3956 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 /*
3959 * something[idx]
3960 *
3961 * Get the (first) variable from inside the [].
3962 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003963 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 if (**arg == ':')
3965 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003966 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003968 else if (vim9 && **arg == ':')
3969 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003970 semsg(_(e_white_space_required_before_and_after_str_at_str),
3971 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003972 clear_tv(&var1);
3973 return FAIL;
3974 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003975 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003977#ifdef FEAT_FLOAT
3978 // allow for indexing with float
3979 if (vim9 && rettv->v_type == VAR_DICT
3980 && var1.v_type == VAR_FLOAT)
3981 {
3982 var1.vval.v_string = typval_tostring(&var1, TRUE);
3983 var1.v_type = VAR_STRING;
3984 }
3985#endif
3986 if (tv_get_string_chk(&var1) == NULL)
3987 {
3988 // not a number or string
3989 clear_tv(&var1);
3990 return FAIL;
3991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003993
3994 /*
3995 * Get the second variable from inside the [:].
3996 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003997 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003998 if (**arg == ':')
3999 {
4000 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004001 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004002 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004003 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004004 semsg(_(e_white_space_required_before_and_after_str_at_str),
4005 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004006 if (!empty1)
4007 clear_tv(&var1);
4008 return FAIL;
4009 }
4010 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004011 if (**arg == ']')
4012 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004013 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (!empty1)
4016 clear_tv(&var1);
4017 return FAIL;
4018 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004021 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022 if (!empty1)
4023 clear_tv(&var1);
4024 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 return FAIL;
4026 }
4027 }
4028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004030 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 if (**arg != ']')
4032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004033 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004034 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004035 clear_tv(&var1);
4036 if (range)
4037 clear_tv(&var2);
4038 return FAIL;
4039 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004040 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004041 }
4042
4043 if (evaluate)
4044 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004046 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004047 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004048
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004049 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004051 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004052 clear_tv(&var2);
4053 return res;
4054 }
4055 return OK;
4056}
4057
4058/*
4059 * Check if "rettv" can have an [index] or [sli:ce]
4060 */
4061 int
4062check_can_index(typval_T *rettv, int evaluate, int verbose)
4063{
4064 switch (rettv->v_type)
4065 {
4066 case VAR_FUNC:
4067 case VAR_PARTIAL:
4068 if (verbose)
4069 emsg(_("E695: Cannot index a Funcref"));
4070 return FAIL;
4071 case VAR_FLOAT:
4072#ifdef FEAT_FLOAT
4073 if (verbose)
4074 emsg(_(e_float_as_string));
4075 return FAIL;
4076#endif
4077 case VAR_BOOL:
4078 case VAR_SPECIAL:
4079 case VAR_JOB:
4080 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004081 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004082 if (verbose)
4083 emsg(_(e_cannot_index_special_variable));
4084 return FAIL;
4085 case VAR_UNKNOWN:
4086 case VAR_ANY:
4087 case VAR_VOID:
4088 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004089 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004090 emsg(_(e_cannot_index_special_variable));
4091 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004092 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004094
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004095 case VAR_STRING:
4096 case VAR_LIST:
4097 case VAR_DICT:
4098 case VAR_BLOB:
4099 break;
4100 case VAR_NUMBER:
4101 if (in_vim9script())
4102 emsg(_(e_cannot_index_number));
4103 break;
4104 }
4105 return OK;
4106}
4107
4108/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004109 * slice() function
4110 */
4111 void
4112f_slice(typval_T *argvars, typval_T *rettv)
4113{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004114 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004115 && ((argvars[0].v_type != VAR_STRING
4116 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004117 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004118 && check_for_list_arg(argvars, 0) == FAIL)
4119 || check_for_number_arg(argvars, 1) == FAIL
4120 || check_for_opt_number_arg(argvars, 2) == FAIL))
4121 return;
4122
Bram Moolenaar6601b622021-01-13 21:47:15 +01004123 if (check_can_index(argvars, TRUE, FALSE) == OK)
4124 {
4125 copy_tv(argvars, rettv);
4126 eval_index_inner(rettv, TRUE, argvars + 1,
4127 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4128 TRUE, NULL, 0, FALSE);
4129 }
4130}
4131
4132/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004133 * Apply index or range to "rettv".
4134 * "var1" is the first index, NULL for [:expr].
4135 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004136 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4137 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004138 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4139 */
4140 int
4141eval_index_inner(
4142 typval_T *rettv,
4143 int is_range,
4144 typval_T *var1,
4145 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004146 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004147 char_u *key,
4148 int keylen,
4149 int verbose)
4150{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004151 varnumber_T n1, n2 = 0;
4152 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153
4154 n1 = 0;
4155 if (var1 != NULL && rettv->v_type != VAR_DICT)
4156 n1 = tv_get_number(var1);
4157
4158 if (is_range)
4159 {
4160 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004161 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (verbose)
4163 emsg(_(e_cannot_slice_dictionary));
4164 return FAIL;
4165 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004168 else
4169 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004171
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004172 switch (rettv->v_type)
4173 {
4174 case VAR_UNKNOWN:
4175 case VAR_ANY:
4176 case VAR_VOID:
4177 case VAR_FUNC:
4178 case VAR_PARTIAL:
4179 case VAR_FLOAT:
4180 case VAR_BOOL:
4181 case VAR_SPECIAL:
4182 case VAR_JOB:
4183 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004184 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004185 break; // not evaluating, skipping over subscript
4186
4187 case VAR_NUMBER:
4188 case VAR_STRING:
4189 {
4190 char_u *s = tv_get_string(rettv);
4191
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004193 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004194 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004195 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004196 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004197 else
4198 s = char_from_string(s, n1);
4199 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004200 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004202 // The resulting variable is a substring. If the indexes
4203 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004204 if (n1 < 0)
4205 {
4206 n1 = len + n1;
4207 if (n1 < 0)
4208 n1 = 0;
4209 }
4210 if (n2 < 0)
4211 n2 = len + n2;
4212 else if (n2 >= len)
4213 n2 = len;
4214 if (n1 >= len || n2 < 0 || n1 > n2)
4215 s = NULL;
4216 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004217 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 }
4219 else
4220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004221 // The resulting variable is a string of a single
4222 // character. If the index is too big or negative the
4223 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 if (n1 >= len || n1 < 0)
4225 s = NULL;
4226 else
4227 s = vim_strnsave(s + n1, 1);
4228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 clear_tv(rettv);
4230 rettv->v_type = VAR_STRING;
4231 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 }
4233 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004236 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4237 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004238 break;
4239
4240 case VAR_LIST:
4241 if (var1 == NULL)
4242 n1 = 0;
4243 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004244 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004246 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 return FAIL;
4248 break;
4249
4250 case VAR_DICT:
4251 {
4252 dictitem_T *item;
4253 typval_T tmp;
4254
4255 if (key == NULL)
4256 {
4257 key = tv_get_string_chk(var1);
4258 if (key == NULL)
4259 return FAIL;
4260 }
4261
4262 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4263
4264 if (item == NULL && verbose)
4265 semsg(_(e_dictkey), key);
4266 if (item == NULL)
4267 return FAIL;
4268
4269 copy_tv(&item->di_tv, &tmp);
4270 clear_tv(rettv);
4271 *rettv = tmp;
4272 }
4273 break;
4274 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 return OK;
4276}
4277
4278/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004279 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004280 */
4281 char_u *
4282partial_name(partial_T *pt)
4283{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004284 if (pt != NULL)
4285 {
4286 if (pt->pt_name != NULL)
4287 return pt->pt_name;
4288 if (pt->pt_func != NULL)
4289 return pt->pt_func->uf_name;
4290 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004291 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004292}
4293
Bram Moolenaarddecc252016-04-06 22:59:37 +02004294 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004295partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004296{
4297 int i;
4298
4299 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004301 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004302 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303 if (pt->pt_name != NULL)
4304 {
4305 func_unref(pt->pt_name);
4306 vim_free(pt->pt_name);
4307 }
4308 else
4309 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004310
Bram Moolenaar54656012021-06-09 20:50:46 +02004311 // "out_up" is no longer used, decrement refcount on partial that owns it.
4312 partial_unref(pt->pt_outer.out_up_partial);
4313
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004314 // Decrease the reference count for the context of a closure. If down
4315 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004316 if (pt->pt_funcstack != NULL)
4317 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004318 --pt->pt_funcstack->fs_refcount;
4319 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004320 }
4321
Bram Moolenaarddecc252016-04-06 22:59:37 +02004322 vim_free(pt);
4323}
4324
4325/*
4326 * Unreference a closure: decrement the reference count and free it when it
4327 * becomes zero.
4328 */
4329 void
4330partial_unref(partial_T *pt)
4331{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004332 if (pt != NULL)
4333 {
4334 if (--pt->pt_refcount <= 0)
4335 partial_free(pt);
4336
4337 // If the reference count goes down to one, the funcstack may be the
4338 // only reference and can be freed if no other partials reference it.
4339 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4340 funcstack_check_refcount(pt->pt_funcstack);
4341 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004342}
4343
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004345 * Return the next (unique) copy ID.
4346 * Used for serializing nested structures.
4347 */
4348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004349get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004350{
4351 current_copyID += COPYID_INC;
4352 return current_copyID;
4353}
4354
4355/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004356 * Garbage collection for lists and dictionaries.
4357 *
4358 * We use reference counts to be able to free most items right away when they
4359 * are no longer used. But for composite items it's possible that it becomes
4360 * unused while the reference count is > 0: When there is a recursive
4361 * reference. Example:
4362 * :let l = [1, 2, 3]
4363 * :let d = {9: l}
4364 * :let l[1] = d
4365 *
4366 * Since this is quite unusual we handle this with garbage collection: every
4367 * once in a while find out which lists and dicts are not referenced from any
4368 * variable.
4369 *
4370 * Here is a good reference text about garbage collection (refers to Python
4371 * but it applies to all reference-counting mechanisms):
4372 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004373 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004374
4375/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004376 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004377 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004378 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004379 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004381garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004382{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004383 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004384 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004385 buf_T *buf;
4386 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004387 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004388 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004390 if (!testing)
4391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004392 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004393 want_garbage_collect = FALSE;
4394 may_garbage_collect = FALSE;
4395 garbage_collect_at_exit = FALSE;
4396 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004397
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004398 // The execution stack can grow big, limit the size.
4399 if (exestack.ga_maxlen - exestack.ga_len > 500)
4400 {
4401 size_t new_len;
4402 char_u *pp;
4403 int n;
4404
4405 // Keep 150% of the current size, with a minimum of the growth size.
4406 n = exestack.ga_len / 2;
4407 if (n < exestack.ga_growsize)
4408 n = exestack.ga_growsize;
4409
4410 // Don't make it bigger though.
4411 if (exestack.ga_len + n < exestack.ga_maxlen)
4412 {
4413 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4414 pp = vim_realloc(exestack.ga_data, new_len);
4415 if (pp == NULL)
4416 return FAIL;
4417 exestack.ga_maxlen = exestack.ga_len + n;
4418 exestack.ga_data = pp;
4419 }
4420 }
4421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004422 // We advance by two because we add one for items referenced through
4423 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004424 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004425
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004426 /*
4427 * 1. Go through all accessible variables and mark all lists and dicts
4428 * with copyID.
4429 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 // Don't free variables in the previous_funccal list unless they are only
4432 // referenced through previous_funccal. This must be first, because if
4433 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004437 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004440 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004441 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4442 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004445 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004446 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4447 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004448 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4450 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004451#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004452 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004453 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4454 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004455 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004456 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004457 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4458 NULL, NULL);
4459#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004461 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004462 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4464 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004465 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004466 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004472 abort = abort || set_ref_in_functions(copyID);
4473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004474 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004478 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004479
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004480 // callbacks in buffers
4481 abort = abort || set_ref_in_buffers(copyID);
4482
Bram Moolenaar1dced572012-04-05 16:54:08 +02004483#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004484 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004485#endif
4486
Bram Moolenaardb913952012-06-29 12:54:53 +02004487#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004488 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004489#endif
4490
4491#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004492 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004493#endif
4494
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004496 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004497 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004498#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004499#ifdef FEAT_NETBEANS_INTG
4500 abort = abort || set_ref_in_nb_channel(copyID);
4501#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004502
Bram Moolenaare3188e22016-05-31 21:13:04 +02004503#ifdef FEAT_TIMERS
4504 abort = abort || set_ref_in_timer(copyID);
4505#endif
4506
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004507#ifdef FEAT_QUICKFIX
4508 abort = abort || set_ref_in_quickfix(copyID);
4509#endif
4510
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004511#ifdef FEAT_TERMINAL
4512 abort = abort || set_ref_in_term(copyID);
4513#endif
4514
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004515#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004516 abort = abort || set_ref_in_popups(copyID);
4517#endif
4518
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004520 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 /*
4522 * 2. Free lists and dictionaries that are not referenced.
4523 */
4524 did_free = free_unref_items(copyID);
4525
4526 /*
4527 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004531 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 else if (p_verbose > 0)
4533 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004534 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004536
4537 return did_free;
4538}
4539
4540/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004541 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004542 */
4543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004544free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004545{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004546 int did_free = FALSE;
4547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // Let all "free" functions know that we are here. This means no
4549 // dictionaries, lists, channels or jobs are to be freed, because we will
4550 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004551 in_free_unref_items = TRUE;
4552
4553 /*
4554 * PASS 1: free the contents of the items. We don't free the items
4555 * themselves yet, so that it is possible to decrement refcount counters
4556 */
4557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004558 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004559 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004562 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004563
4564#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // Go through the list of jobs and free items without the copyID. This
4566 // must happen before doing channels, because jobs refer to channels, but
4567 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004568 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004570 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4572#endif
4573
4574 /*
4575 * PASS 2: free the items themselves.
4576 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004577 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004578 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004579
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of jobs and free items without the copyID. This
4582 // must happen before doing channels, because jobs refer to channels, but
4583 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584 free_unused_jobs(copyID, COPYID_MASK);
4585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004587 free_unused_channels(copyID, COPYID_MASK);
4588#endif
4589
4590 in_free_unref_items = FALSE;
4591
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004592 return did_free;
4593}
4594
4595/*
4596 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004597 * "list_stack" is used to add lists to be marked. Can be NULL.
4598 *
4599 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004602set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004603{
4604 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004606 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004607 hashtab_T *cur_ht;
4608 ht_stack_T *ht_stack = NULL;
4609 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004610
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004611 cur_ht = ht;
4612 for (;;)
4613 {
4614 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004616 // Mark each item in the hashtab. If the item contains a hashtab
4617 // it is added to ht_stack, if it contains a list it is added to
4618 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 todo = (int)cur_ht->ht_used;
4620 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4621 if (!HASHITEM_EMPTY(hi))
4622 {
4623 --todo;
4624 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4625 &ht_stack, list_stack);
4626 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628
4629 if (ht_stack == NULL)
4630 break;
4631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 cur_ht = ht_stack->ht;
4634 tempitem = ht_stack;
4635 ht_stack = ht_stack->prev;
4636 free(tempitem);
4637 }
4638
4639 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640}
4641
4642/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004643 * Mark a dict and its items with "copyID".
4644 * Returns TRUE if setting references failed somehow.
4645 */
4646 int
4647set_ref_in_dict(dict_T *d, int copyID)
4648{
4649 if (d != NULL && d->dv_copyID != copyID)
4650 {
4651 d->dv_copyID = copyID;
4652 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4653 }
4654 return FALSE;
4655}
4656
4657/*
4658 * Mark a list and its items with "copyID".
4659 * Returns TRUE if setting references failed somehow.
4660 */
4661 int
4662set_ref_in_list(list_T *ll, int copyID)
4663{
4664 if (ll != NULL && ll->lv_copyID != copyID)
4665 {
4666 ll->lv_copyID = copyID;
4667 return set_ref_in_list_items(ll, copyID, NULL);
4668 }
4669 return FALSE;
4670}
4671
4672/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004673 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004674 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4675 *
4676 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004677 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004678 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004679set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 listitem_T *li;
4682 int abort = FALSE;
4683 list_T *cur_l;
4684 list_stack_T *list_stack = NULL;
4685 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004686
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004687 cur_l = l;
4688 for (;;)
4689 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004690 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // Mark each item in the list. If the item contains a hashtab
4692 // it is added to ht_stack, if it contains a list it is added to
4693 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4695 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4696 ht_stack, &list_stack);
4697 if (list_stack == NULL)
4698 break;
4699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 cur_l = list_stack->list;
4702 tempitem = list_stack;
4703 list_stack = list_stack->prev;
4704 free(tempitem);
4705 }
4706
4707 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004708}
4709
4710/*
4711 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 * "list_stack" is used to add lists to be marked. Can be NULL.
4713 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4714 *
4715 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004718set_ref_in_item(
4719 typval_T *tv,
4720 int copyID,
4721 ht_stack_T **ht_stack,
4722 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004723{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004724 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004725
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004727 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 dict_T *dd = tv->vval.v_dict;
4729
Bram Moolenaara03f2332016-02-06 18:09:59 +01004730 if (dd != NULL && dd->dv_copyID != copyID)
4731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004733 dd->dv_copyID = copyID;
4734 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004735 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004736 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4737 }
4738 else
4739 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004740 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4741
Bram Moolenaara03f2332016-02-06 18:09:59 +01004742 if (newitem == NULL)
4743 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004744 else
4745 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004746 newitem->ht = &dd->dv_hashtab;
4747 newitem->prev = *ht_stack;
4748 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004749 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004750 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004751 }
4752 }
4753 else if (tv->v_type == VAR_LIST)
4754 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004755 list_T *ll = tv->vval.v_list;
4756
Bram Moolenaara03f2332016-02-06 18:09:59 +01004757 if (ll != NULL && ll->lv_copyID != copyID)
4758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004760 ll->lv_copyID = copyID;
4761 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004762 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004763 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004764 }
4765 else
4766 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004767 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4768
Bram Moolenaara03f2332016-02-06 18:09:59 +01004769 if (newitem == NULL)
4770 abort = TRUE;
4771 else
4772 {
4773 newitem->list = ll;
4774 newitem->prev = *list_stack;
4775 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004776 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004777 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004779 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004780 else if (tv->v_type == VAR_FUNC)
4781 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004782 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004783 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004784 else if (tv->v_type == VAR_PARTIAL)
4785 {
4786 partial_T *pt = tv->vval.v_partial;
4787 int i;
4788
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004789 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004791 // Didn't see this partial yet.
4792 pt->pt_copyID = copyID;
4793
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004794 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004795
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004796 if (pt->pt_dict != NULL)
4797 {
4798 typval_T dtv;
4799
4800 dtv.v_type = VAR_DICT;
4801 dtv.vval.v_dict = pt->pt_dict;
4802 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4803 }
4804
4805 for (i = 0; i < pt->pt_argc; ++i)
4806 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4807 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004808 if (pt->pt_funcstack != NULL)
4809 {
4810 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4811
4812 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4813 abort = abort || set_ref_in_item(stack + i, copyID,
4814 ht_stack, list_stack);
4815 }
4816
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 }
4818 }
4819#ifdef FEAT_JOB_CHANNEL
4820 else if (tv->v_type == VAR_JOB)
4821 {
4822 job_T *job = tv->vval.v_job;
4823 typval_T dtv;
4824
4825 if (job != NULL && job->jv_copyID != copyID)
4826 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004827 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 if (job->jv_channel != NULL)
4829 {
4830 dtv.v_type = VAR_CHANNEL;
4831 dtv.vval.v_channel = job->jv_channel;
4832 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4833 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004834 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835 {
4836 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004837 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4839 }
4840 }
4841 }
4842 else if (tv->v_type == VAR_CHANNEL)
4843 {
4844 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004845 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 typval_T dtv;
4847 jsonq_T *jq;
4848 cbq_T *cq;
4849
4850 if (ch != NULL && ch->ch_copyID != copyID)
4851 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004852 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004853 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 {
4855 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4856 jq = jq->jq_next)
4857 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4858 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4859 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004860 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004861 {
4862 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004863 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4865 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004866 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 {
4868 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004869 dtv.vval.v_partial =
4870 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004871 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4872 }
4873 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 if (ch->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 = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4879 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 {
4882 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004883 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4885 }
4886 }
4887 }
4888#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004889 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004890}
4891
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 * Return a string with the string representation of a variable.
4894 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004895 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004896 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004897 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004898 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004899 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004900 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4901 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004902 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004904 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004905echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004906 typval_T *tv,
4907 char_u **tofree,
4908 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004909 int copyID,
4910 int echo_style,
4911 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004912 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004914 static int recurse = 0;
4915 char_u *r = NULL;
4916
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004918 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004919 if (!did_echo_string_emsg)
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Only give this message once for a recursive call to avoid
4922 // flooding the user with errors. And stop iterating over lists
4923 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004924 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004925 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004927 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004928 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 }
4930 ++recurse;
4931
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 switch (tv->v_type)
4933 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004934 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004935 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004936 {
4937 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004938 r = tv->vval.v_string;
4939 if (r == NULL)
4940 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004941 }
4942 else
4943 {
4944 *tofree = string_quote(tv->vval.v_string, FALSE);
4945 r = *tofree;
4946 }
4947 break;
4948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004950 if (echo_style)
4951 {
4952 *tofree = NULL;
4953 r = tv->vval.v_string;
4954 }
4955 else
4956 {
4957 *tofree = string_quote(tv->vval.v_string, TRUE);
4958 r = *tofree;
4959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004960 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004961
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004962 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004963 {
4964 partial_T *pt = tv->vval.v_partial;
4965 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004966 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004967 garray_T ga;
4968 int i;
4969 char_u *tf;
4970
4971 ga_init2(&ga, 1, 100);
4972 ga_concat(&ga, (char_u *)"function(");
4973 if (fname != NULL)
4974 {
4975 ga_concat(&ga, fname);
4976 vim_free(fname);
4977 }
4978 if (pt != NULL && pt->pt_argc > 0)
4979 {
4980 ga_concat(&ga, (char_u *)", [");
4981 for (i = 0; i < pt->pt_argc; ++i)
4982 {
4983 if (i > 0)
4984 ga_concat(&ga, (char_u *)", ");
4985 ga_concat(&ga,
4986 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4987 vim_free(tf);
4988 }
4989 ga_concat(&ga, (char_u *)"]");
4990 }
4991 if (pt != NULL && pt->pt_dict != NULL)
4992 {
4993 typval_T dtv;
4994
4995 ga_concat(&ga, (char_u *)", ");
4996 dtv.v_type = VAR_DICT;
4997 dtv.vval.v_dict = pt->pt_dict;
4998 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4999 vim_free(tf);
5000 }
5001 ga_concat(&ga, (char_u *)")");
5002
5003 *tofree = ga.ga_data;
5004 r = *tofree;
5005 break;
5006 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005007
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005008 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005009 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005010 break;
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005013 if (tv->vval.v_list == NULL)
5014 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005015 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005016 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005017 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005018 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005019 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5020 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005021 {
5022 *tofree = NULL;
5023 r = (char_u *)"[...]";
5024 }
5025 else
5026 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005027 int old_copyID = tv->vval.v_list->lv_copyID;
5028
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005030 *tofree = list2string(tv, copyID, restore_copyID);
5031 if (restore_copyID)
5032 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005033 r = *tofree;
5034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005035 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005036
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038 if (tv->vval.v_dict == NULL)
5039 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005040 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005042 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005043 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005044 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5045 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005046 {
5047 *tofree = NULL;
5048 r = (char_u *)"{...}";
5049 }
5050 else
5051 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005052 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005053
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005055 *tofree = dict2string(tv, copyID, restore_copyID);
5056 if (restore_copyID)
5057 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 r = *tofree;
5059 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005061
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005063 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005064 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005066 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005067 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005068 break;
5069
Bram Moolenaar835dc632016-02-07 14:27:38 +01005070 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005071 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005074 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5075 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005076 if (composite_val)
5077 {
5078 *tofree = string_quote(r, FALSE);
5079 r = *tofree;
5080 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005081#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005083
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005084 case VAR_INSTR:
5085 *tofree = NULL;
5086 r = (char_u *)"instructions";
5087 break;
5088
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005090#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 *tofree = NULL;
5092 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5093 r = numbuf;
5094 break;
5095#endif
5096
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005097 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005098 case VAR_SPECIAL:
5099 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005100 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005101 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103
Bram Moolenaar8502c702014-06-17 12:51:16 +02005104 if (--recurse == 0)
5105 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107}
5108
5109/*
5110 * Return a string with the string representation of a variable.
5111 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5112 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005113 * Does not put quotes around strings, as ":echo" displays values.
5114 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5115 * May return NULL.
5116 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005117 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005118echo_string(
5119 typval_T *tv,
5120 char_u **tofree,
5121 char_u *numbuf,
5122 int copyID)
5123{
5124 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5125}
5126
5127/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005128 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5129 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005130 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005131 */
5132 int
5133buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5134{
5135 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005136 char_u *t;
5137 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005138
5139 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5140 return -1;
5141
5142 if (lnum > buf->b_ml.ml_line_count)
5143 lnum = buf->b_ml.ml_line_count;
5144
5145 str = ml_get_buf(buf, lnum, FALSE);
5146 if (str == NULL)
5147 return -1;
5148
5149 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005150 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005151
Bram Moolenaar91458462021-01-13 20:08:38 +01005152 // count the number of characters
5153 t = str;
5154 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5155 t += mb_ptr2len(t);
5156
5157 // In insert mode, when the cursor is at the end of a non-empty line,
5158 // byteidx points to the NUL character immediately past the end of the
5159 // string. In this case, add one to the character count.
5160 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5161 count++;
5162
5163 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005164}
5165
5166/*
5167 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005168 * byte index. Works only for loaded buffers. Returns -1 on failure.
5169 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005170 */
5171 int
5172buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5173{
5174 char_u *str;
5175 char_u *t;
5176
5177 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5178 return -1;
5179
5180 if (lnum > buf->b_ml.ml_line_count)
5181 lnum = buf->b_ml.ml_line_count;
5182
5183 str = ml_get_buf(buf, lnum, FALSE);
5184 if (str == NULL)
5185 return -1;
5186
5187 // Convert the character offset to a byte offset
5188 t = str;
5189 while (*t != NUL && --charidx > 0)
5190 t += mb_ptr2len(t);
5191
Bram Moolenaar91458462021-01-13 20:08:38 +01005192 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005193}
5194
5195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005197 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005200var2fpos(
5201 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005203 int *fnum, // set to fnum for '0, 'A, etc.
5204 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005206 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005208 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005211 if (varp->v_type == VAR_LIST)
5212 {
5213 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005214 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005215 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005216 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005217
5218 l = varp->vval.v_list;
5219 if (l == NULL)
5220 return NULL;
5221
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005222 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005223 pos.lnum = list_find_nr(l, 0L, &error);
5224 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005225 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005226 if (charcol)
5227 len = (long)mb_charlen(ml_get(pos.lnum));
5228 else
5229 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005230
Bram Moolenaarec65d772020-08-20 22:29:12 +02005231 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005233 li = list_find(l, 1L);
5234 if (li != NULL && li->li_tv.v_type == VAR_STRING
5235 && li->li_tv.vval.v_string != NULL
5236 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005237 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005238 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005239 }
5240 else
5241 {
5242 pos.col = list_find_nr(l, 1L, &error);
5243 if (error)
5244 return NULL;
5245 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005247 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005248 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005249 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005250 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005253 pos.coladd = list_find_nr(l, 2L, &error);
5254 if (error)
5255 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005256
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005257 return &pos;
5258 }
5259
Bram Moolenaarc5809432021-03-27 21:23:30 +01005260 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5261 return NULL;
5262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005263 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005264 if (name == NULL)
5265 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005266 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005267 {
5268 pos = curwin->w_cursor;
5269 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005270 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005271 return &pos;
5272 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005274 {
5275 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005276 pos = VIsual;
5277 else
5278 pos = curwin->w_cursor;
5279 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005280 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005281 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005282 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005285 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5287 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005289 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 return pp;
5291 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005292
Bram Moolenaara5525202006-03-02 22:52:09 +00005293 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005294
Bram Moolenaar477933c2007-07-17 14:32:23 +00005295 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005296 {
5297 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005299 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005300 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 // In silent Ex mode topline is zero, but that's not a valid line
5302 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005303 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005304 return &pos;
5305 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005306 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005307 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005308 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005310 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005311 return &pos;
5312 }
5313 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005316 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
5318 pos.lnum = curbuf->b_ml.ml_line_count;
5319 pos.col = 0;
5320 }
5321 else
5322 {
5323 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005324 if (charcol)
5325 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5326 else
5327 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 return &pos;
5330 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005331 if (in_vim9script())
5332 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return NULL;
5334}
5335
5336/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005337 * Convert list in "arg" into a position and optional file number.
5338 * When "fnump" is NULL there is no file number, only 3 items.
5339 * Note that the column is passed on as-is, the caller may want to decrement
5340 * it to use 1 for the first column.
5341 * Return FAIL when conversion is not possible, doesn't check the position for
5342 * validity.
5343 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005345list2fpos(
5346 typval_T *arg,
5347 pos_T *posp,
5348 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005349 colnr_T *curswantp,
5350 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005351{
5352 list_T *l = arg->vval.v_list;
5353 long i = 0;
5354 long n;
5355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5357 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005358 if (arg->v_type != VAR_LIST
5359 || l == NULL
5360 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005361 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 return FAIL;
5363
5364 if (fnump != NULL)
5365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367 if (n < 0)
5368 return FAIL;
5369 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005371 *fnump = n;
5372 }
5373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 if (n < 0)
5376 return FAIL;
5377 posp->lnum = n;
5378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005380 if (n < 0)
5381 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005382 // If character position is specified, then convert to byte position
5383 if (charcol)
5384 {
5385 buf_T *buf;
5386
5387 // Get the text for the specified line in a loaded buffer
5388 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5389 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5390 return FAIL;
5391
Bram Moolenaar91458462021-01-13 20:08:38 +01005392 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005394 posp->col = n;
5395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005397 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005398 posp->coladd = 0;
5399 else
5400 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005401
Bram Moolenaar493c1782014-05-28 14:34:46 +02005402 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005404
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405 return OK;
5406}
5407
5408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * Get the length of an environment variable name.
5410 * Advance "arg" to the first character after the name.
5411 * Return 0 for error.
5412 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005414get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 char_u *p;
5417 int len;
5418
5419 for (p = *arg; vim_isIDc(*p); ++p)
5420 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return 0;
5423
5424 len = (int)(p - *arg);
5425 *arg = p;
5426 return len;
5427}
5428
5429/*
5430 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005431 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * Return 0 if something is wrong.
5433 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 char_u *p;
5438 int len;
5439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005442 {
5443 if (*p == ':')
5444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 // "s:" is start of "s:var", but "n:" is not and can be used in
5446 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005447 len = (int)(p - *arg);
5448 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5449 || len > 1)
5450 break;
5451 }
5452 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return 0;
5455
5456 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005457 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 return len;
5460}
5461
5462/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005463 * Get the length of the name of a variable or function.
5464 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 * Return -1 if curly braces expansion failed.
5467 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 * If the name contains 'magic' {}'s, expand them and return the
5469 * expanded name in an allocated string via 'alias' - caller must free.
5470 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472get_name_len(
5473 char_u **arg,
5474 char_u **alias,
5475 int evaluate,
5476 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 char_u *p;
5480 char_u *expr_start;
5481 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5486 && (*arg)[2] == (int)KE_SNR)
5487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 *arg += 3;
5490 return get_id_len(arg) + 3;
5491 }
5492 len = eval_fname_script(*arg);
5493 if (len > 0)
5494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 *arg += len;
5497 }
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005502 p = find_name_end(*arg, &expr_start, &expr_end,
5503 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (expr_start != NULL)
5505 {
5506 char_u *temp_string;
5507
5508 if (!evaluate)
5509 {
5510 len += (int)(p - *arg);
5511 *arg = skipwhite(p);
5512 return len;
5513 }
5514
5515 /*
5516 * Include any <SID> etc in the expanded string:
5517 * Thus the -len here.
5518 */
5519 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5520 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005521 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 *alias = temp_string;
5523 *arg = skipwhite(p);
5524 return (int)STRLEN(temp_string);
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005528 // Only give an error when there is something, otherwise it will be
5529 // reported at a higher level.
5530 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005531 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 return len;
5534}
5535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536/*
5537 * Find the end of a variable or function name, taking care of magic braces.
5538 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5539 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005540 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 * Return a pointer to just after the name. Equal to "arg" if there is no
5542 * valid name.
5543 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005545find_name_end(
5546 char_u *arg,
5547 char_u **expr_start,
5548 char_u **expr_end,
5549 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 int mb_nest = 0;
5552 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005554 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005555 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 *expr_start = NULL;
5560 *expr_end = NULL;
5561 }
5562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005564 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5565 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005566 return arg;
5567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 for (p = arg; *p != NUL
5569 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005570 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005571 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005572 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005574 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005576 if (*p == '\'')
5577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005579 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580 ;
5581 if (*p == NUL)
5582 break;
5583 }
5584 else if (*p == '"')
5585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // skip over "str\"ing" 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 if (*p == '\\' && p[1] != NUL)
5589 ++p;
5590 if (*p == NUL)
5591 break;
5592 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005593 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // "s:" is start of "s:var", but "n:" is not and can be used in
5596 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005597 len = (int)(p - arg);
5598 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005599 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005600 break;
5601 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 if (*p == '[')
5606 ++br_nest;
5607 else if (*p == ']')
5608 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005610
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005611 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 if (*p == '{')
5614 {
5615 mb_nest++;
5616 if (expr_start != NULL && *expr_start == NULL)
5617 *expr_start = p;
5618 }
5619 else if (*p == '}')
5620 {
5621 mb_nest--;
5622 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5623 *expr_end = p;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627
5628 return p;
5629}
5630
5631/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632 * Expands out the 'magic' {}'s in a variable/function name.
5633 * Note that this can call itself recursively, to deal with
5634 * constructs like foo{bar}{baz}{bam}
5635 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5636 * "in_start" ^
5637 * "expr_start" ^
5638 * "expr_end" ^
5639 * "in_end" ^
5640 *
5641 * Returns a new allocated string, which the caller must free.
5642 * Returns NULL for failure.
5643 */
5644 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645make_expanded_name(
5646 char_u *in_start,
5647 char_u *expr_start,
5648 char_u *expr_end,
5649 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650{
5651 char_u c1;
5652 char_u *retval = NULL;
5653 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654
5655 if (expr_end == NULL || in_end == NULL)
5656 return NULL;
5657 *expr_start = NUL;
5658 *expr_end = NUL;
5659 c1 = *in_end;
5660 *in_end = NUL;
5661
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005662 temp_result = eval_to_string(expr_start + 1, FALSE);
5663 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005664 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005665 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5666 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005667 if (retval != NULL)
5668 {
5669 STRCPY(retval, in_start);
5670 STRCAT(retval, temp_result);
5671 STRCAT(retval, expr_end + 1);
5672 }
5673 }
5674 vim_free(temp_result);
5675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005677 *expr_start = '{';
5678 *expr_end = '}';
5679
5680 if (retval != NULL)
5681 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005682 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005683 if (expr_start != NULL)
5684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005685 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005686 temp_result = make_expanded_name(retval, expr_start,
5687 expr_end, temp_result);
5688 vim_free(retval);
5689 retval = temp_result;
5690 }
5691 }
5692
5693 return retval;
5694}
5695
5696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005698 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005703 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005704}
5705
5706/*
5707 * Return TRUE if character "c" can be used as the first character in a
5708 * variable or function name (excluding '{' and '}').
5709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005712{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005713 return ASCII_ISALPHA(c) || c == '_';
5714}
5715
5716/*
5717 * Return TRUE if character "c" can be used as the first character of a
5718 * dictionary key.
5719 */
5720 int
5721eval_isdictc(int c)
5722{
5723 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005727 * Handle:
5728 * - expr[expr], expr[expr:expr] subscript
5729 * - ".name" lookup
5730 * - function call with Funcref variable: func(expr)
5731 * - method call: var->method()
5732 *
5733 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005736handle_subscript(
5737 char_u **arg,
5738 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005739 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005740 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005741{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005742 int evaluate = evalarg != NULL
5743 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005744 int ret = OK;
5745 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005746 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005747 int getnext;
5748 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005749
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005750 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005751 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005752 // When at the end of the line and ".name" or "->{" or "->X" follows in
5753 // the next line then consume the line break.
5754 p = eval_next_non_blank(*arg, evalarg, &getnext);
5755 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005756 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005757 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5758 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5759 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005760 {
5761 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005762 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005763 check_white = FALSE;
5764 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005765
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005766 if (rettv->v_type == VAR_ANY)
5767 {
5768 char_u *exp_name;
5769 int cc;
5770 int idx;
5771 ufunc_T *ufunc;
5772 type_T *type;
5773
5774 // Found script from "import * as {name}", script item name must
5775 // follow.
5776 if (**arg != '.')
5777 {
5778 if (verbose)
5779 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5780 ret = FAIL;
5781 break;
5782 }
5783 ++*arg;
5784 if (IS_WHITE_OR_NUL(**arg))
5785 {
5786 if (verbose)
5787 emsg(_(e_no_white_space_allowed_after_dot));
5788 ret = FAIL;
5789 break;
5790 }
5791
5792 // isolate the name
5793 exp_name = *arg;
5794 while (eval_isnamec(**arg))
5795 ++*arg;
5796 cc = **arg;
5797 **arg = NUL;
5798
5799 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5800 evalarg->eval_cctx, verbose);
5801 **arg = cc;
5802 *arg = skipwhite(*arg);
5803
5804 if (idx < 0 && ufunc == NULL)
5805 {
5806 ret = FAIL;
5807 break;
5808 }
5809 if (idx >= 0)
5810 {
5811 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5812 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5813
5814 copy_tv(sv->sv_tv, rettv);
5815 }
5816 else
5817 {
5818 rettv->v_type = VAR_FUNC;
5819 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5820 }
5821 }
5822
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005823 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5824 || rettv->v_type == VAR_PARTIAL))
5825 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005826 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005827 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5828 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005829
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005830 // Stop the expression evaluation when immediately aborting on
5831 // error, or when an interrupt occurred or an exception was thrown
5832 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005833 if (aborting())
5834 {
5835 if (ret == OK)
5836 clear_tv(rettv);
5837 ret = FAIL;
5838 }
5839 dict_unref(selfdict);
5840 selfdict = NULL;
5841 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005842 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005843 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005844 if (in_vim9script())
5845 *arg = skipwhite(p + 2);
5846 else
5847 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005848 if (ret == OK)
5849 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005850 if (VIM_ISWHITE(**arg))
5851 {
5852 emsg(_(e_nowhitespace));
5853 ret = FAIL;
5854 }
5855 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005856 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005857 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005858 else
5859 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005860 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005861 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005862 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005863 // "." is ".name" lookup when we found a dict or when evaluating and
5864 // scriptversion is at least 2, where string concatenation is "..".
5865 else if (**arg == '['
5866 || (**arg == '.' && (rettv->v_type == VAR_DICT
5867 || (!evaluate
5868 && (*arg)[1] != '.'
5869 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005870 {
5871 dict_unref(selfdict);
5872 if (rettv->v_type == VAR_DICT)
5873 {
5874 selfdict = rettv->vval.v_dict;
5875 if (selfdict != NULL)
5876 ++selfdict->dv_refcount;
5877 }
5878 else
5879 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005880 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005881 {
5882 clear_tv(rettv);
5883 ret = FAIL;
5884 }
5885 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005886 else
5887 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005888 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005890 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5891 // Don't do this when "Func" is already a partial that was bound
5892 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005893 if (selfdict != NULL
5894 && (rettv->v_type == VAR_FUNC
5895 || (rettv->v_type == VAR_PARTIAL
5896 && (rettv->vval.v_partial->pt_auto
5897 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005898 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005899
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005900 dict_unref(selfdict);
5901 return ret;
5902}
5903
5904/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 * Make a copy of an item.
5906 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005907 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5908 * reference to an already copied list/dict can be used.
5909 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005911 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912item_copy(
5913 typval_T *from,
5914 typval_T *to,
5915 int deep,
5916 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917{
5918 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005919 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005920
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005923 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005924 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 }
5926 ++recurse;
5927
5928 switch (from->v_type)
5929 {
5930 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005931 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 case VAR_STRING:
5933 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005934 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005935 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005936 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005937 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005938 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005939 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 copy_tv(from, to);
5941 break;
5942 case VAR_LIST:
5943 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005944 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 if (from->vval.v_list == NULL)
5946 to->vval.v_list = NULL;
5947 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005949 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005950 to->vval.v_list = from->vval.v_list->lv_copylist;
5951 ++to->vval.v_list->lv_refcount;
5952 }
5953 else
5954 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5955 if (to->vval.v_list == NULL)
5956 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005957 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005958 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005960 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961 case VAR_DICT:
5962 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005963 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005964 if (from->vval.v_dict == NULL)
5965 to->vval.v_dict = NULL;
5966 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005968 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005969 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5970 ++to->vval.v_dict->dv_refcount;
5971 }
5972 else
5973 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5974 if (to->vval.v_dict == NULL)
5975 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005977 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005978 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005980 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005982 }
5983 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005984 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985}
5986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 void
5988echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5989{
5990 char_u *tofree;
5991 char_u numbuf[NUMBUFLEN];
5992 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5993
5994 if (*atstart)
5995 {
5996 *atstart = FALSE;
5997 // Call msg_start() after eval1(), evaluating the expression
5998 // may cause a message to appear.
5999 if (with_space)
6000 {
6001 // Mark the saved text as finishing the line, so that what
6002 // follows is displayed on a new line when scrolling back
6003 // at the more prompt.
6004 msg_sb_eol();
6005 msg_start();
6006 }
6007 }
6008 else if (with_space)
6009 msg_puts_attr(" ", echo_attr);
6010
6011 if (p != NULL)
6012 for ( ; *p != NUL && !got_int; ++p)
6013 {
6014 if (*p == '\n' || *p == '\r' || *p == TAB)
6015 {
6016 if (*p != TAB && *needclr)
6017 {
6018 // remove any text still there from the command
6019 msg_clr_eos();
6020 *needclr = FALSE;
6021 }
6022 msg_putchar_attr(*p, echo_attr);
6023 }
6024 else
6025 {
6026 if (has_mbyte)
6027 {
6028 int i = (*mb_ptr2len)(p);
6029
6030 (void)msg_outtrans_len_attr(p, i, echo_attr);
6031 p += i - 1;
6032 }
6033 else
6034 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6035 }
6036 }
6037 vim_free(tofree);
6038}
6039
Bram Moolenaare9a41262005-01-15 22:18:47 +00006040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * ":echo expr1 ..." print each argument separated with a space, add a
6042 * newline at the end.
6043 * ":echon expr1 ..." print each argument plain.
6044 */
6045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006050 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int needclr = TRUE;
6052 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006053 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006054 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006055 evalarg_T evalarg;
6056
Bram Moolenaare707c882020-07-01 13:07:37 +02006057 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 if (eap->skip)
6060 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006061 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 // If eval1() causes an error message the text from the command may
6064 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006065 need_clr_eos = needclr;
6066
Bram Moolenaar68db9962021-05-09 23:19:22 +02006067 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006068 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
6070 /*
6071 * Report the invalid expression unless the expression evaluation
6072 * has been cancelled due to an aborting error, an interrupt, or an
6073 * exception.
6074 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006075 if (!aborting() && did_emsg == did_emsg_before
6076 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006077 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006078 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 break;
6080 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006081 need_clr_eos = FALSE;
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006084 {
6085 if (rettv.v_type == VAR_VOID)
6086 {
6087 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6088 break;
6089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006091 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006093 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 arg = skipwhite(arg);
6095 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006096 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006097 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 if (eap->skip)
6100 --emsg_skip;
6101 else
6102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (needclr)
6105 msg_clr_eos();
6106 if (eap->cmdidx == CMD_echo)
6107 msg_end();
6108 }
6109}
6110
6111/*
6112 * ":echohl {name}".
6113 */
6114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006115ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006117 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118}
6119
6120/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006121 * Returns the :echo attribute
6122 */
6123 int
6124get_echo_attr(void)
6125{
6126 return echo_attr;
6127}
6128
6129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 * ":execute expr1 ..." execute the result of an expression.
6131 * ":echomsg expr1 ..." Print a message
6132 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006133 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 * Each gets spaces around each argument and a newline at the end for
6135 * echo commands
6136 */
6137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 int ret = OK;
6143 char_u *p;
6144 garray_T ga;
6145 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006146 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 ga_init2(&ga, 1, 80);
6149
6150 if (eap->skip)
6151 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006152 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006154 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006155 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 if (!eap->skip)
6159 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006160 char_u buf[NUMBUFLEN];
6161
6162 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006163 {
6164 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6165 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006166 semsg(_(e_using_invalid_value_as_string_str),
6167 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006168 p = NULL;
6169 }
6170 else
6171 p = tv_get_string_buf(&rettv, buf);
6172 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006173 else
6174 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006175 if (p == NULL)
6176 {
6177 clear_tv(&rettv);
6178 ret = FAIL;
6179 break;
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 len = (int)STRLEN(p);
6182 if (ga_grow(&ga, len + 2) == FAIL)
6183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 ret = FAIL;
6186 break;
6187 }
6188 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 ga.ga_len += len;
6192 }
6193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 arg = skipwhite(arg);
6196 }
6197
6198 if (ret != FAIL && ga.ga_data != NULL)
6199 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006200 // use the first line of continuation lines for messages
6201 SOURCING_LNUM = start_lnum;
6202
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006203 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006205 // Mark the already saved text as finishing the line, so that what
6206 // follows is displayed on a new line when scrolling back at the
6207 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006208 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006209 }
6210
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006212 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006213 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006214 out_flush();
6215 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006216 else if (eap->cmdidx == CMD_echoconsole)
6217 {
6218 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6219 ui_write((char_u *)"\r\n", 2, TRUE);
6220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 else if (eap->cmdidx == CMD_echoerr)
6222 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006223 int save_did_emsg = did_emsg;
6224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006226 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (!force_abort)
6228 did_emsg = save_did_emsg;
6229 }
6230 else if (eap->cmdidx == CMD_execute)
6231 do_cmdline((char_u *)ga.ga_data,
6232 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6233 }
6234
6235 ga_clear(&ga);
6236
6237 if (eap->skip)
6238 --emsg_skip;
6239
Bram Moolenaar63b91732021-08-05 20:40:03 +02006240 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241}
6242
6243/*
6244 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6245 * "arg" points to the "&" or '+' when called, to "option" when returning.
6246 * Returns NULL when no option name found. Otherwise pointer to the char
6247 * after the option name.
6248 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006249 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006250find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 char_u *p = *arg;
6253
6254 ++p;
6255 if (*p == 'g' && p[1] == ':')
6256 {
6257 *opt_flags = OPT_GLOBAL;
6258 p += 2;
6259 }
6260 else if (*p == 'l' && p[1] == ':')
6261 {
6262 *opt_flags = OPT_LOCAL;
6263 p += 2;
6264 }
6265 else
6266 *opt_flags = 0;
6267
6268 if (!ASCII_ISALPHA(*p))
6269 return NULL;
6270 *arg = p;
6271
6272 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006273 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 else
6275 while (ASCII_ISALPHA(*p))
6276 ++p;
6277 return p;
6278}
6279
6280/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006281 * Display script name where an item was last set.
6282 * Should only be invoked when 'verbose' is non-zero.
6283 */
6284 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006285last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006286{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006287 char_u *p;
6288
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006289 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006290 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006291 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006292 if (p != NULL)
6293 {
6294 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006295 msg_puts(_("\n\tLast set from "));
6296 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006297 if (script_ctx.sc_lnum > 0)
6298 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006299 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006300 msg_outnum((long)script_ctx.sc_lnum);
6301 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006302 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006303 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006304 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006305 }
6306}
6307
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006308#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310/*
6311 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006312 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 * "flags" can be "g" to do a global substitute.
6314 * Returns an allocated string, NULL for error.
6315 */
6316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317do_string_sub(
6318 char_u *str,
6319 char_u *pat,
6320 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006321 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006322 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 int sublen;
6325 regmatch_T regmatch;
6326 int i;
6327 int do_all;
6328 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006329 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 garray_T ga;
6331 char_u *ret;
6332 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006333 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006337 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 ga_init2(&ga, 1, 200);
6340
6341 do_all = (flags[0] == 'g');
6342
6343 regmatch.rm_ic = p_ic;
6344 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6345 if (regmatch.regprog != NULL)
6346 {
6347 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006348 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6350 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006351 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006352 if (regmatch.startp[0] == regmatch.endp[0])
6353 {
6354 if (zero_width == regmatch.startp[0])
6355 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006356 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006357 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006358 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6359 (size_t)i);
6360 ga.ga_len += i;
6361 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006362 continue;
6363 }
6364 zero_width = regmatch.startp[0];
6365 }
6366
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 /*
6368 * Get some space for a temporary buffer to do the substitution
6369 * into. It will contain:
6370 * - The text up to where the match is.
6371 * - The substituted text.
6372 * - The text after the match.
6373 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006374 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006375 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6377 {
6378 ga_clear(&ga);
6379 break;
6380 }
6381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006382 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 i = (int)(regmatch.startp[0] - tail);
6384 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006385 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006386 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 + ga.ga_len + i, TRUE, TRUE, FALSE);
6388 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006389 tail = regmatch.endp[0];
6390 if (*tail == NUL)
6391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 if (!do_all)
6393 break;
6394 }
6395
6396 if (ga.ga_data != NULL)
6397 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6398
Bram Moolenaar473de612013-06-08 18:19:48 +02006399 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401
6402 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6403 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006404 if (p_cpo == empty_option)
6405 p_cpo = save_cpo;
6406 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006407 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006409 // If it's still empty it was changed and restored, need to restore in
6410 // the complicated way.
6411 if (*p_cpo == NUL)
6412 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006413 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
6416 return ret;
6417}