blob: a99d149a0f31e83de59060074e5b23656fe2a54b [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{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100146 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200147 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 Moolenaar851f86b2021-12-13 14:26:44 +0000259 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200260 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 Moolenaar851f86b2021-12-13 14:26:44 +0000283 funcexe.fe_evaluate = TRUE;
284 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 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;
Christian Brabandt070ac342021-09-09 12:12:03 +0200564 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200566 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200567 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200571 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200572 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000573 if (use_sandbox)
574 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200575 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200576 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200577 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200578 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return retval;
580}
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/*
583 * Top level evaluation function, returning a number.
584 * Evaluates "expr" silently.
585 * Returns -1 for an error.
586 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200587 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100588eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
Bram Moolenaar33570922005-01-25 22:26:29 +0000590 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200591 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000592 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 ++emsg_off;
595
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200596 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 retval = -1;
598 else
599 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100600 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000601 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603 --emsg_off;
604
605 return retval;
606}
607
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000608/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000609 * Top level evaluation function.
610 * Returns an allocated typval_T with the result.
611 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000612 */
613 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200614eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615{
616 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200617 evalarg_T evalarg;
618
619 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200621 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100623 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000624
Bram Moolenaar37c83712020-06-30 21:18:36 +0200625 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000626 return tv;
627}
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100630 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200631 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
632 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000633 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100636call_vim_function(
637 char_u *func,
638 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200639 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200640 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000642 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200643 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100645 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200646 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000647 funcexe.fe_firstline = curwin->w_cursor.lnum;
648 funcexe.fe_lastline = curwin->w_cursor.lnum;
649 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200650 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000651 if (ret == FAIL)
652 clear_tv(rettv);
653
654 return ret;
655}
656
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100657/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100658 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100659 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200660 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
661 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200663 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100664call_func_retnr(
665 char_u *func,
666 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200667 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668{
669 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200670 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100671
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673 return -1;
674
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100675 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676 clear_tv(&rettv);
677 return retval;
678}
679
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000680/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100681 * Call Vim script function like call_func_retnr() and drop the result.
682 * Returns FAIL when calling the function fails.
683 */
684 int
685call_func_noret(
686 char_u *func,
687 int argc,
688 typval_T *argv)
689{
690 typval_T rettv;
691
692 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
693 return FAIL;
694 clear_tv(&rettv);
695 return OK;
696}
697
698/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100699 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100700 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000701 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702 */
703 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704call_func_retstr(
705 char_u *func,
706 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200707 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708{
709 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000710 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711
Bram Moolenaarded27a12018-08-01 19:06:03 +0200712 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713 return NULL;
714
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100715 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 return retval;
718}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000719
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000720/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100721 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100722 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000723 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724 */
725 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100726call_func_retlist(
727 char_u *func,
728 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200729 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000730{
731 typval_T rettv;
732
Bram Moolenaarded27a12018-08-01 19:06:03 +0200733 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734 return NULL;
735
736 if (rettv.v_type != VAR_LIST)
737 {
738 clear_tv(&rettv);
739 return NULL;
740 }
741
742 return rettv.vval.v_list;
743}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#ifdef FEAT_FOLDING
746/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100747 * Evaluate "arg", which is 'foldexpr'.
748 * Note: caller must set "curwin" to match "arg".
749 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
750 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200756 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000758 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
759 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
761 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000762 if (use_sandbox)
763 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200764 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200766 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 retval = 0;
768 else
769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 if (tv.v_type == VAR_NUMBER)
772 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000773 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 retval = 0;
775 else
776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 // If the result is a string, check if there is a non-digit before
778 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (!VIM_ISDIGIT(*s) && *s != '-')
781 *cp = *s++;
782 retval = atol((char *)s);
783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000787 if (use_sandbox)
788 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200789 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200790 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200792 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
794#endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 * Get an lval: variable, Dict item or List item that can be assigned a value
798 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
799 * "name.key", "name.key[expr]" etc.
800 * Indexing only works if "name" is an existing List or Dictionary.
801 * "name" points to the start of the name.
802 * If "rettv" is not NULL it points to the value to be assigned.
803 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
804 * wrong; must end in space or cmd separator.
805 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100806 * flags:
807 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100808 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100809 * GLV_NO_AUTOLOAD: do not use script autoloading
810 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000811 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000812 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000813 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200815 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816get_lval(
817 char_u *name,
818 typval_T *rettv,
819 lval_T *lp,
820 int unlet,
821 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100822 int flags, // GLV_ values
823 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 char_u *expr_start, *expr_end;
827 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 dictitem_T *v;
829 typval_T var1;
830 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000833 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100834 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100835 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100836 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100838 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200839 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100841 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100843 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200845 lp->ll_name_end = find_name_end(name, NULL, NULL,
846 FNE_INCL_BR | fne_flags);
847 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 }
849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000851 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 if (expr_start != NULL)
854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100855 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100856 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 && *p != '[' && *p != '.')
858 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000859 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 return NULL;
861 }
862
863 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
864 if (lp->ll_exp_name == NULL)
865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100866 // Report an invalid expression in braces, unless the
867 // expression evaluation has been cancelled due to an
868 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 if (!aborting() && !quiet)
870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000871 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000872 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 return NULL;
874 }
875 }
876 lp->ll_name = lp->ll_exp_name;
877 }
878 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 lp->ll_name = name;
881
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200882 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200884 // "a: type" is declaring variable "a" with a type, not "a:".
885 if (p == name + 2 && p[-1] == ':')
886 {
887 --p;
888 lp->ll_name_end = p;
889 }
890 if (*p == ':')
891 {
892 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200893 char_u *tp = skipwhite(p + 1);
894
895 if (tp == p + 1 && !quiet)
896 {
897 semsg(_(e_white_space_required_after_str_str), ":", p);
898 return NULL;
899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200901 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100902 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 if (lp->ll_type == NULL && !quiet)
904 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 lp->ll_name_end = tp;
906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 }
908 }
909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 return p;
913
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200914 if (in_vim9script() && lval_root != NULL)
915 {
916 // using local variable
917 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200918 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200919 }
920 else
921 {
922 cc = *p;
923 *p = NUL;
924 // When we would write to the variable pass &ht and prevent autoload.
925 writing = !(flags & GLV_READ_ONLY);
926 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100927 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200928 if (v == NULL && !quiet)
929 semsg(_(e_undefined_variable_str), lp->ll_name);
930 *p = cc;
931 if (v == NULL)
932 return NULL;
933 lp->ll_tv = &v->di_tv;
934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000935
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100936 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
937 {
938 if (!quiet)
939 semsg(_(e_variable_already_declared), lp->ll_name);
940 return NULL;
941 }
942
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 /*
944 * Loop until no more [idx] or .key is following.
945 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100946 var1.v_type = VAR_UNKNOWN;
947 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200948 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200950 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
951 {
952 if (!quiet)
953 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
954 return NULL;
955 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200956 if (lp->ll_tv->v_type != VAR_LIST
957 && lp->ll_tv->v_type != VAR_DICT
958 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000961 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200964
965 // a NULL list/blob works like an empty list/blob, allocate one now.
966 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
967 rettv_list_alloc(lp->ll_tv);
968 else if (lp->ll_tv->v_type == VAR_BLOB
969 && lp->ll_tv->vval.v_blob == NULL)
970 rettv_blob_alloc(lp->ll_tv);
971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000975 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000977 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000978
Bram Moolenaar10c65862020-10-08 21:16:42 +0200979 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200980 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200981 && lp->ll_tv == &v->di_tv
982 && ht != NULL && ht == get_script_local_ht())
983 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200984 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200985
986 // Vim9 script local variable: get the type
987 if (sv != NULL)
988 lp->ll_valtype = sv->sv_type;
989 }
990
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 len = -1;
992 if (*p == '.')
993 {
994 key = p + 1;
995 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
996 ;
997 if (len == 0)
998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001000 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003 p = key + len;
1004 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001005 else
1006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001007 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001008 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 if (*p == ':')
1010 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 else
1012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001014 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001016 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001017 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001018 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001019 clear_tv(&var1);
1020 return NULL;
1021 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001022 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 }
1024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 if (*p == ':')
1027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001031 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001034 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001035 if (rettv != NULL
1036 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001037 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001038 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001039 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001042 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001043 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 }
1046 p = skipwhite(p + 1);
1047 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 else
1050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001052 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001053 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001054 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001055 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001061 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001062 clear_tv(&var2);
1063 return NULL;
1064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001070
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001074 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001075 clear_tv(&var1);
1076 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 }
1079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001080 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 ++p;
1082 }
1083
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 {
1086 if (len == -1)
1087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001088 // "[key]": get key from "var1"
1089 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001090 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
1095 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001096 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001097
1098 // a NULL dict is equivalent with an empty dict
1099 if (lp->ll_tv->vval.v_dict == NULL)
1100 {
1101 lp->ll_tv->vval.v_dict = dict_alloc();
1102 if (lp->ll_tv->vval.v_dict == NULL)
1103 {
1104 clear_tv(&var1);
1105 return NULL;
1106 }
1107 ++lp->ll_tv->vval.v_dict->dv_refcount;
1108 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001109 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001110
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001111 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001113 // When assigning to a scope dictionary check that a function and
1114 // variable name is valid (only variable name unless it is l: or
1115 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001116 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001118 int prevval;
1119 int wrong;
1120
1121 if (len != -1)
1122 {
1123 prevval = key[len];
1124 key[len] = NUL;
1125 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001126 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001127 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001128 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1129 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001130 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001131 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001132 if (len != -1)
1133 key[len] = prevval;
1134 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001135 {
1136 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001137 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001138 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001139 }
1140
Bram Moolenaar10c65862020-10-08 21:16:42 +02001141 if (lp->ll_valtype != NULL)
1142 // use the type of the member
1143 lp->ll_valtype = lp->ll_valtype->tt_member;
1144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001147 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001148 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001149 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001150 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001151 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001152 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001153 return NULL;
1154 }
1155
Bram Moolenaar31b81602019-02-10 22:14:27 +01001156 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001160 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
1164 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001166 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001168 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 p = NULL;
1171 break;
1172 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001173 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001174 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001175 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1176 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001177 {
1178 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001179 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001180 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001181
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001182 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 else if (lp->ll_tv->v_type == VAR_BLOB)
1186 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001187 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1188
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 /*
1190 * Get the number and item for the only or first index of the List.
1191 */
1192 if (empty1)
1193 lp->ll_n1 = 0;
1194 else
1195 // is number or string
1196 lp->ll_n1 = (long)tv_get_number(&var1);
1197 clear_tv(&var1);
1198
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001199 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001201 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001202 return NULL;
1203 }
1204 if (lp->ll_range && !lp->ll_empty2)
1205 {
1206 lp->ll_n2 = (long)tv_get_number(&var2);
1207 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001208 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1209 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 }
1212 lp->ll_blob = lp->ll_tv->vval.v_blob;
1213 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001214 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 else
1217 {
1218 /*
1219 * Get the number and item for the only or first index of the List.
1220 */
1221 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001224 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001225 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
1227
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001228 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001230 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001231 if (lp->ll_li == NULL)
1232 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001233 clear_tv(&var2);
1234 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 }
1236
Bram Moolenaar10c65862020-10-08 21:16:42 +02001237 if (lp->ll_valtype != NULL)
1238 // use the type of the member
1239 lp->ll_valtype = lp->ll_valtype->tt_member;
1240
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 /*
1242 * May need to find the item or absolute index for the second
1243 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 * When no index given: "lp->ll_empty2" is TRUE.
1245 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001249 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001250 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001252 if (check_range_index_two(lp->ll_list,
1253 &lp->ll_n1, lp->ll_li,
1254 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001256 }
1257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 }
1261
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001262 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 return p;
1265}
1266
1267/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272{
1273 vim_free(lp->ll_exp_name);
1274 vim_free(lp->ll_newkey);
1275}
1276
1277/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001278 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001280 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1281 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284set_var_lval(
1285 lval_T *lp,
1286 char_u *endp,
1287 typval_T *rettv,
1288 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001289 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1290 char_u *op,
1291 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292{
1293 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295
1296 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001298 cc = *endp;
1299 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001300 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1301 return;
1302
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 if (lp->ll_blob != NULL)
1304 {
1305 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 if (op != NULL && *op != '=')
1308 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001309 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 return;
1311 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001312 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001313 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314
1315 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1316 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001317 if (lp->ll_empty2)
1318 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1319
Bram Moolenaar68452172021-04-12 21:21:02 +02001320 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1321 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 }
1324 else
1325 {
1326 val = (int)tv_get_number_chk(rettv, &error);
1327 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001328 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329 }
1330 }
1331 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001333 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001335 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1336 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001337 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001338 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001339 *endp = cc;
1340 return;
1341 }
1342
Bram Moolenaarff697e62019-02-12 22:28:33 +01001343 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001344 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001345 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001346 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 {
1348 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001349 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1350 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001351 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001352 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1353 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001358 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001359 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1360 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001362 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1363 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001364 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001367 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001368 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001369 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001370 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 else if (lp->ll_range)
1372 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001373 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1374 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001375 {
1376 emsg(_("E996: Cannot lock a range"));
1377 return;
1378 }
1379
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001380 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1381 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001382 }
1383 else
1384 {
1385 /*
1386 * Assign to a List or Dictionary item.
1387 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001388 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1389 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001390 {
1391 emsg(_("E996: Cannot lock a list or dict"));
1392 return;
1393 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001394
1395 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001396 && check_typval_arg_type(lp->ll_valtype, rettv,
1397 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001398 return;
1399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 if (lp->ll_newkey != NULL)
1401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 if (op != NULL && *op != '=')
1403 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001404 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 return;
1406 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001407 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1408 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001409 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001411 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001412 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (di == NULL)
1414 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001415 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1416 {
1417 vim_free(di);
1418 return;
1419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001421 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001422 else if (op != NULL && *op != '=')
1423 {
1424 tv_op(lp->ll_tv, rettv, op);
1425 return;
1426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001429
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 /*
1431 * Assign the value to the variable or list item.
1432 */
1433 if (copy)
1434 copy_tv(rettv, lp->ll_tv);
1435 else
1436 {
1437 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001438 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001440 }
1441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442}
1443
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001445 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1446 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 * Returns OK or FAIL.
1448 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001452 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 char_u numbuf[NUMBUFLEN];
1454 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001455 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001457 // Can't do anything with a Funcref or Dict on the right.
1458 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001459 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001460 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1461 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 {
1463 switch (tv1->v_type)
1464 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001465 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001466 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 case VAR_DICT:
1469 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001470 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001471 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001472 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001473 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001474 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001475 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 break;
1477
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 case VAR_BLOB:
1479 if (*op != '+' || tv2->v_type != VAR_BLOB)
1480 break;
1481 // BLOB += BLOB
1482 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1483 {
1484 blob_T *b1 = tv1->vval.v_blob;
1485 blob_T *b2 = tv2->vval.v_blob;
1486 int i, len = blob_len(b2);
1487 for (i = 0; i < len; i++)
1488 ga_append(&b1->bv_ga, blob_get(b2, i));
1489 }
1490 return OK;
1491
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 case VAR_LIST:
1493 if (*op != '+' || tv2->v_type != VAR_LIST)
1494 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001496 if (tv2->vval.v_list != NULL)
1497 {
1498 if (tv1->vval.v_list == NULL)
1499 {
1500 tv1->vval.v_list = tv2->vval.v_list;
1501 ++tv1->vval.v_list->lv_refcount;
1502 }
1503 else
1504 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 return OK;
1507
1508 case VAR_NUMBER:
1509 case VAR_STRING:
1510 if (tv2->v_type == VAR_LIST)
1511 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001512 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001514 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001515 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001516#ifdef FEAT_FLOAT
1517 if (tv2->v_type == VAR_FLOAT)
1518 {
1519 float_T f = n;
1520
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 if (*op == '%')
1522 break;
1523 switch (*op)
1524 {
1525 case '+': f += tv2->vval.v_float; break;
1526 case '-': f -= tv2->vval.v_float; break;
1527 case '*': f *= tv2->vval.v_float; break;
1528 case '/': f /= tv2->vval.v_float; break;
1529 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001530 clear_tv(tv1);
1531 tv1->v_type = VAR_FLOAT;
1532 tv1->vval.v_float = f;
1533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535#endif
1536 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 switch (*op)
1538 {
1539 case '+': n += tv_get_number(tv2); break;
1540 case '-': n -= tv_get_number(tv2); break;
1541 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001542 case '/': n = num_divide(n, tv_get_number(tv2),
1543 &failed); break;
1544 case '%': n = num_modulus(n, tv_get_number(tv2),
1545 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001546 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001547 clear_tv(tv1);
1548 tv1->v_type = VAR_NUMBER;
1549 tv1->vval.v_number = n;
1550 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001551 }
1552 else
1553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 if (tv2->v_type == VAR_FLOAT)
1555 break;
1556
Bram Moolenaarff697e62019-02-12 22:28:33 +01001557 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001558 s = tv_get_string(tv1);
1559 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001560 clear_tv(tv1);
1561 tv1->v_type = VAR_STRING;
1562 tv1->vval.v_string = s;
1563 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001564 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001567#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568 {
1569 float_T f;
1570
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 if (*op == '%' || *op == '.'
1572 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573 && tv2->v_type != VAR_NUMBER
1574 && tv2->v_type != VAR_STRING))
1575 break;
1576 if (tv2->v_type == VAR_FLOAT)
1577 f = tv2->vval.v_float;
1578 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001579 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001580 switch (*op)
1581 {
1582 case '+': tv1->vval.v_float += f; break;
1583 case '-': tv1->vval.v_float -= f; break;
1584 case '*': tv1->vval.v_float *= f; break;
1585 case '/': tv1->vval.v_float /= f; break;
1586 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001589 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 }
1591 }
1592
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001593 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 return FAIL;
1595}
1596
1597/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598 * Evaluate the expression used in a ":for var in expr" command.
1599 * "arg" points to "var".
1600 * Set "*errp" to TRUE for an error, FALSE otherwise;
1601 * Return a pointer that holds the info. Null when there is an error.
1602 */
1603 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604eval_for_line(
1605 char_u *arg,
1606 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001607 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001608 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609{
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001611 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T tv;
1614 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001615 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001619 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 if (fi == NULL)
1621 return NULL;
1622
Bram Moolenaar404557e2021-07-05 21:41:48 +02001623 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1624 &fi->fi_semicolon, FALSE);
1625 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 return fi;
1627
Bram Moolenaar404557e2021-07-05 21:41:48 +02001628 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001629 if (expr[0] != 'i' || expr[1] != 'n'
1630 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001632 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1633 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1634 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001635 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 return fi;
1637 }
1638
1639 if (skip)
1640 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001641 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1642 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 {
1644 *errp = FALSE;
1645 if (!skip)
1646 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001647 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001648 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001649 l = tv.vval.v_list;
1650 if (l == NULL)
1651 {
1652 // a null list is like an empty list: do nothing
1653 clear_tv(&tv);
1654 }
1655 else
1656 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001658 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 // No need to increment the refcount, it's already set for
1661 // the list being used in "tv".
1662 fi->fi_list = l;
1663 list_add_watch(l, &fi->fi_lw);
1664 fi->fi_lw.lw_item = l->lv_first;
1665 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001666 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001668 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001669 fi->fi_bi = 0;
1670 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001672 typval_T btv;
1673
1674 // Make a copy, so that the iteration still works when the
1675 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001677 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001678 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001679 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001680 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001681 else if (tv.v_type == VAR_STRING)
1682 {
1683 fi->fi_byte_idx = 0;
1684 fi->fi_string = tv.vval.v_string;
1685 tv.vval.v_string = NULL;
1686 if (fi->fi_string == NULL)
1687 fi->fi_string = vim_strsave((char_u *)"");
1688 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 else
1690 {
Sean Dewar80d73952021-08-04 19:25:54 +02001691 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 }
1694 }
1695 }
1696 if (skip)
1697 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001698 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699
1700 return fi;
1701}
1702
1703/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001704 * Used when looping over a :for line, skip the "in expr" part.
1705 */
1706 void
1707skip_for_lines(void *fi_void, evalarg_T *evalarg)
1708{
1709 forinfo_T *fi = (forinfo_T *)fi_void;
1710 int i;
1711
1712 for (i = 0; i < fi->fi_break_count; ++i)
1713 eval_next_line(evalarg);
1714}
1715
1716/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 * Use the first item in a ":for" list. Advance to the next.
1718 * Assign the values to the variable (list). "arg" points to the first one.
1719 * Return TRUE when a valid item was found, FALSE when at end of list or
1720 * something wrong.
1721 */
1722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001723next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001725 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001727 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001728 ? (ASSIGN_FINAL
1729 // first round: error if variable exists
1730 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1731 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001732 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001733 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001734 int skip_assign = in_vim9script() && arg[0] == '_'
1735 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 if (fi->fi_blob != NULL)
1738 {
1739 typval_T tv;
1740
1741 if (fi->fi_bi >= blob_len(fi->fi_blob))
1742 return FALSE;
1743 tv.v_type = VAR_NUMBER;
1744 tv.v_lock = VAR_FIXED;
1745 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1746 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001747 if (skip_assign)
1748 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001749 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001750 fi->fi_varcount, flag, NULL) == OK;
1751 }
1752
1753 if (fi->fi_string != NULL)
1754 {
1755 typval_T tv;
1756 int len;
1757
1758 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1759 if (len == 0)
1760 return FALSE;
1761 tv.v_type = VAR_STRING;
1762 tv.v_lock = VAR_FIXED;
1763 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1764 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001765 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001766 if (skip_assign)
1767 result = TRUE;
1768 else
1769 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001770 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001771 vim_free(tv.vval.v_string);
1772 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001773 }
1774
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 item = fi->fi_lw.lw_item;
1776 if (item == NULL)
1777 result = FALSE;
1778 else
1779 {
1780 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001781 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001782 if (skip_assign)
1783 result = TRUE;
1784 else
1785 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001786 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 }
1788 return result;
1789}
1790
1791/*
1792 * Free the structure used to store info used by ":for".
1793 */
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796{
Bram Moolenaar33570922005-01-25 22:26:29 +00001797 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001799 if (fi == NULL)
1800 return;
1801 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001802 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001804 list_unref(fi->fi_list);
1805 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001806 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001807 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001808 else
1809 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 vim_free(fi);
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814set_context_for_expression(
1815 expand_T *xp,
1816 char_u *arg,
1817 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001819 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001823 if (cmdidx == CMD_let || cmdidx == CMD_var
1824 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 {
1826 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001829 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 {
1832 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001833 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001834 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835 break;
1836 }
1837 return;
1838 }
1839 }
1840 else
1841 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1842 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 while ((xp->xp_pattern = vim_strpbrk(arg,
1844 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1845 {
1846 c = *xp->xp_pattern;
1847 if (c == '&')
1848 {
1849 c = xp->xp_pattern[1];
1850 if (c == '&')
1851 {
1852 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001858 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1859 xp->xp_pattern += 2;
1860
1861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 else if (c == '$')
1864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 xp->xp_context = EXPAND_ENV_VARS;
1867 }
1868 else if (c == '=')
1869 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001870 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 xp->xp_context = EXPAND_EXPRESSION;
1872 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001873 else if (c == '#'
1874 && xp->xp_context == EXPAND_EXPRESSION)
1875 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001877 break;
1878 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001879 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 && xp->xp_context == EXPAND_FUNCTIONS
1881 && vim_strchr(xp->xp_pattern, '(') == NULL)
1882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001883 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 break;
1885 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001886 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001888 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1891 if (c == '\\' && xp->xp_pattern[1] != NUL)
1892 ++xp->xp_pattern;
1893 xp->xp_context = EXPAND_NOTHING;
1894 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1899 /* skip */ ;
1900 xp->xp_context = EXPAND_NOTHING;
1901 }
1902 else if (c == '|')
1903 {
1904 if (xp->xp_pattern[1] == '|')
1905 {
1906 ++xp->xp_pattern;
1907 xp->xp_context = EXPAND_EXPRESSION;
1908 }
1909 else
1910 xp->xp_context = EXPAND_COMMANDS;
1911 }
1912 else
1913 xp->xp_context = EXPAND_EXPRESSION;
1914 }
1915 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 // Doesn't look like something valid, expand as an expression
1917 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 arg = xp->xp_pattern;
1920 if (*arg != NUL)
1921 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1922 /* skip */ ;
1923 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001924
1925 // ":exe one two" completes "two"
1926 if ((cmdidx == CMD_execute
1927 || cmdidx == CMD_echo
1928 || cmdidx == CMD_echon
1929 || cmdidx == CMD_echomsg)
1930 && xp->xp_context == EXPAND_EXPRESSION)
1931 {
1932 for (;;)
1933 {
1934 char_u *n = skiptowhite(arg);
1935
1936 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1937 break;
1938 arg = skipwhite(n);
1939 }
1940 }
1941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 xp->xp_pattern = arg;
1943}
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001946 * Return TRUE if "pat" matches "text".
1947 * Does not use 'cpo' and always uses 'magic'.
1948 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001949 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001950pattern_match(char_u *pat, char_u *text, int ic)
1951{
1952 int matches = FALSE;
1953 char_u *save_cpo;
1954 regmatch_T regmatch;
1955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001956 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001957 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001958 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001959 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1960 if (regmatch.regprog != NULL)
1961 {
1962 regmatch.rm_ic = ic;
1963 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1964 vim_regfree(regmatch.regprog);
1965 }
1966 p_cpo = save_cpo;
1967 return matches;
1968}
1969
1970/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001971 * Handle a name followed by "(". Both for just "name(arg)" and for
1972 * "expr->name(arg)".
1973 * Returns OK or FAIL.
1974 */
1975 static int
1976eval_func(
1977 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001978 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001979 char_u *name,
1980 int name_len,
1981 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001982 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001983 typval_T *basetv) // "expr" for "expr->name(arg)"
1984{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001985 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001986 char_u *s = name;
1987 int len = name_len;
1988 partial_T *partial;
1989 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001990 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001991 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992
1993 if (!evaluate)
1994 check_vars(s, len);
1995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001996 // If "s" is the name of a variable of type VAR_FUNC
1997 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001998 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001999 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // Need to make a copy, in case evaluating the arguments makes
2002 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002003 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002004 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 ret = FAIL;
2006 else
2007 {
2008 funcexe_T funcexe;
2009
2010 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002011 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002012 funcexe.fe_firstline = curwin->w_cursor.lnum;
2013 funcexe.fe_lastline = curwin->w_cursor.lnum;
2014 funcexe.fe_evaluate = evaluate;
2015 funcexe.fe_partial = partial;
2016 funcexe.fe_basetv = basetv;
2017 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002018 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002019 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020 }
2021 vim_free(s);
2022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002023 // If evaluate is FALSE rettv->v_type was not set in
2024 // get_func_tv, but it's needed in handle_subscript() to parse
2025 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2027 {
2028 rettv->vval.v_string = NULL;
2029 rettv->v_type = VAR_FUNC;
2030 }
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // Stop the expression evaluation when immediately
2033 // aborting on error, or when an interrupt occurred or
2034 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035 if (evaluate && aborting())
2036 {
2037 if (ret == OK)
2038 clear_tv(rettv);
2039 ret = FAIL;
2040 }
2041 return ret;
2042}
2043
2044/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002045 * Get the next line source line without advancing. But do skip over comment
2046 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002047 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002048 */
2049 static char_u *
2050getline_peek_skip_comments(evalarg_T *evalarg)
2051{
2052 for (;;)
2053 {
2054 char_u *next = getline_peek(evalarg->eval_getline,
2055 evalarg->eval_cookie);
2056 char_u *p;
2057
2058 if (next == NULL)
2059 break;
2060 p = skipwhite(next);
2061 if (*p != NUL && !vim9_comment_start(p))
2062 return next;
2063 (void)eval_next_line(evalarg);
2064 }
2065 return NULL;
2066}
2067
2068/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002069 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2070 * comment) and there is a next line, return the next line (skipping blanks)
2071 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002072 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2073 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074 * "arg" must point somewhere inside a line, not at the start.
2075 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002076 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002077eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2078{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002079 char_u *p = skipwhite(arg);
2080
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002081 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002082 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002083 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002084 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002085 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002086 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002087 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002088
2089 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002090 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002091 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002092 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002093
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 {
2096 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002097 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 }
2099 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002100 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101}
2102
2103/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002104 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002105 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002106 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002107 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002108eval_next_line(evalarg_T *evalarg)
2109{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002110 garray_T *gap = &evalarg->eval_ga;
2111 char_u *line;
2112
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002113 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002114 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2115 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002116 else
2117 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002118 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002119 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2120 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002121 char_u *p = skipwhite(line);
2122
2123 // Going to concatenate the lines after parsing. For an empty or
2124 // comment line use an empty string.
2125 if (*p == NUL || vim9_comment_start(p))
2126 {
2127 vim_free(line);
2128 line = vim_strsave((char_u *)"");
2129 }
2130
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002131 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2132 ++gap->ga_len;
2133 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002134 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 {
2136 vim_free(evalarg->eval_tofree);
2137 evalarg->eval_tofree = line;
2138 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002139
2140 // Advanced to the next line, "arg" no longer points into the previous
2141 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002142 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002143 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002144}
2145
Bram Moolenaare6b53242020-07-01 17:28:33 +02002146/*
2147 * Call eval_next_non_blank() and get the next line if needed.
2148 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002149 char_u *
2150skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2151{
2152 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002153 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002154
Bram Moolenaar962d7212020-07-04 14:15:00 +02002155 if (evalarg == NULL)
2156 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002157 eval_next_non_blank(p, evalarg, &getnext);
2158 if (getnext)
2159 return eval_next_line(evalarg);
2160 return p;
2161}
2162
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002163/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002164 * Initialize "evalarg" for use.
2165 */
2166 void
2167init_evalarg(evalarg_T *evalarg)
2168{
2169 CLEAR_POINTER(evalarg);
2170 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2171}
2172
2173/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002175 */
2176 void
2177clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2178{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002179 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002180 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002181 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002182 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002183 if (eap != NULL)
2184 {
2185 // We may need to keep the original command line, e.g. for
2186 // ":let" it has the variable names. But we may also need the
2187 // new one, "nextcmd" points into it. Keep both.
2188 vim_free(eap->cmdline_tofree);
2189 eap->cmdline_tofree = *eap->cmdlinep;
2190 *eap->cmdlinep = evalarg->eval_tofree;
2191 }
2192 else
2193 vim_free(evalarg->eval_tofree);
2194 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002195 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002196
Bram Moolenaar844fb642021-10-23 13:32:30 +01002197 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002198 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002199 }
2200}
2201
2202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2206 */
2207
2208/*
2209 * Handle zero level expression.
2210 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002212 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002213 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 * Return OK or FAIL.
2215 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217eval0(
2218 char_u *arg,
2219 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002220 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 int ret;
2224 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002225 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002226 int did_emsg_before = did_emsg;
2227 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002228 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002229 int check_for_end = TRUE;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002230 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002234 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002235 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002236
Bram Moolenaar02929a32021-12-17 14:46:12 +00002237 // In Vim9 script a command block is not split at NL characters for
2238 // commands using an expression argument. Skip over a '#' comment to check
2239 // for a following NL. Require white space before the '#'.
2240 if (in_vim9script() && p > expr_end)
2241 while (*p == '#')
2242 {
2243 char_u *nl = vim_strchr(p, NL);
2244
2245 if (nl == NULL)
2246 break;
2247 p = skipwhite(nl + 1);
2248 if (eap != NULL && *p != NUL)
2249 eap->nextcmd = p;
2250 check_for_end = FALSE;
2251 }
2252
2253 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002254 end_error = !ends_excmd2(arg, p);
2255 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
2257 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 /*
2260 * Report the invalid expression unless the expression evaluation has
2261 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002262 * exception, or we already gave a more specific error.
2263 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002265 if (!aborting()
2266 && did_emsg == did_emsg_before
2267 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002268 && (flags & EVAL_CONSTANT) == 0
2269 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002270 {
2271 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002272 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002273 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002274 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002275 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002276
2277 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002278 // a next command to avoid more errors, unless "|" is following, which
2279 // could only be a command separator.
2280 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2281 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002284
Bram Moolenaar02929a32021-12-17 14:46:12 +00002285 if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002286 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return ret;
2289}
2290
2291/*
2292 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002293 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002294 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 *
2296 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002297 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002299 * Note: "rettv.v_lock" is not set.
2300 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Return OK or FAIL.
2302 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002303 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002306 char_u *p;
2307 int getnext;
2308
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002309 CLEAR_POINTER(rettv);
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 /*
2312 * Get the first variable.
2313 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 return FAIL;
2316
Bram Moolenaar793648f2020-06-26 21:28:25 +02002317 p = eval_next_non_blank(*arg, evalarg, &getnext);
2318 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002320 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002321 int result;
2322 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002323 evalarg_T *evalarg_used = evalarg;
2324 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002326 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002327 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002328
2329 if (evalarg == NULL)
2330 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002331 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002332 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002334 orig_flags = evalarg_used->eval_flags;
2335 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002336
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002337 if (getnext)
2338 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002339 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002340 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002341 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002342 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002343 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002344 clear_tv(rettv);
2345 return FAIL;
2346 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002347 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002348 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002351 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 int error = FALSE;
2354
Bram Moolenaar13106602020-10-04 16:06:05 +02002355 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002356 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002357 else if (vim9script)
2358 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002359 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002361 if (error || !op_falsy || !result)
2362 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002363 if (error)
2364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002368 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002370 if (op_falsy)
2371 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002372 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002373 {
mityu4ac198c2021-05-28 17:52:40 +02002374 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002375 clear_tv(rettv);
2376 return FAIL;
2377 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002378 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002379 evalarg_used->eval_flags = (op_falsy ? !result : result)
2380 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2381 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002382 {
2383 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002385 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 if (!op_falsy || !result)
2387 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002389 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002391 /*
2392 * Check for the ":".
2393 */
2394 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2395 if (*p != ':')
2396 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002397 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002398 if (evaluate && result)
2399 clear_tv(rettv);
2400 evalarg_used->eval_flags = orig_flags;
2401 return FAIL;
2402 }
2403 if (getnext)
2404 *arg = eval_next_line(evalarg_used);
2405 else
2406 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002407 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002408 {
2409 error_white_both(p, 1);
2410 clear_tv(rettv);
2411 evalarg_used->eval_flags = orig_flags;
2412 return FAIL;
2413 }
2414 *arg = p;
2415 }
2416
2417 /*
2418 * Get the third variable. Recursive!
2419 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002420 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002421 {
mityu4ac198c2021-05-28 17:52:40 +02002422 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002423 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002424 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002425 return FAIL;
2426 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002427 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2428 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002430 if (eval1(arg, &var2, evalarg_used) == FAIL)
2431 {
2432 if (evaluate && result)
2433 clear_tv(rettv);
2434 evalarg_used->eval_flags = orig_flags;
2435 return FAIL;
2436 }
2437 if (evaluate && !result)
2438 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440
2441 if (evalarg == NULL)
2442 clear_evalarg(&local_evalarg, NULL);
2443 else
2444 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446
2447 return OK;
2448}
2449
2450/*
2451 * Handle first level expression:
2452 * expr2 || expr2 || expr2 logical OR
2453 *
2454 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002455 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 *
2457 * Return OK or FAIL.
2458 */
2459 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002462 char_u *p;
2463 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 /*
2466 * Get the first variable.
2467 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 return FAIL;
2470
2471 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002474 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002475 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002477 evalarg_T *evalarg_used = evalarg;
2478 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 int evaluate;
2480 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 long result = FALSE;
2482 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002483 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002484 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002485
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486 if (evalarg == NULL)
2487 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002488 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 orig_flags = evalarg_used->eval_flags;
2492 evaluate = orig_flags & EVAL_EVALUATE;
2493 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002494 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002495 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002496 result = tv_get_bool_chk(rettv, &error);
2497 else if (tv_get_number_chk(rettv, &error) != 0)
2498 result = TRUE;
2499 clear_tv(rettv);
2500 if (error)
2501 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503
2504 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 while (p[0] == '|' && p[1] == '|')
2508 {
2509 if (getnext)
2510 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002511 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002512 {
2513 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2514 {
2515 error_white_both(p, 2);
2516 clear_tv(rettv);
2517 return FAIL;
2518 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002519 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521
2522 /*
2523 * Get the second variable.
2524 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002525 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2526 {
mityu4ac198c2021-05-28 17:52:40 +02002527 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002528 clear_tv(rettv);
2529 return FAIL;
2530 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2532 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002533 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536
2537 /*
2538 * Compute the result.
2539 */
2540 if (evaluate && !result)
2541 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002542 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002543 result = tv_get_bool_chk(&var2, &error);
2544 else if (tv_get_number_chk(&var2, &error) != 0)
2545 result = TRUE;
2546 clear_tv(&var2);
2547 if (error)
2548 return FAIL;
2549 }
2550 if (evaluate)
2551 {
2552 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002553 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002554 rettv->v_type = VAR_BOOL;
2555 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002556 }
2557 else
2558 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002559 rettv->v_type = VAR_NUMBER;
2560 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002561 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563
2564 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002566
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 if (evalarg == NULL)
2568 clear_evalarg(&local_evalarg, NULL);
2569 else
2570 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572
2573 return OK;
2574}
2575
2576/*
2577 * Handle second level expression:
2578 * expr3 && expr3 && expr3 logical AND
2579 *
2580 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002581 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 *
2583 * Return OK or FAIL.
2584 */
2585 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002588 char_u *p;
2589 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 /*
2592 * Get the first variable.
2593 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002594 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return FAIL;
2596
2597 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002600 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002603 evalarg_T *evalarg_used = evalarg;
2604 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605 int orig_flags;
2606 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 long result = TRUE;
2608 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002609 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002610 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002611
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002612 if (evalarg == NULL)
2613 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002614 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002617 orig_flags = evalarg_used->eval_flags;
2618 evaluate = orig_flags & EVAL_EVALUATE;
2619 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002620 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002621 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002622 result = tv_get_bool_chk(rettv, &error);
2623 else if (tv_get_number_chk(rettv, &error) == 0)
2624 result = FALSE;
2625 clear_tv(rettv);
2626 if (error)
2627 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629
2630 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 while (p[0] == '&' && p[1] == '&')
2634 {
2635 if (getnext)
2636 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002637 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002638 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002639 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002640 {
2641 error_white_both(p, 2);
2642 clear_tv(rettv);
2643 return FAIL;
2644 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002645 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 /*
2649 * Get the second variable.
2650 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002651 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2652 {
mityu4ac198c2021-05-28 17:52:40 +02002653 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002654 clear_tv(rettv);
2655 return FAIL;
2656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2658 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002660 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002662 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663
2664 /*
2665 * Compute the result.
2666 */
2667 if (evaluate && result)
2668 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002669 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002670 result = tv_get_bool_chk(&var2, &error);
2671 else if (tv_get_number_chk(&var2, &error) == 0)
2672 result = FALSE;
2673 clear_tv(&var2);
2674 if (error)
2675 return FAIL;
2676 }
2677 if (evaluate)
2678 {
2679 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002680 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002681 rettv->v_type = VAR_BOOL;
2682 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002683 }
2684 else
2685 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002686 rettv->v_type = VAR_NUMBER;
2687 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002688 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002690
2691 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002693
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 if (evalarg == NULL)
2695 clear_evalarg(&local_evalarg, NULL);
2696 else
2697 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699
2700 return OK;
2701}
2702
2703/*
2704 * Handle third level expression:
2705 * var1 == var2
2706 * var1 =~ var2
2707 * var1 != var2
2708 * var1 !~ var2
2709 * var1 > var2
2710 * var1 >= var2
2711 * var1 < var2
2712 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002713 * var1 is var2
2714 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 *
2716 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002717 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 *
2719 * Return OK or FAIL.
2720 */
2721 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002725 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002726 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002728 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 /*
2731 * Get the first variable.
2732 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 return FAIL;
2735
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002736 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002737 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002740 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002742 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002744 typval_T var2;
2745 int ic;
2746 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002747 int evaluate = evalarg == NULL
2748 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002749
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002750 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002751 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002752 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002753 p = *arg;
2754 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002755 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2756 {
mityu4ac198c2021-05-28 17:52:40 +02002757 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002758 clear_tv(rettv);
2759 return FAIL;
2760 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002761
Bram Moolenaar696ba232020-07-29 21:20:41 +02002762 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2763 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002764 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002765 clear_tv(rettv);
2766 return FAIL;
2767 }
2768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002769 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (p[len] == '?')
2771 {
2772 ic = TRUE;
2773 ++len;
2774 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002775 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else if (p[len] == '#')
2777 {
2778 ic = FALSE;
2779 ++len;
2780 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002781 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002783 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /*
2786 * Get the second variable.
2787 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002788 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2789 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002790 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002791 clear_tv(rettv);
2792 return FAIL;
2793 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002794 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002797 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 return FAIL;
2799 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002800 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002801 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002802 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002803
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002804 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002805 {
2806 ret = FAIL;
2807 clear_tv(rettv);
2808 }
2809 else
2810 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002811 clear_tv(&var2);
2812 return ret;
2813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815
2816 return OK;
2817}
2818
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002819/*
2820 * Make a copy of blob "tv1" and append blob "tv2".
2821 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 void
2823eval_addblob(typval_T *tv1, typval_T *tv2)
2824{
2825 blob_T *b1 = tv1->vval.v_blob;
2826 blob_T *b2 = tv2->vval.v_blob;
2827 blob_T *b = blob_alloc();
2828 int i;
2829
2830 if (b != NULL)
2831 {
2832 for (i = 0; i < blob_len(b1); i++)
2833 ga_append(&b->bv_ga, blob_get(b1, i));
2834 for (i = 0; i < blob_len(b2); i++)
2835 ga_append(&b->bv_ga, blob_get(b2, i));
2836
2837 clear_tv(tv1);
2838 rettv_blob_set(tv1, b);
2839 }
2840}
2841
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002842/*
2843 * Make a copy of list "tv1" and append list "tv2".
2844 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 int
2846eval_addlist(typval_T *tv1, typval_T *tv2)
2847{
2848 typval_T var3;
2849
2850 // concatenate Lists
2851 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2852 {
2853 clear_tv(tv1);
2854 clear_tv(tv2);
2855 return FAIL;
2856 }
2857 clear_tv(tv1);
2858 *tv1 = var3;
2859 return OK;
2860}
2861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862/*
2863 * Handle fourth level expression:
2864 * + number addition
2865 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002866 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002867 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 *
2869 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002870 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 *
2872 * Return OK or FAIL.
2873 */
2874 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002875eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 /*
2878 * Get the first variable.
2879 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 return FAIL;
2882
2883 /*
2884 * Repeat computing, until no '+', '-' or '.' is following.
2885 */
2886 for (;;)
2887 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002888 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889 int getnext;
2890 char_u *p;
2891 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002892 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 int concat;
2894 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002895 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002897 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002898 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002899 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 p = eval_next_non_blank(*arg, evalarg, &getnext);
2901 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002902 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002903 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2904 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002906 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2907 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002908
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002909 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002910 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002911 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002912 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002913 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002914 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002915 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002916 {
mityu4ac198c2021-05-28 17:52:40 +02002917 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002918 clear_tv(rettv);
2919 return FAIL;
2920 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002921 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002922 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002923 if ((op != '+' || (rettv->v_type != VAR_LIST
2924 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002925#ifdef FEAT_FLOAT
2926 && (op == '.' || rettv->v_type != VAR_FLOAT)
2927#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002928 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002929 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002930 int error = FALSE;
2931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002932 // For "list + ...", an illegal use of the first operand as
2933 // a number cannot be determined before evaluating the 2nd
2934 // operand: if this is also a list, all is ok.
2935 // For "something . ...", "something - ..." or "non-list + ...",
2936 // we know that the first operand needs to be a string or number
2937 // without evaluating the 2nd operand. So check before to avoid
2938 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002939 if (op != '.')
2940 tv_get_number_chk(rettv, &error);
2941 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
2943 clear_tv(rettv);
2944 return FAIL;
2945 }
2946 }
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 /*
2949 * Get the second variable.
2950 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002951 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002952 {
mityu89dcb4d2021-05-28 13:50:17 +02002953 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002954 clear_tv(rettv);
2955 return FAIL;
2956 }
2957 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002958 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 return FAIL;
2962 }
2963
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {
2966 /*
2967 * Compute the result.
2968 */
2969 if (op == '.')
2970 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2972 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002973 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974
Bram Moolenaar2e086612020-08-25 22:37:48 +02002975 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002976 || var2.v_type == VAR_CHANNEL
2977 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002978 semsg(_(e_using_invalid_value_as_string_str),
2979 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002980#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002981 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002982 {
2983 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2984 var2.vval.v_float);
2985 s2 = buf2;
2986 }
2987#endif
2988 else
2989 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002990 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991 {
2992 clear_tv(rettv);
2993 clear_tv(&var2);
2994 return FAIL;
2995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 clear_tv(rettv);
2998 rettv->v_type = VAR_STRING;
2999 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003001 else if (op == '+' && rettv->v_type == VAR_BLOB
3002 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003004 else if (op == '+' && rettv->v_type == VAR_LIST
3005 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003006 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003008 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 else
3011 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 int error = FALSE;
3013 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003015 float_T f1 = 0, f2 = 0;
3016
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 f1 = rettv->vval.v_float;
3020 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003021 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003022 else
3023#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003025 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026 if (error)
3027 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003028 // This can only happen for "list + non-list" or
3029 // "blob + non-blob". For "non-list + ..." or
3030 // "something - ...", we returned before evaluating the
3031 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003033 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034 return FAIL;
3035 }
3036#ifdef FEAT_FLOAT
3037 if (var2.v_type == VAR_FLOAT)
3038 f1 = n1;
3039#endif
3040 }
3041#ifdef FEAT_FLOAT
3042 if (var2.v_type == VAR_FLOAT)
3043 {
3044 f2 = var2.vval.v_float;
3045 n2 = 0;
3046 }
3047 else
3048#endif
3049 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003050 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051 if (error)
3052 {
3053 clear_tv(rettv);
3054 clear_tv(&var2);
3055 return FAIL;
3056 }
3057#ifdef FEAT_FLOAT
3058 if (rettv->v_type == VAR_FLOAT)
3059 f2 = n2;
3060#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063
3064#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3067 {
3068 if (op == '+')
3069 f1 = f1 + f2;
3070 else
3071 f1 = f1 - f2;
3072 rettv->v_type = VAR_FLOAT;
3073 rettv->vval.v_float = f1;
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076#endif
3077 {
3078 if (op == '+')
3079 n1 = n1 + n2;
3080 else
3081 n1 = n1 - n2;
3082 rettv->v_type = VAR_NUMBER;
3083 rettv->vval.v_number = n1;
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088 }
3089 return OK;
3090}
3091
3092/*
3093 * Handle fifth level expression:
3094 * * number multiplication
3095 * / number division
3096 * % number modulo
3097 *
3098 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003099 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 *
3101 * Return OK or FAIL.
3102 */
3103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104eval6(
3105 char_u **arg,
3106 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003107 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003108 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003111 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /*
3115 * Get the first variable.
3116 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003117 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 return FAIL;
3119
3120 /*
3121 * Repeat computing, until no '*', '/' or '%' is following.
3122 */
3123 for (;;)
3124 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003125 int evaluate;
3126 int getnext;
3127 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003128 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003129 int op;
3130 varnumber_T n1, n2;
3131#ifdef FEAT_FLOAT
3132 float_T f1, f2;
3133#endif
3134 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003135
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003136 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003137 p = eval_next_non_blank(*arg, evalarg, &getnext);
3138 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003139 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003141
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003142 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003143 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003144 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003145 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003146 {
3147 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3148 {
mityu4ac198c2021-05-28 17:52:40 +02003149 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003150 clear_tv(rettv);
3151 return FAIL;
3152 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003153 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003156#ifdef FEAT_FLOAT
3157 f1 = 0;
3158 f2 = 0;
3159#endif
3160 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003161 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163#ifdef FEAT_FLOAT
3164 if (rettv->v_type == VAR_FLOAT)
3165 {
3166 f1 = rettv->vval.v_float;
3167 use_float = TRUE;
3168 n1 = 0;
3169 }
3170 else
3171#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003172 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003173 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003174 if (error)
3175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177 else
3178 n1 = 0;
3179
3180 /*
3181 * Get the second variable.
3182 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003183 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3184 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003185 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003186 clear_tv(rettv);
3187 return FAIL;
3188 }
3189 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003190 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 return FAIL;
3192
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003193 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195#ifdef FEAT_FLOAT
3196 if (var2.v_type == VAR_FLOAT)
3197 {
3198 if (!use_float)
3199 {
3200 f1 = n1;
3201 use_float = TRUE;
3202 }
3203 f2 = var2.vval.v_float;
3204 n2 = 0;
3205 }
3206 else
3207#endif
3208 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003209 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 clear_tv(&var2);
3211 if (error)
3212 return FAIL;
3213#ifdef FEAT_FLOAT
3214 if (use_float)
3215 f2 = n2;
3216#endif
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 /*
3220 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003221 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223#ifdef FEAT_FLOAT
3224 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226 if (op == '*')
3227 f1 = f1 * f2;
3228 else if (op == '/')
3229 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003230# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003231 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003232 if (f2 == 0.0)
3233 {
3234 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003235 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003236 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003237 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003238 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003239 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003240 }
3241 else
3242 f1 = f1 / f2;
3243# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003244 // We rely on the floating point library to handle divide
3245 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003246 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003247# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003250 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003251 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003252 return FAIL;
3253 }
3254 rettv->v_type = VAR_FLOAT;
3255 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003260 int failed = FALSE;
3261
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003262 if (op == '*')
3263 n1 = n1 * n2;
3264 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003265 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003267 n1 = num_modulus(n1, n2, &failed);
3268 if (failed)
3269 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271 rettv->v_type = VAR_NUMBER;
3272 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275 }
3276
3277 return OK;
3278}
3279
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003280/*
3281 * Handle a type cast before a base level expression.
3282 * "arg" must point to the first non-white of the expression.
3283 * "arg" is advanced to just after the recognized expression.
3284 * Return OK or FAIL.
3285 */
3286 static int
3287eval7t(
3288 char_u **arg,
3289 typval_T *rettv,
3290 evalarg_T *evalarg,
3291 int want_string) // after "." operator
3292{
3293 type_T *want_type = NULL;
3294 garray_T type_list; // list of pointers to allocated types
3295 int res;
3296 int evaluate = evalarg == NULL ? 0
3297 : (evalarg->eval_flags & EVAL_EVALUATE);
3298
3299 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003300 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3301 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003302 {
3303 ++*arg;
3304 ga_init2(&type_list, sizeof(type_T *), 10);
3305 want_type = parse_type(arg, &type_list, TRUE);
3306 if (want_type == NULL && (evaluate || **arg != '>'))
3307 {
3308 clear_type_list(&type_list);
3309 return FAIL;
3310 }
3311
3312 if (**arg != '>')
3313 {
3314 if (*skipwhite(*arg) == '>')
3315 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3316 else
3317 emsg(_(e_missing_gt));
3318 clear_type_list(&type_list);
3319 return FAIL;
3320 }
3321 ++*arg;
3322 *arg = skipwhite_and_linebreak(*arg, evalarg);
3323 }
3324
3325 res = eval7(arg, rettv, evalarg, want_string);
3326
3327 if (want_type != NULL && evaluate)
3328 {
3329 if (res == OK)
3330 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003331 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3332 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003333
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003334 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003335 {
3336 if (want_type == &t_bool && actual != &t_bool
3337 && (actual->tt_flags & TTFLAG_BOOL_OK))
3338 {
3339 int n = tv2bool(rettv);
3340
3341 // can use "0" and "1" for boolean in some places
3342 clear_tv(rettv);
3343 rettv->v_type = VAR_BOOL;
3344 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3345 }
3346 else
3347 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003348 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003349
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003350 where.wt_variable = TRUE;
3351 res = check_type(want_type, actual, TRUE, where);
3352 }
3353 }
3354 }
3355 clear_type_list(&type_list);
3356 }
3357
3358 return res;
3359}
3360
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003361 int
3362eval_leader(char_u **arg, int vim9)
3363{
3364 char_u *s = *arg;
3365 char_u *p = *arg;
3366
3367 while (*p == '!' || *p == '-' || *p == '+')
3368 {
3369 char_u *n = skipwhite(p + 1);
3370
3371 // ++, --, -+ and +- are not accepted in Vim9 script
3372 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3373 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003374 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003375 return FAIL;
3376 }
3377 p = n;
3378 }
3379 *arg = p;
3380 return OK;
3381}
3382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383/*
3384 * Handle sixth level expression:
3385 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003386 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003387 * "string" string constant
3388 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * &option-name option value
3390 * @r register contents
3391 * identifier variable value
3392 * function() function call
3393 * $VAR environment variable
3394 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003395 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003397 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003398 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *
3400 * Also handle:
3401 * ! in front logical NOT
3402 * - in front unary minus
3403 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003404 * trailing [] subscript in String or List
3405 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003406 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 *
3408 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003409 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 *
3411 * Return OK or FAIL.
3412 */
3413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414eval7(
3415 char_u **arg,
3416 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003417 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003420 int evaluate = evalarg != NULL
3421 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 int len;
3423 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *start_leader, *end_leader;
3425 int ret = OK;
3426 char_u *alias;
3427
3428 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003430 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003432 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003435 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003438 if (eval_leader(arg, in_vim9script()) == FAIL)
3439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 end_leader = *arg;
3441
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003442 if (**arg == '.' && (!isdigit(*(*arg + 1))
3443#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003444 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003445#endif
3446 ))
3447 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003448 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003449 ++*arg;
3450 return FAIL;
3451 }
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 switch (**arg)
3454 {
3455 /*
3456 * Number constant.
3457 */
3458 case '0':
3459 case '1':
3460 case '2':
3461 case '3':
3462 case '4':
3463 case '5':
3464 case '6':
3465 case '7':
3466 case '8':
3467 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003468 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003469
3470 // Apply prefixed "-" and "+" now. Matters especially when
3471 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003472 if (ret == OK && evaluate && end_leader > start_leader
3473 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003474 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 break;
3476
3477 /*
3478 * String constant: "string".
3479 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 break;
3482
3483 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003486 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003487 break;
3488
3489 /*
3490 * List: [expr, expr]
3491 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 break;
3494
3495 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003496 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003497 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003498 case '#': if (in_vim9script())
3499 {
3500 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3501 }
3502 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003503 {
3504 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003505 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003506 }
3507 else
3508 ret = NOTDONE;
3509 break;
3510
3511 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003512 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003513 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003515 case '{': if (in_vim9script())
3516 ret = NOTDONE;
3517 else
3518 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003519 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003520 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 break;
3522
3523 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003524 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003526 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 break;
3528
3529 /*
3530 * Environment variable: $VAR.
3531 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003532 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534
3535 /*
3536 * Register contents: @r.
3537 */
3538 case '@': ++*arg;
3539 if (evaluate)
3540 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003541 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3542 semsg(_(e_syntax_error_at_str), *arg);
3543 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3544 emsg_invreg(**arg);
3545 else
3546 {
3547 rettv->v_type = VAR_STRING;
3548 rettv->vval.v_string = get_reg_contents(**arg,
3549 GREG_EXPR_SRC);
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 if (**arg != NUL)
3553 ++*arg;
3554 break;
3555
3556 /*
3557 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003558 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003560 case '(': ret = NOTDONE;
3561 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003562 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003563 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003564 if (ret == OK && evaluate)
3565 {
3566 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3567
Bram Moolenaara9931532021-06-12 15:58:16 +02003568 // Compile it here to get the return type. The return
3569 // type is optional, when it's missing use t_unknown.
3570 // This is recognized in compile_return().
3571 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3572 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003573 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003574 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003575 {
3576 clear_tv(rettv);
3577 ret = FAIL;
3578 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003579 }
3580 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003581 if (ret == NOTDONE)
3582 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003583 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003584 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003585
Bram Moolenaar9215f012020-06-27 21:18:00 +02003586 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003587 if (**arg == ')')
3588 ++*arg;
3589 else if (ret == OK)
3590 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003591 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003592 clear_tv(rettv);
3593 ret = FAIL;
3594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 break;
3597
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 break;
3600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601
3602 if (ret == NOTDONE)
3603 {
3604 /*
3605 * Must be a variable or function name.
3606 * Can also be a curly-braces kind of name: {expr}.
3607 */
3608 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003609 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 if (alias != NULL)
3611 s = alias;
3612
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003613 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 ret = FAIL;
3615 else
3616 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003617 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3618
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003619 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003620 {
3621 emsg(_(e_cannot_use_underscore_here));
3622 ret = FAIL;
3623 }
3624 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003625 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003626 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003627 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003628 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003629 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003630 else if (flags & EVAL_CONSTANT)
3631 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003633 {
3634 // get the value of "true", "false" or a variable
3635 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3636 {
3637 rettv->v_type = VAR_BOOL;
3638 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003639 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003640 }
3641 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003642 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003643 {
3644 rettv->v_type = VAR_BOOL;
3645 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003646 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003647 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003648 else if (len == 4 && in_vim9script()
3649 && STRNCMP(s, "null", 4) == 0)
3650 {
3651 rettv->v_type = VAR_SPECIAL;
3652 rettv->vval.v_number = VVAL_NULL;
3653 ret = OK;
3654 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003655 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003656 ret = eval_variable(s, len, rettv, NULL,
3657 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003659 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003660 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003661 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003662 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003663 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003666 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 }
3668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003669 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3670 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003671 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003672 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 /*
3675 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3676 */
3677 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003678 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003679 return ret;
3680}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003681
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003682/*
3683 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003684 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003685 * Adjusts "end_leaderp" until it is at "start_leader".
3686 */
3687 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003688eval7_leader(
3689 typval_T *rettv,
3690 int numeric_only,
3691 char_u *start_leader,
3692 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003693{
3694 char_u *end_leader = *end_leaderp;
3695 int ret = OK;
3696 int error = FALSE;
3697 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003698 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003699#ifdef FEAT_FLOAT
3700 float_T f = 0.0;
3701
3702 if (rettv->v_type == VAR_FLOAT)
3703 f = rettv->vval.v_float;
3704 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003705#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003706 {
3707 while (VIM_ISWHITE(end_leader[-1]))
3708 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003709 if (in_vim9script() && end_leader[-1] == '!')
3710 val = tv2bool(rettv);
3711 else
3712 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003713 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003714 if (error)
3715 {
3716 clear_tv(rettv);
3717 ret = FAIL;
3718 }
3719 else
3720 {
3721 while (end_leader > start_leader)
3722 {
3723 --end_leader;
3724 if (*end_leader == '!')
3725 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003726 if (numeric_only)
3727 {
3728 ++end_leader;
3729 break;
3730 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003731#ifdef FEAT_FLOAT
3732 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003733 {
3734 if (in_vim9script())
3735 {
3736 rettv->v_type = VAR_BOOL;
3737 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3738 }
3739 else
3740 f = !f;
3741 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742 else
3743#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003744 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003746 type = VAR_BOOL;
3747 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748 }
3749 else if (*end_leader == '-')
3750 {
3751#ifdef FEAT_FLOAT
3752 if (rettv->v_type == VAR_FLOAT)
3753 f = -f;
3754 else
3755#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003756 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003757 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003758 type = VAR_NUMBER;
3759 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003760 }
3761 }
3762#ifdef FEAT_FLOAT
3763 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003766 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003769#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003771 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003772 if (in_vim9script())
3773 rettv->v_type = type;
3774 else
3775 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003776 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003779 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return ret;
3781}
3782
3783/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003784 * Call the function referred to in "rettv".
3785 */
3786 static int
3787call_func_rettv(
3788 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003789 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003790 typval_T *rettv,
3791 int evaluate,
3792 dict_T *selfdict,
3793 typval_T *basetv)
3794{
3795 partial_T *pt = NULL;
3796 funcexe_T funcexe;
3797 typval_T functv;
3798 char_u *s;
3799 int ret;
3800
3801 // need to copy the funcref so that we can clear rettv
3802 if (evaluate)
3803 {
3804 functv = *rettv;
3805 rettv->v_type = VAR_UNKNOWN;
3806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003807 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003808 if (functv.v_type == VAR_PARTIAL)
3809 {
3810 pt = functv.vval.v_partial;
3811 s = partial_name(pt);
3812 }
3813 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003814 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003815 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003816 if (s == NULL || *s == NUL)
3817 {
3818 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003819 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003820 goto theend;
3821 }
3822 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003823 }
3824 else
3825 s = (char_u *)"";
3826
Bram Moolenaara80faa82020-04-12 19:37:17 +02003827 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003828 funcexe.fe_firstline = curwin->w_cursor.lnum;
3829 funcexe.fe_lastline = curwin->w_cursor.lnum;
3830 funcexe.fe_evaluate = evaluate;
3831 funcexe.fe_partial = pt;
3832 funcexe.fe_selfdict = selfdict;
3833 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003834 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003836theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003837 // Clear the funcref afterwards, so that deleting it while
3838 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839 if (evaluate)
3840 clear_tv(&functv);
3841
3842 return ret;
3843}
3844
3845/*
3846 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003847 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003848 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3849 */
3850 static int
3851eval_lambda(
3852 char_u **arg,
3853 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003854 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003855 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003856{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003857 int evaluate = evalarg != NULL
3858 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003859 typval_T base = *rettv;
3860 int ret;
3861
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862 rettv->v_type = VAR_UNKNOWN;
3863
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003864 if (**arg == '{')
3865 {
3866 // ->{lambda}()
3867 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3868 }
3869 else
3870 {
3871 // ->(lambda)()
3872 ++*arg;
3873 ret = eval1(arg, rettv, evalarg);
3874 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003875 if (**arg == ')')
3876 {
3877 ++*arg;
3878 }
3879 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003880 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003881 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003882 ret = FAIL;
3883 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003884 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003885 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003886 return FAIL;
3887 else if (**arg != '(')
3888 {
3889 if (verbose)
3890 {
3891 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003892 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003893 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003894 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003895 }
3896 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003897 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003898 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003899 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003900 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003901
3902 // Clear the funcref afterwards, so that deleting it while
3903 // evaluating the arguments is possible (see test55).
3904 if (evaluate)
3905 clear_tv(&base);
3906
3907 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003908}
3909
3910/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003911 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003912 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003913 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3914 */
3915 static int
3916eval_method(
3917 char_u **arg,
3918 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003919 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003920 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003921{
3922 char_u *name;
3923 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003924 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003925 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003926 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003927 int evaluate = evalarg != NULL
3928 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003930 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003933 len = get_name_len(arg, &alias, evaluate, TRUE);
3934 if (alias != NULL)
3935 name = alias;
3936
3937 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 {
3939 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003940 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003941 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003942 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003943 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003944 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003945 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003946 if (**arg != '(')
3947 {
3948 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00003949 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003950 ret = FAIL;
3951 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003952 else if (VIM_ISWHITE((*arg)[-1]))
3953 {
3954 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003955 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02003956 ret = FAIL;
3957 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003958 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003959 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003960 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003961 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003962
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003963 // Clear the funcref afterwards, so that deleting it while
3964 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003965 if (evaluate)
3966 clear_tv(&base);
3967
Bram Moolenaarac92e252019-08-03 21:58:38 +02003968 return ret;
3969}
3970
3971/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003972 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3973 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3975 */
3976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003977eval_index(
3978 char_u **arg,
3979 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003980 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003981 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003983 int evaluate = evalarg != NULL
3984 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003985 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003987 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003988 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003989 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003990 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003992 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3993 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003995 init_tv(&var1);
3996 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003998 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003999 /*
4000 * dict.name
4001 */
4002 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004003 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004005 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004006 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004007 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 }
4009 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004011 /*
4012 * something[idx]
4013 *
4014 * Get the (first) variable from inside the [].
4015 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004016 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004017 if (**arg == ':')
4018 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004019 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004021 else if (vim9 && **arg == ':')
4022 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004023 semsg(_(e_white_space_required_before_and_after_str_at_str),
4024 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004025 clear_tv(&var1);
4026 return FAIL;
4027 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004028 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004029 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004030 int error = FALSE;
4031
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004032#ifdef FEAT_FLOAT
4033 // allow for indexing with float
4034 if (vim9 && rettv->v_type == VAR_DICT
4035 && var1.v_type == VAR_FLOAT)
4036 {
4037 var1.vval.v_string = typval_tostring(&var1, TRUE);
4038 var1.v_type = VAR_STRING;
4039 }
4040#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004041 if (vim9 && rettv->v_type == VAR_LIST)
4042 tv_get_number_chk(&var1, &error);
4043 else
4044 error = tv_get_string_chk(&var1) == NULL;
4045 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004046 {
4047 // not a number or string
4048 clear_tv(&var1);
4049 return FAIL;
4050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052
4053 /*
4054 * Get the second variable from inside the [:].
4055 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004056 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 if (**arg == ':')
4058 {
4059 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004060 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004061 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004062 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004063 semsg(_(e_white_space_required_before_and_after_str_at_str),
4064 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004065 if (!empty1)
4066 clear_tv(&var1);
4067 return FAIL;
4068 }
4069 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004070 if (**arg == ']')
4071 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004072 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004074 if (!empty1)
4075 clear_tv(&var1);
4076 return FAIL;
4077 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004078 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 if (!empty1)
4082 clear_tv(&var1);
4083 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 return FAIL;
4085 }
4086 }
4087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004088 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004089 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004090 if (**arg != ']')
4091 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004092 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004093 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004094 clear_tv(&var1);
4095 if (range)
4096 clear_tv(&var2);
4097 return FAIL;
4098 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004099 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 }
4101
4102 if (evaluate)
4103 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004104 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004105 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004107
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004108 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004110 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 clear_tv(&var2);
4112 return res;
4113 }
4114 return OK;
4115}
4116
4117/*
4118 * Check if "rettv" can have an [index] or [sli:ce]
4119 */
4120 int
4121check_can_index(typval_T *rettv, int evaluate, int verbose)
4122{
4123 switch (rettv->v_type)
4124 {
4125 case VAR_FUNC:
4126 case VAR_PARTIAL:
4127 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004128 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 return FAIL;
4130 case VAR_FLOAT:
4131#ifdef FEAT_FLOAT
4132 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004133 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 return FAIL;
4135#endif
4136 case VAR_BOOL:
4137 case VAR_SPECIAL:
4138 case VAR_JOB:
4139 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004140 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004141 if (verbose)
4142 emsg(_(e_cannot_index_special_variable));
4143 return FAIL;
4144 case VAR_UNKNOWN:
4145 case VAR_ANY:
4146 case VAR_VOID:
4147 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004148 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 emsg(_(e_cannot_index_special_variable));
4150 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004151 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004152 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004153
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 case VAR_STRING:
4155 case VAR_LIST:
4156 case VAR_DICT:
4157 case VAR_BLOB:
4158 break;
4159 case VAR_NUMBER:
4160 if (in_vim9script())
4161 emsg(_(e_cannot_index_number));
4162 break;
4163 }
4164 return OK;
4165}
4166
4167/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004168 * slice() function
4169 */
4170 void
4171f_slice(typval_T *argvars, typval_T *rettv)
4172{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004173 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004174 && ((argvars[0].v_type != VAR_STRING
4175 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004176 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004177 && check_for_list_arg(argvars, 0) == FAIL)
4178 || check_for_number_arg(argvars, 1) == FAIL
4179 || check_for_opt_number_arg(argvars, 2) == FAIL))
4180 return;
4181
Bram Moolenaar6601b622021-01-13 21:47:15 +01004182 if (check_can_index(argvars, TRUE, FALSE) == OK)
4183 {
4184 copy_tv(argvars, rettv);
4185 eval_index_inner(rettv, TRUE, argvars + 1,
4186 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4187 TRUE, NULL, 0, FALSE);
4188 }
4189}
4190
4191/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004192 * Apply index or range to "rettv".
4193 * "var1" is the first index, NULL for [:expr].
4194 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004195 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4196 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004197 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4198 */
4199 int
4200eval_index_inner(
4201 typval_T *rettv,
4202 int is_range,
4203 typval_T *var1,
4204 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004205 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004206 char_u *key,
4207 int keylen,
4208 int verbose)
4209{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004210 varnumber_T n1, n2 = 0;
4211 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004212
4213 n1 = 0;
4214 if (var1 != NULL && rettv->v_type != VAR_DICT)
4215 n1 = tv_get_number(var1);
4216
4217 if (is_range)
4218 {
4219 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004220 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004221 if (verbose)
4222 emsg(_(e_cannot_slice_dictionary));
4223 return FAIL;
4224 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004225 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004226 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004227 else
4228 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004229 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004230
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004231 switch (rettv->v_type)
4232 {
4233 case VAR_UNKNOWN:
4234 case VAR_ANY:
4235 case VAR_VOID:
4236 case VAR_FUNC:
4237 case VAR_PARTIAL:
4238 case VAR_FLOAT:
4239 case VAR_BOOL:
4240 case VAR_SPECIAL:
4241 case VAR_JOB:
4242 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004243 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004244 break; // not evaluating, skipping over subscript
4245
4246 case VAR_NUMBER:
4247 case VAR_STRING:
4248 {
4249 char_u *s = tv_get_string(rettv);
4250
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004252 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004253 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004254 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004255 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004256 else
4257 s = char_from_string(s, n1);
4258 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004259 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004261 // The resulting variable is a substring. If the indexes
4262 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 if (n1 < 0)
4264 {
4265 n1 = len + n1;
4266 if (n1 < 0)
4267 n1 = 0;
4268 }
4269 if (n2 < 0)
4270 n2 = len + n2;
4271 else if (n2 >= len)
4272 n2 = len;
4273 if (n1 >= len || n2 < 0 || n1 > n2)
4274 s = NULL;
4275 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004276 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 }
4278 else
4279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004280 // The resulting variable is a string of a single
4281 // character. If the index is too big or negative the
4282 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283 if (n1 >= len || n1 < 0)
4284 s = NULL;
4285 else
4286 s = vim_strnsave(s + n1, 1);
4287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(rettv);
4289 rettv->v_type = VAR_STRING;
4290 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 }
4292 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004293
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004294 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004295 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4296 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004297 break;
4298
4299 case VAR_LIST:
4300 if (var1 == NULL)
4301 n1 = 0;
4302 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004303 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004304 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004305 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004306 return FAIL;
4307 break;
4308
4309 case VAR_DICT:
4310 {
4311 dictitem_T *item;
4312 typval_T tmp;
4313
4314 if (key == NULL)
4315 {
4316 key = tv_get_string_chk(var1);
4317 if (key == NULL)
4318 return FAIL;
4319 }
4320
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004321 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004322
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004324 {
4325 if (verbose)
4326 {
4327 if (keylen > 0)
4328 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004329 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004330 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004331 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004332 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004333
4334 copy_tv(&item->di_tv, &tmp);
4335 clear_tv(rettv);
4336 *rettv = tmp;
4337 }
4338 break;
4339 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004340 return OK;
4341}
4342
4343/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004344 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004345 */
4346 char_u *
4347partial_name(partial_T *pt)
4348{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004349 if (pt != NULL)
4350 {
4351 if (pt->pt_name != NULL)
4352 return pt->pt_name;
4353 if (pt->pt_func != NULL)
4354 return pt->pt_func->uf_name;
4355 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004356 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004357}
4358
Bram Moolenaarddecc252016-04-06 22:59:37 +02004359 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004360partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004361{
4362 int i;
4363
4364 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004365 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004366 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004367 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004368 if (pt->pt_name != NULL)
4369 {
4370 func_unref(pt->pt_name);
4371 vim_free(pt->pt_name);
4372 }
4373 else
4374 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004375
Bram Moolenaar54656012021-06-09 20:50:46 +02004376 // "out_up" is no longer used, decrement refcount on partial that owns it.
4377 partial_unref(pt->pt_outer.out_up_partial);
4378
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004379 // Decrease the reference count for the context of a closure. If down
4380 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004381 if (pt->pt_funcstack != NULL)
4382 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004383 --pt->pt_funcstack->fs_refcount;
4384 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004385 }
4386
Bram Moolenaarddecc252016-04-06 22:59:37 +02004387 vim_free(pt);
4388}
4389
4390/*
4391 * Unreference a closure: decrement the reference count and free it when it
4392 * becomes zero.
4393 */
4394 void
4395partial_unref(partial_T *pt)
4396{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004397 if (pt != NULL)
4398 {
4399 if (--pt->pt_refcount <= 0)
4400 partial_free(pt);
4401
4402 // If the reference count goes down to one, the funcstack may be the
4403 // only reference and can be freed if no other partials reference it.
4404 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4405 funcstack_check_refcount(pt->pt_funcstack);
4406 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004407}
4408
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004410 * Return the next (unique) copy ID.
4411 * Used for serializing nested structures.
4412 */
4413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004414get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004415{
4416 current_copyID += COPYID_INC;
4417 return current_copyID;
4418}
4419
4420/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004421 * Garbage collection for lists and dictionaries.
4422 *
4423 * We use reference counts to be able to free most items right away when they
4424 * are no longer used. But for composite items it's possible that it becomes
4425 * unused while the reference count is > 0: When there is a recursive
4426 * reference. Example:
4427 * :let l = [1, 2, 3]
4428 * :let d = {9: l}
4429 * :let l[1] = d
4430 *
4431 * Since this is quite unusual we handle this with garbage collection: every
4432 * once in a while find out which lists and dicts are not referenced from any
4433 * variable.
4434 *
4435 * Here is a good reference text about garbage collection (refers to Python
4436 * but it applies to all reference-counting mechanisms):
4437 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004438 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004439
4440/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004441 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004442 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004444 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004445 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004446garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004447{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004448 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004450 buf_T *buf;
4451 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004452 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004453 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004455 if (!testing)
4456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004458 want_garbage_collect = FALSE;
4459 may_garbage_collect = FALSE;
4460 garbage_collect_at_exit = FALSE;
4461 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004462
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004463 // The execution stack can grow big, limit the size.
4464 if (exestack.ga_maxlen - exestack.ga_len > 500)
4465 {
4466 size_t new_len;
4467 char_u *pp;
4468 int n;
4469
4470 // Keep 150% of the current size, with a minimum of the growth size.
4471 n = exestack.ga_len / 2;
4472 if (n < exestack.ga_growsize)
4473 n = exestack.ga_growsize;
4474
4475 // Don't make it bigger though.
4476 if (exestack.ga_len + n < exestack.ga_maxlen)
4477 {
4478 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4479 pp = vim_realloc(exestack.ga_data, new_len);
4480 if (pp == NULL)
4481 return FAIL;
4482 exestack.ga_maxlen = exestack.ga_len + n;
4483 exestack.ga_data = pp;
4484 }
4485 }
4486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 // We advance by two because we add one for items referenced through
4488 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004489 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004490
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004491 /*
4492 * 1. Go through all accessible variables and mark all lists and dicts
4493 * with copyID.
4494 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // Don't free variables in the previous_funccal list unless they are only
4497 // referenced through previous_funccal. This must be first, because if
4498 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004502 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004504 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004505 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004506 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4507 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004510 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004511 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4512 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004513 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004514 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4515 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004516#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004517 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004518 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4519 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004520 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004521 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004522 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4523 NULL, NULL);
4524#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004527 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004528 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4529 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004531 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004533 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004537 abort = abort || set_ref_in_functions(copyID);
4538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004540 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004541
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004542 // funcstacks keep variables for closures
4543 abort = abort || set_ref_in_funcstacks(copyID);
4544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004546 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004547
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004548 // callbacks in buffers
4549 abort = abort || set_ref_in_buffers(copyID);
4550
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004551 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4552 abort = abort || set_ref_in_insexpand_funcs(copyID);
4553
4554 // 'operatorfunc' callback
4555 abort = abort || set_ref_in_opfunc(copyID);
4556
4557 // 'tagfunc' callback
4558 abort = abort || set_ref_in_tagfunc(copyID);
4559
4560 // 'imactivatefunc' and 'imstatusfunc' callbacks
4561 abort = abort || set_ref_in_im_funcs(copyID);
4562
Bram Moolenaar1dced572012-04-05 16:54:08 +02004563#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004564 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004565#endif
4566
Bram Moolenaardb913952012-06-29 12:54:53 +02004567#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004568 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004569#endif
4570
4571#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004573#endif
4574
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004576 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004577 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004578#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004579#ifdef FEAT_NETBEANS_INTG
4580 abort = abort || set_ref_in_nb_channel(copyID);
4581#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004582
Bram Moolenaare3188e22016-05-31 21:13:04 +02004583#ifdef FEAT_TIMERS
4584 abort = abort || set_ref_in_timer(copyID);
4585#endif
4586
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004587#ifdef FEAT_QUICKFIX
4588 abort = abort || set_ref_in_quickfix(copyID);
4589#endif
4590
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004591#ifdef FEAT_TERMINAL
4592 abort = abort || set_ref_in_term(copyID);
4593#endif
4594
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004595#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004596 abort = abort || set_ref_in_popups(copyID);
4597#endif
4598
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004599 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004600 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 /*
4602 * 2. Free lists and dictionaries that are not referenced.
4603 */
4604 did_free = free_unref_items(copyID);
4605
4606 /*
4607 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004608 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004611 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 else if (p_verbose > 0)
4613 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004614 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004616
4617 return did_free;
4618}
4619
4620/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004622 */
4623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004624free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004625{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004626 int did_free = FALSE;
4627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // Let all "free" functions know that we are here. This means no
4629 // dictionaries, lists, channels or jobs are to be freed, because we will
4630 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631 in_free_unref_items = TRUE;
4632
4633 /*
4634 * PASS 1: free the contents of the items. We don't free the items
4635 * themselves yet, so that it is possible to decrement refcount counters
4636 */
4637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004639 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004642 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004643
4644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004645 // Go through the list of jobs and free items without the copyID. This
4646 // must happen before doing channels, because jobs refer to channels, but
4647 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004648 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004651 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4652#endif
4653
4654 /*
4655 * PASS 2: free the items themselves.
4656 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004657 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004658 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004659
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004660#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // Go through the list of jobs and free items without the copyID. This
4662 // must happen before doing channels, because jobs refer to channels, but
4663 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004664 free_unused_jobs(copyID, COPYID_MASK);
4665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004667 free_unused_channels(copyID, COPYID_MASK);
4668#endif
4669
4670 in_free_unref_items = FALSE;
4671
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004672 return did_free;
4673}
4674
4675/*
4676 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004677 * "list_stack" is used to add lists to be marked. Can be NULL.
4678 *
4679 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004682set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004683{
4684 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004686 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004687 hashtab_T *cur_ht;
4688 ht_stack_T *ht_stack = NULL;
4689 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004690
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004691 cur_ht = ht;
4692 for (;;)
4693 {
4694 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004696 // Mark each item in the hashtab. If the item contains a hashtab
4697 // it is added to ht_stack, if it contains a list it is added to
4698 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004699 todo = (int)cur_ht->ht_used;
4700 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4701 if (!HASHITEM_EMPTY(hi))
4702 {
4703 --todo;
4704 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4705 &ht_stack, list_stack);
4706 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004708
4709 if (ht_stack == NULL)
4710 break;
4711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004713 cur_ht = ht_stack->ht;
4714 tempitem = ht_stack;
4715 ht_stack = ht_stack->prev;
4716 free(tempitem);
4717 }
4718
4719 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004720}
4721
4722/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004723 * Mark a dict and its items with "copyID".
4724 * Returns TRUE if setting references failed somehow.
4725 */
4726 int
4727set_ref_in_dict(dict_T *d, int copyID)
4728{
4729 if (d != NULL && d->dv_copyID != copyID)
4730 {
4731 d->dv_copyID = copyID;
4732 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4733 }
4734 return FALSE;
4735}
4736
4737/*
4738 * Mark a list and its items with "copyID".
4739 * Returns TRUE if setting references failed somehow.
4740 */
4741 int
4742set_ref_in_list(list_T *ll, int copyID)
4743{
4744 if (ll != NULL && ll->lv_copyID != copyID)
4745 {
4746 ll->lv_copyID = copyID;
4747 return set_ref_in_list_items(ll, copyID, NULL);
4748 }
4749 return FALSE;
4750}
4751
4752/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004753 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4755 *
4756 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004757 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004758 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004759set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004760{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004761 listitem_T *li;
4762 int abort = FALSE;
4763 list_T *cur_l;
4764 list_stack_T *list_stack = NULL;
4765 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004766
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004767 cur_l = l;
4768 for (;;)
4769 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004770 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // Mark each item in the list. If the item contains a hashtab
4772 // it is added to ht_stack, if it contains a list it is added to
4773 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004774 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4775 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4776 ht_stack, &list_stack);
4777 if (list_stack == NULL)
4778 break;
4779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004781 cur_l = list_stack->list;
4782 tempitem = list_stack;
4783 list_stack = list_stack->prev;
4784 free(tempitem);
4785 }
4786
4787 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004788}
4789
4790/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004791 * Mark the partial in callback 'cb' with "copyID".
4792 */
4793 int
4794set_ref_in_callback(callback_T *cb, int copyID)
4795{
4796 typval_T tv;
4797
4798 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4799 return FALSE;
4800
4801 tv.v_type = VAR_PARTIAL;
4802 tv.vval.v_partial = cb->cb_partial;
4803 return set_ref_in_item(&tv, copyID, NULL, NULL);
4804}
4805
4806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004807 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004808 * "list_stack" is used to add lists to be marked. Can be NULL.
4809 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4810 *
4811 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004812 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814set_ref_in_item(
4815 typval_T *tv,
4816 int copyID,
4817 ht_stack_T **ht_stack,
4818 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004819{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004820 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004821
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004822 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004823 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004824 dict_T *dd = tv->vval.v_dict;
4825
Bram Moolenaara03f2332016-02-06 18:09:59 +01004826 if (dd != NULL && dd->dv_copyID != copyID)
4827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004829 dd->dv_copyID = copyID;
4830 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004831 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004832 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4833 }
4834 else
4835 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004836 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4837
Bram Moolenaara03f2332016-02-06 18:09:59 +01004838 if (newitem == NULL)
4839 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004840 else
4841 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004842 newitem->ht = &dd->dv_hashtab;
4843 newitem->prev = *ht_stack;
4844 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004845 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004846 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004847 }
4848 }
4849 else if (tv->v_type == VAR_LIST)
4850 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004851 list_T *ll = tv->vval.v_list;
4852
Bram Moolenaara03f2332016-02-06 18:09:59 +01004853 if (ll != NULL && ll->lv_copyID != copyID)
4854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004856 ll->lv_copyID = copyID;
4857 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004858 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004859 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004860 }
4861 else
4862 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004863 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4864
Bram Moolenaara03f2332016-02-06 18:09:59 +01004865 if (newitem == NULL)
4866 abort = TRUE;
4867 else
4868 {
4869 newitem->list = ll;
4870 newitem->prev = *list_stack;
4871 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004872 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004873 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004874 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004875 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004876 else if (tv->v_type == VAR_FUNC)
4877 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004878 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004879 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 else if (tv->v_type == VAR_PARTIAL)
4881 {
4882 partial_T *pt = tv->vval.v_partial;
4883 int i;
4884
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004885 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004887 // Didn't see this partial yet.
4888 pt->pt_copyID = copyID;
4889
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004890 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004891
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 if (pt->pt_dict != NULL)
4893 {
4894 typval_T dtv;
4895
4896 dtv.v_type = VAR_DICT;
4897 dtv.vval.v_dict = pt->pt_dict;
4898 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4899 }
4900
4901 for (i = 0; i < pt->pt_argc; ++i)
4902 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4903 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004904 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004905 }
4906 }
4907#ifdef FEAT_JOB_CHANNEL
4908 else if (tv->v_type == VAR_JOB)
4909 {
4910 job_T *job = tv->vval.v_job;
4911 typval_T dtv;
4912
4913 if (job != NULL && job->jv_copyID != copyID)
4914 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004915 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004916 if (job->jv_channel != NULL)
4917 {
4918 dtv.v_type = VAR_CHANNEL;
4919 dtv.vval.v_channel = job->jv_channel;
4920 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4921 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004922 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004923 {
4924 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004925 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004926 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4927 }
4928 }
4929 }
4930 else if (tv->v_type == VAR_CHANNEL)
4931 {
4932 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004933 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 typval_T dtv;
4935 jsonq_T *jq;
4936 cbq_T *cq;
4937
4938 if (ch != NULL && ch->ch_copyID != copyID)
4939 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004940 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004941 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004942 {
4943 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4944 jq = jq->jq_next)
4945 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4946 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4947 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004948 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004949 {
4950 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004951 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004952 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4953 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004954 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004955 {
4956 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004957 dtv.vval.v_partial =
4958 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004959 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4960 }
4961 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004962 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004963 {
4964 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004965 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004966 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4967 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004968 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004969 {
4970 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004971 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004972 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4973 }
4974 }
4975 }
4976#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004977 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004978}
4979
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 * Return a string with the string representation of a variable.
4982 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004983 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004984 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004985 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004986 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004987 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004988 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4989 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004990 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004992 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004993echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004994 typval_T *tv,
4995 char_u **tofree,
4996 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004997 int copyID,
4998 int echo_style,
4999 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005000 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005002 static int recurse = 0;
5003 char_u *r = NULL;
5004
Bram Moolenaar33570922005-01-25 22:26:29 +00005005 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005006 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005007 if (!did_echo_string_emsg)
5008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 // Only give this message once for a recursive call to avoid
5010 // flooding the user with errors. And stop iterating over lists
5011 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005012 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005013 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005015 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005016 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005017 }
5018 ++recurse;
5019
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 switch (tv->v_type)
5021 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005022 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005023 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005024 {
5025 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005026 r = tv->vval.v_string;
5027 if (r == NULL)
5028 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005029 }
5030 else
5031 {
5032 *tofree = string_quote(tv->vval.v_string, FALSE);
5033 r = *tofree;
5034 }
5035 break;
5036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 if (echo_style)
5039 {
5040 *tofree = NULL;
5041 r = tv->vval.v_string;
5042 }
5043 else
5044 {
5045 *tofree = string_quote(tv->vval.v_string, TRUE);
5046 r = *tofree;
5047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005048 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005050 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005051 {
5052 partial_T *pt = tv->vval.v_partial;
5053 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005054 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005055 garray_T ga;
5056 int i;
5057 char_u *tf;
5058
5059 ga_init2(&ga, 1, 100);
5060 ga_concat(&ga, (char_u *)"function(");
5061 if (fname != NULL)
5062 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005063 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005064 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005065 && vim_isupper(fname[1]))
5066 {
5067 ga_concat(&ga, (char_u *)"'g:");
5068 ga_concat(&ga, fname + 1);
5069 }
5070 else
5071 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005072 vim_free(fname);
5073 }
5074 if (pt != NULL && pt->pt_argc > 0)
5075 {
5076 ga_concat(&ga, (char_u *)", [");
5077 for (i = 0; i < pt->pt_argc; ++i)
5078 {
5079 if (i > 0)
5080 ga_concat(&ga, (char_u *)", ");
5081 ga_concat(&ga,
5082 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5083 vim_free(tf);
5084 }
5085 ga_concat(&ga, (char_u *)"]");
5086 }
5087 if (pt != NULL && pt->pt_dict != NULL)
5088 {
5089 typval_T dtv;
5090
5091 ga_concat(&ga, (char_u *)", ");
5092 dtv.v_type = VAR_DICT;
5093 dtv.vval.v_dict = pt->pt_dict;
5094 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5095 vim_free(tf);
5096 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005097 // terminate with ')' and a NUL
5098 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005099
5100 *tofree = ga.ga_data;
5101 r = *tofree;
5102 break;
5103 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005104
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005105 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005106 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005107 break;
5108
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005110 if (tv->vval.v_list == NULL)
5111 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005112 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005113 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005114 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005115 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005116 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5117 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005118 {
5119 *tofree = NULL;
5120 r = (char_u *)"[...]";
5121 }
5122 else
5123 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005124 int old_copyID = tv->vval.v_list->lv_copyID;
5125
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005126 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005127 *tofree = list2string(tv, copyID, restore_copyID);
5128 if (restore_copyID)
5129 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005130 r = *tofree;
5131 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005132 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005133
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005135 if (tv->vval.v_dict == NULL)
5136 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005137 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005138 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005139 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005140 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005141 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5142 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005143 {
5144 *tofree = NULL;
5145 r = (char_u *)"{...}";
5146 }
5147 else
5148 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005149 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005150
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005151 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005152 *tofree = dict2string(tv, copyID, restore_copyID);
5153 if (restore_copyID)
5154 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005155 r = *tofree;
5156 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005158
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005160 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005161 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005163 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005165 break;
5166
Bram Moolenaar835dc632016-02-07 14:27:38 +01005167 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005168 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005169#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005171 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5172 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005173 if (composite_val)
5174 {
5175 *tofree = string_quote(r, FALSE);
5176 r = *tofree;
5177 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005178#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005180
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005181 case VAR_INSTR:
5182 *tofree = NULL;
5183 r = (char_u *)"instructions";
5184 break;
5185
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005186 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005187#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *tofree = NULL;
5189 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5190 r = numbuf;
5191 break;
5192#endif
5193
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005194 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005195 case VAR_SPECIAL:
5196 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005197 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005198 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005200
Bram Moolenaar8502c702014-06-17 12:51:16 +02005201 if (--recurse == 0)
5202 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005203 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005204}
5205
5206/*
5207 * Return a string with the string representation of a variable.
5208 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5209 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005210 * Does not put quotes around strings, as ":echo" displays values.
5211 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5212 * May return NULL.
5213 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005215echo_string(
5216 typval_T *tv,
5217 char_u **tofree,
5218 char_u *numbuf,
5219 int copyID)
5220{
5221 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5222}
5223
5224/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005225 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5226 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005227 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005228 */
5229 int
5230buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5231{
5232 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005233 char_u *t;
5234 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005235
5236 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5237 return -1;
5238
5239 if (lnum > buf->b_ml.ml_line_count)
5240 lnum = buf->b_ml.ml_line_count;
5241
5242 str = ml_get_buf(buf, lnum, FALSE);
5243 if (str == NULL)
5244 return -1;
5245
5246 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005247 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005248
Bram Moolenaar91458462021-01-13 20:08:38 +01005249 // count the number of characters
5250 t = str;
5251 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5252 t += mb_ptr2len(t);
5253
5254 // In insert mode, when the cursor is at the end of a non-empty line,
5255 // byteidx points to the NUL character immediately past the end of the
5256 // string. In this case, add one to the character count.
5257 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5258 count++;
5259
5260 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005261}
5262
5263/*
5264 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005265 * byte index. Works only for loaded buffers. Returns -1 on failure.
5266 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005267 */
5268 int
5269buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5270{
5271 char_u *str;
5272 char_u *t;
5273
5274 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5275 return -1;
5276
5277 if (lnum > buf->b_ml.ml_line_count)
5278 lnum = buf->b_ml.ml_line_count;
5279
5280 str = ml_get_buf(buf, lnum, FALSE);
5281 if (str == NULL)
5282 return -1;
5283
5284 // Convert the character offset to a byte offset
5285 t = str;
5286 while (*t != NUL && --charidx > 0)
5287 t += mb_ptr2len(t);
5288
Bram Moolenaar91458462021-01-13 20:08:38 +01005289 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005290}
5291
5292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005294 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005297var2fpos(
5298 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005300 int *fnum, // set to fnum for '0, 'A, etc.
5301 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005303 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005305 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005308 if (varp->v_type == VAR_LIST)
5309 {
5310 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005311 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005312 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005313 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005314
5315 l = varp->vval.v_list;
5316 if (l == NULL)
5317 return NULL;
5318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005320 pos.lnum = list_find_nr(l, 0L, &error);
5321 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005323 if (charcol)
5324 len = (long)mb_charlen(ml_get(pos.lnum));
5325 else
5326 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005327
Bram Moolenaarec65d772020-08-20 22:29:12 +02005328 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005329 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005330 li = list_find(l, 1L);
5331 if (li != NULL && li->li_tv.v_type == VAR_STRING
5332 && li->li_tv.vval.v_string != NULL
5333 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005334 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005335 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005336 }
5337 else
5338 {
5339 pos.col = list_find_nr(l, 1L, &error);
5340 if (error)
5341 return NULL;
5342 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005345 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005347 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005349 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005350 pos.coladd = list_find_nr(l, 2L, &error);
5351 if (error)
5352 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005353
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005354 return &pos;
5355 }
5356
Bram Moolenaarc5809432021-03-27 21:23:30 +01005357 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5358 return NULL;
5359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005360 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005361 if (name == NULL)
5362 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005363 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005364 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005365 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005366 pos = curwin->w_cursor;
5367 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005368 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005369 return &pos;
5370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005372 {
5373 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005374 pos = VIsual;
5375 else
5376 pos = curwin->w_cursor;
5377 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005378 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005379 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005380 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005381 if (name[0] == '\'' && (!in_vim9script()
5382 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005384 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005385 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5387 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005388 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005389 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 return pp;
5391 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005392
Bram Moolenaara5525202006-03-02 22:52:09 +00005393 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005394
Bram Moolenaar477933c2007-07-17 14:32:23 +00005395 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005396 {
5397 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005399 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005400 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005401 // In silent Ex mode topline is zero, but that's not a valid line
5402 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005403 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005404 return &pos;
5405 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005406 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005407 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005408 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005410 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005411 return &pos;
5412 }
5413 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005416 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 pos.lnum = curbuf->b_ml.ml_line_count;
5419 pos.col = 0;
5420 }
5421 else
5422 {
5423 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005424 if (charcol)
5425 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5426 else
5427 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
5429 return &pos;
5430 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005431 if (in_vim9script())
5432 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 return NULL;
5434}
5435
5436/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005437 * Convert list in "arg" into a position and optional file number.
5438 * When "fnump" is NULL there is no file number, only 3 items.
5439 * Note that the column is passed on as-is, the caller may want to decrement
5440 * it to use 1 for the first column.
5441 * Return FAIL when conversion is not possible, doesn't check the position for
5442 * validity.
5443 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445list2fpos(
5446 typval_T *arg,
5447 pos_T *posp,
5448 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005449 colnr_T *curswantp,
5450 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451{
5452 list_T *l = arg->vval.v_list;
5453 long i = 0;
5454 long n;
5455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5457 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005458 if (arg->v_type != VAR_LIST
5459 || l == NULL
5460 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005461 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005462 return FAIL;
5463
5464 if (fnump != NULL)
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005467 if (n < 0)
5468 return FAIL;
5469 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005471 *fnump = n;
5472 }
5473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005475 if (n < 0)
5476 return FAIL;
5477 posp->lnum = n;
5478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005479 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005480 if (n < 0)
5481 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005482 // If character position is specified, then convert to byte position
5483 if (charcol)
5484 {
5485 buf_T *buf;
5486
5487 // Get the text for the specified line in a loaded buffer
5488 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5489 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5490 return FAIL;
5491
Bram Moolenaar91458462021-01-13 20:08:38 +01005492 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005493 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005494 posp->col = n;
5495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005496 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005497 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005498 posp->coladd = 0;
5499 else
5500 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005501
Bram Moolenaar493c1782014-05-28 14:34:46 +02005502 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005503 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005504
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005505 return OK;
5506}
5507
5508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * Get the length of an environment variable name.
5510 * Advance "arg" to the first character after the name.
5511 * Return 0 for error.
5512 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005513 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 char_u *p;
5517 int len;
5518
5519 for (p = *arg; vim_isIDc(*p); ++p)
5520 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005521 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 return 0;
5523
5524 len = (int)(p - *arg);
5525 *arg = p;
5526 return len;
5527}
5528
5529/*
5530 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005531 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * Return 0 if something is wrong.
5533 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005535get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 char_u *p;
5538 int len;
5539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005540 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005542 {
5543 if (*p == ':')
5544 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // "s:" is start of "s:var", but "n:" is not and can be used in
5546 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005547 len = (int)(p - *arg);
5548 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5549 || len > 1)
5550 break;
5551 }
5552 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005553 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return 0;
5555
5556 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005557 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 return len;
5560}
5561
5562/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005563 * Get the length of the name of a variable or function.
5564 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005566 * Return -1 if curly braces expansion failed.
5567 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 * If the name contains 'magic' {}'s, expand them and return the
5569 * expanded name in an allocated string via 'alias' - caller must free.
5570 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005572get_name_len(
5573 char_u **arg,
5574 char_u **alias,
5575 int evaluate,
5576 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 char_u *p;
5580 char_u *expr_start;
5581 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5586 && (*arg)[2] == (int)KE_SNR)
5587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 *arg += 3;
5590 return get_id_len(arg) + 3;
5591 }
5592 len = eval_fname_script(*arg);
5593 if (len > 0)
5594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 *arg += len;
5597 }
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005602 p = find_name_end(*arg, &expr_start, &expr_end,
5603 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (expr_start != NULL)
5605 {
5606 char_u *temp_string;
5607
5608 if (!evaluate)
5609 {
5610 len += (int)(p - *arg);
5611 *arg = skipwhite(p);
5612 return len;
5613 }
5614
5615 /*
5616 * Include any <SID> etc in the expanded string:
5617 * Thus the -len here.
5618 */
5619 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5620 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005621 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 *alias = temp_string;
5623 *arg = skipwhite(p);
5624 return (int)STRLEN(temp_string);
5625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005628 // Only give an error when there is something, otherwise it will be
5629 // reported at a higher level.
5630 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005631 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 return len;
5634}
5635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636/*
5637 * Find the end of a variable or function name, taking care of magic braces.
5638 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5639 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005640 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 * Return a pointer to just after the name. Equal to "arg" if there is no
5642 * valid name.
5643 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645find_name_end(
5646 char_u *arg,
5647 char_u **expr_start,
5648 char_u **expr_end,
5649 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 int mb_nest = 0;
5652 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005654 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005655 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 *expr_start = NULL;
5660 *expr_end = NULL;
5661 }
5662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005664 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5665 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005666 return arg;
5667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 for (p = arg; *p != NUL
5669 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005670 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005671 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005672 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005674 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005676 if (*p == '\'')
5677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005678 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005679 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005680 ;
5681 if (*p == NUL)
5682 break;
5683 }
5684 else if (*p == '"')
5685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005686 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005687 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005688 if (*p == '\\' && p[1] != NUL)
5689 ++p;
5690 if (*p == NUL)
5691 break;
5692 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005693 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005695 // "s:" is start of "s:var", but "n:" is not and can be used in
5696 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005697 len = (int)(p - arg);
5698 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005699 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005700 break;
5701 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 if (*p == '[')
5706 ++br_nest;
5707 else if (*p == ']')
5708 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005710
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005711 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 if (*p == '{')
5714 {
5715 mb_nest++;
5716 if (expr_start != NULL && *expr_start == NULL)
5717 *expr_start = p;
5718 }
5719 else if (*p == '}')
5720 {
5721 mb_nest--;
5722 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5723 *expr_end = p;
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 return p;
5729}
5730
5731/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005732 * Expands out the 'magic' {}'s in a variable/function name.
5733 * Note that this can call itself recursively, to deal with
5734 * constructs like foo{bar}{baz}{bam}
5735 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5736 * "in_start" ^
5737 * "expr_start" ^
5738 * "expr_end" ^
5739 * "in_end" ^
5740 *
5741 * Returns a new allocated string, which the caller must free.
5742 * Returns NULL for failure.
5743 */
5744 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745make_expanded_name(
5746 char_u *in_start,
5747 char_u *expr_start,
5748 char_u *expr_end,
5749 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005750{
5751 char_u c1;
5752 char_u *retval = NULL;
5753 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005754
5755 if (expr_end == NULL || in_end == NULL)
5756 return NULL;
5757 *expr_start = NUL;
5758 *expr_end = NUL;
5759 c1 = *in_end;
5760 *in_end = NUL;
5761
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005762 temp_result = eval_to_string(expr_start + 1, FALSE);
5763 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005764 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005765 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5766 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005767 if (retval != NULL)
5768 {
5769 STRCPY(retval, in_start);
5770 STRCAT(retval, temp_result);
5771 STRCAT(retval, expr_end + 1);
5772 }
5773 }
5774 vim_free(temp_result);
5775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005776 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005777 *expr_start = '{';
5778 *expr_end = '}';
5779
5780 if (retval != NULL)
5781 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005782 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005783 if (expr_start != NULL)
5784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005786 temp_result = make_expanded_name(retval, expr_start,
5787 expr_end, temp_result);
5788 vim_free(retval);
5789 retval = temp_result;
5790 }
5791 }
5792
5793 return retval;
5794}
5795
5796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005798 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005803 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005804}
5805
5806/*
5807 * Return TRUE if character "c" can be used as the first character in a
5808 * variable or function name (excluding '{' and '}').
5809 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005811eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005812{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005813 return ASCII_ISALPHA(c) || c == '_';
5814}
5815
5816/*
5817 * Return TRUE if character "c" can be used as the first character of a
5818 * dictionary key.
5819 */
5820 int
5821eval_isdictc(int c)
5822{
5823 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
5826/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005827 * Handle:
5828 * - expr[expr], expr[expr:expr] subscript
5829 * - ".name" lookup
5830 * - function call with Funcref variable: func(expr)
5831 * - method call: var->method()
5832 *
5833 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005834 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005836handle_subscript(
5837 char_u **arg,
5838 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005839 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005840 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005841{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005842 int evaluate = evalarg != NULL
5843 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005844 int ret = OK;
5845 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005846 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005847 int getnext;
5848 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005849
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005850 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005851 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005852 // When at the end of the line and ".name" or "->{" or "->X" follows in
5853 // the next line then consume the line break.
5854 p = eval_next_non_blank(*arg, evalarg, &getnext);
5855 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005856 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005857 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5858 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5859 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005860 {
5861 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005862 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005863 check_white = FALSE;
5864 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005865
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005866 if (rettv->v_type == VAR_ANY)
5867 {
5868 char_u *exp_name;
5869 int cc;
5870 int idx;
5871 ufunc_T *ufunc;
5872 type_T *type;
5873
5874 // Found script from "import * as {name}", script item name must
5875 // follow.
5876 if (**arg != '.')
5877 {
5878 if (verbose)
5879 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5880 ret = FAIL;
5881 break;
5882 }
5883 ++*arg;
5884 if (IS_WHITE_OR_NUL(**arg))
5885 {
5886 if (verbose)
5887 emsg(_(e_no_white_space_allowed_after_dot));
5888 ret = FAIL;
5889 break;
5890 }
5891
5892 // isolate the name
5893 exp_name = *arg;
5894 while (eval_isnamec(**arg))
5895 ++*arg;
5896 cc = **arg;
5897 **arg = NUL;
5898
5899 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5900 evalarg->eval_cctx, verbose);
5901 **arg = cc;
5902 *arg = skipwhite(*arg);
5903
5904 if (idx < 0 && ufunc == NULL)
5905 {
5906 ret = FAIL;
5907 break;
5908 }
5909 if (idx >= 0)
5910 {
5911 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5912 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5913
5914 copy_tv(sv->sv_tv, rettv);
5915 }
5916 else
5917 {
5918 rettv->v_type = VAR_FUNC;
5919 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5920 }
5921 }
5922
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005923 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5924 || rettv->v_type == VAR_PARTIAL))
5925 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005926 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005927 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5928 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005929
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005930 // Stop the expression evaluation when immediately aborting on
5931 // error, or when an interrupt occurred or an exception was thrown
5932 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005933 if (aborting())
5934 {
5935 if (ret == OK)
5936 clear_tv(rettv);
5937 ret = FAIL;
5938 }
5939 dict_unref(selfdict);
5940 selfdict = NULL;
5941 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005942 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005943 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005944 if (in_vim9script())
5945 *arg = skipwhite(p + 2);
5946 else
5947 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005948 if (ret == OK)
5949 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005950 if (VIM_ISWHITE(**arg))
5951 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005952 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005953 ret = FAIL;
5954 }
5955 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005956 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005957 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005958 else
5959 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005960 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005961 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005962 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005963 // "." is ".name" lookup when we found a dict or when evaluating and
5964 // scriptversion is at least 2, where string concatenation is "..".
5965 else if (**arg == '['
5966 || (**arg == '.' && (rettv->v_type == VAR_DICT
5967 || (!evaluate
5968 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02005969 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005970 {
5971 dict_unref(selfdict);
5972 if (rettv->v_type == VAR_DICT)
5973 {
5974 selfdict = rettv->vval.v_dict;
5975 if (selfdict != NULL)
5976 ++selfdict->dv_refcount;
5977 }
5978 else
5979 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005980 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005981 {
5982 clear_tv(rettv);
5983 ret = FAIL;
5984 }
5985 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005986 else
5987 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005988 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005990 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5991 // Don't do this when "Func" is already a partial that was bound
5992 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005993 if (selfdict != NULL
5994 && (rettv->v_type == VAR_FUNC
5995 || (rettv->v_type == VAR_PARTIAL
5996 && (rettv->vval.v_partial->pt_auto
5997 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005999
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006000 dict_unref(selfdict);
6001 return ret;
6002}
6003
6004/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 * Make a copy of an item.
6006 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006007 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6008 * reference to an already copied list/dict can be used.
6009 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006010 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012item_copy(
6013 typval_T *from,
6014 typval_T *to,
6015 int deep,
6016 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006017{
6018 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006019 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006023 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006024 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006025 }
6026 ++recurse;
6027
6028 switch (from->v_type)
6029 {
6030 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006031 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 case VAR_STRING:
6033 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006034 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006035 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006036 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006037 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006038 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006039 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006040 copy_tv(from, to);
6041 break;
6042 case VAR_LIST:
6043 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006044 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006045 if (from->vval.v_list == NULL)
6046 to->vval.v_list = NULL;
6047 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006049 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 to->vval.v_list = from->vval.v_list->lv_copylist;
6051 ++to->vval.v_list->lv_refcount;
6052 }
6053 else
6054 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6055 if (to->vval.v_list == NULL)
6056 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006058 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006060 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006061 case VAR_DICT:
6062 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006063 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006064 if (from->vval.v_dict == NULL)
6065 to->vval.v_dict = NULL;
6066 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006069 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6070 ++to->vval.v_dict->dv_refcount;
6071 }
6072 else
6073 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6074 if (to->vval.v_dict == NULL)
6075 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006076 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006077 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006078 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006080 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006081 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006082 }
6083 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006084 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006085}
6086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 void
6088echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6089{
6090 char_u *tofree;
6091 char_u numbuf[NUMBUFLEN];
6092 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6093
6094 if (*atstart)
6095 {
6096 *atstart = FALSE;
6097 // Call msg_start() after eval1(), evaluating the expression
6098 // may cause a message to appear.
6099 if (with_space)
6100 {
6101 // Mark the saved text as finishing the line, so that what
6102 // follows is displayed on a new line when scrolling back
6103 // at the more prompt.
6104 msg_sb_eol();
6105 msg_start();
6106 }
6107 }
6108 else if (with_space)
6109 msg_puts_attr(" ", echo_attr);
6110
6111 if (p != NULL)
6112 for ( ; *p != NUL && !got_int; ++p)
6113 {
6114 if (*p == '\n' || *p == '\r' || *p == TAB)
6115 {
6116 if (*p != TAB && *needclr)
6117 {
6118 // remove any text still there from the command
6119 msg_clr_eos();
6120 *needclr = FALSE;
6121 }
6122 msg_putchar_attr(*p, echo_attr);
6123 }
6124 else
6125 {
6126 if (has_mbyte)
6127 {
6128 int i = (*mb_ptr2len)(p);
6129
6130 (void)msg_outtrans_len_attr(p, i, echo_attr);
6131 p += i - 1;
6132 }
6133 else
6134 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6135 }
6136 }
6137 vim_free(tofree);
6138}
6139
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * ":echo expr1 ..." print each argument separated with a space, add a
6142 * newline at the end.
6143 * ":echon expr1 ..." print each argument plain.
6144 */
6145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006150 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 int needclr = TRUE;
6152 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006153 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006154 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006155 evalarg_T evalarg;
6156
Bram Moolenaare707c882020-07-01 13:07:37 +02006157 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 if (eap->skip)
6160 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006161 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006163 // If eval1() causes an error message the text from the command may
6164 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 need_clr_eos = needclr;
6166
Bram Moolenaar68db9962021-05-09 23:19:22 +02006167 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006168 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
6170 /*
6171 * Report the invalid expression unless the expression evaluation
6172 * has been cancelled due to an aborting error, an interrupt, or an
6173 * exception.
6174 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006175 if (!aborting() && did_emsg == did_emsg_before
6176 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006177 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006178 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 break;
6180 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006181 need_clr_eos = FALSE;
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006184 {
6185 if (rettv.v_type == VAR_VOID)
6186 {
6187 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6188 break;
6189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006191 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 arg = skipwhite(arg);
6195 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006196 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006197 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 if (eap->skip)
6200 --emsg_skip;
6201 else
6202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (needclr)
6205 msg_clr_eos();
6206 if (eap->cmdidx == CMD_echo)
6207 msg_end();
6208 }
6209}
6210
6211/*
6212 * ":echohl {name}".
6213 */
6214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006217 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218}
6219
6220/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006221 * Returns the :echo attribute
6222 */
6223 int
6224get_echo_attr(void)
6225{
6226 return echo_attr;
6227}
6228
6229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 * ":execute expr1 ..." execute the result of an expression.
6231 * ":echomsg expr1 ..." Print a message
6232 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006233 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 * Each gets spaces around each argument and a newline at the end for
6235 * echo commands
6236 */
6237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 int ret = OK;
6243 char_u *p;
6244 garray_T ga;
6245 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006246 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
6248 ga_init2(&ga, 1, 80);
6249
6250 if (eap->skip)
6251 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006252 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006254 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006255 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 if (!eap->skip)
6259 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006260 char_u buf[NUMBUFLEN];
6261
6262 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006263 {
6264 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6265 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006266 semsg(_(e_using_invalid_value_as_string_str),
6267 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006268 p = NULL;
6269 }
6270 else
6271 p = tv_get_string_buf(&rettv, buf);
6272 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006273 else
6274 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006275 if (p == NULL)
6276 {
6277 clear_tv(&rettv);
6278 ret = FAIL;
6279 break;
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 len = (int)STRLEN(p);
6282 if (ga_grow(&ga, len + 2) == FAIL)
6283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006284 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 ret = FAIL;
6286 break;
6287 }
6288 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 ga.ga_len += len;
6292 }
6293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 arg = skipwhite(arg);
6296 }
6297
6298 if (ret != FAIL && ga.ga_data != NULL)
6299 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006300 // use the first line of continuation lines for messages
6301 SOURCING_LNUM = start_lnum;
6302
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006303 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6304 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 // Mark the already saved text as finishing the line, so that what
6306 // follows is displayed on a new line when scrolling back at the
6307 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006308 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006309 }
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006312 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006313 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006314 out_flush();
6315 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006316 else if (eap->cmdidx == CMD_echoconsole)
6317 {
6318 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6319 ui_write((char_u *)"\r\n", 2, TRUE);
6320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 else if (eap->cmdidx == CMD_echoerr)
6322 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006323 int save_did_emsg = did_emsg;
6324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006325 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006326 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (!force_abort)
6328 did_emsg = save_did_emsg;
6329 }
6330 else if (eap->cmdidx == CMD_execute)
6331 do_cmdline((char_u *)ga.ga_data,
6332 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6333 }
6334
6335 ga_clear(&ga);
6336
6337 if (eap->skip)
6338 --emsg_skip;
6339
Bram Moolenaar63b91732021-08-05 20:40:03 +02006340 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
6343/*
6344 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6345 * "arg" points to the "&" or '+' when called, to "option" when returning.
6346 * Returns NULL when no option name found. Otherwise pointer to the char
6347 * after the option name.
6348 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006349 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006350find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 char_u *p = *arg;
6353
6354 ++p;
6355 if (*p == 'g' && p[1] == ':')
6356 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006357 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 p += 2;
6359 }
6360 else if (*p == 'l' && p[1] == ':')
6361 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006362 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 p += 2;
6364 }
6365 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006366 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 if (!ASCII_ISALPHA(*p))
6369 return NULL;
6370 *arg = p;
6371
6372 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 else
6375 while (ASCII_ISALPHA(*p))
6376 ++p;
6377 return p;
6378}
6379
6380/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006381 * Display script name where an item was last set.
6382 * Should only be invoked when 'verbose' is non-zero.
6383 */
6384 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006385last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006386{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006387 char_u *p;
6388
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006389 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006390 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006391 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006392 if (p != NULL)
6393 {
6394 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006395 msg_puts(_("\n\tLast set from "));
6396 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006397 if (script_ctx.sc_lnum > 0)
6398 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006399 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006400 msg_outnum((long)script_ctx.sc_lnum);
6401 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006402 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006403 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006404 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006405 }
6406}
6407
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006408#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410/*
6411 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006412 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 * "flags" can be "g" to do a global substitute.
6414 * Returns an allocated string, NULL for error.
6415 */
6416 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417do_string_sub(
6418 char_u *str,
6419 char_u *pat,
6420 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006421 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006422 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
6424 int sublen;
6425 regmatch_T regmatch;
6426 int i;
6427 int do_all;
6428 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006429 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 garray_T ga;
6431 char_u *ret;
6432 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006433 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006435 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006437 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
6439 ga_init2(&ga, 1, 200);
6440
6441 do_all = (flags[0] == 'g');
6442
6443 regmatch.rm_ic = p_ic;
6444 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6445 if (regmatch.regprog != NULL)
6446 {
6447 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006448 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006451 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006452 if (regmatch.startp[0] == regmatch.endp[0])
6453 {
6454 if (zero_width == regmatch.startp[0])
6455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006457 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006458 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6459 (size_t)i);
6460 ga.ga_len += i;
6461 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006462 continue;
6463 }
6464 zero_width = regmatch.startp[0];
6465 }
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 /*
6468 * Get some space for a temporary buffer to do the substitution
6469 * into. It will contain:
6470 * - The text up to where the match is.
6471 * - The substituted text.
6472 * - The text after the match.
6473 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006474 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006475 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6477 {
6478 ga_clear(&ga);
6479 break;
6480 }
6481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 i = (int)(regmatch.startp[0] - tail);
6484 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006485 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006486 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 + ga.ga_len + i, TRUE, TRUE, FALSE);
6488 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006489 tail = regmatch.endp[0];
6490 if (*tail == NUL)
6491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (!do_all)
6493 break;
6494 }
6495
6496 if (ga.ga_data != NULL)
6497 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6498
Bram Moolenaar473de612013-06-08 18:19:48 +02006499 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 }
6501
6502 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6503 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006504 if (p_cpo == empty_option)
6505 p_cpo = save_cpo;
6506 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006507 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006509 // If it's still empty it was changed and restored, need to restore in
6510 // the complicated way.
6511 if (*p_cpo == NUL)
6512 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006513 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 return ret;
6517}