blob: 2e370c1b2f7ef6045e1490a50553eec28dafc4da [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 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000892 garray_T tmp_type_list;
893 garray_T *type_list;
894 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200895
896 if (tp == p + 1 && !quiet)
897 {
898 semsg(_(e_white_space_required_after_str_str), ":", p);
899 return NULL;
900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000902 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
903 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
904 else
905 {
906 type_list = &tmp_type_list;
907 ga_init2(type_list, sizeof(type_T), 10);
908 }
909
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200910 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000911 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100912 if (lp->ll_type == NULL && !quiet)
913 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200914 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000915
916 // drop the type when not in a script
917 if (type_list == &tmp_type_list)
918 {
919 lp->ll_type = NULL;
920 clear_type_list(type_list);
921 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 }
924 }
925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100926 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
928 return p;
929
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200930 if (in_vim9script() && lval_root != NULL)
931 {
932 // using local variable
933 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200934 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200935 }
936 else
937 {
938 cc = *p;
939 *p = NUL;
940 // When we would write to the variable pass &ht and prevent autoload.
941 writing = !(flags & GLV_READ_ONLY);
942 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100943 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200944 if (v == NULL && !quiet)
945 semsg(_(e_undefined_variable_str), lp->ll_name);
946 *p = cc;
947 if (v == NULL)
948 return NULL;
949 lp->ll_tv = &v->di_tv;
950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100952 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
953 {
954 if (!quiet)
955 semsg(_(e_variable_already_declared), lp->ll_name);
956 return NULL;
957 }
958
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 /*
960 * Loop until no more [idx] or .key is following.
961 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100962 var1.v_type = VAR_UNKNOWN;
963 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200964 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200966 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
967 {
968 if (!quiet)
969 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
970 return NULL;
971 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200972 if (lp->ll_tv->v_type != VAR_LIST
973 && lp->ll_tv->v_type != VAR_DICT
974 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000977 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000979 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200980
981 // a NULL list/blob works like an empty list/blob, allocate one now.
982 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
983 rettv_list_alloc(lp->ll_tv);
984 else if (lp->ll_tv->v_type == VAR_BLOB
985 && lp->ll_tv->vval.v_blob == NULL)
986 rettv_blob_alloc(lp->ll_tv);
987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000991 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000993 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000994
Bram Moolenaar10c65862020-10-08 21:16:42 +0200995 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200996 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200997 && lp->ll_tv == &v->di_tv
998 && ht != NULL && ht == get_script_local_ht())
999 {
Bram Moolenaarc967d572021-07-08 21:38:50 +02001000 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001001
1002 // Vim9 script local variable: get the type
1003 if (sv != NULL)
1004 lp->ll_valtype = sv->sv_type;
1005 }
1006
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 len = -1;
1008 if (*p == '.')
1009 {
1010 key = p + 1;
1011 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1012 ;
1013 if (len == 0)
1014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001016 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 }
1019 p = key + len;
1020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001021 else
1022 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001024 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 if (*p == ':')
1026 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001027 else
1028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001030 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001032 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001035 clear_tv(&var1);
1036 return NULL;
1037 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001038 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 }
1040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001041 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 if (*p == ':')
1043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001047 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001048 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001050 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 if (rettv != NULL
1052 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001053 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001054 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001055 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001058 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001059 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001061 }
1062 p = skipwhite(p + 1);
1063 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 else
1066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001068 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001069 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001074 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001076 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001077 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001078 clear_tv(&var2);
1079 return NULL;
1080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001082 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001086
Bram Moolenaar8c711452005-01-14 21:53:12 +00001087 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001088 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001090 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001091 clear_tv(&var1);
1092 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
1095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 ++p;
1098 }
1099
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 {
1102 if (len == -1)
1103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 // "[key]": get key from "var1"
1105 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001106 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 }
1111 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001112 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001113
1114 // a NULL dict is equivalent with an empty dict
1115 if (lp->ll_tv->vval.v_dict == NULL)
1116 {
1117 lp->ll_tv->vval.v_dict = dict_alloc();
1118 if (lp->ll_tv->vval.v_dict == NULL)
1119 {
1120 clear_tv(&var1);
1121 return NULL;
1122 }
1123 ++lp->ll_tv->vval.v_dict->dv_refcount;
1124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001125 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001127 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001129 // When assigning to a scope dictionary check that a function and
1130 // variable name is valid (only variable name unless it is l: or
1131 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001132 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001133 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001134 int prevval;
1135 int wrong;
1136
1137 if (len != -1)
1138 {
1139 prevval = key[len];
1140 key[len] = NUL;
1141 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001142 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001143 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001144 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1145 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001146 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001147 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001148 if (len != -1)
1149 key[len] = prevval;
1150 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001151 {
1152 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001153 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001154 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001155 }
1156
Bram Moolenaar10c65862020-10-08 21:16:42 +02001157 if (lp->ll_valtype != NULL)
1158 // use the type of the member
1159 lp->ll_valtype = lp->ll_valtype->tt_member;
1160
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001163 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001164 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001165 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001166 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001167 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001168 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001169 return NULL;
1170 }
1171
Bram Moolenaar31b81602019-02-10 22:14:27 +01001172 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001176 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001177 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 }
1180 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001182 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001184 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 p = NULL;
1187 break;
1188 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001190 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001191 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1192 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001193 {
1194 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001195 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001196 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001197
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001198 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 else if (lp->ll_tv->v_type == VAR_BLOB)
1202 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001203 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1204
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 /*
1206 * Get the number and item for the only or first index of the List.
1207 */
1208 if (empty1)
1209 lp->ll_n1 = 0;
1210 else
1211 // is number or string
1212 lp->ll_n1 = (long)tv_get_number(&var1);
1213 clear_tv(&var1);
1214
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001215 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001217 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001218 return NULL;
1219 }
1220 if (lp->ll_range && !lp->ll_empty2)
1221 {
1222 lp->ll_n2 = (long)tv_get_number(&var2);
1223 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001224 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1225 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001226 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 }
1228 lp->ll_blob = lp->ll_tv->vval.v_blob;
1229 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001230 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 else
1233 {
1234 /*
1235 * Get the number and item for the only or first index of the List.
1236 */
1237 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001241 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
1243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001246 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001247 if (lp->ll_li == NULL)
1248 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001249 clear_tv(&var2);
1250 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 }
1252
Bram Moolenaar10c65862020-10-08 21:16:42 +02001253 if (lp->ll_valtype != NULL)
1254 // use the type of the member
1255 lp->ll_valtype = lp->ll_valtype->tt_member;
1256
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 /*
1258 * May need to find the item or absolute index for the second
1259 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 * When no index given: "lp->ll_empty2" is TRUE.
1261 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001264 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001265 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001267 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001268 if (check_range_index_two(lp->ll_list,
1269 &lp->ll_n1, lp->ll_li,
1270 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 }
1273
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 }
1277
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001278 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 return p;
1281}
1282
1283/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001288{
1289 vim_free(lp->ll_exp_name);
1290 vim_free(lp->ll_newkey);
1291}
1292
1293/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001294 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001296 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1297 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001300set_var_lval(
1301 lval_T *lp,
1302 char_u *endp,
1303 typval_T *rettv,
1304 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001305 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1306 char_u *op,
1307 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308{
1309 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311
1312 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001314 cc = *endp;
1315 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001316 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1317 return;
1318
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 if (lp->ll_blob != NULL)
1320 {
1321 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 if (op != NULL && *op != '=')
1324 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 return;
1327 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001328 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001329 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330
1331 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1332 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001333 if (lp->ll_empty2)
1334 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1335
Bram Moolenaar68452172021-04-12 21:21:02 +02001336 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1337 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339 }
1340 else
1341 {
1342 val = (int)tv_get_number_chk(rettv, &error);
1343 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001344 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001345 }
1346 }
1347 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001349 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001350
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001351 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1352 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001353 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001354 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001355 *endp = cc;
1356 return;
1357 }
1358
Bram Moolenaarff697e62019-02-12 22:28:33 +01001359 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001361 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001362 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 {
1364 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001365 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1366 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001367 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001368 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1369 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001370 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001374 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001375 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1376 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001377 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001378 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1379 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001380 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001381 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001382 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001383 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001384 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001385 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001386 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001387 else if (lp->ll_range)
1388 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001389 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1390 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001391 {
1392 emsg(_("E996: Cannot lock a range"));
1393 return;
1394 }
1395
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001396 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1397 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 }
1399 else
1400 {
1401 /*
1402 * Assign to a List or Dictionary item.
1403 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001404 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1405 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001406 {
1407 emsg(_("E996: Cannot lock a list or dict"));
1408 return;
1409 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001410
1411 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001412 && check_typval_arg_type(lp->ll_valtype, rettv,
1413 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001414 return;
1415
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 if (lp->ll_newkey != NULL)
1417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 if (op != NULL && *op != '=')
1419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001420 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001421 return;
1422 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001423 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1424 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001425 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001427 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001428 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 if (di == NULL)
1430 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001431 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1432 {
1433 vim_free(di);
1434 return;
1435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001437 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 else if (op != NULL && *op != '=')
1439 {
1440 tv_op(lp->ll_tv, rettv, op);
1441 return;
1442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 /*
1447 * Assign the value to the variable or list item.
1448 */
1449 if (copy)
1450 copy_tv(rettv, lp->ll_tv);
1451 else
1452 {
1453 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001454 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 }
1457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458}
1459
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001460/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1462 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 * Returns OK or FAIL.
1464 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001468 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469 char_u numbuf[NUMBUFLEN];
1470 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001471 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001473 // Can't do anything with a Funcref or Dict on the right.
1474 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001475 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001476 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1477 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 {
1479 switch (tv1->v_type)
1480 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001481 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001482 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 case VAR_DICT:
1485 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001486 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001487 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001488 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001489 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001490 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001491 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 break;
1493
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001494 case VAR_BLOB:
1495 if (*op != '+' || tv2->v_type != VAR_BLOB)
1496 break;
1497 // BLOB += BLOB
1498 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1499 {
1500 blob_T *b1 = tv1->vval.v_blob;
1501 blob_T *b2 = tv2->vval.v_blob;
1502 int i, len = blob_len(b2);
1503 for (i = 0; i < len; i++)
1504 ga_append(&b1->bv_ga, blob_get(b2, i));
1505 }
1506 return OK;
1507
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 case VAR_LIST:
1509 if (*op != '+' || tv2->v_type != VAR_LIST)
1510 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001511 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001512 if (tv2->vval.v_list != NULL)
1513 {
1514 if (tv1->vval.v_list == NULL)
1515 {
1516 tv1->vval.v_list = tv2->vval.v_list;
1517 ++tv1->vval.v_list->lv_refcount;
1518 }
1519 else
1520 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1521 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 return OK;
1523
1524 case VAR_NUMBER:
1525 case VAR_STRING:
1526 if (tv2->v_type == VAR_LIST)
1527 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001528 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001530 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001531 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001532#ifdef FEAT_FLOAT
1533 if (tv2->v_type == VAR_FLOAT)
1534 {
1535 float_T f = n;
1536
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 if (*op == '%')
1538 break;
1539 switch (*op)
1540 {
1541 case '+': f += tv2->vval.v_float; break;
1542 case '-': f -= tv2->vval.v_float; break;
1543 case '*': f *= tv2->vval.v_float; break;
1544 case '/': f /= tv2->vval.v_float; break;
1545 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001546 clear_tv(tv1);
1547 tv1->v_type = VAR_FLOAT;
1548 tv1->vval.v_float = f;
1549 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551#endif
1552 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001553 switch (*op)
1554 {
1555 case '+': n += tv_get_number(tv2); break;
1556 case '-': n -= tv_get_number(tv2); break;
1557 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001558 case '/': n = num_divide(n, tv_get_number(tv2),
1559 &failed); break;
1560 case '%': n = num_modulus(n, tv_get_number(tv2),
1561 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 clear_tv(tv1);
1564 tv1->v_type = VAR_NUMBER;
1565 tv1->vval.v_number = n;
1566 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 }
1568 else
1569 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570 if (tv2->v_type == VAR_FLOAT)
1571 break;
1572
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001574 s = tv_get_string(tv1);
1575 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 clear_tv(tv1);
1577 tv1->v_type = VAR_STRING;
1578 tv1->vval.v_string = s;
1579 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001580 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001583#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 {
1585 float_T f;
1586
Bram Moolenaarff697e62019-02-12 22:28:33 +01001587 if (*op == '%' || *op == '.'
1588 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 && tv2->v_type != VAR_NUMBER
1590 && tv2->v_type != VAR_STRING))
1591 break;
1592 if (tv2->v_type == VAR_FLOAT)
1593 f = tv2->vval.v_float;
1594 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001595 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 switch (*op)
1597 {
1598 case '+': tv1->vval.v_float += f; break;
1599 case '-': tv1->vval.v_float -= f; break;
1600 case '*': tv1->vval.v_float *= f; break;
1601 case '/': tv1->vval.v_float /= f; break;
1602 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001605 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 }
1607 }
1608
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001609 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 return FAIL;
1611}
1612
1613/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614 * Evaluate the expression used in a ":for var in expr" command.
1615 * "arg" points to "var".
1616 * Set "*errp" to TRUE for an error, FALSE otherwise;
1617 * Return a pointer that holds the info. Null when there is an error.
1618 */
1619 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620eval_for_line(
1621 char_u *arg,
1622 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001623 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001624 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625{
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001627 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 typval_T tv;
1630 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001631 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001635 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 if (fi == NULL)
1637 return NULL;
1638
Bram Moolenaar404557e2021-07-05 21:41:48 +02001639 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1640 &fi->fi_semicolon, FALSE);
1641 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 return fi;
1643
Bram Moolenaar404557e2021-07-05 21:41:48 +02001644 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 if (expr[0] != 'i' || expr[1] != 'n'
1646 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001648 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1649 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1650 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001651 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 return fi;
1653 }
1654
1655 if (skip)
1656 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001657 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1658 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 {
1660 *errp = FALSE;
1661 if (!skip)
1662 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001664 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001665 l = tv.vval.v_list;
1666 if (l == NULL)
1667 {
1668 // a null list is like an empty list: do nothing
1669 clear_tv(&tv);
1670 }
1671 else
1672 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001674 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 // No need to increment the refcount, it's already set for
1677 // the list being used in "tv".
1678 fi->fi_list = l;
1679 list_add_watch(l, &fi->fi_lw);
1680 fi->fi_lw.lw_item = l->lv_first;
1681 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001682 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001684 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001685 fi->fi_bi = 0;
1686 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001687 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001688 typval_T btv;
1689
1690 // Make a copy, so that the iteration still works when the
1691 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001693 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001695 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001696 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001697 else if (tv.v_type == VAR_STRING)
1698 {
1699 fi->fi_byte_idx = 0;
1700 fi->fi_string = tv.vval.v_string;
1701 tv.vval.v_string = NULL;
1702 if (fi->fi_string == NULL)
1703 fi->fi_string = vim_strsave((char_u *)"");
1704 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 else
1706 {
Sean Dewar80d73952021-08-04 19:25:54 +02001707 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 }
1710 }
1711 }
1712 if (skip)
1713 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001714 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715
1716 return fi;
1717}
1718
1719/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001720 * Used when looping over a :for line, skip the "in expr" part.
1721 */
1722 void
1723skip_for_lines(void *fi_void, evalarg_T *evalarg)
1724{
1725 forinfo_T *fi = (forinfo_T *)fi_void;
1726 int i;
1727
1728 for (i = 0; i < fi->fi_break_count; ++i)
1729 eval_next_line(evalarg);
1730}
1731
1732/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 * Use the first item in a ":for" list. Advance to the next.
1734 * Assign the values to the variable (list). "arg" points to the first one.
1735 * Return TRUE when a valid item was found, FALSE when at end of list or
1736 * something wrong.
1737 */
1738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001741 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001743 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001744 ? (ASSIGN_FINAL
1745 // first round: error if variable exists
1746 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1747 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001748 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001749 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001750 int skip_assign = in_vim9script() && arg[0] == '_'
1751 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 if (fi->fi_blob != NULL)
1754 {
1755 typval_T tv;
1756
1757 if (fi->fi_bi >= blob_len(fi->fi_blob))
1758 return FALSE;
1759 tv.v_type = VAR_NUMBER;
1760 tv.v_lock = VAR_FIXED;
1761 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1762 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001763 if (skip_assign)
1764 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001765 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001766 fi->fi_varcount, flag, NULL) == OK;
1767 }
1768
1769 if (fi->fi_string != NULL)
1770 {
1771 typval_T tv;
1772 int len;
1773
1774 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1775 if (len == 0)
1776 return FALSE;
1777 tv.v_type = VAR_STRING;
1778 tv.v_lock = VAR_FIXED;
1779 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1780 fi->fi_byte_idx += len;
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, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001786 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001787 vim_free(tv.vval.v_string);
1788 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001789 }
1790
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 item = fi->fi_lw.lw_item;
1792 if (item == NULL)
1793 result = FALSE;
1794 else
1795 {
1796 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001797 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001798 if (skip_assign)
1799 result = TRUE;
1800 else
1801 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001802 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 }
1804 return result;
1805}
1806
1807/*
1808 * Free the structure used to store info used by ":for".
1809 */
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812{
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001815 if (fi == NULL)
1816 return;
1817 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001818 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001820 list_unref(fi->fi_list);
1821 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001822 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001823 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001824 else
1825 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 vim_free(fi);
1827}
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830set_context_for_expression(
1831 expand_T *xp,
1832 char_u *arg,
1833 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001835 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001839 if (cmdidx == CMD_let || cmdidx == CMD_var
1840 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 {
1842 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001845 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
1848 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001849 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001850 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 break;
1852 }
1853 return;
1854 }
1855 }
1856 else
1857 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1858 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 while ((xp->xp_pattern = vim_strpbrk(arg,
1860 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1861 {
1862 c = *xp->xp_pattern;
1863 if (c == '&')
1864 {
1865 c = xp->xp_pattern[1];
1866 if (c == '&')
1867 {
1868 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001869 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001874 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1875 xp->xp_pattern += 2;
1876
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 else if (c == '$')
1880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001881 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 xp->xp_context = EXPAND_ENV_VARS;
1883 }
1884 else if (c == '=')
1885 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001886 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_EXPRESSION;
1888 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001889 else if (c == '#'
1890 && xp->xp_context == EXPAND_EXPRESSION)
1891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001892 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001893 break;
1894 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001895 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 && xp->xp_context == EXPAND_FUNCTIONS
1897 && vim_strchr(xp->xp_pattern, '(') == NULL)
1898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 break;
1901 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001902 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1907 if (c == '\\' && xp->xp_pattern[1] != NUL)
1908 ++xp->xp_pattern;
1909 xp->xp_context = EXPAND_NOTHING;
1910 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1915 /* skip */ ;
1916 xp->xp_context = EXPAND_NOTHING;
1917 }
1918 else if (c == '|')
1919 {
1920 if (xp->xp_pattern[1] == '|')
1921 {
1922 ++xp->xp_pattern;
1923 xp->xp_context = EXPAND_EXPRESSION;
1924 }
1925 else
1926 xp->xp_context = EXPAND_COMMANDS;
1927 }
1928 else
1929 xp->xp_context = EXPAND_EXPRESSION;
1930 }
1931 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001932 // Doesn't look like something valid, expand as an expression
1933 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 arg = xp->xp_pattern;
1936 if (*arg != NUL)
1937 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1938 /* skip */ ;
1939 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001940
1941 // ":exe one two" completes "two"
1942 if ((cmdidx == CMD_execute
1943 || cmdidx == CMD_echo
1944 || cmdidx == CMD_echon
1945 || cmdidx == CMD_echomsg)
1946 && xp->xp_context == EXPAND_EXPRESSION)
1947 {
1948 for (;;)
1949 {
1950 char_u *n = skiptowhite(arg);
1951
1952 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1953 break;
1954 arg = skipwhite(n);
1955 }
1956 }
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 xp->xp_pattern = arg;
1959}
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001962 * Return TRUE if "pat" matches "text".
1963 * Does not use 'cpo' and always uses 'magic'.
1964 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001965 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001966pattern_match(char_u *pat, char_u *text, int ic)
1967{
1968 int matches = FALSE;
1969 char_u *save_cpo;
1970 regmatch_T regmatch;
1971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001973 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001974 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001975 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1976 if (regmatch.regprog != NULL)
1977 {
1978 regmatch.rm_ic = ic;
1979 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1980 vim_regfree(regmatch.regprog);
1981 }
1982 p_cpo = save_cpo;
1983 return matches;
1984}
1985
1986/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001987 * Handle a name followed by "(". Both for just "name(arg)" and for
1988 * "expr->name(arg)".
1989 * Returns OK or FAIL.
1990 */
1991 static int
1992eval_func(
1993 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001994 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995 char_u *name,
1996 int name_len,
1997 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001998 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 typval_T *basetv) // "expr" for "expr->name(arg)"
2000{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002001 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 char_u *s = name;
2003 int len = name_len;
2004 partial_T *partial;
2005 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002006 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002007 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008
2009 if (!evaluate)
2010 check_vars(s, len);
2011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // If "s" is the name of a variable of type VAR_FUNC
2013 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002014 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002015 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // Need to make a copy, in case evaluating the arguments makes
2018 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002020 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 ret = FAIL;
2022 else
2023 {
2024 funcexe_T funcexe;
2025
2026 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002027 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002028 funcexe.fe_firstline = curwin->w_cursor.lnum;
2029 funcexe.fe_lastline = curwin->w_cursor.lnum;
2030 funcexe.fe_evaluate = evaluate;
2031 funcexe.fe_partial = partial;
2032 funcexe.fe_basetv = basetv;
2033 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002034 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002035 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036 }
2037 vim_free(s);
2038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // If evaluate is FALSE rettv->v_type was not set in
2040 // get_func_tv, but it's needed in handle_subscript() to parse
2041 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002042 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2043 {
2044 rettv->vval.v_string = NULL;
2045 rettv->v_type = VAR_FUNC;
2046 }
2047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002048 // Stop the expression evaluation when immediately
2049 // aborting on error, or when an interrupt occurred or
2050 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002051 if (evaluate && aborting())
2052 {
2053 if (ret == OK)
2054 clear_tv(rettv);
2055 ret = FAIL;
2056 }
2057 return ret;
2058}
2059
2060/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002061 * Get the next line source line without advancing. But do skip over comment
2062 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002063 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002064 */
2065 static char_u *
2066getline_peek_skip_comments(evalarg_T *evalarg)
2067{
2068 for (;;)
2069 {
2070 char_u *next = getline_peek(evalarg->eval_getline,
2071 evalarg->eval_cookie);
2072 char_u *p;
2073
2074 if (next == NULL)
2075 break;
2076 p = skipwhite(next);
2077 if (*p != NUL && !vim9_comment_start(p))
2078 return next;
2079 (void)eval_next_line(evalarg);
2080 }
2081 return NULL;
2082}
2083
2084/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002085 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2086 * comment) and there is a next line, return the next line (skipping blanks)
2087 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002088 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2089 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 * "arg" must point somewhere inside a line, not at the start.
2091 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002092 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002093eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2094{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002095 char_u *p = skipwhite(arg);
2096
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002097 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002098 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002100 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002101 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002103 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002104
2105 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002106 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002107 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002108 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109
Bram Moolenaar9d489562020-07-30 20:08:50 +02002110 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 {
2112 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 }
2115 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002116 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117}
2118
2119/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002120 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002121 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002122 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002123 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002124eval_next_line(evalarg_T *evalarg)
2125{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002126 garray_T *gap = &evalarg->eval_ga;
2127 char_u *line;
2128
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002130 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2131 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002132 else
2133 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002134 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2136 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002137 char_u *p = skipwhite(line);
2138
2139 // Going to concatenate the lines after parsing. For an empty or
2140 // comment line use an empty string.
2141 if (*p == NUL || vim9_comment_start(p))
2142 {
2143 vim_free(line);
2144 line = vim_strsave((char_u *)"");
2145 }
2146
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002147 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2148 ++gap->ga_len;
2149 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002150 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002151 {
2152 vim_free(evalarg->eval_tofree);
2153 evalarg->eval_tofree = line;
2154 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002155
2156 // Advanced to the next line, "arg" no longer points into the previous
2157 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002158 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002159 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002160}
2161
Bram Moolenaare6b53242020-07-01 17:28:33 +02002162/*
2163 * Call eval_next_non_blank() and get the next line if needed.
2164 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002165 char_u *
2166skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2167{
2168 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002169 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002170
Bram Moolenaar962d7212020-07-04 14:15:00 +02002171 if (evalarg == NULL)
2172 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002173 eval_next_non_blank(p, evalarg, &getnext);
2174 if (getnext)
2175 return eval_next_line(evalarg);
2176 return p;
2177}
2178
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002179/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002180 * Initialize "evalarg" for use.
2181 */
2182 void
2183init_evalarg(evalarg_T *evalarg)
2184{
2185 CLEAR_POINTER(evalarg);
2186 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2187}
2188
2189/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002190 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002191 */
2192 void
2193clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2194{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002195 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002196 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002197 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002198 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002199 if (eap != NULL)
2200 {
2201 // We may need to keep the original command line, e.g. for
2202 // ":let" it has the variable names. But we may also need the
2203 // new one, "nextcmd" points into it. Keep both.
2204 vim_free(eap->cmdline_tofree);
2205 eap->cmdline_tofree = *eap->cmdlinep;
2206 *eap->cmdlinep = evalarg->eval_tofree;
2207 }
2208 else
2209 vim_free(evalarg->eval_tofree);
2210 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002211 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002212
Bram Moolenaar844fb642021-10-23 13:32:30 +01002213 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002214 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002215 }
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2222 */
2223
2224/*
2225 * Handle zero level expression.
2226 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002228 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002229 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * Return OK or FAIL.
2231 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233eval0(
2234 char_u *arg,
2235 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002236 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002237 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238{
2239 int ret;
2240 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002241 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002242 int did_emsg_before = did_emsg;
2243 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002244 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002245 int check_for_end = TRUE;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002246 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002249 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002250 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002251 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002252
Bram Moolenaar02929a32021-12-17 14:46:12 +00002253 // In Vim9 script a command block is not split at NL characters for
2254 // commands using an expression argument. Skip over a '#' comment to check
2255 // for a following NL. Require white space before the '#'.
2256 if (in_vim9script() && p > expr_end)
2257 while (*p == '#')
2258 {
2259 char_u *nl = vim_strchr(p, NL);
2260
2261 if (nl == NULL)
2262 break;
2263 p = skipwhite(nl + 1);
2264 if (eap != NULL && *p != NUL)
2265 eap->nextcmd = p;
2266 check_for_end = FALSE;
2267 }
2268
2269 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002270 end_error = !ends_excmd2(arg, p);
2271 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /*
2276 * Report the invalid expression unless the expression evaluation has
2277 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002278 * exception, or we already gave a more specific error.
2279 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002281 if (!aborting()
2282 && did_emsg == did_emsg_before
2283 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002284 && (flags & EVAL_CONSTANT) == 0
2285 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002286 {
2287 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002288 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002289 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002290 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002291 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002292
2293 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002294 // a next command to avoid more errors, unless "|" is following, which
2295 // could only be a command separator.
2296 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2297 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002300
Bram Moolenaar02929a32021-12-17 14:46:12 +00002301 if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002302 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 return ret;
2305}
2306
2307/*
2308 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002309 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002310 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 *
2312 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002313 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002315 * Note: "rettv.v_lock" is not set.
2316 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * Return OK or FAIL.
2318 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002319 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002320eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002322 char_u *p;
2323 int getnext;
2324
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002325 CLEAR_POINTER(rettv);
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 /*
2328 * Get the first variable.
2329 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 return FAIL;
2332
Bram Moolenaar793648f2020-06-26 21:28:25 +02002333 p = eval_next_non_blank(*arg, evalarg, &getnext);
2334 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 int result;
2338 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339 evalarg_T *evalarg_used = evalarg;
2340 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002342 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002343 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002344
2345 if (evalarg == NULL)
2346 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002347 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002348 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002350 orig_flags = evalarg_used->eval_flags;
2351 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002353 if (getnext)
2354 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002355 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002356 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002357 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002358 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002359 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002360 clear_tv(rettv);
2361 return FAIL;
2362 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002363 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002364 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002367 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 int error = FALSE;
2370
Bram Moolenaar13106602020-10-04 16:06:05 +02002371 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002372 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002373 else if (vim9script)
2374 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002375 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002377 if (error || !op_falsy || !result)
2378 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 if (error)
2380 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382
2383 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002384 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 if (op_falsy)
2387 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002388 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002389 {
mityu4ac198c2021-05-28 17:52:40 +02002390 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002391 clear_tv(rettv);
2392 return FAIL;
2393 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002394 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 evalarg_used->eval_flags = (op_falsy ? !result : result)
2396 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2397 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002398 {
2399 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002401 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002402 if (!op_falsy || !result)
2403 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002405 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002407 /*
2408 * Check for the ":".
2409 */
2410 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2411 if (*p != ':')
2412 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002413 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 if (evaluate && result)
2415 clear_tv(rettv);
2416 evalarg_used->eval_flags = orig_flags;
2417 return FAIL;
2418 }
2419 if (getnext)
2420 *arg = eval_next_line(evalarg_used);
2421 else
2422 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002423 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002424 {
2425 error_white_both(p, 1);
2426 clear_tv(rettv);
2427 evalarg_used->eval_flags = orig_flags;
2428 return FAIL;
2429 }
2430 *arg = p;
2431 }
2432
2433 /*
2434 * Get the third variable. Recursive!
2435 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002436 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002437 {
mityu4ac198c2021-05-28 17:52:40 +02002438 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002439 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002440 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 return FAIL;
2442 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002443 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2444 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002446 if (eval1(arg, &var2, evalarg_used) == FAIL)
2447 {
2448 if (evaluate && result)
2449 clear_tv(rettv);
2450 evalarg_used->eval_flags = orig_flags;
2451 return FAIL;
2452 }
2453 if (evaluate && !result)
2454 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456
2457 if (evalarg == NULL)
2458 clear_evalarg(&local_evalarg, NULL);
2459 else
2460 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462
2463 return OK;
2464}
2465
2466/*
2467 * Handle first level expression:
2468 * expr2 || expr2 || expr2 logical OR
2469 *
2470 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002471 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 *
2473 * Return OK or FAIL.
2474 */
2475 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002478 char_u *p;
2479 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * Get the first variable.
2483 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return FAIL;
2486
2487 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002490 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 evalarg_T *evalarg_used = evalarg;
2494 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495 int evaluate;
2496 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 long result = FALSE;
2498 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002499 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002500 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002501
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 if (evalarg == NULL)
2503 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002504 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 orig_flags = evalarg_used->eval_flags;
2508 evaluate = orig_flags & EVAL_EVALUATE;
2509 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002511 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002512 result = tv_get_bool_chk(rettv, &error);
2513 else if (tv_get_number_chk(rettv, &error) != 0)
2514 result = TRUE;
2515 clear_tv(rettv);
2516 if (error)
2517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
2520 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 while (p[0] == '|' && p[1] == '|')
2524 {
2525 if (getnext)
2526 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002527 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002528 {
2529 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2530 {
2531 error_white_both(p, 2);
2532 clear_tv(rettv);
2533 return FAIL;
2534 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002535 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002536 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537
2538 /*
2539 * Get the second variable.
2540 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002541 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2542 {
mityu4ac198c2021-05-28 17:52:40 +02002543 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002544 clear_tv(rettv);
2545 return FAIL;
2546 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2548 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002550 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002551 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552
2553 /*
2554 * Compute the result.
2555 */
2556 if (evaluate && !result)
2557 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002558 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002559 result = tv_get_bool_chk(&var2, &error);
2560 else if (tv_get_number_chk(&var2, &error) != 0)
2561 result = TRUE;
2562 clear_tv(&var2);
2563 if (error)
2564 return FAIL;
2565 }
2566 if (evaluate)
2567 {
2568 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002569 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002570 rettv->v_type = VAR_BOOL;
2571 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002572 }
2573 else
2574 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002575 rettv->v_type = VAR_NUMBER;
2576 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002577 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579
2580 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002582
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 if (evalarg == NULL)
2584 clear_evalarg(&local_evalarg, NULL);
2585 else
2586 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588
2589 return OK;
2590}
2591
2592/*
2593 * Handle second level expression:
2594 * expr3 && expr3 && expr3 logical AND
2595 *
2596 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002597 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 *
2599 * Return OK or FAIL.
2600 */
2601 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002604 char_u *p;
2605 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 /*
2608 * Get the first variable.
2609 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return FAIL;
2612
2613 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002616 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002617 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002619 evalarg_T *evalarg_used = evalarg;
2620 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002621 int orig_flags;
2622 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 long result = TRUE;
2624 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002625 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002626 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002627
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002628 if (evalarg == NULL)
2629 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002630 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002632 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 orig_flags = evalarg_used->eval_flags;
2634 evaluate = orig_flags & EVAL_EVALUATE;
2635 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002637 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002638 result = tv_get_bool_chk(rettv, &error);
2639 else if (tv_get_number_chk(rettv, &error) == 0)
2640 result = FALSE;
2641 clear_tv(rettv);
2642 if (error)
2643 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645
2646 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002649 while (p[0] == '&' && p[1] == '&')
2650 {
2651 if (getnext)
2652 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002653 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002654 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002655 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002656 {
2657 error_white_both(p, 2);
2658 clear_tv(rettv);
2659 return FAIL;
2660 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002661 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002662 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663
2664 /*
2665 * Get the second variable.
2666 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002667 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2668 {
mityu4ac198c2021-05-28 17:52:40 +02002669 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002670 clear_tv(rettv);
2671 return FAIL;
2672 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2674 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002676 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679
2680 /*
2681 * Compute the result.
2682 */
2683 if (evaluate && result)
2684 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002686 result = tv_get_bool_chk(&var2, &error);
2687 else if (tv_get_number_chk(&var2, &error) == 0)
2688 result = FALSE;
2689 clear_tv(&var2);
2690 if (error)
2691 return FAIL;
2692 }
2693 if (evaluate)
2694 {
2695 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002696 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002697 rettv->v_type = VAR_BOOL;
2698 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002699 }
2700 else
2701 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002702 rettv->v_type = VAR_NUMBER;
2703 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002704 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706
2707 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002709
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710 if (evalarg == NULL)
2711 clear_evalarg(&local_evalarg, NULL);
2712 else
2713 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715
2716 return OK;
2717}
2718
2719/*
2720 * Handle third level expression:
2721 * var1 == var2
2722 * var1 =~ var2
2723 * var1 != var2
2724 * var1 !~ var2
2725 * var1 > var2
2726 * var1 >= var2
2727 * var1 < var2
2728 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002729 * var1 is var2
2730 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 *
2732 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002733 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 *
2735 * Return OK or FAIL.
2736 */
2737 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002738eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002741 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002742 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002744 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
2747 * Get the first variable.
2748 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return FAIL;
2751
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002752 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002753 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002758 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002760 typval_T var2;
2761 int ic;
2762 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002763 int evaluate = evalarg == NULL
2764 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002765
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002766 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002767 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002768 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002769 p = *arg;
2770 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002771 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2772 {
mityu4ac198c2021-05-28 17:52:40 +02002773 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002774 clear_tv(rettv);
2775 return FAIL;
2776 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002777
Bram Moolenaar696ba232020-07-29 21:20:41 +02002778 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2779 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002780 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002781 clear_tv(rettv);
2782 return FAIL;
2783 }
2784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002785 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (p[len] == '?')
2787 {
2788 ic = TRUE;
2789 ++len;
2790 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002791 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else if (p[len] == '#')
2793 {
2794 ic = FALSE;
2795 ++len;
2796 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002797 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002799 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
2801 /*
2802 * Get the second variable.
2803 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002804 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2805 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002806 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002807 clear_tv(rettv);
2808 return FAIL;
2809 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002810 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002811 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002813 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return FAIL;
2815 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002816 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002817 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002818 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002819
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002820 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002821 {
2822 ret = FAIL;
2823 clear_tv(rettv);
2824 }
2825 else
2826 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002827 clear_tv(&var2);
2828 return ret;
2829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831
2832 return OK;
2833}
2834
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002835/*
2836 * Make a copy of blob "tv1" and append blob "tv2".
2837 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 void
2839eval_addblob(typval_T *tv1, typval_T *tv2)
2840{
2841 blob_T *b1 = tv1->vval.v_blob;
2842 blob_T *b2 = tv2->vval.v_blob;
2843 blob_T *b = blob_alloc();
2844 int i;
2845
2846 if (b != NULL)
2847 {
2848 for (i = 0; i < blob_len(b1); i++)
2849 ga_append(&b->bv_ga, blob_get(b1, i));
2850 for (i = 0; i < blob_len(b2); i++)
2851 ga_append(&b->bv_ga, blob_get(b2, i));
2852
2853 clear_tv(tv1);
2854 rettv_blob_set(tv1, b);
2855 }
2856}
2857
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002858/*
2859 * Make a copy of list "tv1" and append list "tv2".
2860 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 int
2862eval_addlist(typval_T *tv1, typval_T *tv2)
2863{
2864 typval_T var3;
2865
2866 // concatenate Lists
2867 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2868 {
2869 clear_tv(tv1);
2870 clear_tv(tv2);
2871 return FAIL;
2872 }
2873 clear_tv(tv1);
2874 *tv1 = var3;
2875 return OK;
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * Handle fourth level expression:
2880 * + number addition
2881 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002882 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002883 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002886 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 *
2888 * Return OK or FAIL.
2889 */
2890 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 /*
2894 * Get the first variable.
2895 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return FAIL;
2898
2899 /*
2900 * Repeat computing, until no '+', '-' or '.' is following.
2901 */
2902 for (;;)
2903 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002904 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002905 int getnext;
2906 char_u *p;
2907 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002908 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909 int concat;
2910 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002911 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002912
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002913 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002914 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002915 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 p = eval_next_non_blank(*arg, evalarg, &getnext);
2917 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002918 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002919 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2920 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002922 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2923 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002924
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002925 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002926 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002928 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002929 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002930 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002932 {
mityu4ac198c2021-05-28 17:52:40 +02002933 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002934 clear_tv(rettv);
2935 return FAIL;
2936 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002937 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002938 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002939 if ((op != '+' || (rettv->v_type != VAR_LIST
2940 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002941#ifdef FEAT_FLOAT
2942 && (op == '.' || rettv->v_type != VAR_FLOAT)
2943#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002944 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002946 int error = FALSE;
2947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002948 // For "list + ...", an illegal use of the first operand as
2949 // a number cannot be determined before evaluating the 2nd
2950 // operand: if this is also a list, all is ok.
2951 // For "something . ...", "something - ..." or "non-list + ...",
2952 // we know that the first operand needs to be a string or number
2953 // without evaluating the 2nd operand. So check before to avoid
2954 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002955 if (op != '.')
2956 tv_get_number_chk(rettv, &error);
2957 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002958 {
2959 clear_tv(rettv);
2960 return FAIL;
2961 }
2962 }
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /*
2965 * Get the second variable.
2966 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002967 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002968 {
mityu89dcb4d2021-05-28 13:50:17 +02002969 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002970 clear_tv(rettv);
2971 return FAIL;
2972 }
2973 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002974 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 return FAIL;
2978 }
2979
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
2982 /*
2983 * Compute the result.
2984 */
2985 if (op == '.')
2986 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2988 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002989 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990
Bram Moolenaar2e086612020-08-25 22:37:48 +02002991 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002992 || var2.v_type == VAR_CHANNEL
2993 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002994 semsg(_(e_using_invalid_value_as_string_str),
2995 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002996#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002997 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002998 {
2999 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3000 var2.vval.v_float);
3001 s2 = buf2;
3002 }
3003#endif
3004 else
3005 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003006 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003007 {
3008 clear_tv(rettv);
3009 clear_tv(&var2);
3010 return FAIL;
3011 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003013 clear_tv(rettv);
3014 rettv->v_type = VAR_STRING;
3015 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003017 else if (op == '+' && rettv->v_type == VAR_BLOB
3018 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003020 else if (op == '+' && rettv->v_type == VAR_LIST
3021 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003022 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003024 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 else
3027 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028 int error = FALSE;
3029 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003030#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 float_T f1 = 0, f2 = 0;
3032
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035 f1 = rettv->vval.v_float;
3036 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038 else
3039#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003040 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003041 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 if (error)
3043 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003044 // This can only happen for "list + non-list" or
3045 // "blob + non-blob". For "non-list + ..." or
3046 // "something - ...", we returned before evaluating the
3047 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003049 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050 return FAIL;
3051 }
3052#ifdef FEAT_FLOAT
3053 if (var2.v_type == VAR_FLOAT)
3054 f1 = n1;
3055#endif
3056 }
3057#ifdef FEAT_FLOAT
3058 if (var2.v_type == VAR_FLOAT)
3059 {
3060 f2 = var2.vval.v_float;
3061 n2 = 0;
3062 }
3063 else
3064#endif
3065 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003066 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067 if (error)
3068 {
3069 clear_tv(rettv);
3070 clear_tv(&var2);
3071 return FAIL;
3072 }
3073#ifdef FEAT_FLOAT
3074 if (rettv->v_type == VAR_FLOAT)
3075 f2 = n2;
3076#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079
3080#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003081 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3083 {
3084 if (op == '+')
3085 f1 = f1 + f2;
3086 else
3087 f1 = f1 - f2;
3088 rettv->v_type = VAR_FLOAT;
3089 rettv->vval.v_float = f1;
3090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092#endif
3093 {
3094 if (op == '+')
3095 n1 = n1 + n2;
3096 else
3097 n1 = n1 - n2;
3098 rettv->v_type = VAR_NUMBER;
3099 rettv->vval.v_number = n1;
3100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104 }
3105 return OK;
3106}
3107
3108/*
3109 * Handle fifth level expression:
3110 * * number multiplication
3111 * / number division
3112 * % number modulo
3113 *
3114 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003115 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 *
3117 * Return OK or FAIL.
3118 */
3119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120eval6(
3121 char_u **arg,
3122 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003123 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003124 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003126#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003127 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 /*
3131 * Get the first variable.
3132 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003133 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 return FAIL;
3135
3136 /*
3137 * Repeat computing, until no '*', '/' or '%' is following.
3138 */
3139 for (;;)
3140 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003141 int evaluate;
3142 int getnext;
3143 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003144 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003145 int op;
3146 varnumber_T n1, n2;
3147#ifdef FEAT_FLOAT
3148 float_T f1, f2;
3149#endif
3150 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003151
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003152 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003153 p = eval_next_non_blank(*arg, evalarg, &getnext);
3154 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003155 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003157
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003158 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003159 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003160 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003161 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003162 {
3163 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3164 {
mityu4ac198c2021-05-28 17:52:40 +02003165 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003166 clear_tv(rettv);
3167 return FAIL;
3168 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003169 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003172#ifdef FEAT_FLOAT
3173 f1 = 0;
3174 f2 = 0;
3175#endif
3176 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003177 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#ifdef FEAT_FLOAT
3180 if (rettv->v_type == VAR_FLOAT)
3181 {
3182 f1 = rettv->vval.v_float;
3183 use_float = TRUE;
3184 n1 = 0;
3185 }
3186 else
3187#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003188 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003189 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003190 if (error)
3191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 else
3194 n1 = 0;
3195
3196 /*
3197 * Get the second variable.
3198 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003199 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3200 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003201 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003202 clear_tv(rettv);
3203 return FAIL;
3204 }
3205 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003206 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 return FAIL;
3208
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211#ifdef FEAT_FLOAT
3212 if (var2.v_type == VAR_FLOAT)
3213 {
3214 if (!use_float)
3215 {
3216 f1 = n1;
3217 use_float = TRUE;
3218 }
3219 f2 = var2.vval.v_float;
3220 n2 = 0;
3221 }
3222 else
3223#endif
3224 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003225 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226 clear_tv(&var2);
3227 if (error)
3228 return FAIL;
3229#ifdef FEAT_FLOAT
3230 if (use_float)
3231 f2 = n2;
3232#endif
3233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
3235 /*
3236 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003237 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239#ifdef FEAT_FLOAT
3240 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003242 if (op == '*')
3243 f1 = f1 * f2;
3244 else if (op == '/')
3245 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003246# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003248 if (f2 == 0.0)
3249 {
3250 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003252 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003253 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003254 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003255 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003256 }
3257 else
3258 f1 = f1 / f2;
3259# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 // We rely on the floating point library to handle divide
3261 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003262 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003263# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003266 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003267 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 return FAIL;
3269 }
3270 rettv->v_type = VAR_FLOAT;
3271 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003276 int failed = FALSE;
3277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003278 if (op == '*')
3279 n1 = n1 * n2;
3280 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003281 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003283 n1 = num_modulus(n1, n2, &failed);
3284 if (failed)
3285 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003287 rettv->v_type = VAR_NUMBER;
3288 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 }
3292
3293 return OK;
3294}
3295
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003296/*
3297 * Handle a type cast before a base level expression.
3298 * "arg" must point to the first non-white of the expression.
3299 * "arg" is advanced to just after the recognized expression.
3300 * Return OK or FAIL.
3301 */
3302 static int
3303eval7t(
3304 char_u **arg,
3305 typval_T *rettv,
3306 evalarg_T *evalarg,
3307 int want_string) // after "." operator
3308{
3309 type_T *want_type = NULL;
3310 garray_T type_list; // list of pointers to allocated types
3311 int res;
3312 int evaluate = evalarg == NULL ? 0
3313 : (evalarg->eval_flags & EVAL_EVALUATE);
3314
3315 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003316 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3317 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003318 {
3319 ++*arg;
3320 ga_init2(&type_list, sizeof(type_T *), 10);
3321 want_type = parse_type(arg, &type_list, TRUE);
3322 if (want_type == NULL && (evaluate || **arg != '>'))
3323 {
3324 clear_type_list(&type_list);
3325 return FAIL;
3326 }
3327
3328 if (**arg != '>')
3329 {
3330 if (*skipwhite(*arg) == '>')
3331 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3332 else
3333 emsg(_(e_missing_gt));
3334 clear_type_list(&type_list);
3335 return FAIL;
3336 }
3337 ++*arg;
3338 *arg = skipwhite_and_linebreak(*arg, evalarg);
3339 }
3340
3341 res = eval7(arg, rettv, evalarg, want_string);
3342
3343 if (want_type != NULL && evaluate)
3344 {
3345 if (res == OK)
3346 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003347 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3348 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003349
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003350 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003351 {
3352 if (want_type == &t_bool && actual != &t_bool
3353 && (actual->tt_flags & TTFLAG_BOOL_OK))
3354 {
3355 int n = tv2bool(rettv);
3356
3357 // can use "0" and "1" for boolean in some places
3358 clear_tv(rettv);
3359 rettv->v_type = VAR_BOOL;
3360 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3361 }
3362 else
3363 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003364 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003365
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003366 where.wt_variable = TRUE;
3367 res = check_type(want_type, actual, TRUE, where);
3368 }
3369 }
3370 }
3371 clear_type_list(&type_list);
3372 }
3373
3374 return res;
3375}
3376
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003377 int
3378eval_leader(char_u **arg, int vim9)
3379{
3380 char_u *s = *arg;
3381 char_u *p = *arg;
3382
3383 while (*p == '!' || *p == '-' || *p == '+')
3384 {
3385 char_u *n = skipwhite(p + 1);
3386
3387 // ++, --, -+ and +- are not accepted in Vim9 script
3388 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3389 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003390 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003391 return FAIL;
3392 }
3393 p = n;
3394 }
3395 *arg = p;
3396 return OK;
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * Handle sixth level expression:
3401 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003402 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003403 * "string" string constant
3404 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * &option-name option value
3406 * @r register contents
3407 * identifier variable value
3408 * function() function call
3409 * $VAR environment variable
3410 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003411 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003413 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003414 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 *
3416 * Also handle:
3417 * ! in front logical NOT
3418 * - in front unary minus
3419 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420 * trailing [] subscript in String or List
3421 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003422 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 *
3424 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003425 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 *
3427 * Return OK or FAIL.
3428 */
3429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003430eval7(
3431 char_u **arg,
3432 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003433 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436 int evaluate = evalarg != NULL
3437 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 int len;
3439 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 char_u *start_leader, *end_leader;
3441 int ret = OK;
3442 char_u *alias;
3443
3444 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003448 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003451 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003454 if (eval_leader(arg, in_vim9script()) == FAIL)
3455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 end_leader = *arg;
3457
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003458 if (**arg == '.' && (!isdigit(*(*arg + 1))
3459#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003460 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003461#endif
3462 ))
3463 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003464 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003465 ++*arg;
3466 return FAIL;
3467 }
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 switch (**arg)
3470 {
3471 /*
3472 * Number constant.
3473 */
3474 case '0':
3475 case '1':
3476 case '2':
3477 case '3':
3478 case '4':
3479 case '5':
3480 case '6':
3481 case '7':
3482 case '8':
3483 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003484 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003485
3486 // Apply prefixed "-" and "+" now. Matters especially when
3487 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003488 if (ret == OK && evaluate && end_leader > start_leader
3489 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003490 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 break;
3492
3493 /*
3494 * String constant: "string".
3495 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003496 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 break;
3498
3499 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003502 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 break;
3504
3505 /*
3506 * List: [expr, expr]
3507 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003508 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 break;
3510
3511 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003512 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003513 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003514 case '#': if (in_vim9script())
3515 {
3516 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3517 }
3518 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003519 {
3520 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003521 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003522 }
3523 else
3524 ret = NOTDONE;
3525 break;
3526
3527 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003528 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003529 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003531 case '{': if (in_vim9script())
3532 ret = NOTDONE;
3533 else
3534 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003535 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003536 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 break;
3538
3539 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003540 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003542 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 break;
3544
3545 /*
3546 * Environment variable: $VAR.
3547 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003548 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550
3551 /*
3552 * Register contents: @r.
3553 */
3554 case '@': ++*arg;
3555 if (evaluate)
3556 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003557 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3558 semsg(_(e_syntax_error_at_str), *arg);
3559 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3560 emsg_invreg(**arg);
3561 else
3562 {
3563 rettv->v_type = VAR_STRING;
3564 rettv->vval.v_string = get_reg_contents(**arg,
3565 GREG_EXPR_SRC);
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568 if (**arg != NUL)
3569 ++*arg;
3570 break;
3571
3572 /*
3573 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003574 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003576 case '(': ret = NOTDONE;
3577 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003578 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003579 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003580 if (ret == OK && evaluate)
3581 {
3582 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3583
Bram Moolenaara9931532021-06-12 15:58:16 +02003584 // Compile it here to get the return type. The return
3585 // type is optional, when it's missing use t_unknown.
3586 // This is recognized in compile_return().
3587 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3588 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003589 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003590 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003591 {
3592 clear_tv(rettv);
3593 ret = FAIL;
3594 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003595 }
3596 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003597 if (ret == NOTDONE)
3598 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003599 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003600 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003601
Bram Moolenaar9215f012020-06-27 21:18:00 +02003602 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003603 if (**arg == ')')
3604 ++*arg;
3605 else if (ret == OK)
3606 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003607 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003608 clear_tv(rettv);
3609 ret = FAIL;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 break;
3613
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 break;
3616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617
3618 if (ret == NOTDONE)
3619 {
3620 /*
3621 * Must be a variable or function name.
3622 * Can also be a curly-braces kind of name: {expr}.
3623 */
3624 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003625 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 if (alias != NULL)
3627 s = alias;
3628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003629 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 ret = FAIL;
3631 else
3632 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003633 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3634
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003635 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003636 {
3637 emsg(_(e_cannot_use_underscore_here));
3638 ret = FAIL;
3639 }
3640 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003641 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003642 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003643 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003644 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003645 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003646 else if (flags & EVAL_CONSTANT)
3647 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003648 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003649 {
3650 // get the value of "true", "false" or a variable
3651 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3652 {
3653 rettv->v_type = VAR_BOOL;
3654 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003655 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003656 }
3657 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003658 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003659 {
3660 rettv->v_type = VAR_BOOL;
3661 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003662 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003663 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003664 else if (len == 4 && in_vim9script()
3665 && STRNCMP(s, "null", 4) == 0)
3666 {
3667 rettv->v_type = VAR_SPECIAL;
3668 rettv->vval.v_number = VVAL_NULL;
3669 ret = OK;
3670 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003671 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003672 ret = eval_variable(s, len, rettv, NULL,
3673 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003675 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003676 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003677 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003678 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003679 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003682 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003683 }
3684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003685 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3686 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003687 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003688 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 /*
3691 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3692 */
3693 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003694 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003695 return ret;
3696}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003697
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003698/*
3699 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003700 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003701 * Adjusts "end_leaderp" until it is at "start_leader".
3702 */
3703 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003704eval7_leader(
3705 typval_T *rettv,
3706 int numeric_only,
3707 char_u *start_leader,
3708 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709{
3710 char_u *end_leader = *end_leaderp;
3711 int ret = OK;
3712 int error = FALSE;
3713 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003714 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003715#ifdef FEAT_FLOAT
3716 float_T f = 0.0;
3717
3718 if (rettv->v_type == VAR_FLOAT)
3719 f = rettv->vval.v_float;
3720 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003721#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003722 {
3723 while (VIM_ISWHITE(end_leader[-1]))
3724 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003725 if (in_vim9script() && end_leader[-1] == '!')
3726 val = tv2bool(rettv);
3727 else
3728 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003729 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730 if (error)
3731 {
3732 clear_tv(rettv);
3733 ret = FAIL;
3734 }
3735 else
3736 {
3737 while (end_leader > start_leader)
3738 {
3739 --end_leader;
3740 if (*end_leader == '!')
3741 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003742 if (numeric_only)
3743 {
3744 ++end_leader;
3745 break;
3746 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003747#ifdef FEAT_FLOAT
3748 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003749 {
3750 if (in_vim9script())
3751 {
3752 rettv->v_type = VAR_BOOL;
3753 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3754 }
3755 else
3756 f = !f;
3757 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 else
3759#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003760 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003762 type = VAR_BOOL;
3763 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003764 }
3765 else if (*end_leader == '-')
3766 {
3767#ifdef FEAT_FLOAT
3768 if (rettv->v_type == VAR_FLOAT)
3769 f = -f;
3770 else
3771#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003772 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003773 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003774 type = VAR_NUMBER;
3775 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003776 }
3777 }
3778#ifdef FEAT_FLOAT
3779 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003785#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003787 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003788 if (in_vim9script())
3789 rettv->v_type = type;
3790 else
3791 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003792 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return ret;
3797}
3798
3799/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003800 * Call the function referred to in "rettv".
3801 */
3802 static int
3803call_func_rettv(
3804 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003805 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003806 typval_T *rettv,
3807 int evaluate,
3808 dict_T *selfdict,
3809 typval_T *basetv)
3810{
3811 partial_T *pt = NULL;
3812 funcexe_T funcexe;
3813 typval_T functv;
3814 char_u *s;
3815 int ret;
3816
3817 // need to copy the funcref so that we can clear rettv
3818 if (evaluate)
3819 {
3820 functv = *rettv;
3821 rettv->v_type = VAR_UNKNOWN;
3822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003824 if (functv.v_type == VAR_PARTIAL)
3825 {
3826 pt = functv.vval.v_partial;
3827 s = partial_name(pt);
3828 }
3829 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003830 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003831 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003832 if (s == NULL || *s == NUL)
3833 {
3834 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003835 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003836 goto theend;
3837 }
3838 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839 }
3840 else
3841 s = (char_u *)"";
3842
Bram Moolenaara80faa82020-04-12 19:37:17 +02003843 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003844 funcexe.fe_firstline = curwin->w_cursor.lnum;
3845 funcexe.fe_lastline = curwin->w_cursor.lnum;
3846 funcexe.fe_evaluate = evaluate;
3847 funcexe.fe_partial = pt;
3848 funcexe.fe_selfdict = selfdict;
3849 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003850 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003852theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003853 // Clear the funcref afterwards, so that deleting it while
3854 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003855 if (evaluate)
3856 clear_tv(&functv);
3857
3858 return ret;
3859}
3860
3861/*
3862 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003863 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003864 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3865 */
3866 static int
3867eval_lambda(
3868 char_u **arg,
3869 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003870 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003872{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003873 int evaluate = evalarg != NULL
3874 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003875 typval_T base = *rettv;
3876 int ret;
3877
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003878 rettv->v_type = VAR_UNKNOWN;
3879
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003880 if (**arg == '{')
3881 {
3882 // ->{lambda}()
3883 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3884 }
3885 else
3886 {
3887 // ->(lambda)()
3888 ++*arg;
3889 ret = eval1(arg, rettv, evalarg);
3890 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003891 if (**arg == ')')
3892 {
3893 ++*arg;
3894 }
3895 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003896 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003897 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003898 ret = FAIL;
3899 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003900 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003901 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003902 return FAIL;
3903 else if (**arg != '(')
3904 {
3905 if (verbose)
3906 {
3907 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003908 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003909 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003910 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003911 }
3912 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003913 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003914 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003915 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003916 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003917
3918 // Clear the funcref afterwards, so that deleting it while
3919 // evaluating the arguments is possible (see test55).
3920 if (evaluate)
3921 clear_tv(&base);
3922
3923 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003924}
3925
3926/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003927 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003928 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3930 */
3931 static int
3932eval_method(
3933 char_u **arg,
3934 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003935 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003936 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937{
3938 char_u *name;
3939 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003940 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003942 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003943 int evaluate = evalarg != NULL
3944 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003945
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003946 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003947
Bram Moolenaarac92e252019-08-03 21:58:38 +02003948 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003949 len = get_name_len(arg, &alias, evaluate, TRUE);
3950 if (alias != NULL)
3951 name = alias;
3952
3953 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003954 {
3955 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003956 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003957 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003958 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003959 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003960 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003961 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003962 if (**arg != '(')
3963 {
3964 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00003965 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003966 ret = FAIL;
3967 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003968 else if (VIM_ISWHITE((*arg)[-1]))
3969 {
3970 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003971 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02003972 ret = FAIL;
3973 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003974 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003975 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003976 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003977 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003978
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003979 // Clear the funcref afterwards, so that deleting it while
3980 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003981 if (evaluate)
3982 clear_tv(&base);
3983
Bram Moolenaarac92e252019-08-03 21:58:38 +02003984 return ret;
3985}
3986
3987/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003988 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3989 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003990 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3991 */
3992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003993eval_index(
3994 char_u **arg,
3995 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003996 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003998{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003999 int evaluate = evalarg != NULL
4000 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004003 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004005 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004006 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004007
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4009 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004010
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004011 init_tv(&var1);
4012 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004014 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004015 /*
4016 * dict.name
4017 */
4018 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004019 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004021 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004023 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004024 }
4025 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 /*
4028 * something[idx]
4029 *
4030 * Get the (first) variable from inside the [].
4031 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004032 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 if (**arg == ':')
4034 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004035 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004037 else if (vim9 && **arg == ':')
4038 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004039 semsg(_(e_white_space_required_before_and_after_str_at_str),
4040 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004041 clear_tv(&var1);
4042 return FAIL;
4043 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004044 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004045 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004046 int error = FALSE;
4047
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004048#ifdef FEAT_FLOAT
4049 // allow for indexing with float
4050 if (vim9 && rettv->v_type == VAR_DICT
4051 && var1.v_type == VAR_FLOAT)
4052 {
4053 var1.vval.v_string = typval_tostring(&var1, TRUE);
4054 var1.v_type = VAR_STRING;
4055 }
4056#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004057 if (vim9 && rettv->v_type == VAR_LIST)
4058 tv_get_number_chk(&var1, &error);
4059 else
4060 error = tv_get_string_chk(&var1) == NULL;
4061 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004062 {
4063 // not a number or string
4064 clear_tv(&var1);
4065 return FAIL;
4066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004068
4069 /*
4070 * Get the second variable from inside the [:].
4071 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004072 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 if (**arg == ':')
4074 {
4075 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004076 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004077 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004078 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004079 semsg(_(e_white_space_required_before_and_after_str_at_str),
4080 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004081 if (!empty1)
4082 clear_tv(&var1);
4083 return FAIL;
4084 }
4085 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004086 if (**arg == ']')
4087 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004088 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (!empty1)
4091 clear_tv(&var1);
4092 return FAIL;
4093 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004094 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004096 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004097 if (!empty1)
4098 clear_tv(&var1);
4099 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004100 return FAIL;
4101 }
4102 }
4103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004104 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004105 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004106 if (**arg != ']')
4107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004108 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004109 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 clear_tv(&var1);
4111 if (range)
4112 clear_tv(&var2);
4113 return FAIL;
4114 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004115 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116 }
4117
4118 if (evaluate)
4119 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004120 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004121 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004123
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004124 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 clear_tv(&var2);
4128 return res;
4129 }
4130 return OK;
4131}
4132
4133/*
4134 * Check if "rettv" can have an [index] or [sli:ce]
4135 */
4136 int
4137check_can_index(typval_T *rettv, int evaluate, int verbose)
4138{
4139 switch (rettv->v_type)
4140 {
4141 case VAR_FUNC:
4142 case VAR_PARTIAL:
4143 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004144 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004145 return FAIL;
4146 case VAR_FLOAT:
4147#ifdef FEAT_FLOAT
4148 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004149 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 return FAIL;
4151#endif
4152 case VAR_BOOL:
4153 case VAR_SPECIAL:
4154 case VAR_JOB:
4155 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004156 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 if (verbose)
4158 emsg(_(e_cannot_index_special_variable));
4159 return FAIL;
4160 case VAR_UNKNOWN:
4161 case VAR_ANY:
4162 case VAR_VOID:
4163 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004164 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 emsg(_(e_cannot_index_special_variable));
4166 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 case VAR_STRING:
4171 case VAR_LIST:
4172 case VAR_DICT:
4173 case VAR_BLOB:
4174 break;
4175 case VAR_NUMBER:
4176 if (in_vim9script())
4177 emsg(_(e_cannot_index_number));
4178 break;
4179 }
4180 return OK;
4181}
4182
4183/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004184 * slice() function
4185 */
4186 void
4187f_slice(typval_T *argvars, typval_T *rettv)
4188{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004189 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004190 && ((argvars[0].v_type != VAR_STRING
4191 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004192 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004193 && check_for_list_arg(argvars, 0) == FAIL)
4194 || check_for_number_arg(argvars, 1) == FAIL
4195 || check_for_opt_number_arg(argvars, 2) == FAIL))
4196 return;
4197
Bram Moolenaar6601b622021-01-13 21:47:15 +01004198 if (check_can_index(argvars, TRUE, FALSE) == OK)
4199 {
4200 copy_tv(argvars, rettv);
4201 eval_index_inner(rettv, TRUE, argvars + 1,
4202 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4203 TRUE, NULL, 0, FALSE);
4204 }
4205}
4206
4207/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004208 * Apply index or range to "rettv".
4209 * "var1" is the first index, NULL for [:expr].
4210 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004211 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4212 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004213 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4214 */
4215 int
4216eval_index_inner(
4217 typval_T *rettv,
4218 int is_range,
4219 typval_T *var1,
4220 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004221 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004222 char_u *key,
4223 int keylen,
4224 int verbose)
4225{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004226 varnumber_T n1, n2 = 0;
4227 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004228
4229 n1 = 0;
4230 if (var1 != NULL && rettv->v_type != VAR_DICT)
4231 n1 = tv_get_number(var1);
4232
4233 if (is_range)
4234 {
4235 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004236 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 if (verbose)
4238 emsg(_(e_cannot_slice_dictionary));
4239 return FAIL;
4240 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004241 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004243 else
4244 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004246
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 switch (rettv->v_type)
4248 {
4249 case VAR_UNKNOWN:
4250 case VAR_ANY:
4251 case VAR_VOID:
4252 case VAR_FUNC:
4253 case VAR_PARTIAL:
4254 case VAR_FLOAT:
4255 case VAR_BOOL:
4256 case VAR_SPECIAL:
4257 case VAR_JOB:
4258 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004259 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 break; // not evaluating, skipping over subscript
4261
4262 case VAR_NUMBER:
4263 case VAR_STRING:
4264 {
4265 char_u *s = tv_get_string(rettv);
4266
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004268 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004269 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004270 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004271 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004272 else
4273 s = char_from_string(s, n1);
4274 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004277 // The resulting variable is a substring. If the indexes
4278 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279 if (n1 < 0)
4280 {
4281 n1 = len + n1;
4282 if (n1 < 0)
4283 n1 = 0;
4284 }
4285 if (n2 < 0)
4286 n2 = len + n2;
4287 else if (n2 >= len)
4288 n2 = len;
4289 if (n1 >= len || n2 < 0 || n1 > n2)
4290 s = NULL;
4291 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004292 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004293 }
4294 else
4295 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004296 // The resulting variable is a string of a single
4297 // character. If the index is too big or negative the
4298 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 if (n1 >= len || n1 < 0)
4300 s = NULL;
4301 else
4302 s = vim_strnsave(s + n1, 1);
4303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 clear_tv(rettv);
4305 rettv->v_type = VAR_STRING;
4306 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004307 }
4308 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004309
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004310 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004311 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4312 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004313 break;
4314
4315 case VAR_LIST:
4316 if (var1 == NULL)
4317 n1 = 0;
4318 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004319 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004320 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004321 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004322 return FAIL;
4323 break;
4324
4325 case VAR_DICT:
4326 {
4327 dictitem_T *item;
4328 typval_T tmp;
4329
4330 if (key == NULL)
4331 {
4332 key = tv_get_string_chk(var1);
4333 if (key == NULL)
4334 return FAIL;
4335 }
4336
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004337 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004338
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004339 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004340 {
4341 if (verbose)
4342 {
4343 if (keylen > 0)
4344 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004345 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004346 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004347 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004348 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004349
4350 copy_tv(&item->di_tv, &tmp);
4351 clear_tv(rettv);
4352 *rettv = tmp;
4353 }
4354 break;
4355 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004356 return OK;
4357}
4358
4359/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004360 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004361 */
4362 char_u *
4363partial_name(partial_T *pt)
4364{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004365 if (pt != NULL)
4366 {
4367 if (pt->pt_name != NULL)
4368 return pt->pt_name;
4369 if (pt->pt_func != NULL)
4370 return pt->pt_func->uf_name;
4371 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004372 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004373}
4374
Bram Moolenaarddecc252016-04-06 22:59:37 +02004375 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004376partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004377{
4378 int i;
4379
4380 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004381 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004382 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004383 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004384 if (pt->pt_name != NULL)
4385 {
4386 func_unref(pt->pt_name);
4387 vim_free(pt->pt_name);
4388 }
4389 else
4390 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004391
Bram Moolenaar54656012021-06-09 20:50:46 +02004392 // "out_up" is no longer used, decrement refcount on partial that owns it.
4393 partial_unref(pt->pt_outer.out_up_partial);
4394
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004395 // Decrease the reference count for the context of a closure. If down
4396 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004397 if (pt->pt_funcstack != NULL)
4398 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004399 --pt->pt_funcstack->fs_refcount;
4400 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004401 }
4402
Bram Moolenaarddecc252016-04-06 22:59:37 +02004403 vim_free(pt);
4404}
4405
4406/*
4407 * Unreference a closure: decrement the reference count and free it when it
4408 * becomes zero.
4409 */
4410 void
4411partial_unref(partial_T *pt)
4412{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004413 if (pt != NULL)
4414 {
4415 if (--pt->pt_refcount <= 0)
4416 partial_free(pt);
4417
4418 // If the reference count goes down to one, the funcstack may be the
4419 // only reference and can be freed if no other partials reference it.
4420 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4421 funcstack_check_refcount(pt->pt_funcstack);
4422 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004423}
4424
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004426 * Return the next (unique) copy ID.
4427 * Used for serializing nested structures.
4428 */
4429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004430get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004431{
4432 current_copyID += COPYID_INC;
4433 return current_copyID;
4434}
4435
4436/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004437 * Garbage collection for lists and dictionaries.
4438 *
4439 * We use reference counts to be able to free most items right away when they
4440 * are no longer used. But for composite items it's possible that it becomes
4441 * unused while the reference count is > 0: When there is a recursive
4442 * reference. Example:
4443 * :let l = [1, 2, 3]
4444 * :let d = {9: l}
4445 * :let l[1] = d
4446 *
4447 * Since this is quite unusual we handle this with garbage collection: every
4448 * once in a while find out which lists and dicts are not referenced from any
4449 * variable.
4450 *
4451 * Here is a good reference text about garbage collection (refers to Python
4452 * but it applies to all reference-counting mechanisms):
4453 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004454 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004455
4456/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004457 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004458 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004460 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004461 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004462garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004463{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004464 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004466 buf_T *buf;
4467 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004468 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004469 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004470
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004471 if (!testing)
4472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004474 want_garbage_collect = FALSE;
4475 may_garbage_collect = FALSE;
4476 garbage_collect_at_exit = FALSE;
4477 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004478
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004479 // The execution stack can grow big, limit the size.
4480 if (exestack.ga_maxlen - exestack.ga_len > 500)
4481 {
4482 size_t new_len;
4483 char_u *pp;
4484 int n;
4485
4486 // Keep 150% of the current size, with a minimum of the growth size.
4487 n = exestack.ga_len / 2;
4488 if (n < exestack.ga_growsize)
4489 n = exestack.ga_growsize;
4490
4491 // Don't make it bigger though.
4492 if (exestack.ga_len + n < exestack.ga_maxlen)
4493 {
4494 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4495 pp = vim_realloc(exestack.ga_data, new_len);
4496 if (pp == NULL)
4497 return FAIL;
4498 exestack.ga_maxlen = exestack.ga_len + n;
4499 exestack.ga_data = pp;
4500 }
4501 }
4502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004503 // We advance by two because we add one for items referenced through
4504 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004505 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004506
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507 /*
4508 * 1. Go through all accessible variables and mark all lists and dicts
4509 * with copyID.
4510 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // Don't free variables in the previous_funccal list unless they are only
4513 // referenced through previous_funccal. This must be first, because if
4514 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004518 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004520 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004521 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4523 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004526 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4528 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004529 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4531 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004532#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004533 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004534 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4535 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004536 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004537 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004538 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4539 NULL, NULL);
4540#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004543 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004544 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4545 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004547 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004549 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004553 abort = abort || set_ref_in_functions(copyID);
4554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004557
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004558 // funcstacks keep variables for closures
4559 abort = abort || set_ref_in_funcstacks(copyID);
4560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004562 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004563
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004564 // callbacks in buffers
4565 abort = abort || set_ref_in_buffers(copyID);
4566
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004567 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4568 abort = abort || set_ref_in_insexpand_funcs(copyID);
4569
4570 // 'operatorfunc' callback
4571 abort = abort || set_ref_in_opfunc(copyID);
4572
4573 // 'tagfunc' callback
4574 abort = abort || set_ref_in_tagfunc(copyID);
4575
4576 // 'imactivatefunc' and 'imstatusfunc' callbacks
4577 abort = abort || set_ref_in_im_funcs(copyID);
4578
Bram Moolenaar1dced572012-04-05 16:54:08 +02004579#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004580 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004581#endif
4582
Bram Moolenaardb913952012-06-29 12:54:53 +02004583#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004584 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004585#endif
4586
4587#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004589#endif
4590
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004592 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004593 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004594#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004595#ifdef FEAT_NETBEANS_INTG
4596 abort = abort || set_ref_in_nb_channel(copyID);
4597#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004598
Bram Moolenaare3188e22016-05-31 21:13:04 +02004599#ifdef FEAT_TIMERS
4600 abort = abort || set_ref_in_timer(copyID);
4601#endif
4602
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004603#ifdef FEAT_QUICKFIX
4604 abort = abort || set_ref_in_quickfix(copyID);
4605#endif
4606
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004607#ifdef FEAT_TERMINAL
4608 abort = abort || set_ref_in_term(copyID);
4609#endif
4610
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004611#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004612 abort = abort || set_ref_in_popups(copyID);
4613#endif
4614
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004616 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 /*
4618 * 2. Free lists and dictionaries that are not referenced.
4619 */
4620 did_free = free_unref_items(copyID);
4621
4622 /*
4623 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004624 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004627 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 else if (p_verbose > 0)
4629 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004630 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004631 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004632
4633 return did_free;
4634}
4635
4636/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004637 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004638 */
4639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004640free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004641{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004642 int did_free = FALSE;
4643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // Let all "free" functions know that we are here. This means no
4645 // dictionaries, lists, channels or jobs are to be freed, because we will
4646 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004647 in_free_unref_items = TRUE;
4648
4649 /*
4650 * PASS 1: free the contents of the items. We don't free the items
4651 * themselves yet, so that it is possible to decrement refcount counters
4652 */
4653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004655 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004658 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004659
4660#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 did_free |= free_unused_jobs_contents(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 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4668#endif
4669
4670 /*
4671 * PASS 2: free the items themselves.
4672 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004673 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004674 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004675
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004676#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004677 // Go through the list of jobs and free items without the copyID. This
4678 // must happen before doing channels, because jobs refer to channels, but
4679 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004680 free_unused_jobs(copyID, COPYID_MASK);
4681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004682 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004683 free_unused_channels(copyID, COPYID_MASK);
4684#endif
4685
4686 in_free_unref_items = FALSE;
4687
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688 return did_free;
4689}
4690
4691/*
4692 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004693 * "list_stack" is used to add lists to be marked. Can be NULL.
4694 *
4695 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004696 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004699{
4700 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004702 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004703 hashtab_T *cur_ht;
4704 ht_stack_T *ht_stack = NULL;
4705 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004706
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 cur_ht = ht;
4708 for (;;)
4709 {
4710 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // Mark each item in the hashtab. If the item contains a hashtab
4713 // it is added to ht_stack, if it contains a list it is added to
4714 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004715 todo = (int)cur_ht->ht_used;
4716 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4717 if (!HASHITEM_EMPTY(hi))
4718 {
4719 --todo;
4720 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4721 &ht_stack, list_stack);
4722 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004723 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004724
4725 if (ht_stack == NULL)
4726 break;
4727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004729 cur_ht = ht_stack->ht;
4730 tempitem = ht_stack;
4731 ht_stack = ht_stack->prev;
4732 free(tempitem);
4733 }
4734
4735 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004736}
4737
4738/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004739 * Mark a dict and its items with "copyID".
4740 * Returns TRUE if setting references failed somehow.
4741 */
4742 int
4743set_ref_in_dict(dict_T *d, int copyID)
4744{
4745 if (d != NULL && d->dv_copyID != copyID)
4746 {
4747 d->dv_copyID = copyID;
4748 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4749 }
4750 return FALSE;
4751}
4752
4753/*
4754 * Mark a list and its items with "copyID".
4755 * Returns TRUE if setting references failed somehow.
4756 */
4757 int
4758set_ref_in_list(list_T *ll, int copyID)
4759{
4760 if (ll != NULL && ll->lv_copyID != copyID)
4761 {
4762 ll->lv_copyID = copyID;
4763 return set_ref_in_list_items(ll, copyID, NULL);
4764 }
4765 return FALSE;
4766}
4767
4768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004769 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004770 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4771 *
4772 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004773 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004774 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004775set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004776{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004777 listitem_T *li;
4778 int abort = FALSE;
4779 list_T *cur_l;
4780 list_stack_T *list_stack = NULL;
4781 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004782
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004783 cur_l = l;
4784 for (;;)
4785 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004786 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 // Mark each item in the list. If the item contains a hashtab
4788 // it is added to ht_stack, if it contains a list it is added to
4789 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004790 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4791 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4792 ht_stack, &list_stack);
4793 if (list_stack == NULL)
4794 break;
4795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004797 cur_l = list_stack->list;
4798 tempitem = list_stack;
4799 list_stack = list_stack->prev;
4800 free(tempitem);
4801 }
4802
4803 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004804}
4805
4806/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004807 * Mark the partial in callback 'cb' with "copyID".
4808 */
4809 int
4810set_ref_in_callback(callback_T *cb, int copyID)
4811{
4812 typval_T tv;
4813
4814 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4815 return FALSE;
4816
4817 tv.v_type = VAR_PARTIAL;
4818 tv.vval.v_partial = cb->cb_partial;
4819 return set_ref_in_item(&tv, copyID, NULL, NULL);
4820}
4821
4822/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004823 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004824 * "list_stack" is used to add lists to be marked. Can be NULL.
4825 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4826 *
4827 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004828 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004830set_ref_in_item(
4831 typval_T *tv,
4832 int copyID,
4833 ht_stack_T **ht_stack,
4834 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004835{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004836 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004837
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004839 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004840 dict_T *dd = tv->vval.v_dict;
4841
Bram Moolenaara03f2332016-02-06 18:09:59 +01004842 if (dd != NULL && dd->dv_copyID != copyID)
4843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004844 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004845 dd->dv_copyID = copyID;
4846 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004847 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004848 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4849 }
4850 else
4851 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004852 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4853
Bram Moolenaara03f2332016-02-06 18:09:59 +01004854 if (newitem == NULL)
4855 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004856 else
4857 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004858 newitem->ht = &dd->dv_hashtab;
4859 newitem->prev = *ht_stack;
4860 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004861 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004862 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004863 }
4864 }
4865 else if (tv->v_type == VAR_LIST)
4866 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 list_T *ll = tv->vval.v_list;
4868
Bram Moolenaara03f2332016-02-06 18:09:59 +01004869 if (ll != NULL && ll->lv_copyID != copyID)
4870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004871 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004872 ll->lv_copyID = copyID;
4873 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004874 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004875 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004876 }
4877 else
4878 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004879 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4880
Bram Moolenaara03f2332016-02-06 18:09:59 +01004881 if (newitem == NULL)
4882 abort = TRUE;
4883 else
4884 {
4885 newitem->list = ll;
4886 newitem->prev = *list_stack;
4887 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004888 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004889 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004890 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004891 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004892 else if (tv->v_type == VAR_FUNC)
4893 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004894 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004895 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004896 else if (tv->v_type == VAR_PARTIAL)
4897 {
4898 partial_T *pt = tv->vval.v_partial;
4899 int i;
4900
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004901 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004902 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004903 // Didn't see this partial yet.
4904 pt->pt_copyID = copyID;
4905
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004906 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004907
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 if (pt->pt_dict != NULL)
4909 {
4910 typval_T dtv;
4911
4912 dtv.v_type = VAR_DICT;
4913 dtv.vval.v_dict = pt->pt_dict;
4914 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4915 }
4916
4917 for (i = 0; i < pt->pt_argc; ++i)
4918 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4919 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004920 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004921 }
4922 }
4923#ifdef FEAT_JOB_CHANNEL
4924 else if (tv->v_type == VAR_JOB)
4925 {
4926 job_T *job = tv->vval.v_job;
4927 typval_T dtv;
4928
4929 if (job != NULL && job->jv_copyID != copyID)
4930 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004931 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004932 if (job->jv_channel != NULL)
4933 {
4934 dtv.v_type = VAR_CHANNEL;
4935 dtv.vval.v_channel = job->jv_channel;
4936 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4937 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004938 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004939 {
4940 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004941 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004942 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4943 }
4944 }
4945 }
4946 else if (tv->v_type == VAR_CHANNEL)
4947 {
4948 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004949 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004950 typval_T dtv;
4951 jsonq_T *jq;
4952 cbq_T *cq;
4953
4954 if (ch != NULL && ch->ch_copyID != copyID)
4955 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004956 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004957 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004958 {
4959 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4960 jq = jq->jq_next)
4961 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4962 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4963 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004964 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004965 {
4966 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004967 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004968 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4969 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004970 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004971 {
4972 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004973 dtv.vval.v_partial =
4974 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004975 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4976 }
4977 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004978 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004979 {
4980 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004981 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004982 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4983 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004984 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004985 {
4986 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004987 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004988 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4989 }
4990 }
4991 }
4992#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004993 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004994}
4995
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004997 * Return a string with the string representation of a variable.
4998 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004999 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005000 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005001 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005002 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005003 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005004 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5005 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005006 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005008 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005009echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005010 typval_T *tv,
5011 char_u **tofree,
5012 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005013 int copyID,
5014 int echo_style,
5015 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005016 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005017{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005018 static int recurse = 0;
5019 char_u *r = NULL;
5020
Bram Moolenaar33570922005-01-25 22:26:29 +00005021 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005022 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005023 if (!did_echo_string_emsg)
5024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // Only give this message once for a recursive call to avoid
5026 // flooding the user with errors. And stop iterating over lists
5027 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005028 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005029 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005031 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005032 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005033 }
5034 ++recurse;
5035
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 switch (tv->v_type)
5037 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005039 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005040 {
5041 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005042 r = tv->vval.v_string;
5043 if (r == NULL)
5044 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005045 }
5046 else
5047 {
5048 *tofree = string_quote(tv->vval.v_string, FALSE);
5049 r = *tofree;
5050 }
5051 break;
5052
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005053 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005054 if (echo_style)
5055 {
5056 *tofree = NULL;
5057 r = tv->vval.v_string;
5058 }
5059 else
5060 {
5061 *tofree = string_quote(tv->vval.v_string, TRUE);
5062 r = *tofree;
5063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005064 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005065
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005066 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005067 {
5068 partial_T *pt = tv->vval.v_partial;
5069 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005070 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005071 garray_T ga;
5072 int i;
5073 char_u *tf;
5074
5075 ga_init2(&ga, 1, 100);
5076 ga_concat(&ga, (char_u *)"function(");
5077 if (fname != NULL)
5078 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005079 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005080 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005081 && vim_isupper(fname[1]))
5082 {
5083 ga_concat(&ga, (char_u *)"'g:");
5084 ga_concat(&ga, fname + 1);
5085 }
5086 else
5087 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005088 vim_free(fname);
5089 }
5090 if (pt != NULL && pt->pt_argc > 0)
5091 {
5092 ga_concat(&ga, (char_u *)", [");
5093 for (i = 0; i < pt->pt_argc; ++i)
5094 {
5095 if (i > 0)
5096 ga_concat(&ga, (char_u *)", ");
5097 ga_concat(&ga,
5098 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5099 vim_free(tf);
5100 }
5101 ga_concat(&ga, (char_u *)"]");
5102 }
5103 if (pt != NULL && pt->pt_dict != NULL)
5104 {
5105 typval_T dtv;
5106
5107 ga_concat(&ga, (char_u *)", ");
5108 dtv.v_type = VAR_DICT;
5109 dtv.vval.v_dict = pt->pt_dict;
5110 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5111 vim_free(tf);
5112 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005113 // terminate with ')' and a NUL
5114 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005115
5116 *tofree = ga.ga_data;
5117 r = *tofree;
5118 break;
5119 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005120
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005121 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005122 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005123 break;
5124
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005126 if (tv->vval.v_list == NULL)
5127 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005128 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005129 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005130 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005131 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005132 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5133 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005134 {
5135 *tofree = NULL;
5136 r = (char_u *)"[...]";
5137 }
5138 else
5139 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005140 int old_copyID = tv->vval.v_list->lv_copyID;
5141
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005142 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005143 *tofree = list2string(tv, copyID, restore_copyID);
5144 if (restore_copyID)
5145 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005146 r = *tofree;
5147 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005148 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005149
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005151 if (tv->vval.v_dict == NULL)
5152 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005153 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005154 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005155 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005156 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005157 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5158 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005159 {
5160 *tofree = NULL;
5161 r = (char_u *)"{...}";
5162 }
5163 else
5164 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005165 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005166
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005167 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005168 *tofree = dict2string(tv, copyID, restore_copyID);
5169 if (restore_copyID)
5170 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005171 r = *tofree;
5172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005176 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005177 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005179 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005180 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005181 break;
5182
Bram Moolenaar835dc632016-02-07 14:27:38 +01005183 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005184 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005185#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005186 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005187 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5188 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005189 if (composite_val)
5190 {
5191 *tofree = string_quote(r, FALSE);
5192 r = *tofree;
5193 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005194#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005196
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005197 case VAR_INSTR:
5198 *tofree = NULL;
5199 r = (char_u *)"instructions";
5200 break;
5201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005203#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 *tofree = NULL;
5205 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5206 r = numbuf;
5207 break;
5208#endif
5209
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005210 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005211 case VAR_SPECIAL:
5212 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005213 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005214 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005216
Bram Moolenaar8502c702014-06-17 12:51:16 +02005217 if (--recurse == 0)
5218 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005219 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005220}
5221
5222/*
5223 * Return a string with the string representation of a variable.
5224 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5225 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005226 * Does not put quotes around strings, as ":echo" displays values.
5227 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5228 * May return NULL.
5229 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005231echo_string(
5232 typval_T *tv,
5233 char_u **tofree,
5234 char_u *numbuf,
5235 int copyID)
5236{
5237 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5238}
5239
5240/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005241 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5242 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005243 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005244 */
5245 int
5246buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5247{
5248 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005249 char_u *t;
5250 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251
5252 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5253 return -1;
5254
5255 if (lnum > buf->b_ml.ml_line_count)
5256 lnum = buf->b_ml.ml_line_count;
5257
5258 str = ml_get_buf(buf, lnum, FALSE);
5259 if (str == NULL)
5260 return -1;
5261
5262 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005263 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005264
Bram Moolenaar91458462021-01-13 20:08:38 +01005265 // count the number of characters
5266 t = str;
5267 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5268 t += mb_ptr2len(t);
5269
5270 // In insert mode, when the cursor is at the end of a non-empty line,
5271 // byteidx points to the NUL character immediately past the end of the
5272 // string. In this case, add one to the character count.
5273 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5274 count++;
5275
5276 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277}
5278
5279/*
5280 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005281 * byte index. Works only for loaded buffers. Returns -1 on failure.
5282 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005283 */
5284 int
5285buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5286{
5287 char_u *str;
5288 char_u *t;
5289
5290 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5291 return -1;
5292
5293 if (lnum > buf->b_ml.ml_line_count)
5294 lnum = buf->b_ml.ml_line_count;
5295
5296 str = ml_get_buf(buf, lnum, FALSE);
5297 if (str == NULL)
5298 return -1;
5299
5300 // Convert the character offset to a byte offset
5301 t = str;
5302 while (*t != NUL && --charidx > 0)
5303 t += mb_ptr2len(t);
5304
Bram Moolenaar91458462021-01-13 20:08:38 +01005305 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005306}
5307
5308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005310 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313var2fpos(
5314 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005316 int *fnum, // set to fnum for '0, 'A, etc.
5317 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005319 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005321 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005324 if (varp->v_type == VAR_LIST)
5325 {
5326 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005327 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005328 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005329 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005330
5331 l = varp->vval.v_list;
5332 if (l == NULL)
5333 return NULL;
5334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005336 pos.lnum = list_find_nr(l, 0L, &error);
5337 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005339 if (charcol)
5340 len = (long)mb_charlen(ml_get(pos.lnum));
5341 else
5342 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005343
Bram Moolenaarec65d772020-08-20 22:29:12 +02005344 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005346 li = list_find(l, 1L);
5347 if (li != NULL && li->li_tv.v_type == VAR_STRING
5348 && li->li_tv.vval.v_string != NULL
5349 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005350 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005351 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005352 }
5353 else
5354 {
5355 pos.col = list_find_nr(l, 1L, &error);
5356 if (error)
5357 return NULL;
5358 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005361 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005363 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005366 pos.coladd = list_find_nr(l, 2L, &error);
5367 if (error)
5368 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005369
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005370 return &pos;
5371 }
5372
Bram Moolenaarc5809432021-03-27 21:23:30 +01005373 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5374 return NULL;
5375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005376 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 if (name == NULL)
5378 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005379 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005380 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005381 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005382 pos = curwin->w_cursor;
5383 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005384 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005385 return &pos;
5386 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005388 {
5389 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005390 pos = VIsual;
5391 else
5392 pos = curwin->w_cursor;
5393 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005394 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005395 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005396 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005397 if (name[0] == '\'' && (!in_vim9script()
5398 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005400 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005401 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5403 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005404 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005405 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return pp;
5407 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005408
Bram Moolenaara5525202006-03-02 22:52:09 +00005409 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005410
Bram Moolenaar477933c2007-07-17 14:32:23 +00005411 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005412 {
5413 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005415 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005416 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005417 // In silent Ex mode topline is zero, but that's not a valid line
5418 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005419 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005420 return &pos;
5421 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005423 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005424 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005426 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005427 return &pos;
5428 }
5429 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005430 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005432 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
5434 pos.lnum = curbuf->b_ml.ml_line_count;
5435 pos.col = 0;
5436 }
5437 else
5438 {
5439 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005440 if (charcol)
5441 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5442 else
5443 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 return &pos;
5446 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005447 if (in_vim9script())
5448 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 return NULL;
5450}
5451
5452/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005453 * Convert list in "arg" into a position and optional file number.
5454 * When "fnump" is NULL there is no file number, only 3 items.
5455 * Note that the column is passed on as-is, the caller may want to decrement
5456 * it to use 1 for the first column.
5457 * Return FAIL when conversion is not possible, doesn't check the position for
5458 * validity.
5459 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005461list2fpos(
5462 typval_T *arg,
5463 pos_T *posp,
5464 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005465 colnr_T *curswantp,
5466 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005467{
5468 list_T *l = arg->vval.v_list;
5469 long i = 0;
5470 long n;
5471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5473 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005474 if (arg->v_type != VAR_LIST
5475 || l == NULL
5476 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005477 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005478 return FAIL;
5479
5480 if (fnump != NULL)
5481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005483 if (n < 0)
5484 return FAIL;
5485 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005486 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005487 *fnump = n;
5488 }
5489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005490 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005491 if (n < 0)
5492 return FAIL;
5493 posp->lnum = n;
5494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005496 if (n < 0)
5497 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005498 // If character position is specified, then convert to byte position
5499 if (charcol)
5500 {
5501 buf_T *buf;
5502
5503 // Get the text for the specified line in a loaded buffer
5504 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5505 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5506 return FAIL;
5507
Bram Moolenaar91458462021-01-13 20:08:38 +01005508 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005509 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005510 posp->col = n;
5511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005512 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005513 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005514 posp->coladd = 0;
5515 else
5516 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005517
Bram Moolenaar493c1782014-05-28 14:34:46 +02005518 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005520
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005521 return OK;
5522}
5523
5524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 * Get the length of an environment variable name.
5526 * Advance "arg" to the first character after the name.
5527 * Return 0 for error.
5528 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005530get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 char_u *p;
5533 int len;
5534
5535 for (p = *arg; vim_isIDc(*p); ++p)
5536 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return 0;
5539
5540 len = (int)(p - *arg);
5541 *arg = p;
5542 return len;
5543}
5544
5545/*
5546 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005547 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 * Return 0 if something is wrong.
5549 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005551get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 char_u *p;
5554 int len;
5555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005558 {
5559 if (*p == ':')
5560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005561 // "s:" is start of "s:var", but "n:" is not and can be used in
5562 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005563 len = (int)(p - *arg);
5564 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5565 || len > 1)
5566 break;
5567 }
5568 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 return 0;
5571
5572 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005573 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 return len;
5576}
5577
5578/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005579 * Get the length of the name of a variable or function.
5580 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005582 * Return -1 if curly braces expansion failed.
5583 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 * If the name contains 'magic' {}'s, expand them and return the
5585 * expanded name in an allocated string via 'alias' - caller must free.
5586 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005588get_name_len(
5589 char_u **arg,
5590 char_u **alias,
5591 int evaluate,
5592 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 char_u *p;
5596 char_u *expr_start;
5597 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
5601 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5602 && (*arg)[2] == (int)KE_SNR)
5603 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005604 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 *arg += 3;
5606 return get_id_len(arg) + 3;
5607 }
5608 len = eval_fname_script(*arg);
5609 if (len > 0)
5610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 *arg += len;
5613 }
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005618 p = find_name_end(*arg, &expr_start, &expr_end,
5619 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (expr_start != NULL)
5621 {
5622 char_u *temp_string;
5623
5624 if (!evaluate)
5625 {
5626 len += (int)(p - *arg);
5627 *arg = skipwhite(p);
5628 return len;
5629 }
5630
5631 /*
5632 * Include any <SID> etc in the expanded string:
5633 * Thus the -len here.
5634 */
5635 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5636 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005637 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 *alias = temp_string;
5639 *arg = skipwhite(p);
5640 return (int)STRLEN(temp_string);
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005644 // Only give an error when there is something, otherwise it will be
5645 // reported at a higher level.
5646 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005647 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 return len;
5650}
5651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652/*
5653 * Find the end of a variable or function name, taking care of magic braces.
5654 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5655 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005656 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 * Return a pointer to just after the name. Equal to "arg" if there is no
5658 * valid name.
5659 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661find_name_end(
5662 char_u *arg,
5663 char_u **expr_start,
5664 char_u **expr_end,
5665 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 int mb_nest = 0;
5668 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005670 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005671 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 *expr_start = NULL;
5676 *expr_end = NULL;
5677 }
5678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005679 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005680 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5681 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005682 return arg;
5683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 for (p = arg; *p != NUL
5685 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005686 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005687 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005688 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005690 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005692 if (*p == '\'')
5693 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005694 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005695 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005696 ;
5697 if (*p == NUL)
5698 break;
5699 }
5700 else if (*p == '"')
5701 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005702 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005703 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005704 if (*p == '\\' && p[1] != NUL)
5705 ++p;
5706 if (*p == NUL)
5707 break;
5708 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005709 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005711 // "s:" is start of "s:var", but "n:" is not and can be used in
5712 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005713 len = (int)(p - arg);
5714 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005715 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005716 break;
5717 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 if (*p == '[')
5722 ++br_nest;
5723 else if (*p == ']')
5724 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005726
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005727 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729 if (*p == '{')
5730 {
5731 mb_nest++;
5732 if (expr_start != NULL && *expr_start == NULL)
5733 *expr_start = p;
5734 }
5735 else if (*p == '}')
5736 {
5737 mb_nest--;
5738 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5739 *expr_end = p;
5740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743
5744 return p;
5745}
5746
5747/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 * Expands out the 'magic' {}'s in a variable/function name.
5749 * Note that this can call itself recursively, to deal with
5750 * constructs like foo{bar}{baz}{bam}
5751 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5752 * "in_start" ^
5753 * "expr_start" ^
5754 * "expr_end" ^
5755 * "in_end" ^
5756 *
5757 * Returns a new allocated string, which the caller must free.
5758 * Returns NULL for failure.
5759 */
5760 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005761make_expanded_name(
5762 char_u *in_start,
5763 char_u *expr_start,
5764 char_u *expr_end,
5765 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005766{
5767 char_u c1;
5768 char_u *retval = NULL;
5769 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005770
5771 if (expr_end == NULL || in_end == NULL)
5772 return NULL;
5773 *expr_start = NUL;
5774 *expr_end = NUL;
5775 c1 = *in_end;
5776 *in_end = NUL;
5777
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005778 temp_result = eval_to_string(expr_start + 1, FALSE);
5779 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005780 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005781 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5782 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005783 if (retval != NULL)
5784 {
5785 STRCPY(retval, in_start);
5786 STRCAT(retval, temp_result);
5787 STRCAT(retval, expr_end + 1);
5788 }
5789 }
5790 vim_free(temp_result);
5791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005793 *expr_start = '{';
5794 *expr_end = '}';
5795
5796 if (retval != NULL)
5797 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005798 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005799 if (expr_start != NULL)
5800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005802 temp_result = make_expanded_name(retval, expr_start,
5803 expr_end, temp_result);
5804 vim_free(retval);
5805 retval = temp_result;
5806 }
5807 }
5808
5809 return retval;
5810}
5811
5812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005814 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005817eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005819 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005820}
5821
5822/*
5823 * Return TRUE if character "c" can be used as the first character in a
5824 * variable or function name (excluding '{' and '}').
5825 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005827eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005828{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005829 return ASCII_ISALPHA(c) || c == '_';
5830}
5831
5832/*
5833 * Return TRUE if character "c" can be used as the first character of a
5834 * dictionary key.
5835 */
5836 int
5837eval_isdictc(int c)
5838{
5839 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841
5842/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005843 * Handle:
5844 * - expr[expr], expr[expr:expr] subscript
5845 * - ".name" lookup
5846 * - function call with Funcref variable: func(expr)
5847 * - method call: var->method()
5848 *
5849 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005850 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005852handle_subscript(
5853 char_u **arg,
5854 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005855 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005856 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005857{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005858 int evaluate = evalarg != NULL
5859 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005860 int ret = OK;
5861 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005862 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005863 int getnext;
5864 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005865
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005866 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005867 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005868 // When at the end of the line and ".name" or "->{" or "->X" follows in
5869 // the next line then consume the line break.
5870 p = eval_next_non_blank(*arg, evalarg, &getnext);
5871 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005872 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005873 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5874 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5875 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005876 {
5877 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005878 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005879 check_white = FALSE;
5880 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005881
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005882 if (rettv->v_type == VAR_ANY)
5883 {
5884 char_u *exp_name;
5885 int cc;
5886 int idx;
5887 ufunc_T *ufunc;
5888 type_T *type;
5889
5890 // Found script from "import * as {name}", script item name must
5891 // follow.
5892 if (**arg != '.')
5893 {
5894 if (verbose)
5895 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5896 ret = FAIL;
5897 break;
5898 }
5899 ++*arg;
5900 if (IS_WHITE_OR_NUL(**arg))
5901 {
5902 if (verbose)
5903 emsg(_(e_no_white_space_allowed_after_dot));
5904 ret = FAIL;
5905 break;
5906 }
5907
5908 // isolate the name
5909 exp_name = *arg;
5910 while (eval_isnamec(**arg))
5911 ++*arg;
5912 cc = **arg;
5913 **arg = NUL;
5914
5915 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5916 evalarg->eval_cctx, verbose);
5917 **arg = cc;
5918 *arg = skipwhite(*arg);
5919
5920 if (idx < 0 && ufunc == NULL)
5921 {
5922 ret = FAIL;
5923 break;
5924 }
5925 if (idx >= 0)
5926 {
5927 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5928 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5929
5930 copy_tv(sv->sv_tv, rettv);
5931 }
5932 else
5933 {
5934 rettv->v_type = VAR_FUNC;
5935 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5936 }
5937 }
5938
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005939 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5940 || rettv->v_type == VAR_PARTIAL))
5941 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005942 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005943 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5944 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005945
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005946 // Stop the expression evaluation when immediately aborting on
5947 // error, or when an interrupt occurred or an exception was thrown
5948 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005949 if (aborting())
5950 {
5951 if (ret == OK)
5952 clear_tv(rettv);
5953 ret = FAIL;
5954 }
5955 dict_unref(selfdict);
5956 selfdict = NULL;
5957 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005958 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005959 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005960 if (in_vim9script())
5961 *arg = skipwhite(p + 2);
5962 else
5963 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005964 if (ret == OK)
5965 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005966 if (VIM_ISWHITE(**arg))
5967 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005968 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005969 ret = FAIL;
5970 }
5971 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005972 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005973 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005974 else
5975 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005976 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005977 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005978 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005979 // "." is ".name" lookup when we found a dict or when evaluating and
5980 // scriptversion is at least 2, where string concatenation is "..".
5981 else if (**arg == '['
5982 || (**arg == '.' && (rettv->v_type == VAR_DICT
5983 || (!evaluate
5984 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02005985 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005986 {
5987 dict_unref(selfdict);
5988 if (rettv->v_type == VAR_DICT)
5989 {
5990 selfdict = rettv->vval.v_dict;
5991 if (selfdict != NULL)
5992 ++selfdict->dv_refcount;
5993 }
5994 else
5995 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005996 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005997 {
5998 clear_tv(rettv);
5999 ret = FAIL;
6000 }
6001 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006002 else
6003 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006004 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006006 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6007 // Don't do this when "Func" is already a partial that was bound
6008 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006009 if (selfdict != NULL
6010 && (rettv->v_type == VAR_FUNC
6011 || (rettv->v_type == VAR_PARTIAL
6012 && (rettv->vval.v_partial->pt_auto
6013 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006015
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006016 dict_unref(selfdict);
6017 return ret;
6018}
6019
6020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 * Make a copy of an item.
6022 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6024 * reference to an already copied list/dict can be used.
6025 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028item_copy(
6029 typval_T *from,
6030 typval_T *to,
6031 int deep,
6032 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006033{
6034 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006035 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006039 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006040 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 }
6042 ++recurse;
6043
6044 switch (from->v_type)
6045 {
6046 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006047 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 case VAR_STRING:
6049 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006050 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006051 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006052 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006053 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006054 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006055 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 copy_tv(from, to);
6057 break;
6058 case VAR_LIST:
6059 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006060 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006061 if (from->vval.v_list == NULL)
6062 to->vval.v_list = NULL;
6063 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006065 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006066 to->vval.v_list = from->vval.v_list->lv_copylist;
6067 ++to->vval.v_list->lv_refcount;
6068 }
6069 else
6070 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6071 if (to->vval.v_list == NULL)
6072 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006073 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006074 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006076 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077 case VAR_DICT:
6078 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006079 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006080 if (from->vval.v_dict == NULL)
6081 to->vval.v_dict = NULL;
6082 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006084 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006085 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6086 ++to->vval.v_dict->dv_refcount;
6087 }
6088 else
6089 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6090 if (to->vval.v_dict == NULL)
6091 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006093 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006094 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006096 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006097 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 }
6099 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006100 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101}
6102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 void
6104echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6105{
6106 char_u *tofree;
6107 char_u numbuf[NUMBUFLEN];
6108 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6109
6110 if (*atstart)
6111 {
6112 *atstart = FALSE;
6113 // Call msg_start() after eval1(), evaluating the expression
6114 // may cause a message to appear.
6115 if (with_space)
6116 {
6117 // Mark the saved text as finishing the line, so that what
6118 // follows is displayed on a new line when scrolling back
6119 // at the more prompt.
6120 msg_sb_eol();
6121 msg_start();
6122 }
6123 }
6124 else if (with_space)
6125 msg_puts_attr(" ", echo_attr);
6126
6127 if (p != NULL)
6128 for ( ; *p != NUL && !got_int; ++p)
6129 {
6130 if (*p == '\n' || *p == '\r' || *p == TAB)
6131 {
6132 if (*p != TAB && *needclr)
6133 {
6134 // remove any text still there from the command
6135 msg_clr_eos();
6136 *needclr = FALSE;
6137 }
6138 msg_putchar_attr(*p, echo_attr);
6139 }
6140 else
6141 {
6142 if (has_mbyte)
6143 {
6144 int i = (*mb_ptr2len)(p);
6145
6146 (void)msg_outtrans_len_attr(p, i, echo_attr);
6147 p += i - 1;
6148 }
6149 else
6150 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6151 }
6152 }
6153 vim_free(tofree);
6154}
6155
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 * ":echo expr1 ..." print each argument separated with a space, add a
6158 * newline at the end.
6159 * ":echon expr1 ..." print each argument plain.
6160 */
6161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006166 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 int needclr = TRUE;
6168 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006169 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006170 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006171 evalarg_T evalarg;
6172
Bram Moolenaare707c882020-07-01 13:07:37 +02006173 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175 if (eap->skip)
6176 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006177 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006179 // If eval1() causes an error message the text from the command may
6180 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006181 need_clr_eos = needclr;
6182
Bram Moolenaar68db9962021-05-09 23:19:22 +02006183 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006184 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
6186 /*
6187 * Report the invalid expression unless the expression evaluation
6188 * has been cancelled due to an aborting error, an interrupt, or an
6189 * exception.
6190 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006191 if (!aborting() && did_emsg == did_emsg_before
6192 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006193 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006194 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 break;
6196 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006197 need_clr_eos = FALSE;
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006200 {
6201 if (rettv.v_type == VAR_VOID)
6202 {
6203 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6204 break;
6205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006207 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 arg = skipwhite(arg);
6211 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006212 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006213 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
6215 if (eap->skip)
6216 --emsg_skip;
6217 else
6218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006219 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (needclr)
6221 msg_clr_eos();
6222 if (eap->cmdidx == CMD_echo)
6223 msg_end();
6224 }
6225}
6226
6227/*
6228 * ":echohl {name}".
6229 */
6230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006231ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006233 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234}
6235
6236/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006237 * Returns the :echo attribute
6238 */
6239 int
6240get_echo_attr(void)
6241{
6242 return echo_attr;
6243}
6244
6245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * ":execute expr1 ..." execute the result of an expression.
6247 * ":echomsg expr1 ..." Print a message
6248 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006249 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 * Each gets spaces around each argument and a newline at the end for
6251 * echo commands
6252 */
6253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006254ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 int ret = OK;
6259 char_u *p;
6260 garray_T ga;
6261 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006262 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 ga_init2(&ga, 1, 80);
6265
6266 if (eap->skip)
6267 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006268 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006270 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006271 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 if (!eap->skip)
6275 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006276 char_u buf[NUMBUFLEN];
6277
6278 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006279 {
6280 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6281 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006282 semsg(_(e_using_invalid_value_as_string_str),
6283 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006284 p = NULL;
6285 }
6286 else
6287 p = tv_get_string_buf(&rettv, buf);
6288 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006289 else
6290 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006291 if (p == NULL)
6292 {
6293 clear_tv(&rettv);
6294 ret = FAIL;
6295 break;
6296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 len = (int)STRLEN(p);
6298 if (ga_grow(&ga, len + 2) == FAIL)
6299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006300 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 ret = FAIL;
6302 break;
6303 }
6304 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 ga.ga_len += len;
6308 }
6309
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006310 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 arg = skipwhite(arg);
6312 }
6313
6314 if (ret != FAIL && ga.ga_data != NULL)
6315 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006316 // use the first line of continuation lines for messages
6317 SOURCING_LNUM = start_lnum;
6318
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006319 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006321 // Mark the already saved text as finishing the line, so that what
6322 // follows is displayed on a new line when scrolling back at the
6323 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006324 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006325 }
6326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006328 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006329 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006330 out_flush();
6331 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006332 else if (eap->cmdidx == CMD_echoconsole)
6333 {
6334 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6335 ui_write((char_u *)"\r\n", 2, TRUE);
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 else if (eap->cmdidx == CMD_echoerr)
6338 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006339 int save_did_emsg = did_emsg;
6340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006342 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (!force_abort)
6344 did_emsg = save_did_emsg;
6345 }
6346 else if (eap->cmdidx == CMD_execute)
6347 do_cmdline((char_u *)ga.ga_data,
6348 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6349 }
6350
6351 ga_clear(&ga);
6352
6353 if (eap->skip)
6354 --emsg_skip;
6355
Bram Moolenaar63b91732021-08-05 20:40:03 +02006356 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359/*
6360 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6361 * "arg" points to the "&" or '+' when called, to "option" when returning.
6362 * Returns NULL when no option name found. Otherwise pointer to the char
6363 * after the option name.
6364 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006365 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006366find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 char_u *p = *arg;
6369
6370 ++p;
6371 if (*p == 'g' && p[1] == ':')
6372 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006373 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 p += 2;
6375 }
6376 else if (*p == 'l' && p[1] == ':')
6377 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006378 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 p += 2;
6380 }
6381 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006382 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 if (!ASCII_ISALPHA(*p))
6385 return NULL;
6386 *arg = p;
6387
6388 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006389 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else
6391 while (ASCII_ISALPHA(*p))
6392 ++p;
6393 return p;
6394}
6395
6396/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006397 * Display script name where an item was last set.
6398 * Should only be invoked when 'verbose' is non-zero.
6399 */
6400 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006401last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006402{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006403 char_u *p;
6404
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006405 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006406 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006407 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006408 if (p != NULL)
6409 {
6410 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006411 msg_puts(_("\n\tLast set from "));
6412 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006413 if (script_ctx.sc_lnum > 0)
6414 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006415 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006416 msg_outnum((long)script_ctx.sc_lnum);
6417 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006418 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006419 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006420 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006421 }
6422}
6423
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006424#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426/*
6427 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006428 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 * "flags" can be "g" to do a global substitute.
6430 * Returns an allocated string, NULL for error.
6431 */
6432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433do_string_sub(
6434 char_u *str,
6435 char_u *pat,
6436 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006437 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 int sublen;
6441 regmatch_T regmatch;
6442 int i;
6443 int do_all;
6444 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006445 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 garray_T ga;
6447 char_u *ret;
6448 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006449 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006451 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006453 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 ga_init2(&ga, 1, 200);
6456
6457 do_all = (flags[0] == 'g');
6458
6459 regmatch.rm_ic = p_ic;
6460 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6461 if (regmatch.regprog != NULL)
6462 {
6463 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006464 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6466 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006468 if (regmatch.startp[0] == regmatch.endp[0])
6469 {
6470 if (zero_width == regmatch.startp[0])
6471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006472 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006473 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006474 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6475 (size_t)i);
6476 ga.ga_len += i;
6477 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006478 continue;
6479 }
6480 zero_width = regmatch.startp[0];
6481 }
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 /*
6484 * Get some space for a temporary buffer to do the substitution
6485 * into. It will contain:
6486 * - The text up to where the match is.
6487 * - The substituted text.
6488 * - The text after the match.
6489 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006490 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006491 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6493 {
6494 ga_clear(&ga);
6495 break;
6496 }
6497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 i = (int)(regmatch.startp[0] - tail);
6500 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006502 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 + ga.ga_len + i, TRUE, TRUE, FALSE);
6504 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006505 tail = regmatch.endp[0];
6506 if (*tail == NUL)
6507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (!do_all)
6509 break;
6510 }
6511
6512 if (ga.ga_data != NULL)
6513 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6514
Bram Moolenaar473de612013-06-08 18:19:48 +02006515 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517
6518 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6519 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006520 if (p_cpo == empty_option)
6521 p_cpo = save_cpo;
6522 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006524 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006525 // If it's still empty it was changed and restored, need to restore in
6526 // the complicated way.
6527 if (*p_cpo == NUL)
6528 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006529 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 return ret;
6533}