blob: 6b3d78aa6e4374300b24a965d56ad2ec201910d4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020059static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000115
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200116#ifdef EBCDIC
117 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100118 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200119 */
120 sortFunctions();
121#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000122}
123
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124#if defined(EXITFREE) || defined(PROTO)
125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100126eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000127{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100129 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200130 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000131
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200132 // autoloaded script names
133 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000134
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200135 // unreferenced lists and dicts
136 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200137
138 // functions not garbage collected
139 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140}
141#endif
142
Bram Moolenaare6b53242020-07-01 17:28:33 +0200143 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200144fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
145{
146 CLEAR_FIELD(*evalarg);
147 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200148 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200150 evalarg->eval_cstack = eap->cstack;
151 if (getline_equal(eap->getline, eap->cookie, getsourceline))
152 {
153 evalarg->eval_getline = eap->getline;
154 evalarg->eval_cookie = eap->cookie;
155 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200156 }
157}
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Top level evaluation function, returning a boolean.
161 * Sets "error" to TRUE if there was an error.
162 * Return TRUE or FALSE.
163 */
164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100165eval_to_bool(
166 char_u *arg,
167 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200168 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaar33570922005-01-25 22:26:29 +0000171 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200172 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200173 evalarg_T evalarg;
174
Bram Moolenaar37c83712020-06-30 21:18:36 +0200175 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
177 if (skip)
178 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200179 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 else
182 {
183 *error = FALSE;
184 if (!skip)
185 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200186 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200187 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200188 else
189 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192 }
193 if (skip)
194 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200197 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200/*
201 * Call eval1() and give an error message if not done at a lower level.
202 */
203 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100206 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 int ret;
208 int did_emsg_before = did_emsg;
209 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200212 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
213
214 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215 if (ret == FAIL)
216 {
217 // Report the invalid expression unless the expression evaluation has
218 // been cancelled due to an aborting error, an interrupt, or an
219 // exception, or we already gave a more specific error.
220 // Also check called_emsg for when using assert_fails().
221 if (!aborting() && did_emsg == did_emsg_before
222 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200223 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100224 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200225 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100226 return ret;
227}
228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200230 * Return whether a typval is a valid expression to pass to eval_expr_typval()
231 * or eval_expr_to_bool(). An empty string returns FALSE;
232 */
233 int
234eval_expr_valid_arg(typval_T *tv)
235{
236 return tv->v_type != VAR_UNKNOWN
237 && (tv->v_type != VAR_STRING
238 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
239}
240
241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 * Evaluate an expression, which can be a function, partial or string.
243 * Pass arguments "argv[argc]".
244 * Return the result in "rettv" and OK or FAIL.
245 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200246 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100247eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
248{
249 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100250 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200251 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100252
253 if (expr->v_type == VAR_FUNC)
254 {
255 s = expr->vval.v_string;
256 if (s == NULL || *s == NUL)
257 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200258 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 funcexe.evaluate = TRUE;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100261 return FAIL;
262 }
263 else if (expr->v_type == VAR_PARTIAL)
264 {
265 partial_T *partial = expr->vval.v_partial;
266
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200267 if (partial == NULL)
268 return FAIL;
269
Bram Moolenaar822ba242020-05-24 23:00:18 +0200270 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200271 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200273 if (call_def_function(partial->pt_func, argc, argv,
274 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 return FAIL;
276 }
277 else
278 {
279 s = partial_name(partial);
280 if (s == NULL || *s == NUL)
281 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200282 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 funcexe.evaluate = TRUE;
284 funcexe.partial = partial;
285 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
286 return FAIL;
287 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200289 else if (expr->v_type == VAR_INSTR)
290 {
291 return exe_typval_instr(expr, rettv);
292 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 else
294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100295 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 if (s == NULL)
297 return FAIL;
298 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200299 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200301 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200303 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200304 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 return FAIL;
306 }
307 }
308 return OK;
309}
310
311/*
312 * Like eval_to_bool() but using a typval_T instead of a string.
313 * Works for string, funcref and partial.
314 */
315 int
316eval_expr_to_bool(typval_T *expr, int *error)
317{
318 typval_T rettv;
319 int res;
320
321 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
322 {
323 *error = TRUE;
324 return FALSE;
325 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200326 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100327 clear_tv(&rettv);
328 return res;
329}
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331/*
332 * Top level evaluation function, returning a string. If "skip" is TRUE,
333 * only parsing to "nextcmd" is done, without reporting errors. Return
334 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
335 */
336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100337eval_to_string_skip(
338 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200339 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100340 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
Bram Moolenaar33570922005-01-25 22:26:29 +0000342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar37c83712020-06-30 21:18:36 +0200346 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (skip)
348 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200349 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 retval = NULL;
351 else
352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100353 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356 if (skip)
357 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200358 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360 return retval;
361}
362
363/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364 * Skip over an expression at "*pp".
365 * Return FAIL for an error, OK otherwise.
366 */
367 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200368skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369{
Bram Moolenaar33570922005-01-25 22:26:29 +0000370 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371
372 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200373 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374}
375
376/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200377 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200378 * If in Vim9 script and line breaks are encountered, the lines are
379 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200380 * "arg" is advanced to just after the expression.
381 * "start" is set to the start of the expression, "end" to just after the end.
382 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200386skip_expr_concatenate(
387 char_u **arg,
388 char_u **start,
389 char_u **end,
390 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200391{
392 typval_T rettv;
393 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200394 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200395 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200396 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 int evaluate = evalarg == NULL
399 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200401 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 {
404 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200405 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200408 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200410 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411
412 // Don't evaluate the expression.
413 if (evalarg != NULL)
414 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200415 *arg = skipwhite(*arg);
416 res = eval1(arg, &rettv, evalarg);
417 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 if (evalarg != NULL)
419 evalarg->eval_flags = save_flags;
420
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200421 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 if (evalarg->eval_ga.ga_len == 1)
425 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200426 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 ga_clear(gap);
428 gap->ga_itemsize = 0;
429 }
430 else
431 {
432 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200433 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 // Line breaks encountered, concatenate all the lines.
436 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200437 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438
439 // free the lines only when using getsourceline()
440 if (evalarg->eval_cookie != NULL)
441 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200442 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200444 // Do not free the last line, "arg" points into it, free it
445 // later.
446 vim_free(evalarg->eval_tofree);
447 evalarg->eval_tofree =
448 ((char_u **)gap->ga_data)[gap->ga_len - 1];
449 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear_strings(gap);
451 }
452 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200453 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200454 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200455
456 // free lines that were explicitly marked for freeing
457 ga_clear_strings(freegap);
458 }
459
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200460 gap->ga_itemsize = 0;
461 if (p == NULL)
462 return FAIL;
463 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200464 vim_free(evalarg->eval_tofree_lambda);
465 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 // Compute "end" relative to the end.
467 *end = *start + STRLEN(*start) - endoff;
468 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200469 }
470
471 return res;
472}
473
474/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100475 * Convert "tv" to a string.
476 * When "convert" is TRUE convert a List into a sequence of lines and convert
477 * a Float to a String.
478 * Returns an allocated string (NULL when out of memory).
479 */
480 char_u *
481typval2string(typval_T *tv, int convert)
482{
483 garray_T ga;
484 char_u *retval;
485#ifdef FEAT_FLOAT
486 char_u numbuf[NUMBUFLEN];
487#endif
488
489 if (convert && tv->v_type == VAR_LIST)
490 {
491 ga_init2(&ga, (int)sizeof(char), 80);
492 if (tv->vval.v_list != NULL)
493 {
494 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
495 if (tv->vval.v_list->lv_len > 0)
496 ga_append(&ga, NL);
497 }
498 ga_append(&ga, NUL);
499 retval = (char_u *)ga.ga_data;
500 }
501#ifdef FEAT_FLOAT
502 else if (convert && tv->v_type == VAR_FLOAT)
503 {
504 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
505 retval = vim_strsave(numbuf);
506 }
507#endif
508 else
509 retval = vim_strsave(tv_get_string(tv));
510 return retval;
511}
512
513/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200514 * Top level evaluation function, returning a string. Does not handle line
515 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000516 * When "convert" is TRUE convert a List into a sequence of lines and convert
517 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * Return pointer to allocated memory, or NULL for failure.
519 */
520 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 int convert,
524 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
Bram Moolenaar33570922005-01-25 22:26:29 +0000526 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100528 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
531 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 retval = NULL;
533 else
534 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100535 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000536 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 return retval;
541}
542
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100543 char_u *
544eval_to_string(
545 char_u *arg,
546 int convert)
547{
548 return eval_to_string_eap(arg, convert, NULL);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000552 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200554 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 */
556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100557eval_to_string_safe(
558 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100559 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200563 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200570 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000571 if (use_sandbox)
572 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200573 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 return retval;
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579/*
580 * Top level evaluation function, returning a number.
581 * Evaluates "expr" silently.
582 * Returns -1 for an error.
583 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100585eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar33570922005-01-25 22:26:29 +0000587 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000589 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 ++emsg_off;
592
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200593 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 retval = -1;
595 else
596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100597 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600 --emsg_off;
601
602 return retval;
603}
604
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000606 * Top level evaluation function.
607 * Returns an allocated typval_T with the result.
608 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609 */
610 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200611eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000612{
613 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200614 evalarg_T evalarg;
615
616 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200618 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623 return tv;
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100627 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200628 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
629 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000630 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633call_vim_function(
634 char_u *func,
635 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200637 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200640 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200643 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200644 funcexe.firstline = curwin->w_cursor.lnum;
645 funcexe.lastline = curwin->w_cursor.lnum;
646 funcexe.evaluate = TRUE;
647 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000648 if (ret == FAIL)
649 clear_tv(rettv);
650
651 return ret;
652}
653
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100654/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100655 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200657 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
658 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100659 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200660 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100661call_func_retnr(
662 char_u *func,
663 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200664 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665{
666 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200667 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668
Bram Moolenaarded27a12018-08-01 19:06:03 +0200669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 return -1;
671
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100672 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673 clear_tv(&rettv);
674 return retval;
675}
676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000677/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100678 * Call Vim script function like call_func_retnr() and drop the result.
679 * Returns FAIL when calling the function fails.
680 */
681 int
682call_func_noret(
683 char_u *func,
684 int argc,
685 typval_T *argv)
686{
687 typval_T rettv;
688
689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
690 return FAIL;
691 clear_tv(&rettv);
692 return OK;
693}
694
695/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100696 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100697 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000698 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000699 */
700 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701call_func_retstr(
702 char_u *func,
703 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200704 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705{
706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708
Bram Moolenaarded27a12018-08-01 19:06:03 +0200709 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 return NULL;
711
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100712 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return retval;
715}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100718 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100719 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000720 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 */
722 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723call_func_retlist(
724 char_u *func,
725 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200726 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727{
728 typval_T rettv;
729
Bram Moolenaarded27a12018-08-01 19:06:03 +0200730 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 return NULL;
732
733 if (rettv.v_type != VAR_LIST)
734 {
735 clear_tv(&rettv);
736 return NULL;
737 }
738
739 return rettv.vval.v_list;
740}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#ifdef FEAT_FOLDING
743/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100744 * Evaluate "arg", which is 'foldexpr'.
745 * Note: caller must set "curwin" to match "arg".
746 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
747 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100750eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200753 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000755 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
756 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000759 if (use_sandbox)
760 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200761 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200763 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 retval = 0;
765 else
766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100767 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000768 if (tv.v_type == VAR_NUMBER)
769 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000770 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 retval = 0;
772 else
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // If the result is a string, check if there is a non-digit before
775 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (!VIM_ISDIGIT(*s) && *s != '-')
778 *cp = *s++;
779 retval = atol((char *)s);
780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000784 if (use_sandbox)
785 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200786 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200787 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791#endif
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 * Get an lval: variable, Dict item or List item that can be assigned a value
795 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
796 * "name.key", "name.key[expr]" etc.
797 * Indexing only works if "name" is an existing List or Dictionary.
798 * "name" points to the start of the name.
799 * If "rettv" is not NULL it points to the value to be assigned.
800 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
801 * wrong; must end in space or cmd separator.
802 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * flags:
804 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100805 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100806 * GLV_NO_AUTOLOAD: do not use script autoloading
807 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000809 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000811 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813get_lval(
814 char_u *name,
815 typval_T *rettv,
816 lval_T *lp,
817 int unlet,
818 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100819 int flags, // GLV_ values
820 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 char_u *expr_start, *expr_end;
824 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 dictitem_T *v;
826 typval_T var1;
827 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000829 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100831 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100832 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100833 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200836 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200842 lp->ll_name_end = find_name_end(name, NULL, NULL,
843 FNE_INCL_BR | fne_flags);
844 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 }
846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100847 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 if (expr_start != NULL)
851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100853 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 && *p != '[' && *p != '.')
855 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200856 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 return NULL;
858 }
859
860 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
861 if (lp->ll_exp_name == NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Report an invalid expression in braces, unless the
864 // expression evaluation has been cancelled due to an
865 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 if (!aborting() && !quiet)
867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000868 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 return NULL;
871 }
872 }
873 lp->ll_name = lp->ll_exp_name;
874 }
875 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 lp->ll_name = name;
878
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 // "a: type" is declaring variable "a" with a type, not "a:".
882 if (p == name + 2 && p[-1] == ':')
883 {
884 --p;
885 lp->ll_name_end = p;
886 }
887 if (*p == ':')
888 {
889 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200890 char_u *tp = skipwhite(p + 1);
891
892 if (tp == p + 1 && !quiet)
893 {
894 semsg(_(e_white_space_required_after_str_str), ":", p);
895 return NULL;
896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200898 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100899 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
900 if (lp->ll_type == NULL && !quiet)
901 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200902 lp->ll_name_end = tp;
903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 }
905 }
906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100907 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
909 return p;
910
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200911 if (in_vim9script() && lval_root != NULL)
912 {
913 // using local variable
914 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200915 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200916 }
917 else
918 {
919 cc = *p;
920 *p = NUL;
921 // When we would write to the variable pass &ht and prevent autoload.
922 writing = !(flags & GLV_READ_ONLY);
923 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100924 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200925 if (v == NULL && !quiet)
926 semsg(_(e_undefined_variable_str), lp->ll_name);
927 *p = cc;
928 if (v == NULL)
929 return NULL;
930 lp->ll_tv = &v->di_tv;
931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000932
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100933 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
934 {
935 if (!quiet)
936 semsg(_(e_variable_already_declared), lp->ll_name);
937 return NULL;
938 }
939
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 /*
941 * Loop until no more [idx] or .key is following.
942 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100943 var1.v_type = VAR_UNKNOWN;
944 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200945 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000946 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200947 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
948 {
949 if (!quiet)
950 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
951 return NULL;
952 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200953 if (lp->ll_tv->v_type != VAR_LIST
954 && lp->ll_tv->v_type != VAR_DICT
955 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200961
962 // a NULL list/blob works like an empty list/blob, allocate one now.
963 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
964 rettv_list_alloc(lp->ll_tv);
965 else if (lp->ll_tv->v_type == VAR_BLOB
966 && lp->ll_tv->vval.v_blob == NULL)
967 rettv_blob_alloc(lp->ll_tv);
968
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000971 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000974 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000975
Bram Moolenaar10c65862020-10-08 21:16:42 +0200976 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200977 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200978 && lp->ll_tv == &v->di_tv
979 && ht != NULL && ht == get_script_local_ht())
980 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200981 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200982
983 // Vim9 script local variable: get the type
984 if (sv != NULL)
985 lp->ll_valtype = sv->sv_type;
986 }
987
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 len = -1;
989 if (*p == '.')
990 {
991 key = p + 1;
992 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
993 ;
994 if (len == 0)
995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 }
1000 p = key + len;
1001 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001002 else
1003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001005 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 if (*p == ':')
1007 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001008 else
1009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001010 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001011 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001013 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001015 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001016 clear_tv(&var1);
1017 return NULL;
1018 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001019 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 }
1021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001022 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 if (*p == ':')
1024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001028 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001029 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001031 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001032 if (rettv != NULL
1033 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001034 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001035 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001036 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001039 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
1043 p = skipwhite(p + 1);
1044 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 else
1047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001049 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001050 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001051 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001054 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001055 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001058 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001059 clear_tv(&var2);
1060 return NULL;
1061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001067
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001069 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001071 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001072 clear_tv(&var1);
1073 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 }
1076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001077 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 ++p;
1079 }
1080
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001082 {
1083 if (len == -1)
1084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 // "[key]": get key from "var1"
1086 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001087 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001089 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 }
1092 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001093 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001094
1095 // a NULL dict is equivalent with an empty dict
1096 if (lp->ll_tv->vval.v_dict == NULL)
1097 {
1098 lp->ll_tv->vval.v_dict = dict_alloc();
1099 if (lp->ll_tv->vval.v_dict == NULL)
1100 {
1101 clear_tv(&var1);
1102 return NULL;
1103 }
1104 ++lp->ll_tv->vval.v_dict->dv_refcount;
1105 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001106 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001108 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // When assigning to a scope dictionary check that a function and
1111 // variable name is valid (only variable name unless it is l: or
1112 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001113 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001115 int prevval;
1116 int wrong;
1117
1118 if (len != -1)
1119 {
1120 prevval = key[len];
1121 key[len] = NUL;
1122 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001123 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001124 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001125 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1126 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001127 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001128 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001129 if (len != -1)
1130 key[len] = prevval;
1131 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001132 {
1133 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001134 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001135 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001136 }
1137
Bram Moolenaar10c65862020-10-08 21:16:42 +02001138 if (lp->ll_valtype != NULL)
1139 // use the type of the member
1140 lp->ll_valtype = lp->ll_valtype->tt_member;
1141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001144 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001145 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001146 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001149 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001150 return NULL;
1151 }
1152
Bram Moolenaar31b81602019-02-10 22:14:27 +01001153 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001157 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001158 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 }
1161 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001165 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 p = NULL;
1168 break;
1169 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001170 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001171 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001172 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1173 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001174 {
1175 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001176 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001177 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001178
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001179 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001181 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001182 else if (lp->ll_tv->v_type == VAR_BLOB)
1183 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1185
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 /*
1187 * Get the number and item for the only or first index of the List.
1188 */
1189 if (empty1)
1190 lp->ll_n1 = 0;
1191 else
1192 // is number or string
1193 lp->ll_n1 = (long)tv_get_number(&var1);
1194 clear_tv(&var1);
1195
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001196 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001198 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001199 return NULL;
1200 }
1201 if (lp->ll_range && !lp->ll_empty2)
1202 {
1203 lp->ll_n2 = (long)tv_get_number(&var2);
1204 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001205 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1206 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001207 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001208 }
1209 lp->ll_blob = lp->ll_tv->vval.v_blob;
1210 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001211 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 else
1214 {
1215 /*
1216 * Get the number and item for the only or first index of the List.
1217 */
1218 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001221 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001222 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001223 clear_tv(&var1);
1224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001225 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001227 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001228 if (lp->ll_li == NULL)
1229 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001230 clear_tv(&var2);
1231 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 }
1233
Bram Moolenaar10c65862020-10-08 21:16:42 +02001234 if (lp->ll_valtype != NULL)
1235 // use the type of the member
1236 lp->ll_valtype = lp->ll_valtype->tt_member;
1237
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 /*
1239 * May need to find the item or absolute index for the second
1240 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 * When no index given: "lp->ll_empty2" is TRUE.
1242 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001246 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001249 if (check_range_index_two(lp->ll_list,
1250 &lp->ll_n1, lp->ll_li,
1251 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001253 }
1254
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257 }
1258
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001259 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 return p;
1262}
1263
1264/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269{
1270 vim_free(lp->ll_exp_name);
1271 vim_free(lp->ll_newkey);
1272}
1273
1274/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001275 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001277 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1278 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001281set_var_lval(
1282 lval_T *lp,
1283 char_u *endp,
1284 typval_T *rettv,
1285 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001286 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1287 char_u *op,
1288 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289{
1290 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001291 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292
1293 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001294 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001295 cc = *endp;
1296 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001297 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1298 return;
1299
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 if (lp->ll_blob != NULL)
1301 {
1302 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 if (op != NULL && *op != '=')
1305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001306 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 return;
1308 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001309 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001310 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311
1312 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1313 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001314 if (lp->ll_empty2)
1315 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1316
Bram Moolenaar68452172021-04-12 21:21:02 +02001317 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1318 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 }
1321 else
1322 {
1323 val = (int)tv_get_number_chk(rettv, &error);
1324 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001325 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 }
1327 }
1328 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001330 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001332 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1333 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001334 {
1335 emsg(_(e_cannot_mod));
1336 *endp = cc;
1337 return;
1338 }
1339
Bram Moolenaarff697e62019-02-12 22:28:33 +01001340 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001342 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001343 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001344 {
1345 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001346 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1347 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001348 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001349 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1350 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001351 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001355 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001356 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1357 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001358 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001359 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1360 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001362 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001364 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001365 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001366 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001367 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368 else if (lp->ll_range)
1369 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001370 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1371 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001372 {
1373 emsg(_("E996: Cannot lock a range"));
1374 return;
1375 }
1376
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001377 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1378 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 }
1380 else
1381 {
1382 /*
1383 * Assign to a List or Dictionary item.
1384 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001385 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1386 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001387 {
1388 emsg(_("E996: Cannot lock a list or dict"));
1389 return;
1390 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001391
1392 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001393 && check_typval_arg_type(lp->ll_valtype, rettv,
1394 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001395 return;
1396
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 if (lp->ll_newkey != NULL)
1398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 if (op != NULL && *op != '=')
1400 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001401 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 return;
1403 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001404 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1405 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001406 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001408 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001409 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (di == NULL)
1411 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001412 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1413 {
1414 vim_free(di);
1415 return;
1416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419 else if (op != NULL && *op != '=')
1420 {
1421 tv_op(lp->ll_tv, rettv, op);
1422 return;
1423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001426
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 /*
1428 * Assign the value to the variable or list item.
1429 */
1430 if (copy)
1431 copy_tv(rettv, lp->ll_tv);
1432 else
1433 {
1434 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001435 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 }
1438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439}
1440
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001441/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001442 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1443 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 * Returns OK or FAIL.
1445 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001449 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001450 char_u numbuf[NUMBUFLEN];
1451 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001452 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001454 // Can't do anything with a Funcref or Dict on the right.
1455 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001456 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001457 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1458 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 {
1460 switch (tv1->v_type)
1461 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001462 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001463 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 case VAR_DICT:
1466 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001467 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001468 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001469 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001470 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001471 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001472 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 break;
1474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 case VAR_BLOB:
1476 if (*op != '+' || tv2->v_type != VAR_BLOB)
1477 break;
1478 // BLOB += BLOB
1479 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1480 {
1481 blob_T *b1 = tv1->vval.v_blob;
1482 blob_T *b2 = tv2->vval.v_blob;
1483 int i, len = blob_len(b2);
1484 for (i = 0; i < len; i++)
1485 ga_append(&b1->bv_ga, blob_get(b2, i));
1486 }
1487 return OK;
1488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 case VAR_LIST:
1490 if (*op != '+' || tv2->v_type != VAR_LIST)
1491 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001492 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001493 if (tv2->vval.v_list != NULL)
1494 {
1495 if (tv1->vval.v_list == NULL)
1496 {
1497 tv1->vval.v_list = tv2->vval.v_list;
1498 ++tv1->vval.v_list->lv_refcount;
1499 }
1500 else
1501 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503 return OK;
1504
1505 case VAR_NUMBER:
1506 case VAR_STRING:
1507 if (tv2->v_type == VAR_LIST)
1508 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001511 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001513#ifdef FEAT_FLOAT
1514 if (tv2->v_type == VAR_FLOAT)
1515 {
1516 float_T f = n;
1517
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 if (*op == '%')
1519 break;
1520 switch (*op)
1521 {
1522 case '+': f += tv2->vval.v_float; break;
1523 case '-': f -= tv2->vval.v_float; break;
1524 case '*': f *= tv2->vval.v_float; break;
1525 case '/': f /= tv2->vval.v_float; break;
1526 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001527 clear_tv(tv1);
1528 tv1->v_type = VAR_FLOAT;
1529 tv1->vval.v_float = f;
1530 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001532#endif
1533 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001534 switch (*op)
1535 {
1536 case '+': n += tv_get_number(tv2); break;
1537 case '-': n -= tv_get_number(tv2); break;
1538 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001539 case '/': n = num_divide(n, tv_get_number(tv2),
1540 &failed); break;
1541 case '%': n = num_modulus(n, tv_get_number(tv2),
1542 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544 clear_tv(tv1);
1545 tv1->v_type = VAR_NUMBER;
1546 tv1->vval.v_number = n;
1547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 }
1549 else
1550 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 if (tv2->v_type == VAR_FLOAT)
1552 break;
1553
Bram Moolenaarff697e62019-02-12 22:28:33 +01001554 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001555 s = tv_get_string(tv1);
1556 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001557 clear_tv(tv1);
1558 tv1->v_type = VAR_STRING;
1559 tv1->vval.v_string = s;
1560 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001561 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001564#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565 {
1566 float_T f;
1567
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 if (*op == '%' || *op == '.'
1569 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570 && tv2->v_type != VAR_NUMBER
1571 && tv2->v_type != VAR_STRING))
1572 break;
1573 if (tv2->v_type == VAR_FLOAT)
1574 f = tv2->vval.v_float;
1575 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001576 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 switch (*op)
1578 {
1579 case '+': tv1->vval.v_float += f; break;
1580 case '-': tv1->vval.v_float -= f; break;
1581 case '*': tv1->vval.v_float *= f; break;
1582 case '/': tv1->vval.v_float /= f; break;
1583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001586 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 }
1588 }
1589
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001590 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 return FAIL;
1592}
1593
1594/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001595 * Evaluate the expression used in a ":for var in expr" command.
1596 * "arg" points to "var".
1597 * Set "*errp" to TRUE for an error, FALSE otherwise;
1598 * Return a pointer that holds the info. Null when there is an error.
1599 */
1600 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_for_line(
1602 char_u *arg,
1603 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001604 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001605 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606{
Bram Moolenaar33570922005-01-25 22:26:29 +00001607 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001608 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 typval_T tv;
1611 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001612 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001614 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001616 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 if (fi == NULL)
1618 return NULL;
1619
Bram Moolenaar404557e2021-07-05 21:41:48 +02001620 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1621 &fi->fi_semicolon, FALSE);
1622 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 return fi;
1624
Bram Moolenaar404557e2021-07-05 21:41:48 +02001625 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001626 if (expr[0] != 'i' || expr[1] != 'n'
1627 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001629 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1630 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1631 else
1632 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 return fi;
1634 }
1635
1636 if (skip)
1637 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001638 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1639 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 {
1641 *errp = FALSE;
1642 if (!skip)
1643 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001645 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001646 l = tv.vval.v_list;
1647 if (l == NULL)
1648 {
1649 // a null list is like an empty list: do nothing
1650 clear_tv(&tv);
1651 }
1652 else
1653 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001655 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001657 // No need to increment the refcount, it's already set for
1658 // the list being used in "tv".
1659 fi->fi_list = l;
1660 list_add_watch(l, &fi->fi_lw);
1661 fi->fi_lw.lw_item = l->lv_first;
1662 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001663 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001665 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001666 fi->fi_bi = 0;
1667 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001669 typval_T btv;
1670
1671 // Make a copy, so that the iteration still works when the
1672 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001674 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001676 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001677 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001678 else if (tv.v_type == VAR_STRING)
1679 {
1680 fi->fi_byte_idx = 0;
1681 fi->fi_string = tv.vval.v_string;
1682 tv.vval.v_string = NULL;
1683 if (fi->fi_string == NULL)
1684 fi->fi_string = vim_strsave((char_u *)"");
1685 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 else
1687 {
Sean Dewar80d73952021-08-04 19:25:54 +02001688 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001689 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 }
1691 }
1692 }
1693 if (skip)
1694 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001695 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696
1697 return fi;
1698}
1699
1700/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 * Used when looping over a :for line, skip the "in expr" part.
1702 */
1703 void
1704skip_for_lines(void *fi_void, evalarg_T *evalarg)
1705{
1706 forinfo_T *fi = (forinfo_T *)fi_void;
1707 int i;
1708
1709 for (i = 0; i < fi->fi_break_count; ++i)
1710 eval_next_line(evalarg);
1711}
1712
1713/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 * Use the first item in a ":for" list. Advance to the next.
1715 * Assign the values to the variable (list). "arg" points to the first one.
1716 * Return TRUE when a valid item was found, FALSE when at end of list or
1717 * something wrong.
1718 */
1719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001722 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001724 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001725 ? (ASSIGN_FINAL
1726 // first round: error if variable exists
1727 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1728 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001729 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001730 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001731 int skip_assign = in_vim9script() && arg[0] == '_'
1732 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 if (fi->fi_blob != NULL)
1735 {
1736 typval_T tv;
1737
1738 if (fi->fi_bi >= blob_len(fi->fi_blob))
1739 return FALSE;
1740 tv.v_type = VAR_NUMBER;
1741 tv.v_lock = VAR_FIXED;
1742 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1743 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001744 if (skip_assign)
1745 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001746 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001747 fi->fi_varcount, flag, NULL) == OK;
1748 }
1749
1750 if (fi->fi_string != NULL)
1751 {
1752 typval_T tv;
1753 int len;
1754
1755 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1756 if (len == 0)
1757 return FALSE;
1758 tv.v_type = VAR_STRING;
1759 tv.v_lock = VAR_FIXED;
1760 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1761 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001762 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001763 if (skip_assign)
1764 result = TRUE;
1765 else
1766 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001767 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001768 vim_free(tv.vval.v_string);
1769 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001770 }
1771
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 item = fi->fi_lw.lw_item;
1773 if (item == NULL)
1774 result = FALSE;
1775 else
1776 {
1777 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001778 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001779 if (skip_assign)
1780 result = TRUE;
1781 else
1782 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001783 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 }
1785 return result;
1786}
1787
1788/*
1789 * Free the structure used to store info used by ":for".
1790 */
1791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793{
Bram Moolenaar33570922005-01-25 22:26:29 +00001794 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001796 if (fi == NULL)
1797 return;
1798 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001799 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001801 list_unref(fi->fi_list);
1802 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001803 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001804 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001805 else
1806 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 vim_free(fi);
1808}
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811set_context_for_expression(
1812 expand_T *xp,
1813 char_u *arg,
1814 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001816 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001820 if (cmdidx == CMD_let || cmdidx == CMD_var
1821 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 {
1823 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001824 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001826 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 {
1829 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001830 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001831 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 break;
1833 }
1834 return;
1835 }
1836 }
1837 else
1838 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1839 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 while ((xp->xp_pattern = vim_strpbrk(arg,
1841 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1842 {
1843 c = *xp->xp_pattern;
1844 if (c == '&')
1845 {
1846 c = xp->xp_pattern[1];
1847 if (c == '&')
1848 {
1849 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001850 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001855 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1856 xp->xp_pattern += 2;
1857
1858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 else if (c == '$')
1861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001862 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 xp->xp_context = EXPAND_ENV_VARS;
1864 }
1865 else if (c == '=')
1866 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001867 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 xp->xp_context = EXPAND_EXPRESSION;
1869 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001870 else if (c == '#'
1871 && xp->xp_context == EXPAND_EXPRESSION)
1872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001873 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001874 break;
1875 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001876 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 && xp->xp_context == EXPAND_FUNCTIONS
1878 && vim_strchr(xp->xp_pattern, '(') == NULL)
1879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 break;
1882 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001883 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001885 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1888 if (c == '\\' && xp->xp_pattern[1] != NUL)
1889 ++xp->xp_pattern;
1890 xp->xp_context = EXPAND_NOTHING;
1891 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001892 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001894 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1896 /* skip */ ;
1897 xp->xp_context = EXPAND_NOTHING;
1898 }
1899 else if (c == '|')
1900 {
1901 if (xp->xp_pattern[1] == '|')
1902 {
1903 ++xp->xp_pattern;
1904 xp->xp_context = EXPAND_EXPRESSION;
1905 }
1906 else
1907 xp->xp_context = EXPAND_COMMANDS;
1908 }
1909 else
1910 xp->xp_context = EXPAND_EXPRESSION;
1911 }
1912 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Doesn't look like something valid, expand as an expression
1914 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 arg = xp->xp_pattern;
1917 if (*arg != NUL)
1918 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1919 /* skip */ ;
1920 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001921
1922 // ":exe one two" completes "two"
1923 if ((cmdidx == CMD_execute
1924 || cmdidx == CMD_echo
1925 || cmdidx == CMD_echon
1926 || cmdidx == CMD_echomsg)
1927 && xp->xp_context == EXPAND_EXPRESSION)
1928 {
1929 for (;;)
1930 {
1931 char_u *n = skiptowhite(arg);
1932
1933 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1934 break;
1935 arg = skipwhite(n);
1936 }
1937 }
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 xp->xp_pattern = arg;
1940}
1941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001943 * Return TRUE if "pat" matches "text".
1944 * Does not use 'cpo' and always uses 'magic'.
1945 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001946 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001947pattern_match(char_u *pat, char_u *text, int ic)
1948{
1949 int matches = FALSE;
1950 char_u *save_cpo;
1951 regmatch_T regmatch;
1952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001953 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001954 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001955 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001956 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1957 if (regmatch.regprog != NULL)
1958 {
1959 regmatch.rm_ic = ic;
1960 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1961 vim_regfree(regmatch.regprog);
1962 }
1963 p_cpo = save_cpo;
1964 return matches;
1965}
1966
1967/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001968 * Handle a name followed by "(". Both for just "name(arg)" and for
1969 * "expr->name(arg)".
1970 * Returns OK or FAIL.
1971 */
1972 static int
1973eval_func(
1974 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001975 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001976 char_u *name,
1977 int name_len,
1978 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001979 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001980 typval_T *basetv) // "expr" for "expr->name(arg)"
1981{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001982 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001983 char_u *s = name;
1984 int len = name_len;
1985 partial_T *partial;
1986 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001987 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001988
1989 if (!evaluate)
1990 check_vars(s, len);
1991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // If "s" is the name of a variable of type VAR_FUNC
1993 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001994 s = deref_func_name(s, &len, &partial,
1995 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001997 // Need to make a copy, in case evaluating the arguments makes
1998 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002000 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 ret = FAIL;
2002 else
2003 {
2004 funcexe_T funcexe;
2005
2006 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002007 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 funcexe.firstline = curwin->w_cursor.lnum;
2009 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010 funcexe.evaluate = evaluate;
2011 funcexe.partial = partial;
2012 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002013 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002014 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 }
2016 vim_free(s);
2017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002018 // If evaluate is FALSE rettv->v_type was not set in
2019 // get_func_tv, but it's needed in handle_subscript() to parse
2020 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2022 {
2023 rettv->vval.v_string = NULL;
2024 rettv->v_type = VAR_FUNC;
2025 }
2026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // Stop the expression evaluation when immediately
2028 // aborting on error, or when an interrupt occurred or
2029 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030 if (evaluate && aborting())
2031 {
2032 if (ret == OK)
2033 clear_tv(rettv);
2034 ret = FAIL;
2035 }
2036 return ret;
2037}
2038
2039/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002040 * Get the next line source line without advancing. But do skip over comment
2041 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002042 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002043 */
2044 static char_u *
2045getline_peek_skip_comments(evalarg_T *evalarg)
2046{
2047 for (;;)
2048 {
2049 char_u *next = getline_peek(evalarg->eval_getline,
2050 evalarg->eval_cookie);
2051 char_u *p;
2052
2053 if (next == NULL)
2054 break;
2055 p = skipwhite(next);
2056 if (*p != NUL && !vim9_comment_start(p))
2057 return next;
2058 (void)eval_next_line(evalarg);
2059 }
2060 return NULL;
2061}
2062
2063/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002064 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2065 * comment) and there is a next line, return the next line (skipping blanks)
2066 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002067 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2068 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002069 * "arg" must point somewhere inside a line, not at the start.
2070 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002071 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2073{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002074 char_u *p = skipwhite(arg);
2075
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002076 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002077 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002078 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002079 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002080 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002081 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002082 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002083
2084 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002085 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002086 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002087 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088
Bram Moolenaar9d489562020-07-30 20:08:50 +02002089 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 {
2091 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002092 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002093 }
2094 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002095 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096}
2097
2098/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002099 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002100 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002101 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002102 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002103eval_next_line(evalarg_T *evalarg)
2104{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002105 garray_T *gap = &evalarg->eval_ga;
2106 char_u *line;
2107
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002108 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002109 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2110 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002111 else
2112 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002113 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002114 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2115 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002116 char_u *p = skipwhite(line);
2117
2118 // Going to concatenate the lines after parsing. For an empty or
2119 // comment line use an empty string.
2120 if (*p == NUL || vim9_comment_start(p))
2121 {
2122 vim_free(line);
2123 line = vim_strsave((char_u *)"");
2124 }
2125
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002126 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2127 ++gap->ga_len;
2128 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002129 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002130 {
2131 vim_free(evalarg->eval_tofree);
2132 evalarg->eval_tofree = line;
2133 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002134
2135 // Advanced to the next line, "arg" no longer points into the previous
2136 // line.
2137 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2138
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002139 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002140}
2141
Bram Moolenaare6b53242020-07-01 17:28:33 +02002142/*
2143 * Call eval_next_non_blank() and get the next line if needed.
2144 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002145 char_u *
2146skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2147{
2148 int getnext;
2149 char_u *p = skipwhite(arg);
2150
Bram Moolenaar962d7212020-07-04 14:15:00 +02002151 if (evalarg == NULL)
2152 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002153 eval_next_non_blank(p, evalarg, &getnext);
2154 if (getnext)
2155 return eval_next_line(evalarg);
2156 return p;
2157}
2158
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002159/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002160 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002161 */
2162 void
2163clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2164{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002165 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002166 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002167 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002168 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002169 if (eap != NULL)
2170 {
2171 // We may need to keep the original command line, e.g. for
2172 // ":let" it has the variable names. But we may also need the
2173 // new one, "nextcmd" points into it. Keep both.
2174 vim_free(eap->cmdline_tofree);
2175 eap->cmdline_tofree = *eap->cmdlinep;
2176 *eap->cmdlinep = evalarg->eval_tofree;
2177 }
2178 else
2179 vim_free(evalarg->eval_tofree);
2180 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002181 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002182
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002183 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2184 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002185 }
2186}
2187
2188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2192 */
2193
2194/*
2195 * Handle zero level expression.
2196 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002198 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002199 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 * Return OK or FAIL.
2201 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203eval0(
2204 char_u *arg,
2205 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002206 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002207 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 int ret;
2210 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002211 int did_emsg_before = did_emsg;
2212 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002213 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002214 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002217 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002218 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002219
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002220 if (ret != FAIL)
2221 end_error = !ends_excmd2(arg, p);
2222 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /*
2227 * Report the invalid expression unless the expression evaluation has
2228 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002229 * exception, or we already gave a more specific error.
2230 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002232 if (!aborting()
2233 && did_emsg == did_emsg_before
2234 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002235 && (flags & EVAL_CONSTANT) == 0
2236 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002237 {
2238 if (end_error)
2239 semsg(_(e_trailing_arg), p);
2240 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002241 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002242 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002243
2244 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002245 // a next command to avoid more errors, unless "|" is following, which
2246 // could only be a command separator.
2247 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2248 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002251
2252 if (eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002253 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 return ret;
2256}
2257
2258/*
2259 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002260 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002261 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 *
2263 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002264 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002266 * Note: "rettv.v_lock" is not set.
2267 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 * Return OK or FAIL.
2269 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002270 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002273 char_u *p;
2274 int getnext;
2275
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002276 CLEAR_POINTER(rettv);
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 /*
2279 * Get the first variable.
2280 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002281 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return FAIL;
2283
Bram Moolenaar793648f2020-06-26 21:28:25 +02002284 p = eval_next_non_blank(*arg, evalarg, &getnext);
2285 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002287 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288 int result;
2289 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002290 evalarg_T *evalarg_used = evalarg;
2291 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002292 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002293 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002294 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295
2296 if (evalarg == NULL)
2297 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298 CLEAR_FIELD(local_evalarg);
2299 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002301 orig_flags = evalarg_used->eval_flags;
2302 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 if (getnext)
2305 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002307 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002308 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002309 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002310 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002311 clear_tv(rettv);
2312 return FAIL;
2313 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002314 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002315 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 int error = FALSE;
2321
Bram Moolenaar13106602020-10-04 16:06:05 +02002322 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002323 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002324 else if (vim9script)
2325 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002326 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002328 if (error || !op_falsy || !result)
2329 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 if (error)
2331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
2334 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002335 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002337 if (op_falsy)
2338 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002339 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002340 {
mityu4ac198c2021-05-28 17:52:40 +02002341 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002342 clear_tv(rettv);
2343 return FAIL;
2344 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002345 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002346 evalarg_used->eval_flags = (op_falsy ? !result : result)
2347 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2348 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002349 {
2350 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002352 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002353 if (!op_falsy || !result)
2354 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002356 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 /*
2359 * Check for the ":".
2360 */
2361 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2362 if (*p != ':')
2363 {
2364 emsg(_(e_missing_colon));
2365 if (evaluate && result)
2366 clear_tv(rettv);
2367 evalarg_used->eval_flags = orig_flags;
2368 return FAIL;
2369 }
2370 if (getnext)
2371 *arg = eval_next_line(evalarg_used);
2372 else
2373 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002374 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002375 {
2376 error_white_both(p, 1);
2377 clear_tv(rettv);
2378 evalarg_used->eval_flags = orig_flags;
2379 return FAIL;
2380 }
2381 *arg = p;
2382 }
2383
2384 /*
2385 * Get the third variable. Recursive!
2386 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002387 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002388 {
mityu4ac198c2021-05-28 17:52:40 +02002389 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002390 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002391 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002392 return FAIL;
2393 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002394 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2395 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002397 if (eval1(arg, &var2, evalarg_used) == FAIL)
2398 {
2399 if (evaluate && result)
2400 clear_tv(rettv);
2401 evalarg_used->eval_flags = orig_flags;
2402 return FAIL;
2403 }
2404 if (evaluate && !result)
2405 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002407
2408 if (evalarg == NULL)
2409 clear_evalarg(&local_evalarg, NULL);
2410 else
2411 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413
2414 return OK;
2415}
2416
2417/*
2418 * Handle first level expression:
2419 * expr2 || expr2 || expr2 logical OR
2420 *
2421 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002422 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 *
2424 * Return OK or FAIL.
2425 */
2426 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002429 char_u *p;
2430 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 /*
2433 * Get the first variable.
2434 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 return FAIL;
2437
2438 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002441 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 evalarg_T *evalarg_used = evalarg;
2445 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002446 int evaluate;
2447 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 long result = FALSE;
2449 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002450 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002451 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002452
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 if (evalarg == NULL)
2454 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 CLEAR_FIELD(local_evalarg);
2456 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002458 orig_flags = evalarg_used->eval_flags;
2459 evaluate = orig_flags & EVAL_EVALUATE;
2460 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002462 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002463 result = tv_get_bool_chk(rettv, &error);
2464 else if (tv_get_number_chk(rettv, &error) != 0)
2465 result = TRUE;
2466 clear_tv(rettv);
2467 if (error)
2468 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470
2471 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 while (p[0] == '|' && p[1] == '|')
2475 {
2476 if (getnext)
2477 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002478 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002479 {
2480 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2481 {
2482 error_white_both(p, 2);
2483 clear_tv(rettv);
2484 return FAIL;
2485 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002486 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488
2489 /*
2490 * Get the second variable.
2491 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002492 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2493 {
mityu4ac198c2021-05-28 17:52:40 +02002494 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002495 clear_tv(rettv);
2496 return FAIL;
2497 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2499 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002500 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002503
2504 /*
2505 * Compute the result.
2506 */
2507 if (evaluate && !result)
2508 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002509 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002510 result = tv_get_bool_chk(&var2, &error);
2511 else if (tv_get_number_chk(&var2, &error) != 0)
2512 result = TRUE;
2513 clear_tv(&var2);
2514 if (error)
2515 return FAIL;
2516 }
2517 if (evaluate)
2518 {
2519 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002520 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002521 rettv->v_type = VAR_BOOL;
2522 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002523 }
2524 else
2525 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002526 rettv->v_type = VAR_NUMBER;
2527 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002528 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530
2531 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002533
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534 if (evalarg == NULL)
2535 clear_evalarg(&local_evalarg, NULL);
2536 else
2537 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
2540 return OK;
2541}
2542
2543/*
2544 * Handle second level expression:
2545 * expr3 && expr3 && expr3 logical AND
2546 *
2547 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 *
2550 * Return OK or FAIL.
2551 */
2552 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002555 char_u *p;
2556 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
2559 * Get the first variable.
2560 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return FAIL;
2563
2564 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 evalarg_T *evalarg_used = evalarg;
2571 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572 int orig_flags;
2573 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 long result = TRUE;
2575 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002577 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002578
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 if (evalarg == NULL)
2580 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 CLEAR_FIELD(local_evalarg);
2582 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 orig_flags = evalarg_used->eval_flags;
2585 evaluate = orig_flags & EVAL_EVALUATE;
2586 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002588 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002589 result = tv_get_bool_chk(rettv, &error);
2590 else if (tv_get_number_chk(rettv, &error) == 0)
2591 result = FALSE;
2592 clear_tv(rettv);
2593 if (error)
2594 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
2597 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 while (p[0] == '&' && p[1] == '&')
2601 {
2602 if (getnext)
2603 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002604 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002606 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002607 {
2608 error_white_both(p, 2);
2609 clear_tv(rettv);
2610 return FAIL;
2611 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002612 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614
2615 /*
2616 * Get the second variable.
2617 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002618 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2619 {
mityu4ac198c2021-05-28 17:52:40 +02002620 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2625 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002627 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002629 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630
2631 /*
2632 * Compute the result.
2633 */
2634 if (evaluate && result)
2635 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002636 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002637 result = tv_get_bool_chk(&var2, &error);
2638 else if (tv_get_number_chk(&var2, &error) == 0)
2639 result = FALSE;
2640 clear_tv(&var2);
2641 if (error)
2642 return FAIL;
2643 }
2644 if (evaluate)
2645 {
2646 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002647 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002648 rettv->v_type = VAR_BOOL;
2649 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002650 }
2651 else
2652 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002653 rettv->v_type = VAR_NUMBER;
2654 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657
2658 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002660
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 if (evalarg == NULL)
2662 clear_evalarg(&local_evalarg, NULL);
2663 else
2664 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666
2667 return OK;
2668}
2669
2670/*
2671 * Handle third level expression:
2672 * var1 == var2
2673 * var1 =~ var2
2674 * var1 != var2
2675 * var1 !~ var2
2676 * var1 > var2
2677 * var1 >= var2
2678 * var1 < var2
2679 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002680 * var1 is var2
2681 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 *
2683 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002684 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 *
2686 * Return OK or FAIL.
2687 */
2688 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002692 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002693 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002695 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 /*
2698 * Get the first variable.
2699 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return FAIL;
2702
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002703 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002704 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002707 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002709 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002711 typval_T var2;
2712 int ic;
2713 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002714 int evaluate = evalarg == NULL
2715 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002716
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002717 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002718 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002719 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002720 p = *arg;
2721 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002722 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2723 {
mityu4ac198c2021-05-28 17:52:40 +02002724 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002725 clear_tv(rettv);
2726 return FAIL;
2727 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002728
Bram Moolenaar696ba232020-07-29 21:20:41 +02002729 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2730 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002731 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002732 clear_tv(rettv);
2733 return FAIL;
2734 }
2735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002736 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (p[len] == '?')
2738 {
2739 ic = TRUE;
2740 ++len;
2741 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002742 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 else if (p[len] == '#')
2744 {
2745 ic = FALSE;
2746 ++len;
2747 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002748 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002750 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 /*
2753 * Get the second variable.
2754 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002755 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2756 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002757 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002758 clear_tv(rettv);
2759 return FAIL;
2760 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002761 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002764 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 return FAIL;
2766 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002767 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002768 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002769 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002770
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002771 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002772 {
2773 ret = FAIL;
2774 clear_tv(rettv);
2775 }
2776 else
2777 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002778 clear_tv(&var2);
2779 return ret;
2780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782
2783 return OK;
2784}
2785
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002786/*
2787 * Make a copy of blob "tv1" and append blob "tv2".
2788 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 void
2790eval_addblob(typval_T *tv1, typval_T *tv2)
2791{
2792 blob_T *b1 = tv1->vval.v_blob;
2793 blob_T *b2 = tv2->vval.v_blob;
2794 blob_T *b = blob_alloc();
2795 int i;
2796
2797 if (b != NULL)
2798 {
2799 for (i = 0; i < blob_len(b1); i++)
2800 ga_append(&b->bv_ga, blob_get(b1, i));
2801 for (i = 0; i < blob_len(b2); i++)
2802 ga_append(&b->bv_ga, blob_get(b2, i));
2803
2804 clear_tv(tv1);
2805 rettv_blob_set(tv1, b);
2806 }
2807}
2808
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002809/*
2810 * Make a copy of list "tv1" and append list "tv2".
2811 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 int
2813eval_addlist(typval_T *tv1, typval_T *tv2)
2814{
2815 typval_T var3;
2816
2817 // concatenate Lists
2818 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2819 {
2820 clear_tv(tv1);
2821 clear_tv(tv2);
2822 return FAIL;
2823 }
2824 clear_tv(tv1);
2825 *tv1 = var3;
2826 return OK;
2827}
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829/*
2830 * Handle fourth level expression:
2831 * + number addition
2832 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002833 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002834 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 *
2836 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002837 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 *
2839 * Return OK or FAIL.
2840 */
2841 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002842eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 /*
2845 * Get the first variable.
2846 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 return FAIL;
2849
2850 /*
2851 * Repeat computing, until no '+', '-' or '.' is following.
2852 */
2853 for (;;)
2854 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002855 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 int getnext;
2857 char_u *p;
2858 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002859 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002860 int concat;
2861 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002862 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002864 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002865 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002866 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 p = eval_next_non_blank(*arg, evalarg, &getnext);
2868 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002869 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002870 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2871 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002873 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2874 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002875
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002876 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002877 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002878 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002879 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002880 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002881 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002882 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002883 {
mityu4ac198c2021-05-28 17:52:40 +02002884 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002885 clear_tv(rettv);
2886 return FAIL;
2887 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002888 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002890 if ((op != '+' || (rettv->v_type != VAR_LIST
2891 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002892#ifdef FEAT_FLOAT
2893 && (op == '.' || rettv->v_type != VAR_FLOAT)
2894#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002895 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002896 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002897 int error = FALSE;
2898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002899 // For "list + ...", an illegal use of the first operand as
2900 // a number cannot be determined before evaluating the 2nd
2901 // operand: if this is also a list, all is ok.
2902 // For "something . ...", "something - ..." or "non-list + ...",
2903 // we know that the first operand needs to be a string or number
2904 // without evaluating the 2nd operand. So check before to avoid
2905 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002906 if (op != '.')
2907 tv_get_number_chk(rettv, &error);
2908 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002909 {
2910 clear_tv(rettv);
2911 return FAIL;
2912 }
2913 }
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /*
2916 * Get the second variable.
2917 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002918 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002919 {
mityu89dcb4d2021-05-28 13:50:17 +02002920 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002921 clear_tv(rettv);
2922 return FAIL;
2923 }
2924 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002925 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 return FAIL;
2929 }
2930
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
2933 /*
2934 * Compute the result.
2935 */
2936 if (op == '.')
2937 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002938 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2939 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002940 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002941
Bram Moolenaar2e086612020-08-25 22:37:48 +02002942 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002943 || var2.v_type == VAR_CHANNEL
2944 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002945 semsg(_(e_using_invalid_value_as_string_str),
2946 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002947#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002948 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002949 {
2950 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2951 var2.vval.v_float);
2952 s2 = buf2;
2953 }
2954#endif
2955 else
2956 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002957 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002958 {
2959 clear_tv(rettv);
2960 clear_tv(&var2);
2961 return FAIL;
2962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 clear_tv(rettv);
2965 rettv->v_type = VAR_STRING;
2966 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002968 else if (op == '+' && rettv->v_type == VAR_BLOB
2969 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002971 else if (op == '+' && rettv->v_type == VAR_LIST
2972 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002973 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002975 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else
2978 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002979 int error = FALSE;
2980 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 float_T f1 = 0, f2 = 0;
2983
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 f1 = rettv->vval.v_float;
2987 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002988 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002989 else
2990#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002993 if (error)
2994 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002995 // This can only happen for "list + non-list" or
2996 // "blob + non-blob". For "non-list + ..." or
2997 // "something - ...", we returned before evaluating the
2998 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003000 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003001 return FAIL;
3002 }
3003#ifdef FEAT_FLOAT
3004 if (var2.v_type == VAR_FLOAT)
3005 f1 = n1;
3006#endif
3007 }
3008#ifdef FEAT_FLOAT
3009 if (var2.v_type == VAR_FLOAT)
3010 {
3011 f2 = var2.vval.v_float;
3012 n2 = 0;
3013 }
3014 else
3015#endif
3016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003017 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018 if (error)
3019 {
3020 clear_tv(rettv);
3021 clear_tv(&var2);
3022 return FAIL;
3023 }
3024#ifdef FEAT_FLOAT
3025 if (rettv->v_type == VAR_FLOAT)
3026 f2 = n2;
3027#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003029 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003030
3031#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003032 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3034 {
3035 if (op == '+')
3036 f1 = f1 + f2;
3037 else
3038 f1 = f1 - f2;
3039 rettv->v_type = VAR_FLOAT;
3040 rettv->vval.v_float = f1;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043#endif
3044 {
3045 if (op == '+')
3046 n1 = n1 + n2;
3047 else
3048 n1 = n1 - n2;
3049 rettv->v_type = VAR_NUMBER;
3050 rettv->vval.v_number = n1;
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 }
3056 return OK;
3057}
3058
3059/*
3060 * Handle fifth level expression:
3061 * * number multiplication
3062 * / number division
3063 * % number modulo
3064 *
3065 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003066 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 *
3068 * Return OK or FAIL.
3069 */
3070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003071eval6(
3072 char_u **arg,
3073 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003074 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003078 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 /*
3082 * Get the first variable.
3083 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003084 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return FAIL;
3086
3087 /*
3088 * Repeat computing, until no '*', '/' or '%' is following.
3089 */
3090 for (;;)
3091 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003092 int evaluate;
3093 int getnext;
3094 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003095 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003096 int op;
3097 varnumber_T n1, n2;
3098#ifdef FEAT_FLOAT
3099 float_T f1, f2;
3100#endif
3101 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003102
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003103 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003104 p = eval_next_non_blank(*arg, evalarg, &getnext);
3105 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003106 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003109 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003111 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003112 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003113 {
3114 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3115 {
mityu4ac198c2021-05-28 17:52:40 +02003116 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003117 clear_tv(rettv);
3118 return FAIL;
3119 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003120 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003123#ifdef FEAT_FLOAT
3124 f1 = 0;
3125 f2 = 0;
3126#endif
3127 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003128 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#ifdef FEAT_FLOAT
3131 if (rettv->v_type == VAR_FLOAT)
3132 {
3133 f1 = rettv->vval.v_float;
3134 use_float = TRUE;
3135 n1 = 0;
3136 }
3137 else
3138#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003139 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003140 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003141 if (error)
3142 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144 else
3145 n1 = 0;
3146
3147 /*
3148 * Get the second variable.
3149 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003150 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3151 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003152 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003153 clear_tv(rettv);
3154 return FAIL;
3155 }
3156 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003157 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return FAIL;
3159
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003160 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162#ifdef FEAT_FLOAT
3163 if (var2.v_type == VAR_FLOAT)
3164 {
3165 if (!use_float)
3166 {
3167 f1 = n1;
3168 use_float = TRUE;
3169 }
3170 f2 = var2.vval.v_float;
3171 n2 = 0;
3172 }
3173 else
3174#endif
3175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003176 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 clear_tv(&var2);
3178 if (error)
3179 return FAIL;
3180#ifdef FEAT_FLOAT
3181 if (use_float)
3182 f2 = n2;
3183#endif
3184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 /*
3187 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190#ifdef FEAT_FLOAT
3191 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193 if (op == '*')
3194 f1 = f1 * f2;
3195 else if (op == '/')
3196 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003197# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003199 if (f2 == 0.0)
3200 {
3201 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003202 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003203 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003204 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003205 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003206 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003207 }
3208 else
3209 f1 = f1 / f2;
3210# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003211 // We rely on the floating point library to handle divide
3212 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003213 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003214# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003217 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003219 return FAIL;
3220 }
3221 rettv->v_type = VAR_FLOAT;
3222 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003227 int failed = FALSE;
3228
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003229 if (op == '*')
3230 n1 = n1 * n2;
3231 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003232 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003234 n1 = num_modulus(n1, n2, &failed);
3235 if (failed)
3236 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003238 rettv->v_type = VAR_NUMBER;
3239 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 }
3243
3244 return OK;
3245}
3246
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003247/*
3248 * Handle a type cast before a base level expression.
3249 * "arg" must point to the first non-white of the expression.
3250 * "arg" is advanced to just after the recognized expression.
3251 * Return OK or FAIL.
3252 */
3253 static int
3254eval7t(
3255 char_u **arg,
3256 typval_T *rettv,
3257 evalarg_T *evalarg,
3258 int want_string) // after "." operator
3259{
3260 type_T *want_type = NULL;
3261 garray_T type_list; // list of pointers to allocated types
3262 int res;
3263 int evaluate = evalarg == NULL ? 0
3264 : (evalarg->eval_flags & EVAL_EVALUATE);
3265
3266 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003267 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3268 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003269 {
3270 ++*arg;
3271 ga_init2(&type_list, sizeof(type_T *), 10);
3272 want_type = parse_type(arg, &type_list, TRUE);
3273 if (want_type == NULL && (evaluate || **arg != '>'))
3274 {
3275 clear_type_list(&type_list);
3276 return FAIL;
3277 }
3278
3279 if (**arg != '>')
3280 {
3281 if (*skipwhite(*arg) == '>')
3282 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3283 else
3284 emsg(_(e_missing_gt));
3285 clear_type_list(&type_list);
3286 return FAIL;
3287 }
3288 ++*arg;
3289 *arg = skipwhite_and_linebreak(*arg, evalarg);
3290 }
3291
3292 res = eval7(arg, rettv, evalarg, want_string);
3293
3294 if (want_type != NULL && evaluate)
3295 {
3296 if (res == OK)
3297 {
3298 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3299
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003300 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003301 {
3302 if (want_type == &t_bool && actual != &t_bool
3303 && (actual->tt_flags & TTFLAG_BOOL_OK))
3304 {
3305 int n = tv2bool(rettv);
3306
3307 // can use "0" and "1" for boolean in some places
3308 clear_tv(rettv);
3309 rettv->v_type = VAR_BOOL;
3310 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3311 }
3312 else
3313 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003314 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003315
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003316 where.wt_variable = TRUE;
3317 res = check_type(want_type, actual, TRUE, where);
3318 }
3319 }
3320 }
3321 clear_type_list(&type_list);
3322 }
3323
3324 return res;
3325}
3326
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003327 int
3328eval_leader(char_u **arg, int vim9)
3329{
3330 char_u *s = *arg;
3331 char_u *p = *arg;
3332
3333 while (*p == '!' || *p == '-' || *p == '+')
3334 {
3335 char_u *n = skipwhite(p + 1);
3336
3337 // ++, --, -+ and +- are not accepted in Vim9 script
3338 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3339 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003340 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003341 return FAIL;
3342 }
3343 p = n;
3344 }
3345 *arg = p;
3346 return OK;
3347}
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349/*
3350 * Handle sixth level expression:
3351 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003352 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003353 * "string" string constant
3354 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 * &option-name option value
3356 * @r register contents
3357 * identifier variable value
3358 * function() function call
3359 * $VAR environment variable
3360 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003361 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003363 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003364 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 *
3366 * Also handle:
3367 * ! in front logical NOT
3368 * - in front unary minus
3369 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 * trailing [] subscript in String or List
3371 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003372 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 *
3374 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003375 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 *
3377 * Return OK or FAIL.
3378 */
3379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003380eval7(
3381 char_u **arg,
3382 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003383 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003386 int evaluate = evalarg != NULL
3387 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int len;
3389 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 char_u *start_leader, *end_leader;
3391 int ret = OK;
3392 char_u *alias;
3393
3394 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003396 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003401 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003404 if (eval_leader(arg, in_vim9script()) == FAIL)
3405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 end_leader = *arg;
3407
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003408 if (**arg == '.' && (!isdigit(*(*arg + 1))
3409#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003410 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003411#endif
3412 ))
3413 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003414 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003415 ++*arg;
3416 return FAIL;
3417 }
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 switch (**arg)
3420 {
3421 /*
3422 * Number constant.
3423 */
3424 case '0':
3425 case '1':
3426 case '2':
3427 case '3':
3428 case '4':
3429 case '5':
3430 case '6':
3431 case '7':
3432 case '8':
3433 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003435
3436 // Apply prefixed "-" and "+" now. Matters especially when
3437 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003438 if (ret == OK && evaluate && end_leader > start_leader
3439 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003440 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
3442
3443 /*
3444 * String constant: "string".
3445 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003446 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 break;
3448
3449 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003450 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003452 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 break;
3454
3455 /*
3456 * List: [expr, expr]
3457 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003458 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 break;
3460
3461 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003462 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003463 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003464 case '#': if (in_vim9script())
3465 {
3466 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3467 }
3468 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003469 {
3470 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003471 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003472 }
3473 else
3474 ret = NOTDONE;
3475 break;
3476
3477 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003478 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003479 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003481 case '{': if (in_vim9script())
3482 ret = NOTDONE;
3483 else
3484 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003485 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003486 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003487 break;
3488
3489 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003490 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 break;
3494
3495 /*
3496 * Environment variable: $VAR.
3497 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003498 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
3501 /*
3502 * Register contents: @r.
3503 */
3504 case '@': ++*arg;
3505 if (evaluate)
3506 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003507 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3508 semsg(_(e_syntax_error_at_str), *arg);
3509 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3510 emsg_invreg(**arg);
3511 else
3512 {
3513 rettv->v_type = VAR_STRING;
3514 rettv->vval.v_string = get_reg_contents(**arg,
3515 GREG_EXPR_SRC);
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518 if (**arg != NUL)
3519 ++*arg;
3520 break;
3521
3522 /*
3523 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003524 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003526 case '(': ret = NOTDONE;
3527 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003528 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003529 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003530 if (ret == OK && evaluate)
3531 {
3532 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3533
Bram Moolenaara9931532021-06-12 15:58:16 +02003534 // Compile it here to get the return type. The return
3535 // type is optional, when it's missing use t_unknown.
3536 // This is recognized in compile_return().
3537 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3538 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003539 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003540 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003541 {
3542 clear_tv(rettv);
3543 ret = FAIL;
3544 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003545 }
3546 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003547 if (ret == NOTDONE)
3548 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003549 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003550 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003551
Bram Moolenaar9215f012020-06-27 21:18:00 +02003552 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003553 if (**arg == ')')
3554 ++*arg;
3555 else if (ret == OK)
3556 {
3557 emsg(_(e_missing_close));
3558 clear_tv(rettv);
3559 ret = FAIL;
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562 break;
3563
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003567
3568 if (ret == NOTDONE)
3569 {
3570 /*
3571 * Must be a variable or function name.
3572 * Can also be a curly-braces kind of name: {expr}.
3573 */
3574 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003575 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 if (alias != NULL)
3577 s = alias;
3578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003579 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 ret = FAIL;
3581 else
3582 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003583 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3584
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003585 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003586 {
3587 emsg(_(e_cannot_use_underscore_here));
3588 ret = FAIL;
3589 }
3590 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003591 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003592 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003593 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003594 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003595 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003596 else if (flags & EVAL_CONSTANT)
3597 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003599 {
3600 // get the value of "true", "false" or a variable
3601 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3602 {
3603 rettv->v_type = VAR_BOOL;
3604 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003605 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003606 }
3607 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003608 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003609 {
3610 rettv->v_type = VAR_BOOL;
3611 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003612 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003613 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003614 else if (len == 4 && in_vim9script()
3615 && STRNCMP(s, "null", 4) == 0)
3616 {
3617 rettv->v_type = VAR_SPECIAL;
3618 rettv->vval.v_number = VVAL_NULL;
3619 ret = OK;
3620 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003621 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003622 ret = eval_variable(s, len, rettv, NULL,
3623 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003624 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003625 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003626 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003627 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003628 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003629 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003632 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 }
3634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003635 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3636 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003637 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003638 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 /*
3641 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3642 */
3643 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003644 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003645 return ret;
3646}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003647
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003648/*
3649 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003650 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003651 * Adjusts "end_leaderp" until it is at "start_leader".
3652 */
3653 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003654eval7_leader(
3655 typval_T *rettv,
3656 int numeric_only,
3657 char_u *start_leader,
3658 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003659{
3660 char_u *end_leader = *end_leaderp;
3661 int ret = OK;
3662 int error = FALSE;
3663 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003664 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003665#ifdef FEAT_FLOAT
3666 float_T f = 0.0;
3667
3668 if (rettv->v_type == VAR_FLOAT)
3669 f = rettv->vval.v_float;
3670 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003671#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003672 {
3673 while (VIM_ISWHITE(end_leader[-1]))
3674 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003675 if (in_vim9script() && end_leader[-1] == '!')
3676 val = tv2bool(rettv);
3677 else
3678 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003679 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003680 if (error)
3681 {
3682 clear_tv(rettv);
3683 ret = FAIL;
3684 }
3685 else
3686 {
3687 while (end_leader > start_leader)
3688 {
3689 --end_leader;
3690 if (*end_leader == '!')
3691 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003692 if (numeric_only)
3693 {
3694 ++end_leader;
3695 break;
3696 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003697#ifdef FEAT_FLOAT
3698 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003699 {
3700 if (in_vim9script())
3701 {
3702 rettv->v_type = VAR_BOOL;
3703 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3704 }
3705 else
3706 f = !f;
3707 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003708 else
3709#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003710 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003711 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003712 type = VAR_BOOL;
3713 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003714 }
3715 else if (*end_leader == '-')
3716 {
3717#ifdef FEAT_FLOAT
3718 if (rettv->v_type == VAR_FLOAT)
3719 f = -f;
3720 else
3721#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003722 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003723 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003724 type = VAR_NUMBER;
3725 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003726 }
3727 }
3728#ifdef FEAT_FLOAT
3729 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003732 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003735#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003738 if (in_vim9script())
3739 rettv->v_type = type;
3740 else
3741 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return ret;
3747}
3748
3749/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003750 * Call the function referred to in "rettv".
3751 */
3752 static int
3753call_func_rettv(
3754 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003755 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003756 typval_T *rettv,
3757 int evaluate,
3758 dict_T *selfdict,
3759 typval_T *basetv)
3760{
3761 partial_T *pt = NULL;
3762 funcexe_T funcexe;
3763 typval_T functv;
3764 char_u *s;
3765 int ret;
3766
3767 // need to copy the funcref so that we can clear rettv
3768 if (evaluate)
3769 {
3770 functv = *rettv;
3771 rettv->v_type = VAR_UNKNOWN;
3772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003773 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003774 if (functv.v_type == VAR_PARTIAL)
3775 {
3776 pt = functv.vval.v_partial;
3777 s = partial_name(pt);
3778 }
3779 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003780 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003781 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003782 if (s == NULL || *s == NUL)
3783 {
3784 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003785 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003786 goto theend;
3787 }
3788 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003789 }
3790 else
3791 s = (char_u *)"";
3792
Bram Moolenaara80faa82020-04-12 19:37:17 +02003793 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003794 funcexe.firstline = curwin->w_cursor.lnum;
3795 funcexe.lastline = curwin->w_cursor.lnum;
3796 funcexe.evaluate = evaluate;
3797 funcexe.partial = pt;
3798 funcexe.selfdict = selfdict;
3799 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003800 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003801
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003802theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003803 // Clear the funcref afterwards, so that deleting it while
3804 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003805 if (evaluate)
3806 clear_tv(&functv);
3807
3808 return ret;
3809}
3810
3811/*
3812 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003813 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3815 */
3816 static int
3817eval_lambda(
3818 char_u **arg,
3819 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003820 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003822{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003823 int evaluate = evalarg != NULL
3824 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003825 typval_T base = *rettv;
3826 int ret;
3827
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003828 rettv->v_type = VAR_UNKNOWN;
3829
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003830 if (**arg == '{')
3831 {
3832 // ->{lambda}()
3833 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3834 }
3835 else
3836 {
3837 // ->(lambda)()
3838 ++*arg;
3839 ret = eval1(arg, rettv, evalarg);
3840 *arg = skipwhite_and_linebreak(*arg, evalarg);
3841 if (**arg != ')')
3842 {
3843 emsg(_(e_missing_close));
3844 ret = FAIL;
3845 }
3846 ++*arg;
3847 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003848 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003849 return FAIL;
3850 else if (**arg != '(')
3851 {
3852 if (verbose)
3853 {
3854 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003855 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003856 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003858 }
3859 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003860 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003861 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003862 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003863 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003864
3865 // Clear the funcref afterwards, so that deleting it while
3866 // evaluating the arguments is possible (see test55).
3867 if (evaluate)
3868 clear_tv(&base);
3869
3870 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871}
3872
3873/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003874 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003875 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003876 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3877 */
3878 static int
3879eval_method(
3880 char_u **arg,
3881 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003882 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003883 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003884{
3885 char_u *name;
3886 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003887 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003888 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003889 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003890 int evaluate = evalarg != NULL
3891 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003892
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003893 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003894
Bram Moolenaarac92e252019-08-03 21:58:38 +02003895 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003896 len = get_name_len(arg, &alias, evaluate, TRUE);
3897 if (alias != NULL)
3898 name = alias;
3899
3900 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901 {
3902 if (verbose)
3903 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003904 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003907 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003908 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003909 if (**arg != '(')
3910 {
3911 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003913 ret = FAIL;
3914 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003915 else if (VIM_ISWHITE((*arg)[-1]))
3916 {
3917 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003918 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003919 ret = FAIL;
3920 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003921 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003922 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003923 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003924 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003925
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003926 // Clear the funcref afterwards, so that deleting it while
3927 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003928 if (evaluate)
3929 clear_tv(&base);
3930
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 return ret;
3932}
3933
3934/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003935 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3936 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003937 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3938 */
3939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940eval_index(
3941 char_u **arg,
3942 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003943 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003945{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003946 int evaluate = evalarg != NULL
3947 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003948 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003950 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003951 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003952 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003953 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003954
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003955 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3956 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003957
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003958 init_tv(&var1);
3959 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003960 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 /*
3963 * dict.name
3964 */
3965 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003966 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003969 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003970 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003971 }
3972 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 /*
3975 * something[idx]
3976 *
3977 * Get the (first) variable from inside the [].
3978 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003979 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 if (**arg == ':')
3981 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003982 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003983 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003984 else if (vim9 && **arg == ':')
3985 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003986 semsg(_(e_white_space_required_before_and_after_str_at_str),
3987 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003988 clear_tv(&var1);
3989 return FAIL;
3990 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003991 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003993#ifdef FEAT_FLOAT
3994 // allow for indexing with float
3995 if (vim9 && rettv->v_type == VAR_DICT
3996 && var1.v_type == VAR_FLOAT)
3997 {
3998 var1.vval.v_string = typval_tostring(&var1, TRUE);
3999 var1.v_type = VAR_STRING;
4000 }
4001#endif
4002 if (tv_get_string_chk(&var1) == NULL)
4003 {
4004 // not a number or string
4005 clear_tv(&var1);
4006 return FAIL;
4007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009
4010 /*
4011 * Get the second variable from inside the [:].
4012 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004013 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004014 if (**arg == ':')
4015 {
4016 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004017 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004018 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004019 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004020 semsg(_(e_white_space_required_before_and_after_str_at_str),
4021 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004022 if (!empty1)
4023 clear_tv(&var1);
4024 return FAIL;
4025 }
4026 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 if (**arg == ']')
4028 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004029 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 if (!empty1)
4032 clear_tv(&var1);
4033 return FAIL;
4034 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004035 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 if (!empty1)
4039 clear_tv(&var1);
4040 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 return FAIL;
4042 }
4043 }
4044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004046 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 if (**arg != ']')
4048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004049 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004050 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004051 clear_tv(&var1);
4052 if (range)
4053 clear_tv(&var2);
4054 return FAIL;
4055 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004056 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004057 }
4058
4059 if (evaluate)
4060 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004061 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004062 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004063 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004064
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004067 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004068 clear_tv(&var2);
4069 return res;
4070 }
4071 return OK;
4072}
4073
4074/*
4075 * Check if "rettv" can have an [index] or [sli:ce]
4076 */
4077 int
4078check_can_index(typval_T *rettv, int evaluate, int verbose)
4079{
4080 switch (rettv->v_type)
4081 {
4082 case VAR_FUNC:
4083 case VAR_PARTIAL:
4084 if (verbose)
4085 emsg(_("E695: Cannot index a Funcref"));
4086 return FAIL;
4087 case VAR_FLOAT:
4088#ifdef FEAT_FLOAT
4089 if (verbose)
4090 emsg(_(e_float_as_string));
4091 return FAIL;
4092#endif
4093 case VAR_BOOL:
4094 case VAR_SPECIAL:
4095 case VAR_JOB:
4096 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004097 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004098 if (verbose)
4099 emsg(_(e_cannot_index_special_variable));
4100 return FAIL;
4101 case VAR_UNKNOWN:
4102 case VAR_ANY:
4103 case VAR_VOID:
4104 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 emsg(_(e_cannot_index_special_variable));
4107 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004108 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004110
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 case VAR_STRING:
4112 case VAR_LIST:
4113 case VAR_DICT:
4114 case VAR_BLOB:
4115 break;
4116 case VAR_NUMBER:
4117 if (in_vim9script())
4118 emsg(_(e_cannot_index_number));
4119 break;
4120 }
4121 return OK;
4122}
4123
4124/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004125 * slice() function
4126 */
4127 void
4128f_slice(typval_T *argvars, typval_T *rettv)
4129{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004130 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004131 && ((argvars[0].v_type != VAR_STRING
4132 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004133 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004134 && check_for_list_arg(argvars, 0) == FAIL)
4135 || check_for_number_arg(argvars, 1) == FAIL
4136 || check_for_opt_number_arg(argvars, 2) == FAIL))
4137 return;
4138
Bram Moolenaar6601b622021-01-13 21:47:15 +01004139 if (check_can_index(argvars, TRUE, FALSE) == OK)
4140 {
4141 copy_tv(argvars, rettv);
4142 eval_index_inner(rettv, TRUE, argvars + 1,
4143 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4144 TRUE, NULL, 0, FALSE);
4145 }
4146}
4147
4148/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 * Apply index or range to "rettv".
4150 * "var1" is the first index, NULL for [:expr].
4151 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004152 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4153 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4155 */
4156 int
4157eval_index_inner(
4158 typval_T *rettv,
4159 int is_range,
4160 typval_T *var1,
4161 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004163 char_u *key,
4164 int keylen,
4165 int verbose)
4166{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004167 varnumber_T n1, n2 = 0;
4168 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169
4170 n1 = 0;
4171 if (var1 != NULL && rettv->v_type != VAR_DICT)
4172 n1 = tv_get_number(var1);
4173
4174 if (is_range)
4175 {
4176 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 if (verbose)
4179 emsg(_(e_cannot_slice_dictionary));
4180 return FAIL;
4181 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004182 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004184 else
4185 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004186 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004187
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004188 switch (rettv->v_type)
4189 {
4190 case VAR_UNKNOWN:
4191 case VAR_ANY:
4192 case VAR_VOID:
4193 case VAR_FUNC:
4194 case VAR_PARTIAL:
4195 case VAR_FLOAT:
4196 case VAR_BOOL:
4197 case VAR_SPECIAL:
4198 case VAR_JOB:
4199 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004200 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004201 break; // not evaluating, skipping over subscript
4202
4203 case VAR_NUMBER:
4204 case VAR_STRING:
4205 {
4206 char_u *s = tv_get_string(rettv);
4207
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004209 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004210 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004211 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004212 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004213 else
4214 s = char_from_string(s, n1);
4215 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004216 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 // The resulting variable is a substring. If the indexes
4219 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004220 if (n1 < 0)
4221 {
4222 n1 = len + n1;
4223 if (n1 < 0)
4224 n1 = 0;
4225 }
4226 if (n2 < 0)
4227 n2 = len + n2;
4228 else if (n2 >= len)
4229 n2 = len;
4230 if (n1 >= len || n2 < 0 || n1 > n2)
4231 s = NULL;
4232 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004233 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 }
4235 else
4236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004237 // The resulting variable is a string of a single
4238 // character. If the index is too big or negative the
4239 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004240 if (n1 >= len || n1 < 0)
4241 s = NULL;
4242 else
4243 s = vim_strnsave(s + n1, 1);
4244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
4246 rettv->v_type = VAR_STRING;
4247 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004248 }
4249 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004250
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004251 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004252 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4253 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004254 break;
4255
4256 case VAR_LIST:
4257 if (var1 == NULL)
4258 n1 = 0;
4259 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004260 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004261 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004262 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004263 return FAIL;
4264 break;
4265
4266 case VAR_DICT:
4267 {
4268 dictitem_T *item;
4269 typval_T tmp;
4270
4271 if (key == NULL)
4272 {
4273 key = tv_get_string_chk(var1);
4274 if (key == NULL)
4275 return FAIL;
4276 }
4277
4278 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4279
4280 if (item == NULL && verbose)
4281 semsg(_(e_dictkey), key);
4282 if (item == NULL)
4283 return FAIL;
4284
4285 copy_tv(&item->di_tv, &tmp);
4286 clear_tv(rettv);
4287 *rettv = tmp;
4288 }
4289 break;
4290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004291 return OK;
4292}
4293
4294/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004295 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004296 */
4297 char_u *
4298partial_name(partial_T *pt)
4299{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004300 if (pt != NULL)
4301 {
4302 if (pt->pt_name != NULL)
4303 return pt->pt_name;
4304 if (pt->pt_func != NULL)
4305 return pt->pt_func->uf_name;
4306 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004307 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004308}
4309
Bram Moolenaarddecc252016-04-06 22:59:37 +02004310 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004312{
4313 int i;
4314
4315 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004316 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004317 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004319 if (pt->pt_name != NULL)
4320 {
4321 func_unref(pt->pt_name);
4322 vim_free(pt->pt_name);
4323 }
4324 else
4325 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004326
Bram Moolenaar54656012021-06-09 20:50:46 +02004327 // "out_up" is no longer used, decrement refcount on partial that owns it.
4328 partial_unref(pt->pt_outer.out_up_partial);
4329
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004330 // Decrease the reference count for the context of a closure. If down
4331 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004332 if (pt->pt_funcstack != NULL)
4333 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004334 --pt->pt_funcstack->fs_refcount;
4335 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004336 }
4337
Bram Moolenaarddecc252016-04-06 22:59:37 +02004338 vim_free(pt);
4339}
4340
4341/*
4342 * Unreference a closure: decrement the reference count and free it when it
4343 * becomes zero.
4344 */
4345 void
4346partial_unref(partial_T *pt)
4347{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004348 if (pt != NULL)
4349 {
4350 if (--pt->pt_refcount <= 0)
4351 partial_free(pt);
4352
4353 // If the reference count goes down to one, the funcstack may be the
4354 // only reference and can be freed if no other partials reference it.
4355 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4356 funcstack_check_refcount(pt->pt_funcstack);
4357 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004358}
4359
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004360/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004361 * Return the next (unique) copy ID.
4362 * Used for serializing nested structures.
4363 */
4364 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004365get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004366{
4367 current_copyID += COPYID_INC;
4368 return current_copyID;
4369}
4370
4371/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004372 * Garbage collection for lists and dictionaries.
4373 *
4374 * We use reference counts to be able to free most items right away when they
4375 * are no longer used. But for composite items it's possible that it becomes
4376 * unused while the reference count is > 0: When there is a recursive
4377 * reference. Example:
4378 * :let l = [1, 2, 3]
4379 * :let d = {9: l}
4380 * :let l[1] = d
4381 *
4382 * Since this is quite unusual we handle this with garbage collection: every
4383 * once in a while find out which lists and dicts are not referenced from any
4384 * variable.
4385 *
4386 * Here is a good reference text about garbage collection (refers to Python
4387 * but it applies to all reference-counting mechanisms):
4388 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004389 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390
4391/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004392 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004393 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004394 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004395 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004397garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004398{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004399 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004400 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004401 buf_T *buf;
4402 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004403 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004404 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004405
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004406 if (!testing)
4407 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004409 want_garbage_collect = FALSE;
4410 may_garbage_collect = FALSE;
4411 garbage_collect_at_exit = FALSE;
4412 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004413
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004414 // The execution stack can grow big, limit the size.
4415 if (exestack.ga_maxlen - exestack.ga_len > 500)
4416 {
4417 size_t new_len;
4418 char_u *pp;
4419 int n;
4420
4421 // Keep 150% of the current size, with a minimum of the growth size.
4422 n = exestack.ga_len / 2;
4423 if (n < exestack.ga_growsize)
4424 n = exestack.ga_growsize;
4425
4426 // Don't make it bigger though.
4427 if (exestack.ga_len + n < exestack.ga_maxlen)
4428 {
4429 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4430 pp = vim_realloc(exestack.ga_data, new_len);
4431 if (pp == NULL)
4432 return FAIL;
4433 exestack.ga_maxlen = exestack.ga_len + n;
4434 exestack.ga_data = pp;
4435 }
4436 }
4437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004438 // We advance by two because we add one for items referenced through
4439 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004440 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004441
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004442 /*
4443 * 1. Go through all accessible variables and mark all lists and dicts
4444 * with copyID.
4445 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 // Don't free variables in the previous_funccal list unless they are only
4448 // referenced through previous_funccal. This must be first, because if
4449 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004453 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004456 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4458 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004461 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004462 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4463 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004464 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4466 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004467#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004468 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004469 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4470 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004471 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004472 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004473 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4474 NULL, NULL);
4475#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004478 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004479 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4480 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004482 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004488 abort = abort || set_ref_in_functions(copyID);
4489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004494 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004495
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004496 // callbacks in buffers
4497 abort = abort || set_ref_in_buffers(copyID);
4498
Bram Moolenaar1dced572012-04-05 16:54:08 +02004499#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004501#endif
4502
Bram Moolenaardb913952012-06-29 12:54:53 +02004503#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004505#endif
4506
4507#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004509#endif
4510
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004512 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004513 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004514#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004515#ifdef FEAT_NETBEANS_INTG
4516 abort = abort || set_ref_in_nb_channel(copyID);
4517#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004518
Bram Moolenaare3188e22016-05-31 21:13:04 +02004519#ifdef FEAT_TIMERS
4520 abort = abort || set_ref_in_timer(copyID);
4521#endif
4522
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004523#ifdef FEAT_QUICKFIX
4524 abort = abort || set_ref_in_quickfix(copyID);
4525#endif
4526
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004527#ifdef FEAT_TERMINAL
4528 abort = abort || set_ref_in_term(copyID);
4529#endif
4530
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004531#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004532 abort = abort || set_ref_in_popups(copyID);
4533#endif
4534
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004536 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 /*
4538 * 2. Free lists and dictionaries that are not referenced.
4539 */
4540 did_free = free_unref_items(copyID);
4541
4542 /*
4543 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004544 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004545 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004547 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004548 else if (p_verbose > 0)
4549 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004551 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004552
4553 return did_free;
4554}
4555
4556/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004557 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004558 */
4559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004560free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004561{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004562 int did_free = FALSE;
4563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 // Let all "free" functions know that we are here. This means no
4565 // dictionaries, lists, channels or jobs are to be freed, because we will
4566 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004567 in_free_unref_items = TRUE;
4568
4569 /*
4570 * PASS 1: free the contents of the items. We don't free the items
4571 * themselves yet, so that it is possible to decrement refcount counters
4572 */
4573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004574 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004575 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004578 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579
4580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of jobs and free items without the copyID. This
4582 // must happen before doing channels, because jobs refer to channels, but
4583 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004587 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4588#endif
4589
4590 /*
4591 * PASS 2: free the items themselves.
4592 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004593 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004594 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004595
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004596#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // Go through the list of jobs and free items without the copyID. This
4598 // must happen before doing channels, because jobs refer to channels, but
4599 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004600 free_unused_jobs(copyID, COPYID_MASK);
4601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004603 free_unused_channels(copyID, COPYID_MASK);
4604#endif
4605
4606 in_free_unref_items = FALSE;
4607
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 return did_free;
4609}
4610
4611/*
4612 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004613 * "list_stack" is used to add lists to be marked. Can be NULL.
4614 *
4615 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004616 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004618set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004619{
4620 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004621 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004622 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004623 hashtab_T *cur_ht;
4624 ht_stack_T *ht_stack = NULL;
4625 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004626
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 cur_ht = ht;
4628 for (;;)
4629 {
4630 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004631 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // Mark each item in the hashtab. If the item contains a hashtab
4633 // it is added to ht_stack, if it contains a list it is added to
4634 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004635 todo = (int)cur_ht->ht_used;
4636 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4637 if (!HASHITEM_EMPTY(hi))
4638 {
4639 --todo;
4640 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4641 &ht_stack, list_stack);
4642 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004643 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644
4645 if (ht_stack == NULL)
4646 break;
4647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004649 cur_ht = ht_stack->ht;
4650 tempitem = ht_stack;
4651 ht_stack = ht_stack->prev;
4652 free(tempitem);
4653 }
4654
4655 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656}
4657
4658/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004659 * Mark a dict and its items with "copyID".
4660 * Returns TRUE if setting references failed somehow.
4661 */
4662 int
4663set_ref_in_dict(dict_T *d, int copyID)
4664{
4665 if (d != NULL && d->dv_copyID != copyID)
4666 {
4667 d->dv_copyID = copyID;
4668 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4669 }
4670 return FALSE;
4671}
4672
4673/*
4674 * Mark a list and its items with "copyID".
4675 * Returns TRUE if setting references failed somehow.
4676 */
4677 int
4678set_ref_in_list(list_T *ll, int copyID)
4679{
4680 if (ll != NULL && ll->lv_copyID != copyID)
4681 {
4682 ll->lv_copyID = copyID;
4683 return set_ref_in_list_items(ll, copyID, NULL);
4684 }
4685 return FALSE;
4686}
4687
4688/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004689 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004690 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4691 *
4692 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004693 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004695set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004696{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004697 listitem_T *li;
4698 int abort = FALSE;
4699 list_T *cur_l;
4700 list_stack_T *list_stack = NULL;
4701 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004702
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004703 cur_l = l;
4704 for (;;)
4705 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004706 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004707 // Mark each item in the list. If the item contains a hashtab
4708 // it is added to ht_stack, if it contains a list it is added to
4709 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004710 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4711 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4712 ht_stack, &list_stack);
4713 if (list_stack == NULL)
4714 break;
4715
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004716 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 cur_l = list_stack->list;
4718 tempitem = list_stack;
4719 list_stack = list_stack->prev;
4720 free(tempitem);
4721 }
4722
4723 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004724}
4725
4726/*
4727 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004728 * "list_stack" is used to add lists to be marked. Can be NULL.
4729 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4730 *
4731 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004732 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734set_ref_in_item(
4735 typval_T *tv,
4736 int copyID,
4737 ht_stack_T **ht_stack,
4738 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004739{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004740 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004741
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004743 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004744 dict_T *dd = tv->vval.v_dict;
4745
Bram Moolenaara03f2332016-02-06 18:09:59 +01004746 if (dd != NULL && dd->dv_copyID != copyID)
4747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004749 dd->dv_copyID = copyID;
4750 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004751 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004752 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4753 }
4754 else
4755 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004756 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4757
Bram Moolenaara03f2332016-02-06 18:09:59 +01004758 if (newitem == NULL)
4759 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004760 else
4761 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004762 newitem->ht = &dd->dv_hashtab;
4763 newitem->prev = *ht_stack;
4764 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004765 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004766 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004767 }
4768 }
4769 else if (tv->v_type == VAR_LIST)
4770 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 list_T *ll = tv->vval.v_list;
4772
Bram Moolenaara03f2332016-02-06 18:09:59 +01004773 if (ll != NULL && ll->lv_copyID != copyID)
4774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004776 ll->lv_copyID = copyID;
4777 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004778 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004779 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 }
4781 else
4782 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004783 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4784
Bram Moolenaara03f2332016-02-06 18:09:59 +01004785 if (newitem == NULL)
4786 abort = TRUE;
4787 else
4788 {
4789 newitem->list = ll;
4790 newitem->prev = *list_stack;
4791 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004792 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004793 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004794 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004795 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004796 else if (tv->v_type == VAR_FUNC)
4797 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004798 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004799 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 else if (tv->v_type == VAR_PARTIAL)
4801 {
4802 partial_T *pt = tv->vval.v_partial;
4803 int i;
4804
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004805 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004807 // Didn't see this partial yet.
4808 pt->pt_copyID = copyID;
4809
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004810 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004811
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004812 if (pt->pt_dict != NULL)
4813 {
4814 typval_T dtv;
4815
4816 dtv.v_type = VAR_DICT;
4817 dtv.vval.v_dict = pt->pt_dict;
4818 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4819 }
4820
4821 for (i = 0; i < pt->pt_argc; ++i)
4822 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4823 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004824 if (pt->pt_funcstack != NULL)
4825 {
4826 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4827
4828 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4829 abort = abort || set_ref_in_item(stack + i, copyID,
4830 ht_stack, list_stack);
4831 }
4832
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004833 }
4834 }
4835#ifdef FEAT_JOB_CHANNEL
4836 else if (tv->v_type == VAR_JOB)
4837 {
4838 job_T *job = tv->vval.v_job;
4839 typval_T dtv;
4840
4841 if (job != NULL && job->jv_copyID != copyID)
4842 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004843 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004844 if (job->jv_channel != NULL)
4845 {
4846 dtv.v_type = VAR_CHANNEL;
4847 dtv.vval.v_channel = job->jv_channel;
4848 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4849 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004850 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004851 {
4852 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004853 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4855 }
4856 }
4857 }
4858 else if (tv->v_type == VAR_CHANNEL)
4859 {
4860 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004861 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004862 typval_T dtv;
4863 jsonq_T *jq;
4864 cbq_T *cq;
4865
4866 if (ch != NULL && ch->ch_copyID != copyID)
4867 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004868 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004869 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 {
4871 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4872 jq = jq->jq_next)
4873 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4874 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4875 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004876 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004877 {
4878 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004879 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4881 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004882 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004883 {
4884 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 dtv.vval.v_partial =
4886 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004887 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4888 }
4889 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004890 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 {
4892 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004893 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004894 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4895 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004896 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004897 {
4898 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004899 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004900 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4901 }
4902 }
4903 }
4904#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004905 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004906}
4907
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 * Return a string with the string representation of a variable.
4910 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004911 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004912 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004913 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004914 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004915 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004916 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4917 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004918 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004920 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004921echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922 typval_T *tv,
4923 char_u **tofree,
4924 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004925 int copyID,
4926 int echo_style,
4927 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004928 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004930 static int recurse = 0;
4931 char_u *r = NULL;
4932
Bram Moolenaar33570922005-01-25 22:26:29 +00004933 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004934 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004935 if (!did_echo_string_emsg)
4936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 // Only give this message once for a recursive call to avoid
4938 // flooding the user with errors. And stop iterating over lists
4939 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004940 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004941 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004943 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004944 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004945 }
4946 ++recurse;
4947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 switch (tv->v_type)
4949 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004950 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004951 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952 {
4953 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004954 r = tv->vval.v_string;
4955 if (r == NULL)
4956 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004957 }
4958 else
4959 {
4960 *tofree = string_quote(tv->vval.v_string, FALSE);
4961 r = *tofree;
4962 }
4963 break;
4964
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966 if (echo_style)
4967 {
4968 *tofree = NULL;
4969 r = tv->vval.v_string;
4970 }
4971 else
4972 {
4973 *tofree = string_quote(tv->vval.v_string, TRUE);
4974 r = *tofree;
4975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004978 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004979 {
4980 partial_T *pt = tv->vval.v_partial;
4981 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004982 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004983 garray_T ga;
4984 int i;
4985 char_u *tf;
4986
4987 ga_init2(&ga, 1, 100);
4988 ga_concat(&ga, (char_u *)"function(");
4989 if (fname != NULL)
4990 {
4991 ga_concat(&ga, fname);
4992 vim_free(fname);
4993 }
4994 if (pt != NULL && pt->pt_argc > 0)
4995 {
4996 ga_concat(&ga, (char_u *)", [");
4997 for (i = 0; i < pt->pt_argc; ++i)
4998 {
4999 if (i > 0)
5000 ga_concat(&ga, (char_u *)", ");
5001 ga_concat(&ga,
5002 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5003 vim_free(tf);
5004 }
5005 ga_concat(&ga, (char_u *)"]");
5006 }
5007 if (pt != NULL && pt->pt_dict != NULL)
5008 {
5009 typval_T dtv;
5010
5011 ga_concat(&ga, (char_u *)", ");
5012 dtv.v_type = VAR_DICT;
5013 dtv.vval.v_dict = pt->pt_dict;
5014 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5015 vim_free(tf);
5016 }
5017 ga_concat(&ga, (char_u *)")");
5018
5019 *tofree = ga.ga_data;
5020 r = *tofree;
5021 break;
5022 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005023
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005024 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005025 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005026 break;
5027
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 if (tv->vval.v_list == NULL)
5030 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005031 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005032 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005033 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005034 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005035 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5036 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005037 {
5038 *tofree = NULL;
5039 r = (char_u *)"[...]";
5040 }
5041 else
5042 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005043 int old_copyID = tv->vval.v_list->lv_copyID;
5044
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005045 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005046 *tofree = list2string(tv, copyID, restore_copyID);
5047 if (restore_copyID)
5048 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 r = *tofree;
5050 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005051 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005052
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 if (tv->vval.v_dict == NULL)
5055 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005056 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005058 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005059 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005060 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5061 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005062 {
5063 *tofree = NULL;
5064 r = (char_u *)"{...}";
5065 }
5066 else
5067 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005068 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005069
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005070 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005071 *tofree = dict2string(tv, copyID, restore_copyID);
5072 if (restore_copyID)
5073 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005074 r = *tofree;
5075 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005076 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005079 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005080 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005082 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005083 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005084 break;
5085
Bram Moolenaar835dc632016-02-07 14:27:38 +01005086 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005087 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005088#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005090 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5091 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005092 if (composite_val)
5093 {
5094 *tofree = string_quote(r, FALSE);
5095 r = *tofree;
5096 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005097#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005099
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005100 case VAR_INSTR:
5101 *tofree = NULL;
5102 r = (char_u *)"instructions";
5103 break;
5104
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005106#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 *tofree = NULL;
5108 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5109 r = numbuf;
5110 break;
5111#endif
5112
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005113 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005114 case VAR_SPECIAL:
5115 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005116 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005117 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005119
Bram Moolenaar8502c702014-06-17 12:51:16 +02005120 if (--recurse == 0)
5121 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005122 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123}
5124
5125/*
5126 * Return a string with the string representation of a variable.
5127 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5128 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005129 * Does not put quotes around strings, as ":echo" displays values.
5130 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5131 * May return NULL.
5132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005134echo_string(
5135 typval_T *tv,
5136 char_u **tofree,
5137 char_u *numbuf,
5138 int copyID)
5139{
5140 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5141}
5142
5143/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005144 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5145 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005146 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005147 */
5148 int
5149buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5150{
5151 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005152 char_u *t;
5153 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005154
5155 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5156 return -1;
5157
5158 if (lnum > buf->b_ml.ml_line_count)
5159 lnum = buf->b_ml.ml_line_count;
5160
5161 str = ml_get_buf(buf, lnum, FALSE);
5162 if (str == NULL)
5163 return -1;
5164
5165 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005166 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005167
Bram Moolenaar91458462021-01-13 20:08:38 +01005168 // count the number of characters
5169 t = str;
5170 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5171 t += mb_ptr2len(t);
5172
5173 // In insert mode, when the cursor is at the end of a non-empty line,
5174 // byteidx points to the NUL character immediately past the end of the
5175 // string. In this case, add one to the character count.
5176 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5177 count++;
5178
5179 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005180}
5181
5182/*
5183 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005184 * byte index. Works only for loaded buffers. Returns -1 on failure.
5185 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005186 */
5187 int
5188buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5189{
5190 char_u *str;
5191 char_u *t;
5192
5193 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5194 return -1;
5195
5196 if (lnum > buf->b_ml.ml_line_count)
5197 lnum = buf->b_ml.ml_line_count;
5198
5199 str = ml_get_buf(buf, lnum, FALSE);
5200 if (str == NULL)
5201 return -1;
5202
5203 // Convert the character offset to a byte offset
5204 t = str;
5205 while (*t != NUL && --charidx > 0)
5206 t += mb_ptr2len(t);
5207
Bram Moolenaar91458462021-01-13 20:08:38 +01005208 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005209}
5210
5211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005213 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216var2fpos(
5217 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005219 int *fnum, // set to fnum for '0, 'A, etc.
5220 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005222 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005224 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005226 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005227 if (varp->v_type == VAR_LIST)
5228 {
5229 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005230 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005231 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005232 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005233
5234 l = varp->vval.v_list;
5235 if (l == NULL)
5236 return NULL;
5237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005239 pos.lnum = list_find_nr(l, 0L, &error);
5240 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005242 if (charcol)
5243 len = (long)mb_charlen(ml_get(pos.lnum));
5244 else
5245 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005246
Bram Moolenaarec65d772020-08-20 22:29:12 +02005247 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005248 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005249 li = list_find(l, 1L);
5250 if (li != NULL && li->li_tv.v_type == VAR_STRING
5251 && li->li_tv.vval.v_string != NULL
5252 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005253 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005254 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005255 }
5256 else
5257 {
5258 pos.col = list_find_nr(l, 1L, &error);
5259 if (error)
5260 return NULL;
5261 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005264 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005266 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005269 pos.coladd = list_find_nr(l, 2L, &error);
5270 if (error)
5271 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005272
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005273 return &pos;
5274 }
5275
Bram Moolenaarc5809432021-03-27 21:23:30 +01005276 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5277 return NULL;
5278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005279 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 if (name == NULL)
5281 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005282 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005283 {
5284 pos = curwin->w_cursor;
5285 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005286 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005287 return &pos;
5288 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005290 {
5291 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005292 pos = VIsual;
5293 else
5294 pos = curwin->w_cursor;
5295 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005296 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005297 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005298 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005301 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5303 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005304 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005305 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 return pp;
5307 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005308
Bram Moolenaara5525202006-03-02 22:52:09 +00005309 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005310
Bram Moolenaar477933c2007-07-17 14:32:23 +00005311 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005312 {
5313 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005315 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 // In silent Ex mode topline is zero, but that's not a valid line
5318 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005319 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005320 return &pos;
5321 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005323 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005324 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005326 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005327 return &pos;
5328 }
5329 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005332 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
5334 pos.lnum = curbuf->b_ml.ml_line_count;
5335 pos.col = 0;
5336 }
5337 else
5338 {
5339 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005340 if (charcol)
5341 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5342 else
5343 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 return &pos;
5346 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005347 if (in_vim9script())
5348 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 return NULL;
5350}
5351
5352/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005353 * Convert list in "arg" into a position and optional file number.
5354 * When "fnump" is NULL there is no file number, only 3 items.
5355 * Note that the column is passed on as-is, the caller may want to decrement
5356 * it to use 1 for the first column.
5357 * Return FAIL when conversion is not possible, doesn't check the position for
5358 * validity.
5359 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005361list2fpos(
5362 typval_T *arg,
5363 pos_T *posp,
5364 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005365 colnr_T *curswantp,
5366 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367{
5368 list_T *l = arg->vval.v_list;
5369 long i = 0;
5370 long n;
5371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5373 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005374 if (arg->v_type != VAR_LIST
5375 || l == NULL
5376 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005377 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005378 return FAIL;
5379
5380 if (fnump != NULL)
5381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383 if (n < 0)
5384 return FAIL;
5385 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005387 *fnump = n;
5388 }
5389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005391 if (n < 0)
5392 return FAIL;
5393 posp->lnum = n;
5394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005396 if (n < 0)
5397 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005398 // If character position is specified, then convert to byte position
5399 if (charcol)
5400 {
5401 buf_T *buf;
5402
5403 // Get the text for the specified line in a loaded buffer
5404 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5405 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5406 return FAIL;
5407
Bram Moolenaar91458462021-01-13 20:08:38 +01005408 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005409 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005410 posp->col = n;
5411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005412 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005413 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005414 posp->coladd = 0;
5415 else
5416 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005417
Bram Moolenaar493c1782014-05-28 14:34:46 +02005418 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005419 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005420
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005421 return OK;
5422}
5423
5424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 * Get the length of an environment variable name.
5426 * Advance "arg" to the first character after the name.
5427 * Return 0 for error.
5428 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005430get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 char_u *p;
5433 int len;
5434
5435 for (p = *arg; vim_isIDc(*p); ++p)
5436 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 return 0;
5439
5440 len = (int)(p - *arg);
5441 *arg = p;
5442 return len;
5443}
5444
5445/*
5446 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005447 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 * Return 0 if something is wrong.
5449 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 char_u *p;
5454 int len;
5455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005458 {
5459 if (*p == ':')
5460 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 // "s:" is start of "s:var", but "n:" is not and can be used in
5462 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005463 len = (int)(p - *arg);
5464 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5465 || len > 1)
5466 break;
5467 }
5468 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 return 0;
5471
5472 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005473 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 return len;
5476}
5477
5478/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005479 * Get the length of the name of a variable or function.
5480 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005482 * Return -1 if curly braces expansion failed.
5483 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 * If the name contains 'magic' {}'s, expand them and return the
5485 * expanded name in an allocated string via 'alias' - caller must free.
5486 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005488get_name_len(
5489 char_u **arg,
5490 char_u **alias,
5491 int evaluate,
5492 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 char_u *p;
5496 char_u *expr_start;
5497 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5502 && (*arg)[2] == (int)KE_SNR)
5503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005504 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 *arg += 3;
5506 return get_id_len(arg) + 3;
5507 }
5508 len = eval_fname_script(*arg);
5509 if (len > 0)
5510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005511 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 *arg += len;
5513 }
5514
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005518 p = find_name_end(*arg, &expr_start, &expr_end,
5519 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (expr_start != NULL)
5521 {
5522 char_u *temp_string;
5523
5524 if (!evaluate)
5525 {
5526 len += (int)(p - *arg);
5527 *arg = skipwhite(p);
5528 return len;
5529 }
5530
5531 /*
5532 * Include any <SID> etc in the expanded string:
5533 * Thus the -len here.
5534 */
5535 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5536 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 *alias = temp_string;
5539 *arg = skipwhite(p);
5540 return (int)STRLEN(temp_string);
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005544 // Only give an error when there is something, otherwise it will be
5545 // reported at a higher level.
5546 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005547 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 return len;
5550}
5551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552/*
5553 * Find the end of a variable or function name, taking care of magic braces.
5554 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5555 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005556 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 * Return a pointer to just after the name. Equal to "arg" if there is no
5558 * valid name.
5559 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005561find_name_end(
5562 char_u *arg,
5563 char_u **expr_start,
5564 char_u **expr_end,
5565 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 int mb_nest = 0;
5568 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005570 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005571 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 *expr_start = NULL;
5576 *expr_end = NULL;
5577 }
5578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005579 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005580 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5581 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005582 return arg;
5583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 for (p = arg; *p != NUL
5585 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005586 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005587 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005588 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005590 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005592 if (*p == '\'')
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005595 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005596 ;
5597 if (*p == NUL)
5598 break;
5599 }
5600 else if (*p == '"')
5601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005602 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005603 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005604 if (*p == '\\' && p[1] != NUL)
5605 ++p;
5606 if (*p == NUL)
5607 break;
5608 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005609 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // "s:" is start of "s:var", but "n:" is not and can be used in
5612 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005613 len = (int)(p - arg);
5614 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005615 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005616 break;
5617 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 if (*p == '[')
5622 ++br_nest;
5623 else if (*p == ']')
5624 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005626
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005627 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 if (*p == '{')
5630 {
5631 mb_nest++;
5632 if (expr_start != NULL && *expr_start == NULL)
5633 *expr_start = p;
5634 }
5635 else if (*p == '}')
5636 {
5637 mb_nest--;
5638 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5639 *expr_end = p;
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643
5644 return p;
5645}
5646
5647/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005648 * Expands out the 'magic' {}'s in a variable/function name.
5649 * Note that this can call itself recursively, to deal with
5650 * constructs like foo{bar}{baz}{bam}
5651 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5652 * "in_start" ^
5653 * "expr_start" ^
5654 * "expr_end" ^
5655 * "in_end" ^
5656 *
5657 * Returns a new allocated string, which the caller must free.
5658 * Returns NULL for failure.
5659 */
5660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661make_expanded_name(
5662 char_u *in_start,
5663 char_u *expr_start,
5664 char_u *expr_end,
5665 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005666{
5667 char_u c1;
5668 char_u *retval = NULL;
5669 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005670
5671 if (expr_end == NULL || in_end == NULL)
5672 return NULL;
5673 *expr_start = NUL;
5674 *expr_end = NUL;
5675 c1 = *in_end;
5676 *in_end = NUL;
5677
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005678 temp_result = eval_to_string(expr_start + 1, FALSE);
5679 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005680 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005681 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5682 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005683 if (retval != NULL)
5684 {
5685 STRCPY(retval, in_start);
5686 STRCAT(retval, temp_result);
5687 STRCAT(retval, expr_end + 1);
5688 }
5689 }
5690 vim_free(temp_result);
5691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005692 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005693 *expr_start = '{';
5694 *expr_end = '}';
5695
5696 if (retval != NULL)
5697 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005698 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005699 if (expr_start != NULL)
5700 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005701 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005702 temp_result = make_expanded_name(retval, expr_start,
5703 expr_end, temp_result);
5704 vim_free(retval);
5705 retval = temp_result;
5706 }
5707 }
5708
5709 return retval;
5710}
5711
5712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005717eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005719 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005720}
5721
5722/*
5723 * Return TRUE if character "c" can be used as the first character in a
5724 * variable or function name (excluding '{' and '}').
5725 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005728{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005729 return ASCII_ISALPHA(c) || c == '_';
5730}
5731
5732/*
5733 * Return TRUE if character "c" can be used as the first character of a
5734 * dictionary key.
5735 */
5736 int
5737eval_isdictc(int c)
5738{
5739 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740}
5741
5742/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005743 * Handle:
5744 * - expr[expr], expr[expr:expr] subscript
5745 * - ".name" lookup
5746 * - function call with Funcref variable: func(expr)
5747 * - method call: var->method()
5748 *
5749 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005750 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005752handle_subscript(
5753 char_u **arg,
5754 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005755 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005756 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005758 int evaluate = evalarg != NULL
5759 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005760 int ret = OK;
5761 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005762 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005763 int getnext;
5764 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005765
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005766 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005767 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005768 // When at the end of the line and ".name" or "->{" or "->X" follows in
5769 // the next line then consume the line break.
5770 p = eval_next_non_blank(*arg, evalarg, &getnext);
5771 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005772 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005773 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5774 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5775 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005776 {
5777 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005778 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005779 check_white = FALSE;
5780 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005781
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005782 if (rettv->v_type == VAR_ANY)
5783 {
5784 char_u *exp_name;
5785 int cc;
5786 int idx;
5787 ufunc_T *ufunc;
5788 type_T *type;
5789
5790 // Found script from "import * as {name}", script item name must
5791 // follow.
5792 if (**arg != '.')
5793 {
5794 if (verbose)
5795 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5796 ret = FAIL;
5797 break;
5798 }
5799 ++*arg;
5800 if (IS_WHITE_OR_NUL(**arg))
5801 {
5802 if (verbose)
5803 emsg(_(e_no_white_space_allowed_after_dot));
5804 ret = FAIL;
5805 break;
5806 }
5807
5808 // isolate the name
5809 exp_name = *arg;
5810 while (eval_isnamec(**arg))
5811 ++*arg;
5812 cc = **arg;
5813 **arg = NUL;
5814
5815 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5816 evalarg->eval_cctx, verbose);
5817 **arg = cc;
5818 *arg = skipwhite(*arg);
5819
5820 if (idx < 0 && ufunc == NULL)
5821 {
5822 ret = FAIL;
5823 break;
5824 }
5825 if (idx >= 0)
5826 {
5827 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5828 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5829
5830 copy_tv(sv->sv_tv, rettv);
5831 }
5832 else
5833 {
5834 rettv->v_type = VAR_FUNC;
5835 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5836 }
5837 }
5838
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005839 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5840 || rettv->v_type == VAR_PARTIAL))
5841 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005842 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005843 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5844 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005845
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005846 // Stop the expression evaluation when immediately aborting on
5847 // error, or when an interrupt occurred or an exception was thrown
5848 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005849 if (aborting())
5850 {
5851 if (ret == OK)
5852 clear_tv(rettv);
5853 ret = FAIL;
5854 }
5855 dict_unref(selfdict);
5856 selfdict = NULL;
5857 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005858 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005859 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005860 if (in_vim9script())
5861 *arg = skipwhite(p + 2);
5862 else
5863 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005864 if (ret == OK)
5865 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005866 if (VIM_ISWHITE(**arg))
5867 {
5868 emsg(_(e_nowhitespace));
5869 ret = FAIL;
5870 }
5871 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005872 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005873 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005874 else
5875 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005876 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005877 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005878 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005879 // "." is ".name" lookup when we found a dict or when evaluating and
5880 // scriptversion is at least 2, where string concatenation is "..".
5881 else if (**arg == '['
5882 || (**arg == '.' && (rettv->v_type == VAR_DICT
5883 || (!evaluate
5884 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02005885 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005886 {
5887 dict_unref(selfdict);
5888 if (rettv->v_type == VAR_DICT)
5889 {
5890 selfdict = rettv->vval.v_dict;
5891 if (selfdict != NULL)
5892 ++selfdict->dv_refcount;
5893 }
5894 else
5895 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005896 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005897 {
5898 clear_tv(rettv);
5899 ret = FAIL;
5900 }
5901 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005902 else
5903 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005904 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005906 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5907 // Don't do this when "Func" is already a partial that was bound
5908 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005909 if (selfdict != NULL
5910 && (rettv->v_type == VAR_FUNC
5911 || (rettv->v_type == VAR_PARTIAL
5912 && (rettv->vval.v_partial->pt_auto
5913 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005914 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005916 dict_unref(selfdict);
5917 return ret;
5918}
5919
5920/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 * Make a copy of an item.
5922 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5924 * reference to an already copied list/dict can be used.
5925 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005926 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005928item_copy(
5929 typval_T *from,
5930 typval_T *to,
5931 int deep,
5932 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933{
5934 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005935 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005939 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005941 }
5942 ++recurse;
5943
5944 switch (from->v_type)
5945 {
5946 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005947 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 case VAR_STRING:
5949 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005950 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005951 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005952 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005954 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005955 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005956 copy_tv(from, to);
5957 break;
5958 case VAR_LIST:
5959 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005960 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005961 if (from->vval.v_list == NULL)
5962 to->vval.v_list = NULL;
5963 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005965 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005966 to->vval.v_list = from->vval.v_list->lv_copylist;
5967 ++to->vval.v_list->lv_refcount;
5968 }
5969 else
5970 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5971 if (to->vval.v_list == NULL)
5972 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005974 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005976 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977 case VAR_DICT:
5978 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005979 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 if (from->vval.v_dict == NULL)
5981 to->vval.v_dict = NULL;
5982 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005984 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005985 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5986 ++to->vval.v_dict->dv_refcount;
5987 }
5988 else
5989 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5990 if (to->vval.v_dict == NULL)
5991 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005993 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005994 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005996 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005998 }
5999 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001}
6002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 void
6004echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6005{
6006 char_u *tofree;
6007 char_u numbuf[NUMBUFLEN];
6008 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6009
6010 if (*atstart)
6011 {
6012 *atstart = FALSE;
6013 // Call msg_start() after eval1(), evaluating the expression
6014 // may cause a message to appear.
6015 if (with_space)
6016 {
6017 // Mark the saved text as finishing the line, so that what
6018 // follows is displayed on a new line when scrolling back
6019 // at the more prompt.
6020 msg_sb_eol();
6021 msg_start();
6022 }
6023 }
6024 else if (with_space)
6025 msg_puts_attr(" ", echo_attr);
6026
6027 if (p != NULL)
6028 for ( ; *p != NUL && !got_int; ++p)
6029 {
6030 if (*p == '\n' || *p == '\r' || *p == TAB)
6031 {
6032 if (*p != TAB && *needclr)
6033 {
6034 // remove any text still there from the command
6035 msg_clr_eos();
6036 *needclr = FALSE;
6037 }
6038 msg_putchar_attr(*p, echo_attr);
6039 }
6040 else
6041 {
6042 if (has_mbyte)
6043 {
6044 int i = (*mb_ptr2len)(p);
6045
6046 (void)msg_outtrans_len_attr(p, i, echo_attr);
6047 p += i - 1;
6048 }
6049 else
6050 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6051 }
6052 }
6053 vim_free(tofree);
6054}
6055
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 * ":echo expr1 ..." print each argument separated with a space, add a
6058 * newline at the end.
6059 * ":echon expr1 ..." print each argument plain.
6060 */
6061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006066 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 int needclr = TRUE;
6068 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006069 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006070 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006071 evalarg_T evalarg;
6072
Bram Moolenaare707c882020-07-01 13:07:37 +02006073 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 if (eap->skip)
6076 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006077 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006079 // If eval1() causes an error message the text from the command may
6080 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006081 need_clr_eos = needclr;
6082
Bram Moolenaar68db9962021-05-09 23:19:22 +02006083 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006084 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {
6086 /*
6087 * Report the invalid expression unless the expression evaluation
6088 * has been cancelled due to an aborting error, an interrupt, or an
6089 * exception.
6090 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006091 if (!aborting() && did_emsg == did_emsg_before
6092 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006093 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006094 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 break;
6096 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006097 need_clr_eos = FALSE;
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006100 {
6101 if (rettv.v_type == VAR_VOID)
6102 {
6103 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6104 break;
6105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006106 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006107 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 arg = skipwhite(arg);
6111 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006112 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006113 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115 if (eap->skip)
6116 --emsg_skip;
6117 else
6118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (needclr)
6121 msg_clr_eos();
6122 if (eap->cmdidx == CMD_echo)
6123 msg_end();
6124 }
6125}
6126
6127/*
6128 * ":echohl {name}".
6129 */
6130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006133 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134}
6135
6136/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006137 * Returns the :echo attribute
6138 */
6139 int
6140get_echo_attr(void)
6141{
6142 return echo_attr;
6143}
6144
6145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * ":execute expr1 ..." execute the result of an expression.
6147 * ":echomsg expr1 ..." Print a message
6148 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006149 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 * Each gets spaces around each argument and a newline at the end for
6151 * echo commands
6152 */
6153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006154ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 int ret = OK;
6159 char_u *p;
6160 garray_T ga;
6161 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006162 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 ga_init2(&ga, 1, 80);
6165
6166 if (eap->skip)
6167 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006168 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006170 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006171 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 if (!eap->skip)
6175 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006176 char_u buf[NUMBUFLEN];
6177
6178 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006179 {
6180 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6181 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006182 semsg(_(e_using_invalid_value_as_string_str),
6183 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006184 p = NULL;
6185 }
6186 else
6187 p = tv_get_string_buf(&rettv, buf);
6188 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006189 else
6190 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006191 if (p == NULL)
6192 {
6193 clear_tv(&rettv);
6194 ret = FAIL;
6195 break;
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 len = (int)STRLEN(p);
6198 if (ga_grow(&ga, len + 2) == FAIL)
6199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006200 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 ret = FAIL;
6202 break;
6203 }
6204 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 ga.ga_len += len;
6208 }
6209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006210 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 arg = skipwhite(arg);
6212 }
6213
6214 if (ret != FAIL && ga.ga_data != NULL)
6215 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006216 // use the first line of continuation lines for messages
6217 SOURCING_LNUM = start_lnum;
6218
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006219 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006221 // Mark the already saved text as finishing the line, so that what
6222 // follows is displayed on a new line when scrolling back at the
6223 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006224 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006225 }
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006228 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006229 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006230 out_flush();
6231 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006232 else if (eap->cmdidx == CMD_echoconsole)
6233 {
6234 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6235 ui_write((char_u *)"\r\n", 2, TRUE);
6236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 else if (eap->cmdidx == CMD_echoerr)
6238 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006239 int save_did_emsg = did_emsg;
6240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006242 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (!force_abort)
6244 did_emsg = save_did_emsg;
6245 }
6246 else if (eap->cmdidx == CMD_execute)
6247 do_cmdline((char_u *)ga.ga_data,
6248 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6249 }
6250
6251 ga_clear(&ga);
6252
6253 if (eap->skip)
6254 --emsg_skip;
6255
Bram Moolenaar63b91732021-08-05 20:40:03 +02006256 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258
6259/*
6260 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6261 * "arg" points to the "&" or '+' when called, to "option" when returning.
6262 * Returns NULL when no option name found. Otherwise pointer to the char
6263 * after the option name.
6264 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
6268 char_u *p = *arg;
6269
6270 ++p;
6271 if (*p == 'g' && p[1] == ':')
6272 {
6273 *opt_flags = OPT_GLOBAL;
6274 p += 2;
6275 }
6276 else if (*p == 'l' && p[1] == ':')
6277 {
6278 *opt_flags = OPT_LOCAL;
6279 p += 2;
6280 }
6281 else
6282 *opt_flags = 0;
6283
6284 if (!ASCII_ISALPHA(*p))
6285 return NULL;
6286 *arg = p;
6287
6288 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006289 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 else
6291 while (ASCII_ISALPHA(*p))
6292 ++p;
6293 return p;
6294}
6295
6296/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006297 * Display script name where an item was last set.
6298 * Should only be invoked when 'verbose' is non-zero.
6299 */
6300 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006301last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006302{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006303 char_u *p;
6304
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006305 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006306 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006307 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006308 if (p != NULL)
6309 {
6310 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006311 msg_puts(_("\n\tLast set from "));
6312 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006313 if (script_ctx.sc_lnum > 0)
6314 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006315 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006316 msg_outnum((long)script_ctx.sc_lnum);
6317 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006318 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006319 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006320 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006321 }
6322}
6323
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006324#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326/*
6327 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006328 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 * "flags" can be "g" to do a global substitute.
6330 * Returns an allocated string, NULL for error.
6331 */
6332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333do_string_sub(
6334 char_u *str,
6335 char_u *pat,
6336 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006337 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 int sublen;
6341 regmatch_T regmatch;
6342 int i;
6343 int do_all;
6344 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006345 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 garray_T ga;
6347 char_u *ret;
6348 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006349 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006351 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006353 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 ga_init2(&ga, 1, 200);
6356
6357 do_all = (flags[0] == 'g');
6358
6359 regmatch.rm_ic = p_ic;
6360 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6361 if (regmatch.regprog != NULL)
6362 {
6363 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006364 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6366 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006367 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006368 if (regmatch.startp[0] == regmatch.endp[0])
6369 {
6370 if (zero_width == regmatch.startp[0])
6371 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006372 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006373 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006374 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6375 (size_t)i);
6376 ga.ga_len += i;
6377 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006378 continue;
6379 }
6380 zero_width = regmatch.startp[0];
6381 }
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 /*
6384 * Get some space for a temporary buffer to do the substitution
6385 * into. It will contain:
6386 * - The text up to where the match is.
6387 * - The substituted text.
6388 * - The text after the match.
6389 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006390 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006391 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6393 {
6394 ga_clear(&ga);
6395 break;
6396 }
6397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006398 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 i = (int)(regmatch.startp[0] - tail);
6400 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006401 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006402 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 + ga.ga_len + i, TRUE, TRUE, FALSE);
6404 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006405 tail = regmatch.endp[0];
6406 if (*tail == NUL)
6407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (!do_all)
6409 break;
6410 }
6411
6412 if (ga.ga_data != NULL)
6413 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6414
Bram Moolenaar473de612013-06-08 18:19:48 +02006415 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417
6418 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6419 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006420 if (p_cpo == empty_option)
6421 p_cpo = save_cpo;
6422 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006424 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006425 // If it's still empty it was changed and restored, need to restore in
6426 // the complicated way.
6427 if (*p_cpo == NUL)
6428 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006429 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 return ret;
6433}