blob: 020f55720c5b366d8ec5ac7f6c9e4e33c5bd2efc [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020059static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000115
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200116#ifdef EBCDIC
117 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100118 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200119 */
120 sortFunctions();
121#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000122}
123
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124#if defined(EXITFREE) || defined(PROTO)
125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100126eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000127{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100129 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200130 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000131
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200132 // autoloaded script names
133 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000134
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200135 // unreferenced lists and dicts
136 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200137
138 // functions not garbage collected
139 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140}
141#endif
142
Bram Moolenaare6b53242020-07-01 17:28:33 +0200143 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200144fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
145{
146 CLEAR_FIELD(*evalarg);
147 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200148 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200150 evalarg->eval_cstack = eap->cstack;
151 if (getline_equal(eap->getline, eap->cookie, getsourceline))
152 {
153 evalarg->eval_getline = eap->getline;
154 evalarg->eval_cookie = eap->cookie;
155 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200156 }
157}
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Top level evaluation function, returning a boolean.
161 * Sets "error" to TRUE if there was an error.
162 * Return TRUE or FALSE.
163 */
164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100165eval_to_bool(
166 char_u *arg,
167 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200168 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaar33570922005-01-25 22:26:29 +0000171 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200172 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200173 evalarg_T evalarg;
174
Bram Moolenaar37c83712020-06-30 21:18:36 +0200175 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
177 if (skip)
178 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200179 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 else
182 {
183 *error = FALSE;
184 if (!skip)
185 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200186 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200187 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200188 else
189 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000190 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192 }
193 if (skip)
194 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200197 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198}
199
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200/*
201 * Call eval1() and give an error message if not done at a lower level.
202 */
203 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100206 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 int ret;
208 int did_emsg_before = did_emsg;
209 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200212 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
213
214 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215 if (ret == FAIL)
216 {
217 // Report the invalid expression unless the expression evaluation has
218 // been cancelled due to an aborting error, an interrupt, or an
219 // exception, or we already gave a more specific error.
220 // Also check called_emsg for when using assert_fails().
221 if (!aborting() && did_emsg == did_emsg_before
222 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200223 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100224 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200225 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100226 return ret;
227}
228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200230 * Return whether a typval is a valid expression to pass to eval_expr_typval()
231 * or eval_expr_to_bool(). An empty string returns FALSE;
232 */
233 int
234eval_expr_valid_arg(typval_T *tv)
235{
236 return tv->v_type != VAR_UNKNOWN
237 && (tv->v_type != VAR_STRING
238 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
239}
240
241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 * Evaluate an expression, which can be a function, partial or string.
243 * Pass arguments "argv[argc]".
244 * Return the result in "rettv" and OK or FAIL.
245 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200246 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100247eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
248{
249 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100250 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200251 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100252
253 if (expr->v_type == VAR_FUNC)
254 {
255 s = expr->vval.v_string;
256 if (s == NULL || *s == NUL)
257 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200258 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 funcexe.evaluate = TRUE;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100261 return FAIL;
262 }
263 else if (expr->v_type == VAR_PARTIAL)
264 {
265 partial_T *partial = expr->vval.v_partial;
266
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200267 if (partial == NULL)
268 return FAIL;
269
Bram Moolenaar822ba242020-05-24 23:00:18 +0200270 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200271 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200273 if (call_def_function(partial->pt_func, argc, argv,
274 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275 return FAIL;
276 }
277 else
278 {
279 s = partial_name(partial);
280 if (s == NULL || *s == NUL)
281 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200282 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 funcexe.evaluate = TRUE;
284 funcexe.partial = partial;
285 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
286 return FAIL;
287 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200289 else if (expr->v_type == VAR_INSTR)
290 {
291 return exe_typval_instr(expr, rettv);
292 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 else
294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100295 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 if (s == NULL)
297 return FAIL;
298 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200299 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200301 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200303 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200304 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 return FAIL;
306 }
307 }
308 return OK;
309}
310
311/*
312 * Like eval_to_bool() but using a typval_T instead of a string.
313 * Works for string, funcref and partial.
314 */
315 int
316eval_expr_to_bool(typval_T *expr, int *error)
317{
318 typval_T rettv;
319 int res;
320
321 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
322 {
323 *error = TRUE;
324 return FALSE;
325 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200326 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100327 clear_tv(&rettv);
328 return res;
329}
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331/*
332 * Top level evaluation function, returning a string. If "skip" is TRUE,
333 * only parsing to "nextcmd" is done, without reporting errors. Return
334 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
335 */
336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100337eval_to_string_skip(
338 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200339 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100340 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
Bram Moolenaar33570922005-01-25 22:26:29 +0000342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar37c83712020-06-30 21:18:36 +0200346 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (skip)
348 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200349 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 retval = NULL;
351 else
352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100353 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
356 if (skip)
357 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200358 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360 return retval;
361}
362
363/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364 * Skip over an expression at "*pp".
365 * Return FAIL for an error, OK otherwise.
366 */
367 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200368skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369{
Bram Moolenaar33570922005-01-25 22:26:29 +0000370 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371
372 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200373 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374}
375
376/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200377 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200378 * If in Vim9 script and line breaks are encountered, the lines are
379 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200380 * "arg" is advanced to just after the expression.
381 * "start" is set to the start of the expression, "end" to just after the end.
382 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200386skip_expr_concatenate(
387 char_u **arg,
388 char_u **start,
389 char_u **end,
390 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200391{
392 typval_T rettv;
393 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200394 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200395 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200396 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 int evaluate = evalarg == NULL
399 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200401 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 {
404 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200405 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200408 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200410 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411
412 // Don't evaluate the expression.
413 if (evalarg != NULL)
414 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200415 *arg = skipwhite(*arg);
416 res = eval1(arg, &rettv, evalarg);
417 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 if (evalarg != NULL)
419 evalarg->eval_flags = save_flags;
420
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200421 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 if (evalarg->eval_ga.ga_len == 1)
425 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200426 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 ga_clear(gap);
428 gap->ga_itemsize = 0;
429 }
430 else
431 {
432 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200433 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 // Line breaks encountered, concatenate all the lines.
436 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200437 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438
439 // free the lines only when using getsourceline()
440 if (evalarg->eval_cookie != NULL)
441 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200442 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200444 // Do not free the last line, "arg" points into it, free it
445 // later.
446 vim_free(evalarg->eval_tofree);
447 evalarg->eval_tofree =
448 ((char_u **)gap->ga_data)[gap->ga_len - 1];
449 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear_strings(gap);
451 }
452 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200453 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200454 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200455
456 // free lines that were explicitly marked for freeing
457 ga_clear_strings(freegap);
458 }
459
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200460 gap->ga_itemsize = 0;
461 if (p == NULL)
462 return FAIL;
463 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200464 vim_free(evalarg->eval_tofree_lambda);
465 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 // Compute "end" relative to the end.
467 *end = *start + STRLEN(*start) - endoff;
468 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200469 }
470
471 return res;
472}
473
474/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100475 * Convert "tv" to a string.
476 * When "convert" is TRUE convert a List into a sequence of lines and convert
477 * a Float to a String.
478 * Returns an allocated string (NULL when out of memory).
479 */
480 char_u *
481typval2string(typval_T *tv, int convert)
482{
483 garray_T ga;
484 char_u *retval;
485#ifdef FEAT_FLOAT
486 char_u numbuf[NUMBUFLEN];
487#endif
488
489 if (convert && tv->v_type == VAR_LIST)
490 {
491 ga_init2(&ga, (int)sizeof(char), 80);
492 if (tv->vval.v_list != NULL)
493 {
494 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
495 if (tv->vval.v_list->lv_len > 0)
496 ga_append(&ga, NL);
497 }
498 ga_append(&ga, NUL);
499 retval = (char_u *)ga.ga_data;
500 }
501#ifdef FEAT_FLOAT
502 else if (convert && tv->v_type == VAR_FLOAT)
503 {
504 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
505 retval = vim_strsave(numbuf);
506 }
507#endif
508 else
509 retval = vim_strsave(tv_get_string(tv));
510 return retval;
511}
512
513/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200514 * Top level evaluation function, returning a string. Does not handle line
515 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000516 * When "convert" is TRUE convert a List into a sequence of lines and convert
517 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * Return pointer to allocated memory, or NULL for failure.
519 */
520 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 int convert,
524 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
Bram Moolenaar33570922005-01-25 22:26:29 +0000526 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100528 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
531 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 retval = NULL;
533 else
534 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100535 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000536 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 return retval;
541}
542
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100543 char_u *
544eval_to_string(
545 char_u *arg,
546 int convert)
547{
548 return eval_to_string_eap(arg, convert, NULL);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000552 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200554 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 */
556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100557eval_to_string_safe(
558 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100559 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200563 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200570 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000571 if (use_sandbox)
572 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200573 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 return retval;
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579/*
580 * Top level evaluation function, returning a number.
581 * Evaluates "expr" silently.
582 * Returns -1 for an error.
583 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100585eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
Bram Moolenaar33570922005-01-25 22:26:29 +0000587 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000589 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 ++emsg_off;
592
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200593 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 retval = -1;
595 else
596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100597 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600 --emsg_off;
601
602 return retval;
603}
604
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000606 * Top level evaluation function.
607 * Returns an allocated typval_T with the result.
608 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609 */
610 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200611eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000612{
613 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200614 evalarg_T evalarg;
615
616 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200618 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623 return tv;
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100627 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200628 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
629 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000630 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633call_vim_function(
634 char_u *func,
635 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200637 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200640 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100642 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200643 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200644 funcexe.firstline = curwin->w_cursor.lnum;
645 funcexe.lastline = curwin->w_cursor.lnum;
646 funcexe.evaluate = TRUE;
647 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000648 if (ret == FAIL)
649 clear_tv(rettv);
650
651 return ret;
652}
653
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100654/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100655 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200657 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
658 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100659 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200660 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100661call_func_retnr(
662 char_u *func,
663 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200664 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665{
666 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200667 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100668
Bram Moolenaarded27a12018-08-01 19:06:03 +0200669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 return -1;
671
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100672 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673 clear_tv(&rettv);
674 return retval;
675}
676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000677/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100678 * Call Vim script function like call_func_retnr() and drop the result.
679 * Returns FAIL when calling the function fails.
680 */
681 int
682call_func_noret(
683 char_u *func,
684 int argc,
685 typval_T *argv)
686{
687 typval_T rettv;
688
689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
690 return FAIL;
691 clear_tv(&rettv);
692 return OK;
693}
694
695/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100696 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100697 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000698 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000699 */
700 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701call_func_retstr(
702 char_u *func,
703 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200704 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705{
706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000708
Bram Moolenaarded27a12018-08-01 19:06:03 +0200709 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 return NULL;
711
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100712 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return retval;
715}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100718 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100719 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000720 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 */
722 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723call_func_retlist(
724 char_u *func,
725 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200726 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727{
728 typval_T rettv;
729
Bram Moolenaarded27a12018-08-01 19:06:03 +0200730 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 return NULL;
732
733 if (rettv.v_type != VAR_LIST)
734 {
735 clear_tv(&rettv);
736 return NULL;
737 }
738
739 return rettv.vval.v_list;
740}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#ifdef FEAT_FOLDING
743/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100744 * Evaluate "arg", which is 'foldexpr'.
745 * Note: caller must set "curwin" to match "arg".
746 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
747 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100750eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200753 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000755 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
756 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000759 if (use_sandbox)
760 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200761 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200763 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 retval = 0;
765 else
766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100767 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000768 if (tv.v_type == VAR_NUMBER)
769 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000770 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 retval = 0;
772 else
773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // If the result is a string, check if there is a non-digit before
775 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (!VIM_ISDIGIT(*s) && *s != '-')
778 *cp = *s++;
779 retval = atol((char *)s);
780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000784 if (use_sandbox)
785 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200786 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200787 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791#endif
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 * Get an lval: variable, Dict item or List item that can be assigned a value
795 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
796 * "name.key", "name.key[expr]" etc.
797 * Indexing only works if "name" is an existing List or Dictionary.
798 * "name" points to the start of the name.
799 * If "rettv" is not NULL it points to the value to be assigned.
800 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
801 * wrong; must end in space or cmd separator.
802 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * flags:
804 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100805 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100806 * GLV_NO_AUTOLOAD: do not use script autoloading
807 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000809 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000811 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813get_lval(
814 char_u *name,
815 typval_T *rettv,
816 lval_T *lp,
817 int unlet,
818 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100819 int flags, // GLV_ values
820 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 char_u *expr_start, *expr_end;
824 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 dictitem_T *v;
826 typval_T var1;
827 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000829 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100831 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100832 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100833 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100835 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200836 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200842 lp->ll_name_end = find_name_end(name, NULL, NULL,
843 FNE_INCL_BR | fne_flags);
844 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 }
846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100847 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 if (expr_start != NULL)
851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100853 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 && *p != '[' && *p != '.')
855 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200856 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 return NULL;
858 }
859
860 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
861 if (lp->ll_exp_name == NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Report an invalid expression in braces, unless the
864 // expression evaluation has been cancelled due to an
865 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 if (!aborting() && !quiet)
867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000868 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 return NULL;
871 }
872 }
873 lp->ll_name = lp->ll_exp_name;
874 }
875 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 lp->ll_name = name;
878
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 // "a: type" is declaring variable "a" with a type, not "a:".
882 if (p == name + 2 && p[-1] == ':')
883 {
884 --p;
885 lp->ll_name_end = p;
886 }
887 if (*p == ':')
888 {
889 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
890 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100893 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
894 if (lp->ll_type == NULL && !quiet)
895 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200896 lp->ll_name_end = tp;
897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 }
899 }
900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100901 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
903 return p;
904
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200905 if (in_vim9script() && lval_root != NULL)
906 {
907 // using local variable
908 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200909 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200910 }
911 else
912 {
913 cc = *p;
914 *p = NUL;
915 // When we would write to the variable pass &ht and prevent autoload.
916 writing = !(flags & GLV_READ_ONLY);
917 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100918 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200919 if (v == NULL && !quiet)
920 semsg(_(e_undefined_variable_str), lp->ll_name);
921 *p = cc;
922 if (v == NULL)
923 return NULL;
924 lp->ll_tv = &v->di_tv;
925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000926
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100927 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
928 {
929 if (!quiet)
930 semsg(_(e_variable_already_declared), lp->ll_name);
931 return NULL;
932 }
933
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 /*
935 * Loop until no more [idx] or .key is following.
936 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100937 var1.v_type = VAR_UNKNOWN;
938 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200939 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200941 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
942 {
943 if (!quiet)
944 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
945 return NULL;
946 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200947 if (lp->ll_tv->v_type != VAR_LIST
948 && lp->ll_tv->v_type != VAR_DICT
949 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200955
956 // a NULL list/blob works like an empty list/blob, allocate one now.
957 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
958 rettv_list_alloc(lp->ll_tv);
959 else if (lp->ll_tv->v_type == VAR_BLOB
960 && lp->ll_tv->vval.v_blob == NULL)
961 rettv_blob_alloc(lp->ll_tv);
962
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000969
Bram Moolenaar10c65862020-10-08 21:16:42 +0200970 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200971 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200972 && lp->ll_tv == &v->di_tv
973 && ht != NULL && ht == get_script_local_ht())
974 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200975 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200976
977 // Vim9 script local variable: get the type
978 if (sv != NULL)
979 lp->ll_valtype = sv->sv_type;
980 }
981
Bram Moolenaar8c711452005-01-14 21:53:12 +0000982 len = -1;
983 if (*p == '.')
984 {
985 key = p + 1;
986 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
987 ;
988 if (len == 0)
989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100991 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 }
994 p = key + len;
995 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000996 else
997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000999 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 if (*p == ':')
1001 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001002 else
1003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001005 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001007 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001009 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001010 clear_tv(&var1);
1011 return NULL;
1012 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001013 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 }
1015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001016 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 if (*p == ':')
1018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001022 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001023 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001025 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001026 if (rettv != NULL
1027 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001028 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001029 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001030 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001033 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
1037 p = skipwhite(p + 1);
1038 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 else
1041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001043 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001044 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001046 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001049 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001051 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001053 clear_tv(&var2);
1054 return NULL;
1055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001061
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001065 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001066 clear_tv(&var1);
1067 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 }
1070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 ++p;
1073 }
1074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 {
1077 if (len == -1)
1078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001079 // "[key]": get key from "var1"
1080 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001081 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001082 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 }
1086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001087 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001088
1089 // a NULL dict is equivalent with an empty dict
1090 if (lp->ll_tv->vval.v_dict == NULL)
1091 {
1092 lp->ll_tv->vval.v_dict = dict_alloc();
1093 if (lp->ll_tv->vval.v_dict == NULL)
1094 {
1095 clear_tv(&var1);
1096 return NULL;
1097 }
1098 ++lp->ll_tv->vval.v_dict->dv_refcount;
1099 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001100 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001101
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001102 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 // When assigning to a scope dictionary check that a function and
1105 // variable name is valid (only variable name unless it is l: or
1106 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001108 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 int prevval;
1110 int wrong;
1111
1112 if (len != -1)
1113 {
1114 prevval = key[len];
1115 key[len] = NUL;
1116 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001117 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001118 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001119 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1120 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001121 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001122 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001123 if (len != -1)
1124 key[len] = prevval;
1125 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001126 {
1127 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001129 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 }
1131
Bram Moolenaar10c65862020-10-08 21:16:42 +02001132 if (lp->ll_valtype != NULL)
1133 // use the type of the member
1134 lp->ll_valtype = lp->ll_valtype->tt_member;
1135
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001138 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001139 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001140 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001142 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001143 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001144 return NULL;
1145 }
1146
Bram Moolenaar31b81602019-02-10 22:14:27 +01001147 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001151 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001152 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 }
1155 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 p = NULL;
1162 break;
1163 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001165 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001166 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1167 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001168 {
1169 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001170 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001171 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001172
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001173 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001176 else if (lp->ll_tv->v_type == VAR_BLOB)
1177 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001178 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1179
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180 /*
1181 * Get the number and item for the only or first index of the List.
1182 */
1183 if (empty1)
1184 lp->ll_n1 = 0;
1185 else
1186 // is number or string
1187 lp->ll_n1 = (long)tv_get_number(&var1);
1188 clear_tv(&var1);
1189
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001190 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001191 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001192 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001193 return NULL;
1194 }
1195 if (lp->ll_range && !lp->ll_empty2)
1196 {
1197 lp->ll_n2 = (long)tv_get_number(&var2);
1198 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001199 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1200 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001201 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001202 }
1203 lp->ll_blob = lp->ll_tv->vval.v_blob;
1204 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001205 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 else
1208 {
1209 /*
1210 * Get the number and item for the only or first index of the List.
1211 */
1212 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001215 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001216 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001217 clear_tv(&var1);
1218
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001219 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001221 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001222 if (lp->ll_li == NULL)
1223 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001224 clear_tv(&var2);
1225 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 }
1227
Bram Moolenaar10c65862020-10-08 21:16:42 +02001228 if (lp->ll_valtype != NULL)
1229 // use the type of the member
1230 lp->ll_valtype = lp->ll_valtype->tt_member;
1231
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 /*
1233 * May need to find the item or absolute index for the second
1234 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 * When no index given: "lp->ll_empty2" is TRUE.
1236 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001240 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001243 if (check_range_index_two(lp->ll_list,
1244 &lp->ll_n1, lp->ll_li,
1245 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001247 }
1248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 }
1252
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return p;
1256}
1257
1258/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001259 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263{
1264 vim_free(lp->ll_exp_name);
1265 vim_free(lp->ll_newkey);
1266}
1267
1268/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001269 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001271 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1272 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001273 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275set_var_lval(
1276 lval_T *lp,
1277 char_u *endp,
1278 typval_T *rettv,
1279 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001280 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1281 char_u *op,
1282 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283{
1284 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001285 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286
1287 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001289 cc = *endp;
1290 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001291 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1292 return;
1293
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 if (lp->ll_blob != NULL)
1295 {
1296 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001297
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 if (op != NULL && *op != '=')
1299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001300 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 return;
1302 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001303 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001304 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001305
1306 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1307 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001308 if (lp->ll_empty2)
1309 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1310
Bram Moolenaar68452172021-04-12 21:21:02 +02001311 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1312 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 }
1315 else
1316 {
1317 val = (int)tv_get_number_chk(rettv, &error);
1318 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001319 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 }
1321 }
1322 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001324 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001326 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1327 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001328 {
1329 emsg(_(e_cannot_mod));
1330 *endp = cc;
1331 return;
1332 }
1333
Bram Moolenaarff697e62019-02-12 22:28:33 +01001334 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001335 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001336 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001337 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001338 {
1339 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001340 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1341 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001343 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1344 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001345 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001348 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001349 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001350 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1351 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001352 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001353 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1354 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001355 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001358 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001359 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001360 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001361 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 else if (lp->ll_range)
1363 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001364 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1365 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001366 {
1367 emsg(_("E996: Cannot lock a range"));
1368 return;
1369 }
1370
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001371 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1372 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 }
1374 else
1375 {
1376 /*
1377 * Assign to a List or Dictionary item.
1378 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001379 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1380 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001381 {
1382 emsg(_("E996: Cannot lock a list or dict"));
1383 return;
1384 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001385
1386 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001387 && check_typval_arg_type(lp->ll_valtype, rettv,
1388 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001389 return;
1390
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 if (lp->ll_newkey != NULL)
1392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 if (op != NULL && *op != '=')
1394 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001395 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 return;
1397 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001398 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1399 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001400 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001403 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 if (di == NULL)
1405 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001406 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1407 {
1408 vim_free(di);
1409 return;
1410 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 else if (op != NULL && *op != '=')
1414 {
1415 tv_op(lp->ll_tv, rettv, op);
1416 return;
1417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001420
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 /*
1422 * Assign the value to the variable or list item.
1423 */
1424 if (copy)
1425 copy_tv(rettv, lp->ll_tv);
1426 else
1427 {
1428 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001429 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 }
1432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433}
1434
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001436 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1437 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 * Returns OK or FAIL.
1439 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001441tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001443 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 char_u numbuf[NUMBUFLEN];
1445 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001446 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001448 // Can't do anything with a Funcref or Dict on the right.
1449 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001450 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001451 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1452 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 {
1454 switch (tv1->v_type)
1455 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001456 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001457 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 case VAR_DICT:
1460 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001461 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001462 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001463 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001464 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001465 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001466 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 break;
1468
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 case VAR_BLOB:
1470 if (*op != '+' || tv2->v_type != VAR_BLOB)
1471 break;
1472 // BLOB += BLOB
1473 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1474 {
1475 blob_T *b1 = tv1->vval.v_blob;
1476 blob_T *b2 = tv2->vval.v_blob;
1477 int i, len = blob_len(b2);
1478 for (i = 0; i < len; i++)
1479 ga_append(&b1->bv_ga, blob_get(b2, i));
1480 }
1481 return OK;
1482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 case VAR_LIST:
1484 if (*op != '+' || tv2->v_type != VAR_LIST)
1485 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001486 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001487 if (tv2->vval.v_list != NULL)
1488 {
1489 if (tv1->vval.v_list == NULL)
1490 {
1491 tv1->vval.v_list = tv2->vval.v_list;
1492 ++tv1->vval.v_list->lv_refcount;
1493 }
1494 else
1495 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1496 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 return OK;
1498
1499 case VAR_NUMBER:
1500 case VAR_STRING:
1501 if (tv2->v_type == VAR_LIST)
1502 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001505 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001507#ifdef FEAT_FLOAT
1508 if (tv2->v_type == VAR_FLOAT)
1509 {
1510 float_T f = n;
1511
Bram Moolenaarff697e62019-02-12 22:28:33 +01001512 if (*op == '%')
1513 break;
1514 switch (*op)
1515 {
1516 case '+': f += tv2->vval.v_float; break;
1517 case '-': f -= tv2->vval.v_float; break;
1518 case '*': f *= tv2->vval.v_float; break;
1519 case '/': f /= tv2->vval.v_float; break;
1520 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001521 clear_tv(tv1);
1522 tv1->v_type = VAR_FLOAT;
1523 tv1->vval.v_float = f;
1524 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001526#endif
1527 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001528 switch (*op)
1529 {
1530 case '+': n += tv_get_number(tv2); break;
1531 case '-': n -= tv_get_number(tv2); break;
1532 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001533 case '/': n = num_divide(n, tv_get_number(tv2),
1534 &failed); break;
1535 case '%': n = num_modulus(n, tv_get_number(tv2),
1536 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538 clear_tv(tv1);
1539 tv1->v_type = VAR_NUMBER;
1540 tv1->vval.v_number = n;
1541 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 }
1543 else
1544 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001545 if (tv2->v_type == VAR_FLOAT)
1546 break;
1547
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 s = tv_get_string(tv1);
1550 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001551 clear_tv(tv1);
1552 tv1->v_type = VAR_STRING;
1553 tv1->vval.v_string = s;
1554 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001555 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001558#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 {
1560 float_T f;
1561
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 if (*op == '%' || *op == '.'
1563 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564 && tv2->v_type != VAR_NUMBER
1565 && tv2->v_type != VAR_STRING))
1566 break;
1567 if (tv2->v_type == VAR_FLOAT)
1568 f = tv2->vval.v_float;
1569 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001570 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 switch (*op)
1572 {
1573 case '+': tv1->vval.v_float += f; break;
1574 case '-': tv1->vval.v_float -= f; break;
1575 case '*': tv1->vval.v_float *= f; break;
1576 case '/': tv1->vval.v_float /= f; break;
1577 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001579#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001580 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001581 }
1582 }
1583
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001584 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585 return FAIL;
1586}
1587
1588/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001589 * Evaluate the expression used in a ":for var in expr" command.
1590 * "arg" points to "var".
1591 * Set "*errp" to TRUE for an error, FALSE otherwise;
1592 * Return a pointer that holds the info. Null when there is an error.
1593 */
1594 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595eval_for_line(
1596 char_u *arg,
1597 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001598 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001599 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001600{
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001602 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001603 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T tv;
1605 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001606 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001608 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001610 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 if (fi == NULL)
1612 return NULL;
1613
Bram Moolenaar404557e2021-07-05 21:41:48 +02001614 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1615 &fi->fi_semicolon, FALSE);
1616 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 return fi;
1618
Bram Moolenaar404557e2021-07-05 21:41:48 +02001619 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001620 if (expr[0] != 'i' || expr[1] != 'n'
1621 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001623 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1624 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1625 else
1626 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 return fi;
1628 }
1629
1630 if (skip)
1631 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001632 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1633 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 {
1635 *errp = FALSE;
1636 if (!skip)
1637 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001638 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001639 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001640 l = tv.vval.v_list;
1641 if (l == NULL)
1642 {
1643 // a null list is like an empty list: do nothing
1644 clear_tv(&tv);
1645 }
1646 else
1647 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001649 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001651 // No need to increment the refcount, it's already set for
1652 // the list being used in "tv".
1653 fi->fi_list = l;
1654 list_add_watch(l, &fi->fi_lw);
1655 fi->fi_lw.lw_item = l->lv_first;
1656 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001657 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001658 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001659 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001660 fi->fi_bi = 0;
1661 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001662 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001663 typval_T btv;
1664
1665 // Make a copy, so that the iteration still works when the
1666 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001668 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001669 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001670 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001671 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001672 else if (tv.v_type == VAR_STRING)
1673 {
1674 fi->fi_byte_idx = 0;
1675 fi->fi_string = tv.vval.v_string;
1676 tv.vval.v_string = NULL;
1677 if (fi->fi_string == NULL)
1678 fi->fi_string = vim_strsave((char_u *)"");
1679 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 else
1681 {
Sean Dewar80d73952021-08-04 19:25:54 +02001682 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 }
1685 }
1686 }
1687 if (skip)
1688 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001689 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690
1691 return fi;
1692}
1693
1694/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001695 * Used when looping over a :for line, skip the "in expr" part.
1696 */
1697 void
1698skip_for_lines(void *fi_void, evalarg_T *evalarg)
1699{
1700 forinfo_T *fi = (forinfo_T *)fi_void;
1701 int i;
1702
1703 for (i = 0; i < fi->fi_break_count; ++i)
1704 eval_next_line(evalarg);
1705}
1706
1707/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 * Use the first item in a ":for" list. Advance to the next.
1709 * Assign the values to the variable (list). "arg" points to the first one.
1710 * Return TRUE when a valid item was found, FALSE when at end of list or
1711 * something wrong.
1712 */
1713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001716 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001718 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001719 ? (ASSIGN_FINAL
1720 // first round: error if variable exists
1721 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1722 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001723 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001724 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001725 int skip_assign = in_vim9script() && arg[0] == '_'
1726 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001728 if (fi->fi_blob != NULL)
1729 {
1730 typval_T tv;
1731
1732 if (fi->fi_bi >= blob_len(fi->fi_blob))
1733 return FALSE;
1734 tv.v_type = VAR_NUMBER;
1735 tv.v_lock = VAR_FIXED;
1736 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1737 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001738 if (skip_assign)
1739 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001740 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001741 fi->fi_varcount, flag, NULL) == OK;
1742 }
1743
1744 if (fi->fi_string != NULL)
1745 {
1746 typval_T tv;
1747 int len;
1748
1749 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1750 if (len == 0)
1751 return FALSE;
1752 tv.v_type = VAR_STRING;
1753 tv.v_lock = VAR_FIXED;
1754 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1755 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001756 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001757 if (skip_assign)
1758 result = TRUE;
1759 else
1760 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001761 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001762 vim_free(tv.vval.v_string);
1763 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 }
1765
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 item = fi->fi_lw.lw_item;
1767 if (item == NULL)
1768 result = FALSE;
1769 else
1770 {
1771 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001772 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001773 if (skip_assign)
1774 result = TRUE;
1775 else
1776 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001777 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 }
1779 return result;
1780}
1781
1782/*
1783 * Free the structure used to store info used by ":for".
1784 */
1785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787{
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001790 if (fi == NULL)
1791 return;
1792 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001793 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001795 list_unref(fi->fi_list);
1796 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001797 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001798 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001799 else
1800 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 vim_free(fi);
1802}
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805set_context_for_expression(
1806 expand_T *xp,
1807 char_u *arg,
1808 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001810 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001814 if (cmdidx == CMD_let || cmdidx == CMD_var
1815 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
1817 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001820 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 {
1823 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001824 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001825 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 break;
1827 }
1828 return;
1829 }
1830 }
1831 else
1832 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1833 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 while ((xp->xp_pattern = vim_strpbrk(arg,
1835 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1836 {
1837 c = *xp->xp_pattern;
1838 if (c == '&')
1839 {
1840 c = xp->xp_pattern[1];
1841 if (c == '&')
1842 {
1843 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001844 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001849 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1850 xp->xp_pattern += 2;
1851
1852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 else if (c == '$')
1855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001856 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 xp->xp_context = EXPAND_ENV_VARS;
1858 }
1859 else if (c == '=')
1860 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001861 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 xp->xp_context = EXPAND_EXPRESSION;
1863 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001864 else if (c == '#'
1865 && xp->xp_context == EXPAND_EXPRESSION)
1866 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001867 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001868 break;
1869 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001870 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 && xp->xp_context == EXPAND_FUNCTIONS
1872 && vim_strchr(xp->xp_pattern, '(') == NULL)
1873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 break;
1876 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001877 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001879 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1882 if (c == '\\' && xp->xp_pattern[1] != NUL)
1883 ++xp->xp_pattern;
1884 xp->xp_context = EXPAND_NOTHING;
1885 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001888 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1890 /* skip */ ;
1891 xp->xp_context = EXPAND_NOTHING;
1892 }
1893 else if (c == '|')
1894 {
1895 if (xp->xp_pattern[1] == '|')
1896 {
1897 ++xp->xp_pattern;
1898 xp->xp_context = EXPAND_EXPRESSION;
1899 }
1900 else
1901 xp->xp_context = EXPAND_COMMANDS;
1902 }
1903 else
1904 xp->xp_context = EXPAND_EXPRESSION;
1905 }
1906 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001907 // Doesn't look like something valid, expand as an expression
1908 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 arg = xp->xp_pattern;
1911 if (*arg != NUL)
1912 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1913 /* skip */ ;
1914 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001915
1916 // ":exe one two" completes "two"
1917 if ((cmdidx == CMD_execute
1918 || cmdidx == CMD_echo
1919 || cmdidx == CMD_echon
1920 || cmdidx == CMD_echomsg)
1921 && xp->xp_context == EXPAND_EXPRESSION)
1922 {
1923 for (;;)
1924 {
1925 char_u *n = skiptowhite(arg);
1926
1927 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1928 break;
1929 arg = skipwhite(n);
1930 }
1931 }
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 xp->xp_pattern = arg;
1934}
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001937 * Return TRUE if "pat" matches "text".
1938 * Does not use 'cpo' and always uses 'magic'.
1939 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001940 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001941pattern_match(char_u *pat, char_u *text, int ic)
1942{
1943 int matches = FALSE;
1944 char_u *save_cpo;
1945 regmatch_T regmatch;
1946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001947 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001948 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001949 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001950 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1951 if (regmatch.regprog != NULL)
1952 {
1953 regmatch.rm_ic = ic;
1954 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1955 vim_regfree(regmatch.regprog);
1956 }
1957 p_cpo = save_cpo;
1958 return matches;
1959}
1960
1961/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001962 * Handle a name followed by "(". Both for just "name(arg)" and for
1963 * "expr->name(arg)".
1964 * Returns OK or FAIL.
1965 */
1966 static int
1967eval_func(
1968 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001969 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001970 char_u *name,
1971 int name_len,
1972 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001973 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 typval_T *basetv) // "expr" for "expr->name(arg)"
1975{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001976 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001977 char_u *s = name;
1978 int len = name_len;
1979 partial_T *partial;
1980 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001981 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001982
1983 if (!evaluate)
1984 check_vars(s, len);
1985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001986 // If "s" is the name of a variable of type VAR_FUNC
1987 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001988 s = deref_func_name(s, &len, &partial,
1989 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001991 // Need to make a copy, in case evaluating the arguments makes
1992 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001993 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001994 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995 ret = FAIL;
1996 else
1997 {
1998 funcexe_T funcexe;
1999
2000 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002001 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 funcexe.firstline = curwin->w_cursor.lnum;
2003 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004 funcexe.evaluate = evaluate;
2005 funcexe.partial = partial;
2006 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002007 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002008 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002009 }
2010 vim_free(s);
2011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // If evaluate is FALSE rettv->v_type was not set in
2013 // get_func_tv, but it's needed in handle_subscript() to parse
2014 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2016 {
2017 rettv->vval.v_string = NULL;
2018 rettv->v_type = VAR_FUNC;
2019 }
2020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002021 // Stop the expression evaluation when immediately
2022 // aborting on error, or when an interrupt occurred or
2023 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 if (evaluate && aborting())
2025 {
2026 if (ret == OK)
2027 clear_tv(rettv);
2028 ret = FAIL;
2029 }
2030 return ret;
2031}
2032
2033/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002034 * Get the next line source line without advancing. But do skip over comment
2035 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002036 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002037 */
2038 static char_u *
2039getline_peek_skip_comments(evalarg_T *evalarg)
2040{
2041 for (;;)
2042 {
2043 char_u *next = getline_peek(evalarg->eval_getline,
2044 evalarg->eval_cookie);
2045 char_u *p;
2046
2047 if (next == NULL)
2048 break;
2049 p = skipwhite(next);
2050 if (*p != NUL && !vim9_comment_start(p))
2051 return next;
2052 (void)eval_next_line(evalarg);
2053 }
2054 return NULL;
2055}
2056
2057/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002058 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2059 * comment) and there is a next line, return the next line (skipping blanks)
2060 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002061 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2062 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002063 * "arg" must point somewhere inside a line, not at the start.
2064 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002065 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002066eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2067{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002068 char_u *p = skipwhite(arg);
2069
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002070 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002071 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002073 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002074 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002075 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002076 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002077
2078 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002079 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002080 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002081 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082
Bram Moolenaar9d489562020-07-30 20:08:50 +02002083 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002084 {
2085 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002086 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002087 }
2088 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002089 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090}
2091
2092/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002093 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002094 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002095 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002096 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002097eval_next_line(evalarg_T *evalarg)
2098{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002099 garray_T *gap = &evalarg->eval_ga;
2100 char_u *line;
2101
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002102 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002103 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2104 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002105 else
2106 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002107 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002108 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2109 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002110 char_u *p = skipwhite(line);
2111
2112 // Going to concatenate the lines after parsing. For an empty or
2113 // comment line use an empty string.
2114 if (*p == NUL || vim9_comment_start(p))
2115 {
2116 vim_free(line);
2117 line = vim_strsave((char_u *)"");
2118 }
2119
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002120 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2121 ++gap->ga_len;
2122 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002123 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 {
2125 vim_free(evalarg->eval_tofree);
2126 evalarg->eval_tofree = line;
2127 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002128
2129 // Advanced to the next line, "arg" no longer points into the previous
2130 // line.
2131 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2132
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002133 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002134}
2135
Bram Moolenaare6b53242020-07-01 17:28:33 +02002136/*
2137 * Call eval_next_non_blank() and get the next line if needed.
2138 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002139 char_u *
2140skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2141{
2142 int getnext;
2143 char_u *p = skipwhite(arg);
2144
Bram Moolenaar962d7212020-07-04 14:15:00 +02002145 if (evalarg == NULL)
2146 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002147 eval_next_non_blank(p, evalarg, &getnext);
2148 if (getnext)
2149 return eval_next_line(evalarg);
2150 return p;
2151}
2152
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002153/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002154 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002155 */
2156 void
2157clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2158{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002160 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002161 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002162 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002163 if (eap != NULL)
2164 {
2165 // We may need to keep the original command line, e.g. for
2166 // ":let" it has the variable names. But we may also need the
2167 // new one, "nextcmd" points into it. Keep both.
2168 vim_free(eap->cmdline_tofree);
2169 eap->cmdline_tofree = *eap->cmdlinep;
2170 *eap->cmdlinep = evalarg->eval_tofree;
2171 }
2172 else
2173 vim_free(evalarg->eval_tofree);
2174 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002175 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002176
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002177 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2178 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002179 }
2180}
2181
2182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2186 */
2187
2188/*
2189 * Handle zero level expression.
2190 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002192 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002193 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * Return OK or FAIL.
2195 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197eval0(
2198 char_u *arg,
2199 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002200 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 int ret;
2204 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002205 int did_emsg_before = did_emsg;
2206 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002207 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002208 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002211 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002212 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002213
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002214 if (ret != FAIL)
2215 end_error = !ends_excmd2(arg, p);
2216 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /*
2221 * Report the invalid expression unless the expression evaluation has
2222 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002223 * exception, or we already gave a more specific error.
2224 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002226 if (!aborting()
2227 && did_emsg == did_emsg_before
2228 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002229 && (flags & EVAL_CONSTANT) == 0
2230 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002231 {
2232 if (end_error)
2233 semsg(_(e_trailing_arg), p);
2234 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002235 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002236 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002237
2238 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002239 // a next command to avoid more errors, unless "|" is following, which
2240 // could only be a command separator.
2241 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2242 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002245
2246 if (eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002247 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return ret;
2250}
2251
2252/*
2253 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002254 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002255 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 *
2257 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002258 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002260 * Note: "rettv.v_lock" is not set.
2261 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 * Return OK or FAIL.
2263 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002264 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002267 char_u *p;
2268 int getnext;
2269
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002270 CLEAR_POINTER(rettv);
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 /*
2273 * Get the first variable.
2274 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return FAIL;
2277
Bram Moolenaar793648f2020-06-26 21:28:25 +02002278 p = eval_next_non_blank(*arg, evalarg, &getnext);
2279 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002281 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 int result;
2283 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 evalarg_T *evalarg_used = evalarg;
2285 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002287 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002288 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289
2290 if (evalarg == NULL)
2291 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002292 CLEAR_FIELD(local_evalarg);
2293 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002295 orig_flags = evalarg_used->eval_flags;
2296 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298 if (getnext)
2299 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002300 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002301 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002302 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002303 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002304 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 clear_tv(rettv);
2306 return FAIL;
2307 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002308 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002309 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 int error = FALSE;
2315
Bram Moolenaar13106602020-10-04 16:06:05 +02002316 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002317 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002318 else if (vim9script)
2319 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002320 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002322 if (error || !op_falsy || !result)
2323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002324 if (error)
2325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
2328 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002329 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002331 if (op_falsy)
2332 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002333 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002334 {
mityu4ac198c2021-05-28 17:52:40 +02002335 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002336 clear_tv(rettv);
2337 return FAIL;
2338 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 evalarg_used->eval_flags = (op_falsy ? !result : result)
2341 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2342 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002343 {
2344 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002346 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002347 if (!op_falsy || !result)
2348 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002350 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002352 /*
2353 * Check for the ":".
2354 */
2355 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2356 if (*p != ':')
2357 {
2358 emsg(_(e_missing_colon));
2359 if (evaluate && result)
2360 clear_tv(rettv);
2361 evalarg_used->eval_flags = orig_flags;
2362 return FAIL;
2363 }
2364 if (getnext)
2365 *arg = eval_next_line(evalarg_used);
2366 else
2367 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002368 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002369 {
2370 error_white_both(p, 1);
2371 clear_tv(rettv);
2372 evalarg_used->eval_flags = orig_flags;
2373 return FAIL;
2374 }
2375 *arg = p;
2376 }
2377
2378 /*
2379 * Get the third variable. Recursive!
2380 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002381 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 {
mityu4ac198c2021-05-28 17:52:40 +02002383 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002384 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002385 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002386 return FAIL;
2387 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2389 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002390 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002391 if (eval1(arg, &var2, evalarg_used) == FAIL)
2392 {
2393 if (evaluate && result)
2394 clear_tv(rettv);
2395 evalarg_used->eval_flags = orig_flags;
2396 return FAIL;
2397 }
2398 if (evaluate && !result)
2399 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401
2402 if (evalarg == NULL)
2403 clear_evalarg(&local_evalarg, NULL);
2404 else
2405 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407
2408 return OK;
2409}
2410
2411/*
2412 * Handle first level expression:
2413 * expr2 || expr2 || expr2 logical OR
2414 *
2415 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002416 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 *
2418 * Return OK or FAIL.
2419 */
2420 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002423 char_u *p;
2424 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /*
2427 * Get the first variable.
2428 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 return FAIL;
2431
2432 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002435 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 evalarg_T *evalarg_used = evalarg;
2439 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440 int evaluate;
2441 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 long result = FALSE;
2443 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002444 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002445 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002446
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 if (evalarg == NULL)
2448 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002449 CLEAR_FIELD(local_evalarg);
2450 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 orig_flags = evalarg_used->eval_flags;
2453 evaluate = orig_flags & EVAL_EVALUATE;
2454 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002456 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002457 result = tv_get_bool_chk(rettv, &error);
2458 else if (tv_get_number_chk(rettv, &error) != 0)
2459 result = TRUE;
2460 clear_tv(rettv);
2461 if (error)
2462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464
2465 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 while (p[0] == '|' && p[1] == '|')
2469 {
2470 if (getnext)
2471 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002472 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002473 {
2474 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2475 {
2476 error_white_both(p, 2);
2477 clear_tv(rettv);
2478 return FAIL;
2479 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002480 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002481 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482
2483 /*
2484 * Get the second variable.
2485 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002486 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2487 {
mityu4ac198c2021-05-28 17:52:40 +02002488 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002489 clear_tv(rettv);
2490 return FAIL;
2491 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2493 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002494 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497
2498 /*
2499 * Compute the result.
2500 */
2501 if (evaluate && !result)
2502 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002503 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002504 result = tv_get_bool_chk(&var2, &error);
2505 else if (tv_get_number_chk(&var2, &error) != 0)
2506 result = TRUE;
2507 clear_tv(&var2);
2508 if (error)
2509 return FAIL;
2510 }
2511 if (evaluate)
2512 {
2513 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002514 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002515 rettv->v_type = VAR_BOOL;
2516 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002517 }
2518 else
2519 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002520 rettv->v_type = VAR_NUMBER;
2521 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524
2525 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002527
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 if (evalarg == NULL)
2529 clear_evalarg(&local_evalarg, NULL);
2530 else
2531 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533
2534 return OK;
2535}
2536
2537/*
2538 * Handle second level expression:
2539 * expr3 && expr3 && expr3 logical AND
2540 *
2541 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002542 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 *
2544 * Return OK or FAIL.
2545 */
2546 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002547eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002549 char_u *p;
2550 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 /*
2553 * Get the first variable.
2554 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 return FAIL;
2557
2558 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002561 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 evalarg_T *evalarg_used = evalarg;
2565 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002566 int orig_flags;
2567 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 long result = TRUE;
2569 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002570 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002571 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 if (evalarg == NULL)
2574 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 CLEAR_FIELD(local_evalarg);
2576 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 orig_flags = evalarg_used->eval_flags;
2579 evaluate = orig_flags & EVAL_EVALUATE;
2580 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002582 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002583 result = tv_get_bool_chk(rettv, &error);
2584 else if (tv_get_number_chk(rettv, &error) == 0)
2585 result = FALSE;
2586 clear_tv(rettv);
2587 if (error)
2588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
2590
2591 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 while (p[0] == '&' && p[1] == '&')
2595 {
2596 if (getnext)
2597 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002598 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002599 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002600 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002601 {
2602 error_white_both(p, 2);
2603 clear_tv(rettv);
2604 return FAIL;
2605 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002606 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002607 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608
2609 /*
2610 * Get the second variable.
2611 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002612 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2613 {
mityu4ac198c2021-05-28 17:52:40 +02002614 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002615 clear_tv(rettv);
2616 return FAIL;
2617 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2619 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002620 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002621 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002623 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624
2625 /*
2626 * Compute the result.
2627 */
2628 if (evaluate && result)
2629 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002630 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 result = tv_get_bool_chk(&var2, &error);
2632 else if (tv_get_number_chk(&var2, &error) == 0)
2633 result = FALSE;
2634 clear_tv(&var2);
2635 if (error)
2636 return FAIL;
2637 }
2638 if (evaluate)
2639 {
2640 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002642 rettv->v_type = VAR_BOOL;
2643 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 }
2645 else
2646 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 rettv->v_type = VAR_NUMBER;
2648 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651
2652 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002654
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 if (evalarg == NULL)
2656 clear_evalarg(&local_evalarg, NULL);
2657 else
2658 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660
2661 return OK;
2662}
2663
2664/*
2665 * Handle third level expression:
2666 * var1 == var2
2667 * var1 =~ var2
2668 * var1 != var2
2669 * var1 !~ var2
2670 * var1 > var2
2671 * var1 >= var2
2672 * var1 < var2
2673 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002674 * var1 is var2
2675 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *
2677 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002678 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 *
2680 * Return OK or FAIL.
2681 */
2682 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002683eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002686 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002687 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002689 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /*
2692 * Get the first variable.
2693 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 return FAIL;
2696
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002697 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002698 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002701 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002703 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002705 typval_T var2;
2706 int ic;
2707 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002708 int evaluate = evalarg == NULL
2709 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002710
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002711 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002712 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002713 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002714 p = *arg;
2715 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002716 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2717 {
mityu4ac198c2021-05-28 17:52:40 +02002718 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002719 clear_tv(rettv);
2720 return FAIL;
2721 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002722
Bram Moolenaar696ba232020-07-29 21:20:41 +02002723 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2724 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002725 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002726 clear_tv(rettv);
2727 return FAIL;
2728 }
2729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002730 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (p[len] == '?')
2732 {
2733 ic = TRUE;
2734 ++len;
2735 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002736 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else if (p[len] == '#')
2738 {
2739 ic = FALSE;
2740 ++len;
2741 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002742 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002744 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
2747 * Get the second variable.
2748 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002749 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2750 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002751 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002752 clear_tv(rettv);
2753 return FAIL;
2754 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002755 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002758 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 return FAIL;
2760 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002761 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002762 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002763 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002764
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002765 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002766 {
2767 ret = FAIL;
2768 clear_tv(rettv);
2769 }
2770 else
2771 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002772 clear_tv(&var2);
2773 return ret;
2774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776
2777 return OK;
2778}
2779
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002780/*
2781 * Make a copy of blob "tv1" and append blob "tv2".
2782 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 void
2784eval_addblob(typval_T *tv1, typval_T *tv2)
2785{
2786 blob_T *b1 = tv1->vval.v_blob;
2787 blob_T *b2 = tv2->vval.v_blob;
2788 blob_T *b = blob_alloc();
2789 int i;
2790
2791 if (b != NULL)
2792 {
2793 for (i = 0; i < blob_len(b1); i++)
2794 ga_append(&b->bv_ga, blob_get(b1, i));
2795 for (i = 0; i < blob_len(b2); i++)
2796 ga_append(&b->bv_ga, blob_get(b2, i));
2797
2798 clear_tv(tv1);
2799 rettv_blob_set(tv1, b);
2800 }
2801}
2802
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002803/*
2804 * Make a copy of list "tv1" and append list "tv2".
2805 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 int
2807eval_addlist(typval_T *tv1, typval_T *tv2)
2808{
2809 typval_T var3;
2810
2811 // concatenate Lists
2812 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2813 {
2814 clear_tv(tv1);
2815 clear_tv(tv2);
2816 return FAIL;
2817 }
2818 clear_tv(tv1);
2819 *tv1 = var3;
2820 return OK;
2821}
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823/*
2824 * Handle fourth level expression:
2825 * + number addition
2826 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002827 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002828 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 *
2830 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002831 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 *
2833 * Return OK or FAIL.
2834 */
2835 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 /*
2839 * Get the first variable.
2840 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002841 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 return FAIL;
2843
2844 /*
2845 * Repeat computing, until no '+', '-' or '.' is following.
2846 */
2847 for (;;)
2848 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002849 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850 int getnext;
2851 char_u *p;
2852 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002853 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 int concat;
2855 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002856 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002858 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002859 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002860 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 p = eval_next_non_blank(*arg, evalarg, &getnext);
2862 op = *p;
Bram Moolenaar2596a4e2021-08-14 21:35:40 +02002863 concat = op == '.' && (*(p + 1) == '.'
2864 || (current_sctx.sc_version < 2 && !vim9script));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002865 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2866 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002868 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2869 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002870
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002871 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002872 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002874 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002875 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002876 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002877 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002878 {
mityu4ac198c2021-05-28 17:52:40 +02002879 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002880 clear_tv(rettv);
2881 return FAIL;
2882 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002883 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002884 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002885 if ((op != '+' || (rettv->v_type != VAR_LIST
2886 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002887#ifdef FEAT_FLOAT
2888 && (op == '.' || rettv->v_type != VAR_FLOAT)
2889#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002890 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002892 int error = FALSE;
2893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002894 // For "list + ...", an illegal use of the first operand as
2895 // a number cannot be determined before evaluating the 2nd
2896 // operand: if this is also a list, all is ok.
2897 // For "something . ...", "something - ..." or "non-list + ...",
2898 // we know that the first operand needs to be a string or number
2899 // without evaluating the 2nd operand. So check before to avoid
2900 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002901 if (op != '.')
2902 tv_get_number_chk(rettv, &error);
2903 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002904 {
2905 clear_tv(rettv);
2906 return FAIL;
2907 }
2908 }
2909
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 /*
2911 * Get the second variable.
2912 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002913 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002914 {
mityu89dcb4d2021-05-28 13:50:17 +02002915 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002916 clear_tv(rettv);
2917 return FAIL;
2918 }
2919 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002920 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 return FAIL;
2924 }
2925
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
2928 /*
2929 * Compute the result.
2930 */
2931 if (op == '.')
2932 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2934 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002935 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936
Bram Moolenaar2e086612020-08-25 22:37:48 +02002937 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002938 || var2.v_type == VAR_CHANNEL
2939 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002940 semsg(_(e_using_invalid_value_as_string_str),
2941 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002942#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002943 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002944 {
2945 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2946 var2.vval.v_float);
2947 s2 = buf2;
2948 }
2949#endif
2950 else
2951 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002952 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 {
2954 clear_tv(rettv);
2955 clear_tv(&var2);
2956 return FAIL;
2957 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 clear_tv(rettv);
2960 rettv->v_type = VAR_STRING;
2961 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 else if (op == '+' && rettv->v_type == VAR_BLOB
2964 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002966 else if (op == '+' && rettv->v_type == VAR_LIST
2967 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002968 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002970 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 else
2973 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974 int error = FALSE;
2975 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002977 float_T f1 = 0, f2 = 0;
2978
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981 f1 = rettv->vval.v_float;
2982 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002983 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 else
2985#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002987 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 if (error)
2989 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002990 // This can only happen for "list + non-list" or
2991 // "blob + non-blob". For "non-list + ..." or
2992 // "something - ...", we returned before evaluating the
2993 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002995 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996 return FAIL;
2997 }
2998#ifdef FEAT_FLOAT
2999 if (var2.v_type == VAR_FLOAT)
3000 f1 = n1;
3001#endif
3002 }
3003#ifdef FEAT_FLOAT
3004 if (var2.v_type == VAR_FLOAT)
3005 {
3006 f2 = var2.vval.v_float;
3007 n2 = 0;
3008 }
3009 else
3010#endif
3011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (error)
3014 {
3015 clear_tv(rettv);
3016 clear_tv(&var2);
3017 return FAIL;
3018 }
3019#ifdef FEAT_FLOAT
3020 if (rettv->v_type == VAR_FLOAT)
3021 f2 = n2;
3022#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025
3026#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003027 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3029 {
3030 if (op == '+')
3031 f1 = f1 + f2;
3032 else
3033 f1 = f1 - f2;
3034 rettv->v_type = VAR_FLOAT;
3035 rettv->vval.v_float = f1;
3036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#endif
3039 {
3040 if (op == '+')
3041 n1 = n1 + n2;
3042 else
3043 n1 = n1 - n2;
3044 rettv->v_type = VAR_NUMBER;
3045 rettv->vval.v_number = n1;
3046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 }
3051 return OK;
3052}
3053
3054/*
3055 * Handle fifth level expression:
3056 * * number multiplication
3057 * / number division
3058 * % number modulo
3059 *
3060 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003061 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 *
3063 * Return OK or FAIL.
3064 */
3065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066eval6(
3067 char_u **arg,
3068 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003069 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003070 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003072#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003073 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076 /*
3077 * Get the first variable.
3078 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003079 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 return FAIL;
3081
3082 /*
3083 * Repeat computing, until no '*', '/' or '%' is following.
3084 */
3085 for (;;)
3086 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003087 int evaluate;
3088 int getnext;
3089 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003090 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003091 int op;
3092 varnumber_T n1, n2;
3093#ifdef FEAT_FLOAT
3094 float_T f1, f2;
3095#endif
3096 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003098 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003099 p = eval_next_non_blank(*arg, evalarg, &getnext);
3100 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003101 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003103
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003104 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003106 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003107 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003108 {
3109 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3110 {
mityu4ac198c2021-05-28 17:52:40 +02003111 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003112 clear_tv(rettv);
3113 return FAIL;
3114 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003115 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003118#ifdef FEAT_FLOAT
3119 f1 = 0;
3120 f2 = 0;
3121#endif
3122 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003123 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125#ifdef FEAT_FLOAT
3126 if (rettv->v_type == VAR_FLOAT)
3127 {
3128 f1 = rettv->vval.v_float;
3129 use_float = TRUE;
3130 n1 = 0;
3131 }
3132 else
3133#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003134 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003135 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003136 if (error)
3137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 }
3139 else
3140 n1 = 0;
3141
3142 /*
3143 * Get the second variable.
3144 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003145 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3146 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003147 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003148 clear_tv(rettv);
3149 return FAIL;
3150 }
3151 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003152 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 return FAIL;
3154
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003155 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157#ifdef FEAT_FLOAT
3158 if (var2.v_type == VAR_FLOAT)
3159 {
3160 if (!use_float)
3161 {
3162 f1 = n1;
3163 use_float = TRUE;
3164 }
3165 f2 = var2.vval.v_float;
3166 n2 = 0;
3167 }
3168 else
3169#endif
3170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003171 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 clear_tv(&var2);
3173 if (error)
3174 return FAIL;
3175#ifdef FEAT_FLOAT
3176 if (use_float)
3177 f2 = n2;
3178#endif
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /*
3182 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003185#ifdef FEAT_FLOAT
3186 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188 if (op == '*')
3189 f1 = f1 * f2;
3190 else if (op == '/')
3191 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003193 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003194 if (f2 == 0.0)
3195 {
3196 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003197 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003198 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003199 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003200 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003201 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003202 }
3203 else
3204 f1 = f1 / f2;
3205# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003206 // We rely on the floating point library to handle divide
3207 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003209# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 return FAIL;
3215 }
3216 rettv->v_type = VAR_FLOAT;
3217 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003222 int failed = FALSE;
3223
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003224 if (op == '*')
3225 n1 = n1 * n2;
3226 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003227 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003229 n1 = num_modulus(n1, n2, &failed);
3230 if (failed)
3231 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003232
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 rettv->v_type = VAR_NUMBER;
3234 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 }
3238
3239 return OK;
3240}
3241
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003242/*
3243 * Handle a type cast before a base level expression.
3244 * "arg" must point to the first non-white of the expression.
3245 * "arg" is advanced to just after the recognized expression.
3246 * Return OK or FAIL.
3247 */
3248 static int
3249eval7t(
3250 char_u **arg,
3251 typval_T *rettv,
3252 evalarg_T *evalarg,
3253 int want_string) // after "." operator
3254{
3255 type_T *want_type = NULL;
3256 garray_T type_list; // list of pointers to allocated types
3257 int res;
3258 int evaluate = evalarg == NULL ? 0
3259 : (evalarg->eval_flags & EVAL_EVALUATE);
3260
3261 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003262 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3263 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003264 {
3265 ++*arg;
3266 ga_init2(&type_list, sizeof(type_T *), 10);
3267 want_type = parse_type(arg, &type_list, TRUE);
3268 if (want_type == NULL && (evaluate || **arg != '>'))
3269 {
3270 clear_type_list(&type_list);
3271 return FAIL;
3272 }
3273
3274 if (**arg != '>')
3275 {
3276 if (*skipwhite(*arg) == '>')
3277 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3278 else
3279 emsg(_(e_missing_gt));
3280 clear_type_list(&type_list);
3281 return FAIL;
3282 }
3283 ++*arg;
3284 *arg = skipwhite_and_linebreak(*arg, evalarg);
3285 }
3286
3287 res = eval7(arg, rettv, evalarg, want_string);
3288
3289 if (want_type != NULL && evaluate)
3290 {
3291 if (res == OK)
3292 {
3293 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3294
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003295 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003296 {
3297 if (want_type == &t_bool && actual != &t_bool
3298 && (actual->tt_flags & TTFLAG_BOOL_OK))
3299 {
3300 int n = tv2bool(rettv);
3301
3302 // can use "0" and "1" for boolean in some places
3303 clear_tv(rettv);
3304 rettv->v_type = VAR_BOOL;
3305 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3306 }
3307 else
3308 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003309 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003310
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003311 where.wt_variable = TRUE;
3312 res = check_type(want_type, actual, TRUE, where);
3313 }
3314 }
3315 }
3316 clear_type_list(&type_list);
3317 }
3318
3319 return res;
3320}
3321
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003322 int
3323eval_leader(char_u **arg, int vim9)
3324{
3325 char_u *s = *arg;
3326 char_u *p = *arg;
3327
3328 while (*p == '!' || *p == '-' || *p == '+')
3329 {
3330 char_u *n = skipwhite(p + 1);
3331
3332 // ++, --, -+ and +- are not accepted in Vim9 script
3333 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3334 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003335 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003336 return FAIL;
3337 }
3338 p = n;
3339 }
3340 *arg = p;
3341 return OK;
3342}
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Handle sixth level expression:
3346 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003347 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003348 * "string" string constant
3349 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * &option-name option value
3351 * @r register contents
3352 * identifier variable value
3353 * function() function call
3354 * $VAR environment variable
3355 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003356 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003358 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003359 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 *
3361 * Also handle:
3362 * ! in front logical NOT
3363 * - in front unary minus
3364 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 * trailing [] subscript in String or List
3366 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003367 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 *
3369 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003370 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 *
3372 * Return OK or FAIL.
3373 */
3374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003375eval7(
3376 char_u **arg,
3377 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003378 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003381 int evaluate = evalarg != NULL
3382 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 int len;
3384 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u *start_leader, *end_leader;
3386 int ret = OK;
3387 char_u *alias;
3388
3389 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003391 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003396 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 */
3398 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003399 if (eval_leader(arg, in_vim9script()) == FAIL)
3400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 end_leader = *arg;
3402
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003403 if (**arg == '.' && (!isdigit(*(*arg + 1))
3404#ifdef FEAT_FLOAT
3405 || current_sctx.sc_version < 2
3406#endif
3407 ))
3408 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003409 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003410 ++*arg;
3411 return FAIL;
3412 }
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 switch (**arg)
3415 {
3416 /*
3417 * Number constant.
3418 */
3419 case '0':
3420 case '1':
3421 case '2':
3422 case '3':
3423 case '4':
3424 case '5':
3425 case '6':
3426 case '7':
3427 case '8':
3428 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003429 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003430
3431 // Apply prefixed "-" and "+" now. Matters especially when
3432 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003433 if (ret == OK && evaluate && end_leader > start_leader
3434 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003435 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 break;
3437
3438 /*
3439 * String constant: "string".
3440 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003441 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 break;
3443
3444 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003445 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003447 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003448 break;
3449
3450 /*
3451 * List: [expr, expr]
3452 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003453 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 break;
3455
3456 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003457 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003458 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003459 case '#': if (in_vim9script())
3460 {
3461 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3462 }
3463 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003464 {
3465 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003466 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003467 }
3468 else
3469 ret = NOTDONE;
3470 break;
3471
3472 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003473 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003474 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003475 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003476 case '{': if (in_vim9script())
3477 ret = NOTDONE;
3478 else
3479 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003480 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003481 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003482 break;
3483
3484 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003485 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003487 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 break;
3489
3490 /*
3491 * Environment variable: $VAR.
3492 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003493 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495
3496 /*
3497 * Register contents: @r.
3498 */
3499 case '@': ++*arg;
3500 if (evaluate)
3501 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003502 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3503 semsg(_(e_syntax_error_at_str), *arg);
3504 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3505 emsg_invreg(**arg);
3506 else
3507 {
3508 rettv->v_type = VAR_STRING;
3509 rettv->vval.v_string = get_reg_contents(**arg,
3510 GREG_EXPR_SRC);
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 if (**arg != NUL)
3514 ++*arg;
3515 break;
3516
3517 /*
3518 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003519 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003521 case '(': ret = NOTDONE;
3522 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003523 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003524 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003525 if (ret == OK && evaluate)
3526 {
3527 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3528
Bram Moolenaara9931532021-06-12 15:58:16 +02003529 // Compile it here to get the return type. The return
3530 // type is optional, when it's missing use t_unknown.
3531 // This is recognized in compile_return().
3532 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3533 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003534 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003535 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003536 {
3537 clear_tv(rettv);
3538 ret = FAIL;
3539 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003540 }
3541 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003542 if (ret == NOTDONE)
3543 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003544 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003545 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003546
Bram Moolenaar9215f012020-06-27 21:18:00 +02003547 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003548 if (**arg == ')')
3549 ++*arg;
3550 else if (ret == OK)
3551 {
3552 emsg(_(e_missing_close));
3553 clear_tv(rettv);
3554 ret = FAIL;
3555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 break;
3558
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 break;
3561 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562
3563 if (ret == NOTDONE)
3564 {
3565 /*
3566 * Must be a variable or function name.
3567 * Can also be a curly-braces kind of name: {expr}.
3568 */
3569 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003570 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 if (alias != NULL)
3572 s = alias;
3573
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003574 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 ret = FAIL;
3576 else
3577 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003578 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3579
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003580 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003581 {
3582 emsg(_(e_cannot_use_underscore_here));
3583 ret = FAIL;
3584 }
3585 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003586 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003587 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003588 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003589 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003590 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003591 else if (flags & EVAL_CONSTANT)
3592 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003594 {
3595 // get the value of "true", "false" or a variable
3596 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3597 {
3598 rettv->v_type = VAR_BOOL;
3599 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003600 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003601 }
3602 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003603 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003604 {
3605 rettv->v_type = VAR_BOOL;
3606 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003607 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003608 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003609 else if (len == 4 && in_vim9script()
3610 && STRNCMP(s, "null", 4) == 0)
3611 {
3612 rettv->v_type = VAR_SPECIAL;
3613 rettv->vval.v_number = VVAL_NULL;
3614 ret = OK;
3615 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003616 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003617 ret = eval_variable(s, len, rettv, NULL,
3618 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003619 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003620 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003621 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003622 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003623 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003624 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003627 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 }
3629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3631 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003632 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003633 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635 /*
3636 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3637 */
3638 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003639 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640 return ret;
3641}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003642
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003643/*
3644 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003645 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003646 * Adjusts "end_leaderp" until it is at "start_leader".
3647 */
3648 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003649eval7_leader(
3650 typval_T *rettv,
3651 int numeric_only,
3652 char_u *start_leader,
3653 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003654{
3655 char_u *end_leader = *end_leaderp;
3656 int ret = OK;
3657 int error = FALSE;
3658 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003659 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003660#ifdef FEAT_FLOAT
3661 float_T f = 0.0;
3662
3663 if (rettv->v_type == VAR_FLOAT)
3664 f = rettv->vval.v_float;
3665 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003666#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003667 {
3668 while (VIM_ISWHITE(end_leader[-1]))
3669 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003670 if (in_vim9script() && end_leader[-1] == '!')
3671 val = tv2bool(rettv);
3672 else
3673 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003674 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003675 if (error)
3676 {
3677 clear_tv(rettv);
3678 ret = FAIL;
3679 }
3680 else
3681 {
3682 while (end_leader > start_leader)
3683 {
3684 --end_leader;
3685 if (*end_leader == '!')
3686 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003687 if (numeric_only)
3688 {
3689 ++end_leader;
3690 break;
3691 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003692#ifdef FEAT_FLOAT
3693 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003694 {
3695 if (in_vim9script())
3696 {
3697 rettv->v_type = VAR_BOOL;
3698 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3699 }
3700 else
3701 f = !f;
3702 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003703 else
3704#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003705 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003707 type = VAR_BOOL;
3708 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709 }
3710 else if (*end_leader == '-')
3711 {
3712#ifdef FEAT_FLOAT
3713 if (rettv->v_type == VAR_FLOAT)
3714 f = -f;
3715 else
3716#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003717 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003718 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003719 type = VAR_NUMBER;
3720 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721 }
3722 }
3723#ifdef FEAT_FLOAT
3724 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003732 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003733 if (in_vim9script())
3734 rettv->v_type = type;
3735 else
3736 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003740 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 return ret;
3742}
3743
3744/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003745 * Call the function referred to in "rettv".
3746 */
3747 static int
3748call_func_rettv(
3749 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003750 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003751 typval_T *rettv,
3752 int evaluate,
3753 dict_T *selfdict,
3754 typval_T *basetv)
3755{
3756 partial_T *pt = NULL;
3757 funcexe_T funcexe;
3758 typval_T functv;
3759 char_u *s;
3760 int ret;
3761
3762 // need to copy the funcref so that we can clear rettv
3763 if (evaluate)
3764 {
3765 functv = *rettv;
3766 rettv->v_type = VAR_UNKNOWN;
3767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 if (functv.v_type == VAR_PARTIAL)
3770 {
3771 pt = functv.vval.v_partial;
3772 s = partial_name(pt);
3773 }
3774 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003775 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003776 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003777 if (s == NULL || *s == NUL)
3778 {
3779 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003780 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003781 goto theend;
3782 }
3783 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003784 }
3785 else
3786 s = (char_u *)"";
3787
Bram Moolenaara80faa82020-04-12 19:37:17 +02003788 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003789 funcexe.firstline = curwin->w_cursor.lnum;
3790 funcexe.lastline = curwin->w_cursor.lnum;
3791 funcexe.evaluate = evaluate;
3792 funcexe.partial = pt;
3793 funcexe.selfdict = selfdict;
3794 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003795 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003796
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003797theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003798 // Clear the funcref afterwards, so that deleting it while
3799 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003800 if (evaluate)
3801 clear_tv(&functv);
3802
3803 return ret;
3804}
3805
3806/*
3807 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003808 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3810 */
3811 static int
3812eval_lambda(
3813 char_u **arg,
3814 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003815 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003817{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003818 int evaluate = evalarg != NULL
3819 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 typval_T base = *rettv;
3821 int ret;
3822
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003823 rettv->v_type = VAR_UNKNOWN;
3824
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003825 if (**arg == '{')
3826 {
3827 // ->{lambda}()
3828 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3829 }
3830 else
3831 {
3832 // ->(lambda)()
3833 ++*arg;
3834 ret = eval1(arg, rettv, evalarg);
3835 *arg = skipwhite_and_linebreak(*arg, evalarg);
3836 if (**arg != ')')
3837 {
3838 emsg(_(e_missing_close));
3839 ret = FAIL;
3840 }
3841 ++*arg;
3842 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003843 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003844 return FAIL;
3845 else if (**arg != '(')
3846 {
3847 if (verbose)
3848 {
3849 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003850 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003853 }
3854 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003855 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003856 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003857 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003858 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003859
3860 // Clear the funcref afterwards, so that deleting it while
3861 // evaluating the arguments is possible (see test55).
3862 if (evaluate)
3863 clear_tv(&base);
3864
3865 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003866}
3867
3868/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003869 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003870 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003871 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3872 */
3873 static int
3874eval_method(
3875 char_u **arg,
3876 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003877 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003879{
3880 char_u *name;
3881 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003882 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003883 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003884 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003885 int evaluate = evalarg != NULL
3886 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003888 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003889
Bram Moolenaarac92e252019-08-03 21:58:38 +02003890 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003891 len = get_name_len(arg, &alias, evaluate, TRUE);
3892 if (alias != NULL)
3893 name = alias;
3894
3895 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003896 {
3897 if (verbose)
3898 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003899 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003900 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003901 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003902 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003903 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003904 if (**arg != '(')
3905 {
3906 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003908 ret = FAIL;
3909 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003910 else if (VIM_ISWHITE((*arg)[-1]))
3911 {
3912 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003913 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003914 ret = FAIL;
3915 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003916 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003917 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003918 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003919 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003921 // Clear the funcref afterwards, so that deleting it while
3922 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003923 if (evaluate)
3924 clear_tv(&base);
3925
Bram Moolenaarac92e252019-08-03 21:58:38 +02003926 return ret;
3927}
3928
3929/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003930 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3931 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003932 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3933 */
3934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935eval_index(
3936 char_u **arg,
3937 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003938 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003941 int evaluate = evalarg != NULL
3942 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003943 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003946 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003947 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003948 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003949
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3951 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003953 init_tv(&var1);
3954 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003957 /*
3958 * dict.name
3959 */
3960 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003961 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003963 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003965 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003966 }
3967 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 /*
3970 * something[idx]
3971 *
3972 * Get the (first) variable from inside the [].
3973 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003974 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003975 if (**arg == ':')
3976 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003977 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003979 else if (vim9 && **arg == ':')
3980 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003981 semsg(_(e_white_space_required_before_and_after_str_at_str),
3982 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003983 clear_tv(&var1);
3984 return FAIL;
3985 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003986 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003988#ifdef FEAT_FLOAT
3989 // allow for indexing with float
3990 if (vim9 && rettv->v_type == VAR_DICT
3991 && var1.v_type == VAR_FLOAT)
3992 {
3993 var1.vval.v_string = typval_tostring(&var1, TRUE);
3994 var1.v_type = VAR_STRING;
3995 }
3996#endif
3997 if (tv_get_string_chk(&var1) == NULL)
3998 {
3999 // not a number or string
4000 clear_tv(&var1);
4001 return FAIL;
4002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004
4005 /*
4006 * Get the second variable from inside the [:].
4007 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004008 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 if (**arg == ':')
4010 {
4011 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004012 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004013 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004014 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004015 semsg(_(e_white_space_required_before_and_after_str_at_str),
4016 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004017 if (!empty1)
4018 clear_tv(&var1);
4019 return FAIL;
4020 }
4021 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004022 if (**arg == ']')
4023 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004024 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 if (!empty1)
4027 clear_tv(&var1);
4028 return FAIL;
4029 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004030 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033 if (!empty1)
4034 clear_tv(&var1);
4035 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 return FAIL;
4037 }
4038 }
4039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004041 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004042 if (**arg != ']')
4043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004044 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004046 clear_tv(&var1);
4047 if (range)
4048 clear_tv(&var2);
4049 return FAIL;
4050 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004051 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 }
4053
4054 if (evaluate)
4055 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004056 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004057 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004058 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004059
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004060 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004063 clear_tv(&var2);
4064 return res;
4065 }
4066 return OK;
4067}
4068
4069/*
4070 * Check if "rettv" can have an [index] or [sli:ce]
4071 */
4072 int
4073check_can_index(typval_T *rettv, int evaluate, int verbose)
4074{
4075 switch (rettv->v_type)
4076 {
4077 case VAR_FUNC:
4078 case VAR_PARTIAL:
4079 if (verbose)
4080 emsg(_("E695: Cannot index a Funcref"));
4081 return FAIL;
4082 case VAR_FLOAT:
4083#ifdef FEAT_FLOAT
4084 if (verbose)
4085 emsg(_(e_float_as_string));
4086 return FAIL;
4087#endif
4088 case VAR_BOOL:
4089 case VAR_SPECIAL:
4090 case VAR_JOB:
4091 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004092 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 if (verbose)
4094 emsg(_(e_cannot_index_special_variable));
4095 return FAIL;
4096 case VAR_UNKNOWN:
4097 case VAR_ANY:
4098 case VAR_VOID:
4099 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 emsg(_(e_cannot_index_special_variable));
4102 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004103 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004104 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 case VAR_STRING:
4107 case VAR_LIST:
4108 case VAR_DICT:
4109 case VAR_BLOB:
4110 break;
4111 case VAR_NUMBER:
4112 if (in_vim9script())
4113 emsg(_(e_cannot_index_number));
4114 break;
4115 }
4116 return OK;
4117}
4118
4119/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004120 * slice() function
4121 */
4122 void
4123f_slice(typval_T *argvars, typval_T *rettv)
4124{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004125 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004126 && ((argvars[0].v_type != VAR_STRING
4127 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004128 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004129 && check_for_list_arg(argvars, 0) == FAIL)
4130 || check_for_number_arg(argvars, 1) == FAIL
4131 || check_for_opt_number_arg(argvars, 2) == FAIL))
4132 return;
4133
Bram Moolenaar6601b622021-01-13 21:47:15 +01004134 if (check_can_index(argvars, TRUE, FALSE) == OK)
4135 {
4136 copy_tv(argvars, rettv);
4137 eval_index_inner(rettv, TRUE, argvars + 1,
4138 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4139 TRUE, NULL, 0, FALSE);
4140 }
4141}
4142
4143/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 * Apply index or range to "rettv".
4145 * "var1" is the first index, NULL for [:expr].
4146 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004147 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4148 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4150 */
4151 int
4152eval_index_inner(
4153 typval_T *rettv,
4154 int is_range,
4155 typval_T *var1,
4156 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004157 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 char_u *key,
4159 int keylen,
4160 int verbose)
4161{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 varnumber_T n1, n2 = 0;
4163 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164
4165 n1 = 0;
4166 if (var1 != NULL && rettv->v_type != VAR_DICT)
4167 n1 = tv_get_number(var1);
4168
4169 if (is_range)
4170 {
4171 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 if (verbose)
4174 emsg(_(e_cannot_slice_dictionary));
4175 return FAIL;
4176 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004177 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004179 else
4180 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004181 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004182
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 switch (rettv->v_type)
4184 {
4185 case VAR_UNKNOWN:
4186 case VAR_ANY:
4187 case VAR_VOID:
4188 case VAR_FUNC:
4189 case VAR_PARTIAL:
4190 case VAR_FLOAT:
4191 case VAR_BOOL:
4192 case VAR_SPECIAL:
4193 case VAR_JOB:
4194 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004195 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004196 break; // not evaluating, skipping over subscript
4197
4198 case VAR_NUMBER:
4199 case VAR_STRING:
4200 {
4201 char_u *s = tv_get_string(rettv);
4202
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004204 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004205 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004206 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004207 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004208 else
4209 s = char_from_string(s, n1);
4210 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004211 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // The resulting variable is a substring. If the indexes
4214 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 if (n1 < 0)
4216 {
4217 n1 = len + n1;
4218 if (n1 < 0)
4219 n1 = 0;
4220 }
4221 if (n2 < 0)
4222 n2 = len + n2;
4223 else if (n2 >= len)
4224 n2 = len;
4225 if (n1 >= len || n2 < 0 || n1 > n2)
4226 s = NULL;
4227 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004228 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 }
4230 else
4231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // The resulting variable is a string of a single
4233 // character. If the index is too big or negative the
4234 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 if (n1 >= len || n1 < 0)
4236 s = NULL;
4237 else
4238 s = vim_strnsave(s + n1, 1);
4239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
4241 rettv->v_type = VAR_STRING;
4242 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004243 }
4244 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004245
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004246 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004247 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4248 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004249 break;
4250
4251 case VAR_LIST:
4252 if (var1 == NULL)
4253 n1 = 0;
4254 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004255 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004256 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004257 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 return FAIL;
4259 break;
4260
4261 case VAR_DICT:
4262 {
4263 dictitem_T *item;
4264 typval_T tmp;
4265
4266 if (key == NULL)
4267 {
4268 key = tv_get_string_chk(var1);
4269 if (key == NULL)
4270 return FAIL;
4271 }
4272
4273 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4274
4275 if (item == NULL && verbose)
4276 semsg(_(e_dictkey), key);
4277 if (item == NULL)
4278 return FAIL;
4279
4280 copy_tv(&item->di_tv, &tmp);
4281 clear_tv(rettv);
4282 *rettv = tmp;
4283 }
4284 break;
4285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004286 return OK;
4287}
4288
4289/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004290 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 */
4292 char_u *
4293partial_name(partial_T *pt)
4294{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004295 if (pt != NULL)
4296 {
4297 if (pt->pt_name != NULL)
4298 return pt->pt_name;
4299 if (pt->pt_func != NULL)
4300 return pt->pt_func->uf_name;
4301 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004302 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303}
4304
Bram Moolenaarddecc252016-04-06 22:59:37 +02004305 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004306partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004307{
4308 int i;
4309
4310 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004312 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004313 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 if (pt->pt_name != NULL)
4315 {
4316 func_unref(pt->pt_name);
4317 vim_free(pt->pt_name);
4318 }
4319 else
4320 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004321
Bram Moolenaar54656012021-06-09 20:50:46 +02004322 // "out_up" is no longer used, decrement refcount on partial that owns it.
4323 partial_unref(pt->pt_outer.out_up_partial);
4324
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004325 // Decrease the reference count for the context of a closure. If down
4326 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004327 if (pt->pt_funcstack != NULL)
4328 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004329 --pt->pt_funcstack->fs_refcount;
4330 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004331 }
4332
Bram Moolenaarddecc252016-04-06 22:59:37 +02004333 vim_free(pt);
4334}
4335
4336/*
4337 * Unreference a closure: decrement the reference count and free it when it
4338 * becomes zero.
4339 */
4340 void
4341partial_unref(partial_T *pt)
4342{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004343 if (pt != NULL)
4344 {
4345 if (--pt->pt_refcount <= 0)
4346 partial_free(pt);
4347
4348 // If the reference count goes down to one, the funcstack may be the
4349 // only reference and can be freed if no other partials reference it.
4350 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4351 funcstack_check_refcount(pt->pt_funcstack);
4352 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004353}
4354
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004356 * Return the next (unique) copy ID.
4357 * Used for serializing nested structures.
4358 */
4359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004360get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004361{
4362 current_copyID += COPYID_INC;
4363 return current_copyID;
4364}
4365
4366/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367 * Garbage collection for lists and dictionaries.
4368 *
4369 * We use reference counts to be able to free most items right away when they
4370 * are no longer used. But for composite items it's possible that it becomes
4371 * unused while the reference count is > 0: When there is a recursive
4372 * reference. Example:
4373 * :let l = [1, 2, 3]
4374 * :let d = {9: l}
4375 * :let l[1] = d
4376 *
4377 * Since this is quite unusual we handle this with garbage collection: every
4378 * once in a while find out which lists and dicts are not referenced from any
4379 * variable.
4380 *
4381 * Here is a good reference text about garbage collection (refers to Python
4382 * but it applies to all reference-counting mechanisms):
4383 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004384 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004385
4386/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004387 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004388 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004391 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004392garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004393{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004394 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004395 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396 buf_T *buf;
4397 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004398 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004399 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004400
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004401 if (!testing)
4402 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004403 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004404 want_garbage_collect = FALSE;
4405 may_garbage_collect = FALSE;
4406 garbage_collect_at_exit = FALSE;
4407 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004408
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004409 // The execution stack can grow big, limit the size.
4410 if (exestack.ga_maxlen - exestack.ga_len > 500)
4411 {
4412 size_t new_len;
4413 char_u *pp;
4414 int n;
4415
4416 // Keep 150% of the current size, with a minimum of the growth size.
4417 n = exestack.ga_len / 2;
4418 if (n < exestack.ga_growsize)
4419 n = exestack.ga_growsize;
4420
4421 // Don't make it bigger though.
4422 if (exestack.ga_len + n < exestack.ga_maxlen)
4423 {
4424 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4425 pp = vim_realloc(exestack.ga_data, new_len);
4426 if (pp == NULL)
4427 return FAIL;
4428 exestack.ga_maxlen = exestack.ga_len + n;
4429 exestack.ga_data = pp;
4430 }
4431 }
4432
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004433 // We advance by two because we add one for items referenced through
4434 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004435 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004436
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004437 /*
4438 * 1. Go through all accessible variables and mark all lists and dicts
4439 * with copyID.
4440 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // Don't free variables in the previous_funccal list unless they are only
4443 // referenced through previous_funccal. This must be first, because if
4444 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004448 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004451 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004452 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4453 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004456 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4458 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004459 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4461 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004462#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004463 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004464 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4465 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004466 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004467 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004468 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4469 NULL, NULL);
4470#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004473 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004474 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4475 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004477 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004483 abort = abort || set_ref_in_functions(copyID);
4484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004489 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004490
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004491 // callbacks in buffers
4492 abort = abort || set_ref_in_buffers(copyID);
4493
Bram Moolenaar1dced572012-04-05 16:54:08 +02004494#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004495 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004496#endif
4497
Bram Moolenaardb913952012-06-29 12:54:53 +02004498#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004499 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004500#endif
4501
4502#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004503 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004504#endif
4505
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004507 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004508 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004509#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004510#ifdef FEAT_NETBEANS_INTG
4511 abort = abort || set_ref_in_nb_channel(copyID);
4512#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004513
Bram Moolenaare3188e22016-05-31 21:13:04 +02004514#ifdef FEAT_TIMERS
4515 abort = abort || set_ref_in_timer(copyID);
4516#endif
4517
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004518#ifdef FEAT_QUICKFIX
4519 abort = abort || set_ref_in_quickfix(copyID);
4520#endif
4521
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004522#ifdef FEAT_TERMINAL
4523 abort = abort || set_ref_in_term(copyID);
4524#endif
4525
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004526#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004527 abort = abort || set_ref_in_popups(copyID);
4528#endif
4529
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004531 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 /*
4533 * 2. Free lists and dictionaries that are not referenced.
4534 */
4535 did_free = free_unref_items(copyID);
4536
4537 /*
4538 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004541 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004542 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 else if (p_verbose > 0)
4544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004545 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004546 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004547
4548 return did_free;
4549}
4550
4551/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004552 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004553 */
4554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004555free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004556{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004557 int did_free = FALSE;
4558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // Let all "free" functions know that we are here. This means no
4560 // dictionaries, lists, channels or jobs are to be freed, because we will
4561 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004562 in_free_unref_items = TRUE;
4563
4564 /*
4565 * PASS 1: free the contents of the items. We don't free the items
4566 * themselves yet, so that it is possible to decrement refcount counters
4567 */
4568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004570 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004573 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004574
4575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004576 // Go through the list of jobs and free items without the copyID. This
4577 // must happen before doing channels, because jobs refer to channels, but
4578 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4583#endif
4584
4585 /*
4586 * PASS 2: free the items themselves.
4587 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004588 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004589 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004590
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004591#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004592 // Go through the list of jobs and free items without the copyID. This
4593 // must happen before doing channels, because jobs refer to channels, but
4594 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004595 free_unused_jobs(copyID, COPYID_MASK);
4596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004598 free_unused_channels(copyID, COPYID_MASK);
4599#endif
4600
4601 in_free_unref_items = FALSE;
4602
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004603 return did_free;
4604}
4605
4606/*
4607 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 * "list_stack" is used to add lists to be marked. Can be NULL.
4609 *
4610 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004613set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614{
4615 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004616 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 hashtab_T *cur_ht;
4619 ht_stack_T *ht_stack = NULL;
4620 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004621
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 cur_ht = ht;
4623 for (;;)
4624 {
4625 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // Mark each item in the hashtab. If the item contains a hashtab
4628 // it is added to ht_stack, if it contains a list it is added to
4629 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 todo = (int)cur_ht->ht_used;
4631 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4632 if (!HASHITEM_EMPTY(hi))
4633 {
4634 --todo;
4635 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4636 &ht_stack, list_stack);
4637 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004638 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004639
4640 if (ht_stack == NULL)
4641 break;
4642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644 cur_ht = ht_stack->ht;
4645 tempitem = ht_stack;
4646 ht_stack = ht_stack->prev;
4647 free(tempitem);
4648 }
4649
4650 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004651}
4652
4653/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004654 * Mark a dict and its items with "copyID".
4655 * Returns TRUE if setting references failed somehow.
4656 */
4657 int
4658set_ref_in_dict(dict_T *d, int copyID)
4659{
4660 if (d != NULL && d->dv_copyID != copyID)
4661 {
4662 d->dv_copyID = copyID;
4663 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4664 }
4665 return FALSE;
4666}
4667
4668/*
4669 * Mark a list and its items with "copyID".
4670 * Returns TRUE if setting references failed somehow.
4671 */
4672 int
4673set_ref_in_list(list_T *ll, int copyID)
4674{
4675 if (ll != NULL && ll->lv_copyID != copyID)
4676 {
4677 ll->lv_copyID = copyID;
4678 return set_ref_in_list_items(ll, copyID, NULL);
4679 }
4680 return FALSE;
4681}
4682
4683/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004684 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4686 *
4687 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004690set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004691{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 listitem_T *li;
4693 int abort = FALSE;
4694 list_T *cur_l;
4695 list_stack_T *list_stack = NULL;
4696 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004698 cur_l = l;
4699 for (;;)
4700 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004701 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // Mark each item in the list. If the item contains a hashtab
4703 // it is added to ht_stack, if it contains a list it is added to
4704 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004705 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4706 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4707 ht_stack, &list_stack);
4708 if (list_stack == NULL)
4709 break;
4710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 cur_l = list_stack->list;
4713 tempitem = list_stack;
4714 list_stack = list_stack->prev;
4715 free(tempitem);
4716 }
4717
4718 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004719}
4720
4721/*
4722 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004723 * "list_stack" is used to add lists to be marked. Can be NULL.
4724 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4725 *
4726 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004727 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004729set_ref_in_item(
4730 typval_T *tv,
4731 int copyID,
4732 ht_stack_T **ht_stack,
4733 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004734{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004735 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004736
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004737 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004738 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004739 dict_T *dd = tv->vval.v_dict;
4740
Bram Moolenaara03f2332016-02-06 18:09:59 +01004741 if (dd != NULL && dd->dv_copyID != copyID)
4742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 dd->dv_copyID = copyID;
4745 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004746 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004747 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4748 }
4749 else
4750 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004751 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4752
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 if (newitem == NULL)
4754 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004755 else
4756 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004757 newitem->ht = &dd->dv_hashtab;
4758 newitem->prev = *ht_stack;
4759 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004760 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004761 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004762 }
4763 }
4764 else if (tv->v_type == VAR_LIST)
4765 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 list_T *ll = tv->vval.v_list;
4767
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 if (ll != NULL && ll->lv_copyID != copyID)
4769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004770 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 ll->lv_copyID = copyID;
4772 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004773 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004774 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004775 }
4776 else
4777 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004778 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4779
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 if (newitem == NULL)
4781 abort = TRUE;
4782 else
4783 {
4784 newitem->list = ll;
4785 newitem->prev = *list_stack;
4786 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004787 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004788 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004789 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004790 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004791 else if (tv->v_type == VAR_FUNC)
4792 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004793 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004794 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 else if (tv->v_type == VAR_PARTIAL)
4796 {
4797 partial_T *pt = tv->vval.v_partial;
4798 int i;
4799
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004800 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004802 // Didn't see this partial yet.
4803 pt->pt_copyID = copyID;
4804
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004805 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004806
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 if (pt->pt_dict != NULL)
4808 {
4809 typval_T dtv;
4810
4811 dtv.v_type = VAR_DICT;
4812 dtv.vval.v_dict = pt->pt_dict;
4813 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4814 }
4815
4816 for (i = 0; i < pt->pt_argc; ++i)
4817 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4818 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004819 if (pt->pt_funcstack != NULL)
4820 {
4821 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4822
4823 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4824 abort = abort || set_ref_in_item(stack + i, copyID,
4825 ht_stack, list_stack);
4826 }
4827
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 }
4829 }
4830#ifdef FEAT_JOB_CHANNEL
4831 else if (tv->v_type == VAR_JOB)
4832 {
4833 job_T *job = tv->vval.v_job;
4834 typval_T dtv;
4835
4836 if (job != NULL && job->jv_copyID != copyID)
4837 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004838 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004839 if (job->jv_channel != NULL)
4840 {
4841 dtv.v_type = VAR_CHANNEL;
4842 dtv.vval.v_channel = job->jv_channel;
4843 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4844 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004845 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 {
4847 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004848 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4850 }
4851 }
4852 }
4853 else if (tv->v_type == VAR_CHANNEL)
4854 {
4855 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004856 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004857 typval_T dtv;
4858 jsonq_T *jq;
4859 cbq_T *cq;
4860
4861 if (ch != NULL && ch->ch_copyID != copyID)
4862 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004863 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004864 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004865 {
4866 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4867 jq = jq->jq_next)
4868 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4869 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4870 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004872 {
4873 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4876 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 {
4879 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 dtv.vval.v_partial =
4881 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004882 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4883 }
4884 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 {
4887 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4890 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004891 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 {
4893 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004894 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4896 }
4897 }
4898 }
4899#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004900 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004901}
4902
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904 * Return a string with the string representation of a variable.
4905 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004906 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004907 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004908 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004909 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004910 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004911 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4912 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004913 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004915 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004916echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917 typval_T *tv,
4918 char_u **tofree,
4919 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004920 int copyID,
4921 int echo_style,
4922 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004923 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004925 static int recurse = 0;
4926 char_u *r = NULL;
4927
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004930 if (!did_echo_string_emsg)
4931 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 // Only give this message once for a recursive call to avoid
4933 // flooding the user with errors. And stop iterating over lists
4934 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004935 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004936 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004938 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004939 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004940 }
4941 ++recurse;
4942
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 switch (tv->v_type)
4944 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004945 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004946 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004947 {
4948 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004949 r = tv->vval.v_string;
4950 if (r == NULL)
4951 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952 }
4953 else
4954 {
4955 *tofree = string_quote(tv->vval.v_string, FALSE);
4956 r = *tofree;
4957 }
4958 break;
4959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004961 if (echo_style)
4962 {
4963 *tofree = NULL;
4964 r = tv->vval.v_string;
4965 }
4966 else
4967 {
4968 *tofree = string_quote(tv->vval.v_string, TRUE);
4969 r = *tofree;
4970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004973 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004974 {
4975 partial_T *pt = tv->vval.v_partial;
4976 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004977 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004978 garray_T ga;
4979 int i;
4980 char_u *tf;
4981
4982 ga_init2(&ga, 1, 100);
4983 ga_concat(&ga, (char_u *)"function(");
4984 if (fname != NULL)
4985 {
4986 ga_concat(&ga, fname);
4987 vim_free(fname);
4988 }
4989 if (pt != NULL && pt->pt_argc > 0)
4990 {
4991 ga_concat(&ga, (char_u *)", [");
4992 for (i = 0; i < pt->pt_argc; ++i)
4993 {
4994 if (i > 0)
4995 ga_concat(&ga, (char_u *)", ");
4996 ga_concat(&ga,
4997 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4998 vim_free(tf);
4999 }
5000 ga_concat(&ga, (char_u *)"]");
5001 }
5002 if (pt != NULL && pt->pt_dict != NULL)
5003 {
5004 typval_T dtv;
5005
5006 ga_concat(&ga, (char_u *)", ");
5007 dtv.v_type = VAR_DICT;
5008 dtv.vval.v_dict = pt->pt_dict;
5009 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5010 vim_free(tf);
5011 }
5012 ga_concat(&ga, (char_u *)")");
5013
5014 *tofree = ga.ga_data;
5015 r = *tofree;
5016 break;
5017 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005018
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005019 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005020 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005021 break;
5022
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005024 if (tv->vval.v_list == NULL)
5025 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005026 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005027 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005028 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005030 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5031 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005032 {
5033 *tofree = NULL;
5034 r = (char_u *)"[...]";
5035 }
5036 else
5037 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038 int old_copyID = tv->vval.v_list->lv_copyID;
5039
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005040 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 *tofree = list2string(tv, copyID, restore_copyID);
5042 if (restore_copyID)
5043 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005044 r = *tofree;
5045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005046 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005047
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 if (tv->vval.v_dict == NULL)
5050 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005051 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005052 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005053 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005055 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5056 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057 {
5058 *tofree = NULL;
5059 r = (char_u *)"{...}";
5060 }
5061 else
5062 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005063 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005064
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005065 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005066 *tofree = dict2string(tv, copyID, restore_copyID);
5067 if (restore_copyID)
5068 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069 r = *tofree;
5070 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005071 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005074 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005075 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005077 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005078 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005079 break;
5080
Bram Moolenaar835dc632016-02-07 14:27:38 +01005081 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005082 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005083#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005085 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5086 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005087 if (composite_val)
5088 {
5089 *tofree = string_quote(r, FALSE);
5090 r = *tofree;
5091 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005092#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005094
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005095 case VAR_INSTR:
5096 *tofree = NULL;
5097 r = (char_u *)"instructions";
5098 break;
5099
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005101#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 *tofree = NULL;
5103 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5104 r = numbuf;
5105 break;
5106#endif
5107
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005108 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005109 case VAR_SPECIAL:
5110 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005111 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005112 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114
Bram Moolenaar8502c702014-06-17 12:51:16 +02005115 if (--recurse == 0)
5116 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005117 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118}
5119
5120/*
5121 * Return a string with the string representation of a variable.
5122 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5123 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005124 * Does not put quotes around strings, as ":echo" displays values.
5125 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5126 * May return NULL.
5127 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005129echo_string(
5130 typval_T *tv,
5131 char_u **tofree,
5132 char_u *numbuf,
5133 int copyID)
5134{
5135 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5136}
5137
5138/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005139 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5140 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005141 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005142 */
5143 int
5144buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5145{
5146 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005147 char_u *t;
5148 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005149
5150 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5151 return -1;
5152
5153 if (lnum > buf->b_ml.ml_line_count)
5154 lnum = buf->b_ml.ml_line_count;
5155
5156 str = ml_get_buf(buf, lnum, FALSE);
5157 if (str == NULL)
5158 return -1;
5159
5160 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005161 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005162
Bram Moolenaar91458462021-01-13 20:08:38 +01005163 // count the number of characters
5164 t = str;
5165 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5166 t += mb_ptr2len(t);
5167
5168 // In insert mode, when the cursor is at the end of a non-empty line,
5169 // byteidx points to the NUL character immediately past the end of the
5170 // string. In this case, add one to the character count.
5171 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5172 count++;
5173
5174 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005175}
5176
5177/*
5178 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005179 * byte index. Works only for loaded buffers. Returns -1 on failure.
5180 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005181 */
5182 int
5183buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5184{
5185 char_u *str;
5186 char_u *t;
5187
5188 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5189 return -1;
5190
5191 if (lnum > buf->b_ml.ml_line_count)
5192 lnum = buf->b_ml.ml_line_count;
5193
5194 str = ml_get_buf(buf, lnum, FALSE);
5195 if (str == NULL)
5196 return -1;
5197
5198 // Convert the character offset to a byte offset
5199 t = str;
5200 while (*t != NUL && --charidx > 0)
5201 t += mb_ptr2len(t);
5202
Bram Moolenaar91458462021-01-13 20:08:38 +01005203 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005204}
5205
5206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005208 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005211var2fpos(
5212 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005213 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214 int *fnum, // set to fnum for '0, 'A, etc.
5215 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005217 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005219 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005222 if (varp->v_type == VAR_LIST)
5223 {
5224 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005225 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005226 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005227 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005228
5229 l = varp->vval.v_list;
5230 if (l == NULL)
5231 return NULL;
5232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005234 pos.lnum = list_find_nr(l, 0L, &error);
5235 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237 if (charcol)
5238 len = (long)mb_charlen(ml_get(pos.lnum));
5239 else
5240 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005241
Bram Moolenaarec65d772020-08-20 22:29:12 +02005242 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005243 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005244 li = list_find(l, 1L);
5245 if (li != NULL && li->li_tv.v_type == VAR_STRING
5246 && li->li_tv.vval.v_string != NULL
5247 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005248 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005249 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005250 }
5251 else
5252 {
5253 pos.col = list_find_nr(l, 1L, &error);
5254 if (error)
5255 return NULL;
5256 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005259 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005261 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005264 pos.coladd = list_find_nr(l, 2L, &error);
5265 if (error)
5266 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005267
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005268 return &pos;
5269 }
5270
Bram Moolenaarc5809432021-03-27 21:23:30 +01005271 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5272 return NULL;
5273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 if (name == NULL)
5276 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005277 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005278 {
5279 pos = curwin->w_cursor;
5280 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005281 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005282 return &pos;
5283 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005285 {
5286 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005287 pos = VIsual;
5288 else
5289 pos = curwin->w_cursor;
5290 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005291 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005292 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005296 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5298 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005299 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005300 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 return pp;
5302 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005303
Bram Moolenaara5525202006-03-02 22:52:09 +00005304 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005305
Bram Moolenaar477933c2007-07-17 14:32:23 +00005306 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005307 {
5308 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005310 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005311 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // In silent Ex mode topline is zero, but that's not a valid line
5313 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005314 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005315 return &pos;
5316 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005317 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005318 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005319 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005321 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005322 return &pos;
5323 }
5324 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005327 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 pos.lnum = curbuf->b_ml.ml_line_count;
5330 pos.col = 0;
5331 }
5332 else
5333 {
5334 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005335 if (charcol)
5336 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5337 else
5338 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 return &pos;
5341 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005342 if (in_vim9script())
5343 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return NULL;
5345}
5346
5347/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005348 * Convert list in "arg" into a position and optional file number.
5349 * When "fnump" is NULL there is no file number, only 3 items.
5350 * Note that the column is passed on as-is, the caller may want to decrement
5351 * it to use 1 for the first column.
5352 * Return FAIL when conversion is not possible, doesn't check the position for
5353 * validity.
5354 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005356list2fpos(
5357 typval_T *arg,
5358 pos_T *posp,
5359 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005360 colnr_T *curswantp,
5361 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362{
5363 list_T *l = arg->vval.v_list;
5364 long i = 0;
5365 long n;
5366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5368 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005369 if (arg->v_type != VAR_LIST
5370 || l == NULL
5371 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005372 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005373 return FAIL;
5374
5375 if (fnump != NULL)
5376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005377 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005378 if (n < 0)
5379 return FAIL;
5380 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005382 *fnump = n;
5383 }
5384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005385 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005386 if (n < 0)
5387 return FAIL;
5388 posp->lnum = n;
5389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005391 if (n < 0)
5392 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 // If character position is specified, then convert to byte position
5394 if (charcol)
5395 {
5396 buf_T *buf;
5397
5398 // Get the text for the specified line in a loaded buffer
5399 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5400 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5401 return FAIL;
5402
Bram Moolenaar91458462021-01-13 20:08:38 +01005403 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005404 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405 posp->col = n;
5406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005408 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005409 posp->coladd = 0;
5410 else
5411 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005412
Bram Moolenaar493c1782014-05-28 14:34:46 +02005413 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005415
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005416 return OK;
5417}
5418
5419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * Get the length of an environment variable name.
5421 * Advance "arg" to the first character after the name.
5422 * Return 0 for error.
5423 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005425get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 char_u *p;
5428 int len;
5429
5430 for (p = *arg; vim_isIDc(*p); ++p)
5431 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 return 0;
5434
5435 len = (int)(p - *arg);
5436 *arg = p;
5437 return len;
5438}
5439
5440/*
5441 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005442 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * Return 0 if something is wrong.
5444 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005446get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 char_u *p;
5449 int len;
5450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005453 {
5454 if (*p == ':')
5455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // "s:" is start of "s:var", but "n:" is not and can be used in
5457 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005458 len = (int)(p - *arg);
5459 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5460 || len > 1)
5461 break;
5462 }
5463 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005464 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return 0;
5466
5467 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005468 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 return len;
5471}
5472
5473/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005474 * Get the length of the name of a variable or function.
5475 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005477 * Return -1 if curly braces expansion failed.
5478 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 * If the name contains 'magic' {}'s, expand them and return the
5480 * expanded name in an allocated string via 'alias' - caller must free.
5481 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005483get_name_len(
5484 char_u **arg,
5485 char_u **alias,
5486 int evaluate,
5487 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u *p;
5491 char_u *expr_start;
5492 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5497 && (*arg)[2] == (int)KE_SNR)
5498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *arg += 3;
5501 return get_id_len(arg) + 3;
5502 }
5503 len = eval_fname_script(*arg);
5504 if (len > 0)
5505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 *arg += len;
5508 }
5509
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005513 p = find_name_end(*arg, &expr_start, &expr_end,
5514 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (expr_start != NULL)
5516 {
5517 char_u *temp_string;
5518
5519 if (!evaluate)
5520 {
5521 len += (int)(p - *arg);
5522 *arg = skipwhite(p);
5523 return len;
5524 }
5525
5526 /*
5527 * Include any <SID> etc in the expanded string:
5528 * Thus the -len here.
5529 */
5530 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5531 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005532 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 *alias = temp_string;
5534 *arg = skipwhite(p);
5535 return (int)STRLEN(temp_string);
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005539 // Only give an error when there is something, otherwise it will be
5540 // reported at a higher level.
5541 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005542 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 return len;
5545}
5546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547/*
5548 * Find the end of a variable or function name, taking care of magic braces.
5549 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5550 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005551 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 * Return a pointer to just after the name. Equal to "arg" if there is no
5553 * valid name.
5554 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556find_name_end(
5557 char_u *arg,
5558 char_u **expr_start,
5559 char_u **expr_end,
5560 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 int mb_nest = 0;
5563 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005565 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005566 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 *expr_start = NULL;
5571 *expr_end = NULL;
5572 }
5573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005575 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5576 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005577 return arg;
5578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 for (p = arg; *p != NUL
5580 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005581 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005582 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005583 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005585 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005587 if (*p == '\'')
5588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005589 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005590 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005591 ;
5592 if (*p == NUL)
5593 break;
5594 }
5595 else if (*p == '"')
5596 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005597 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005598 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005599 if (*p == '\\' && p[1] != NUL)
5600 ++p;
5601 if (*p == NUL)
5602 break;
5603 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005604 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 // "s:" is start of "s:var", but "n:" is not and can be used in
5607 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005608 len = (int)(p - arg);
5609 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005610 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005611 break;
5612 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 if (*p == '[')
5617 ++br_nest;
5618 else if (*p == ']')
5619 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005621
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005622 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 if (*p == '{')
5625 {
5626 mb_nest++;
5627 if (expr_start != NULL && *expr_start == NULL)
5628 *expr_start = p;
5629 }
5630 else if (*p == '}')
5631 {
5632 mb_nest--;
5633 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5634 *expr_end = p;
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638
5639 return p;
5640}
5641
5642/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005643 * Expands out the 'magic' {}'s in a variable/function name.
5644 * Note that this can call itself recursively, to deal with
5645 * constructs like foo{bar}{baz}{bam}
5646 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5647 * "in_start" ^
5648 * "expr_start" ^
5649 * "expr_end" ^
5650 * "in_end" ^
5651 *
5652 * Returns a new allocated string, which the caller must free.
5653 * Returns NULL for failure.
5654 */
5655 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005656make_expanded_name(
5657 char_u *in_start,
5658 char_u *expr_start,
5659 char_u *expr_end,
5660 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005661{
5662 char_u c1;
5663 char_u *retval = NULL;
5664 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005665
5666 if (expr_end == NULL || in_end == NULL)
5667 return NULL;
5668 *expr_start = NUL;
5669 *expr_end = NUL;
5670 c1 = *in_end;
5671 *in_end = NUL;
5672
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005673 temp_result = eval_to_string(expr_start + 1, FALSE);
5674 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005675 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005676 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5677 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005678 if (retval != NULL)
5679 {
5680 STRCPY(retval, in_start);
5681 STRCAT(retval, temp_result);
5682 STRCAT(retval, expr_end + 1);
5683 }
5684 }
5685 vim_free(temp_result);
5686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005688 *expr_start = '{';
5689 *expr_end = '}';
5690
5691 if (retval != NULL)
5692 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005693 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005694 if (expr_start != NULL)
5695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005696 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005697 temp_result = make_expanded_name(retval, expr_start,
5698 expr_end, temp_result);
5699 vim_free(retval);
5700 retval = temp_result;
5701 }
5702 }
5703
5704 return retval;
5705}
5706
5707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005709 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005714 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005715}
5716
5717/*
5718 * Return TRUE if character "c" can be used as the first character in a
5719 * variable or function name (excluding '{' and '}').
5720 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005722eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005723{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005724 return ASCII_ISALPHA(c) || c == '_';
5725}
5726
5727/*
5728 * Return TRUE if character "c" can be used as the first character of a
5729 * dictionary key.
5730 */
5731 int
5732eval_isdictc(int c)
5733{
5734 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735}
5736
5737/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005738 * Handle:
5739 * - expr[expr], expr[expr:expr] subscript
5740 * - ".name" lookup
5741 * - function call with Funcref variable: func(expr)
5742 * - method call: var->method()
5743 *
5744 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005745 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005747handle_subscript(
5748 char_u **arg,
5749 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005750 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005751 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005752{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005753 int evaluate = evalarg != NULL
5754 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005755 int ret = OK;
5756 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005757 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005758 int getnext;
5759 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005760
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005761 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005762 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005763 // When at the end of the line and ".name" or "->{" or "->X" follows in
5764 // the next line then consume the line break.
5765 p = eval_next_non_blank(*arg, evalarg, &getnext);
5766 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005767 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005768 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5769 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5770 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005771 {
5772 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005773 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005774 check_white = FALSE;
5775 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005776
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005777 if (rettv->v_type == VAR_ANY)
5778 {
5779 char_u *exp_name;
5780 int cc;
5781 int idx;
5782 ufunc_T *ufunc;
5783 type_T *type;
5784
5785 // Found script from "import * as {name}", script item name must
5786 // follow.
5787 if (**arg != '.')
5788 {
5789 if (verbose)
5790 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5791 ret = FAIL;
5792 break;
5793 }
5794 ++*arg;
5795 if (IS_WHITE_OR_NUL(**arg))
5796 {
5797 if (verbose)
5798 emsg(_(e_no_white_space_allowed_after_dot));
5799 ret = FAIL;
5800 break;
5801 }
5802
5803 // isolate the name
5804 exp_name = *arg;
5805 while (eval_isnamec(**arg))
5806 ++*arg;
5807 cc = **arg;
5808 **arg = NUL;
5809
5810 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5811 evalarg->eval_cctx, verbose);
5812 **arg = cc;
5813 *arg = skipwhite(*arg);
5814
5815 if (idx < 0 && ufunc == NULL)
5816 {
5817 ret = FAIL;
5818 break;
5819 }
5820 if (idx >= 0)
5821 {
5822 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5823 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5824
5825 copy_tv(sv->sv_tv, rettv);
5826 }
5827 else
5828 {
5829 rettv->v_type = VAR_FUNC;
5830 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5831 }
5832 }
5833
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005834 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5835 || rettv->v_type == VAR_PARTIAL))
5836 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005837 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005838 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5839 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005840
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005841 // Stop the expression evaluation when immediately aborting on
5842 // error, or when an interrupt occurred or an exception was thrown
5843 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005844 if (aborting())
5845 {
5846 if (ret == OK)
5847 clear_tv(rettv);
5848 ret = FAIL;
5849 }
5850 dict_unref(selfdict);
5851 selfdict = NULL;
5852 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005853 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005854 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005855 if (in_vim9script())
5856 *arg = skipwhite(p + 2);
5857 else
5858 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005859 if (ret == OK)
5860 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005861 if (VIM_ISWHITE(**arg))
5862 {
5863 emsg(_(e_nowhitespace));
5864 ret = FAIL;
5865 }
5866 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005867 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005868 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005869 else
5870 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005871 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005872 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005873 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005874 // "." is ".name" lookup when we found a dict or when evaluating and
5875 // scriptversion is at least 2, where string concatenation is "..".
5876 else if (**arg == '['
5877 || (**arg == '.' && (rettv->v_type == VAR_DICT
5878 || (!evaluate
5879 && (*arg)[1] != '.'
5880 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005881 {
5882 dict_unref(selfdict);
5883 if (rettv->v_type == VAR_DICT)
5884 {
5885 selfdict = rettv->vval.v_dict;
5886 if (selfdict != NULL)
5887 ++selfdict->dv_refcount;
5888 }
5889 else
5890 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005891 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005892 {
5893 clear_tv(rettv);
5894 ret = FAIL;
5895 }
5896 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005897 else
5898 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005899 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005901 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5902 // Don't do this when "Func" is already a partial that was bound
5903 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005904 if (selfdict != NULL
5905 && (rettv->v_type == VAR_FUNC
5906 || (rettv->v_type == VAR_PARTIAL
5907 && (rettv->vval.v_partial->pt_auto
5908 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005909 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005910
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005911 dict_unref(selfdict);
5912 return ret;
5913}
5914
5915/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005916 * Make a copy of an item.
5917 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005918 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5919 * reference to an already copied list/dict can be used.
5920 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005922 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923item_copy(
5924 typval_T *from,
5925 typval_T *to,
5926 int deep,
5927 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928{
5929 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005930 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005934 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005935 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 }
5937 ++recurse;
5938
5939 switch (from->v_type)
5940 {
5941 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005942 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943 case VAR_STRING:
5944 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005945 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005946 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005947 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005948 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005949 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005950 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005951 copy_tv(from, to);
5952 break;
5953 case VAR_LIST:
5954 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005955 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005956 if (from->vval.v_list == NULL)
5957 to->vval.v_list = NULL;
5958 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005961 to->vval.v_list = from->vval.v_list->lv_copylist;
5962 ++to->vval.v_list->lv_refcount;
5963 }
5964 else
5965 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5966 if (to->vval.v_list == NULL)
5967 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005969 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 case VAR_DICT:
5973 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005974 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 if (from->vval.v_dict == NULL)
5976 to->vval.v_dict = NULL;
5977 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5981 ++to->vval.v_dict->dv_refcount;
5982 }
5983 else
5984 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5985 if (to->vval.v_dict == NULL)
5986 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005988 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005989 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005991 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993 }
5994 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005995 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996}
5997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 void
5999echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6000{
6001 char_u *tofree;
6002 char_u numbuf[NUMBUFLEN];
6003 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6004
6005 if (*atstart)
6006 {
6007 *atstart = FALSE;
6008 // Call msg_start() after eval1(), evaluating the expression
6009 // may cause a message to appear.
6010 if (with_space)
6011 {
6012 // Mark the saved text as finishing the line, so that what
6013 // follows is displayed on a new line when scrolling back
6014 // at the more prompt.
6015 msg_sb_eol();
6016 msg_start();
6017 }
6018 }
6019 else if (with_space)
6020 msg_puts_attr(" ", echo_attr);
6021
6022 if (p != NULL)
6023 for ( ; *p != NUL && !got_int; ++p)
6024 {
6025 if (*p == '\n' || *p == '\r' || *p == TAB)
6026 {
6027 if (*p != TAB && *needclr)
6028 {
6029 // remove any text still there from the command
6030 msg_clr_eos();
6031 *needclr = FALSE;
6032 }
6033 msg_putchar_attr(*p, echo_attr);
6034 }
6035 else
6036 {
6037 if (has_mbyte)
6038 {
6039 int i = (*mb_ptr2len)(p);
6040
6041 (void)msg_outtrans_len_attr(p, i, echo_attr);
6042 p += i - 1;
6043 }
6044 else
6045 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6046 }
6047 }
6048 vim_free(tofree);
6049}
6050
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 * ":echo expr1 ..." print each argument separated with a space, add a
6053 * newline at the end.
6054 * ":echon expr1 ..." print each argument plain.
6055 */
6056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006061 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 int needclr = TRUE;
6063 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006064 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006065 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006066 evalarg_T evalarg;
6067
Bram Moolenaare707c882020-07-01 13:07:37 +02006068 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 if (eap->skip)
6071 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006072 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // If eval1() causes an error message the text from the command may
6075 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006076 need_clr_eos = needclr;
6077
Bram Moolenaar68db9962021-05-09 23:19:22 +02006078 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006079 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081 /*
6082 * Report the invalid expression unless the expression evaluation
6083 * has been cancelled due to an aborting error, an interrupt, or an
6084 * exception.
6085 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006086 if (!aborting() && did_emsg == did_emsg_before
6087 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006088 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 break;
6091 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006092 need_clr_eos = FALSE;
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006095 {
6096 if (rettv.v_type == VAR_VOID)
6097 {
6098 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6099 break;
6100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006102 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 arg = skipwhite(arg);
6106 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006107 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006108 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110 if (eap->skip)
6111 --emsg_skip;
6112 else
6113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (needclr)
6116 msg_clr_eos();
6117 if (eap->cmdidx == CMD_echo)
6118 msg_end();
6119 }
6120}
6121
6122/*
6123 * ":echohl {name}".
6124 */
6125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006128 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129}
6130
6131/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006132 * Returns the :echo attribute
6133 */
6134 int
6135get_echo_attr(void)
6136{
6137 return echo_attr;
6138}
6139
6140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * ":execute expr1 ..." execute the result of an expression.
6142 * ":echomsg expr1 ..." Print a message
6143 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006144 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 * Each gets spaces around each argument and a newline at the end for
6146 * echo commands
6147 */
6148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 int ret = OK;
6154 char_u *p;
6155 garray_T ga;
6156 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006157 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 ga_init2(&ga, 1, 80);
6160
6161 if (eap->skip)
6162 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006163 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006165 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006166 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169 if (!eap->skip)
6170 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006171 char_u buf[NUMBUFLEN];
6172
6173 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006174 {
6175 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6176 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006177 semsg(_(e_using_invalid_value_as_string_str),
6178 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006179 p = NULL;
6180 }
6181 else
6182 p = tv_get_string_buf(&rettv, buf);
6183 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006184 else
6185 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006186 if (p == NULL)
6187 {
6188 clear_tv(&rettv);
6189 ret = FAIL;
6190 break;
6191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 len = (int)STRLEN(p);
6193 if (ga_grow(&ga, len + 2) == FAIL)
6194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 ret = FAIL;
6197 break;
6198 }
6199 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 ga.ga_len += len;
6203 }
6204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 arg = skipwhite(arg);
6207 }
6208
6209 if (ret != FAIL && ga.ga_data != NULL)
6210 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006211 // use the first line of continuation lines for messages
6212 SOURCING_LNUM = start_lnum;
6213
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006214 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006216 // Mark the already saved text as finishing the line, so that what
6217 // follows is displayed on a new line when scrolling back at the
6218 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006219 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006220 }
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006223 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006224 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006225 out_flush();
6226 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006227 else if (eap->cmdidx == CMD_echoconsole)
6228 {
6229 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6230 ui_write((char_u *)"\r\n", 2, TRUE);
6231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 else if (eap->cmdidx == CMD_echoerr)
6233 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006234 int save_did_emsg = did_emsg;
6235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006236 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006237 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (!force_abort)
6239 did_emsg = save_did_emsg;
6240 }
6241 else if (eap->cmdidx == CMD_execute)
6242 do_cmdline((char_u *)ga.ga_data,
6243 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6244 }
6245
6246 ga_clear(&ga);
6247
6248 if (eap->skip)
6249 --emsg_skip;
6250
Bram Moolenaar63b91732021-08-05 20:40:03 +02006251 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252}
6253
6254/*
6255 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6256 * "arg" points to the "&" or '+' when called, to "option" when returning.
6257 * Returns NULL when no option name found. Otherwise pointer to the char
6258 * after the option name.
6259 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 char_u *p = *arg;
6264
6265 ++p;
6266 if (*p == 'g' && p[1] == ':')
6267 {
6268 *opt_flags = OPT_GLOBAL;
6269 p += 2;
6270 }
6271 else if (*p == 'l' && p[1] == ':')
6272 {
6273 *opt_flags = OPT_LOCAL;
6274 p += 2;
6275 }
6276 else
6277 *opt_flags = 0;
6278
6279 if (!ASCII_ISALPHA(*p))
6280 return NULL;
6281 *arg = p;
6282
6283 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 else
6286 while (ASCII_ISALPHA(*p))
6287 ++p;
6288 return p;
6289}
6290
6291/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006292 * Display script name where an item was last set.
6293 * Should only be invoked when 'verbose' is non-zero.
6294 */
6295 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006296last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006297{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006298 char_u *p;
6299
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006300 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006301 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006302 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006303 if (p != NULL)
6304 {
6305 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006306 msg_puts(_("\n\tLast set from "));
6307 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006308 if (script_ctx.sc_lnum > 0)
6309 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006310 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006311 msg_outnum((long)script_ctx.sc_lnum);
6312 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006313 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006314 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006315 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006316 }
6317}
6318
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006319#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321/*
6322 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006323 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 * "flags" can be "g" to do a global substitute.
6325 * Returns an allocated string, NULL for error.
6326 */
6327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006328do_string_sub(
6329 char_u *str,
6330 char_u *pat,
6331 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006332 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 int sublen;
6336 regmatch_T regmatch;
6337 int i;
6338 int do_all;
6339 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006340 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 garray_T ga;
6342 char_u *ret;
6343 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006344 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006348 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 ga_init2(&ga, 1, 200);
6351
6352 do_all = (flags[0] == 'g');
6353
6354 regmatch.rm_ic = p_ic;
6355 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6356 if (regmatch.regprog != NULL)
6357 {
6358 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006359 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006362 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006363 if (regmatch.startp[0] == regmatch.endp[0])
6364 {
6365 if (zero_width == regmatch.startp[0])
6366 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006367 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006368 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006369 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6370 (size_t)i);
6371 ga.ga_len += i;
6372 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006373 continue;
6374 }
6375 zero_width = regmatch.startp[0];
6376 }
6377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 /*
6379 * Get some space for a temporary buffer to do the substitution
6380 * into. It will contain:
6381 * - The text up to where the match is.
6382 * - The substituted text.
6383 * - The text after the match.
6384 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006385 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006386 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6388 {
6389 ga_clear(&ga);
6390 break;
6391 }
6392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 i = (int)(regmatch.startp[0] - tail);
6395 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006396 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006397 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 + ga.ga_len + i, TRUE, TRUE, FALSE);
6399 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006400 tail = regmatch.endp[0];
6401 if (*tail == NUL)
6402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (!do_all)
6404 break;
6405 }
6406
6407 if (ga.ga_data != NULL)
6408 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6409
Bram Moolenaar473de612013-06-08 18:19:48 +02006410 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 }
6412
6413 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6414 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006415 if (p_cpo == empty_option)
6416 p_cpo = save_cpo;
6417 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006420 // If it's still empty it was changed and restored, need to restore in
6421 // the complicated way.
6422 if (*p_cpo == NUL)
6423 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006424 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 return ret;
6428}