blob: 2e8ab3d8fc29e0b9a17909a3f23c7f9da86e37b0 [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 Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020054static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020055static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020056static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static 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 +020060static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020061
Bram Moolenaare21c1582019-03-02 11:57:09 +010062/*
63 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010064 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010065 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020066 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010067num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068{
69 varnumber_T result;
70
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010075 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010076 if (failed != NULL)
77 *failed = TRUE;
78 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010079 if (n1 == 0)
80 result = VARNUM_MIN; // similar to NaN
81 else if (n1 < 0)
82 result = -VARNUM_MAX;
83 else
84 result = VARNUM_MAX;
85 }
86 else
87 result = n1 / n2;
88
89 return result;
90}
91
92/*
93 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010094 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010095 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020096 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010097num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010098{
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100101 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100102 if (failed != NULL)
103 *failed = TRUE;
104 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100105 return (n2 == 0) ? 0 : (n1 % n2);
106}
107
Bram Moolenaar33570922005-01-25 22:26:29 +0000108/*
109 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000110 */
111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100112eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000113{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200114 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200115 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000116
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200117#ifdef EBCDIC
118 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100119 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200120 */
121 sortFunctions();
122#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000123}
124
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000125#if defined(EXITFREE) || defined(PROTO)
126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100127eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000128{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200129 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100130 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200131 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200133 // autoloaded script names
134 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000135
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200136 // unreferenced lists and dicts
137 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200138
139 // functions not garbage collected
140 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000141}
142#endif
143
Bram Moolenaare6b53242020-07-01 17:28:33 +0200144 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200145fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
146{
147 CLEAR_FIELD(*evalarg);
148 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
149 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
150 {
151 evalarg->eval_getline = eap->getline;
152 evalarg->eval_cookie = eap->cookie;
153 }
154}
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * Top level evaluation function, returning a boolean.
158 * Sets "error" to TRUE if there was an error.
159 * Return TRUE or FALSE.
160 */
161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100162eval_to_bool(
163 char_u *arg,
164 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200165 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100166 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
Bram Moolenaar33570922005-01-25 22:26:29 +0000168 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200169 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200170 evalarg_T evalarg;
171
Bram Moolenaar37c83712020-06-30 21:18:36 +0200172 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 if (skip)
175 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200176 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 *error = FALSE;
181 if (!skip)
182 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200183 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200184 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 else
186 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000187 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 }
189 }
190 if (skip)
191 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200192 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195}
196
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197/*
198 * Call eval1() and give an error message if not done at a lower level.
199 */
200 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200201eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100202{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100203 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204 int ret;
205 int did_emsg_before = did_emsg;
206 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200207 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
210
211 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212 if (ret == FAIL)
213 {
214 // Report the invalid expression unless the expression evaluation has
215 // been cancelled due to an aborting error, an interrupt, or an
216 // exception, or we already gave a more specific error.
217 // Also check called_emsg for when using assert_fails().
218 if (!aborting() && did_emsg == did_emsg_before
219 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200220 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200222 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 return ret;
224}
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200227 * Return whether a typval is a valid expression to pass to eval_expr_typval()
228 * or eval_expr_to_bool(). An empty string returns FALSE;
229 */
230 int
231eval_expr_valid_arg(typval_T *tv)
232{
233 return tv->v_type != VAR_UNKNOWN
234 && (tv->v_type != VAR_STRING
235 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
236}
237
238/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 * Evaluate an expression, which can be a function, partial or string.
240 * Pass arguments "argv[argc]".
241 * Return the result in "rettv" and OK or FAIL.
242 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200243 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100244eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
245{
246 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100247 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200248 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249
250 if (expr->v_type == VAR_FUNC)
251 {
252 s = expr->vval.v_string;
253 if (s == NULL || *s == NUL)
254 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200255 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200256 funcexe.evaluate = TRUE;
257 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100258 return FAIL;
259 }
260 else if (expr->v_type == VAR_PARTIAL)
261 {
262 partial_T *partial = expr->vval.v_partial;
263
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200264 if (partial == NULL)
265 return FAIL;
266
Bram Moolenaar822ba242020-05-24 23:00:18 +0200267 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200268 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200270 if (call_def_function(partial->pt_func, argc, argv,
271 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 return FAIL;
273 }
274 else
275 {
276 s = partial_name(partial);
277 if (s == NULL || *s == NUL)
278 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200279 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 funcexe.evaluate = TRUE;
281 funcexe.partial = partial;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
283 return FAIL;
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200286 else if (expr->v_type == VAR_INSTR)
287 {
288 return exe_typval_instr(expr, rettv);
289 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 else
291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100292 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 if (s == NULL)
294 return FAIL;
295 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200296 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200298 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200300 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200301 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
303 }
304 }
305 return OK;
306}
307
308/*
309 * Like eval_to_bool() but using a typval_T instead of a string.
310 * Works for string, funcref and partial.
311 */
312 int
313eval_expr_to_bool(typval_T *expr, int *error)
314{
315 typval_T rettv;
316 int res;
317
318 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
319 {
320 *error = TRUE;
321 return FALSE;
322 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200323 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 clear_tv(&rettv);
325 return res;
326}
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/*
329 * Top level evaluation function, returning a string. If "skip" is TRUE,
330 * only parsing to "nextcmd" is done, without reporting errors. Return
331 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
332 */
333 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100334eval_to_string_skip(
335 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200336 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100337 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
Bram Moolenaar33570922005-01-25 22:26:29 +0000339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar37c83712020-06-30 21:18:36 +0200343 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 if (skip)
345 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 retval = NULL;
348 else
349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100350 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
353 if (skip)
354 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200355 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
357 return retval;
358}
359
360/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361 * Skip over an expression at "*pp".
362 * Return FAIL for an error, OK otherwise.
363 */
364 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366{
Bram Moolenaar33570922005-01-25 22:26:29 +0000367 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368
369 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200370 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371}
372
373/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200374 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * If in Vim9 script and line breaks are encountered, the lines are
376 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200377 * "arg" is advanced to just after the expression.
378 * "start" is set to the start of the expression, "end" to just after the end.
379 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Return FAIL for an error, OK otherwise.
381 */
382 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383skip_expr_concatenate(
384 char_u **arg,
385 char_u **start,
386 char_u **end,
387 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200388{
389 typval_T rettv;
390 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200391 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200392 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200393 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200395 int evaluate = evalarg == NULL
396 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200399 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 {
401 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200405 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408
409 // Don't evaluate the expression.
410 if (evalarg != NULL)
411 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200412 *arg = skipwhite(*arg);
413 res = eval1(arg, &rettv, evalarg);
414 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 if (evalarg != NULL)
416 evalarg->eval_flags = save_flags;
417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 if (evalarg->eval_ga.ga_len == 1)
422 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200423 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 ga_clear(gap);
425 gap->ga_itemsize = 0;
426 }
427 else
428 {
429 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200431
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200432 // Line breaks encountered, concatenate all the lines.
433 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200434 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435
436 // free the lines only when using getsourceline()
437 if (evalarg->eval_cookie != NULL)
438 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200439 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the last line, "arg" points into it, free it
442 // later.
443 vim_free(evalarg->eval_tofree);
444 evalarg->eval_tofree =
445 ((char_u **)gap->ga_data)[gap->ga_len - 1];
446 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 ga_clear_strings(gap);
448 }
449 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200450 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452
453 // free lines that were explicitly marked for freeing
454 ga_clear_strings(freegap);
455 }
456
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 gap->ga_itemsize = 0;
458 if (p == NULL)
459 return FAIL;
460 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200461 vim_free(evalarg->eval_tofree_lambda);
462 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200463 // Compute "end" relative to the end.
464 *end = *start + STRLEN(*start) - endoff;
465 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 }
467
468 return res;
469}
470
471/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100472 * Convert "tv" to a string.
473 * When "convert" is TRUE convert a List into a sequence of lines and convert
474 * a Float to a String.
475 * Returns an allocated string (NULL when out of memory).
476 */
477 char_u *
478typval2string(typval_T *tv, int convert)
479{
480 garray_T ga;
481 char_u *retval;
482#ifdef FEAT_FLOAT
483 char_u numbuf[NUMBUFLEN];
484#endif
485
486 if (convert && tv->v_type == VAR_LIST)
487 {
488 ga_init2(&ga, (int)sizeof(char), 80);
489 if (tv->vval.v_list != NULL)
490 {
491 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
492 if (tv->vval.v_list->lv_len > 0)
493 ga_append(&ga, NL);
494 }
495 ga_append(&ga, NUL);
496 retval = (char_u *)ga.ga_data;
497 }
498#ifdef FEAT_FLOAT
499 else if (convert && tv->v_type == VAR_FLOAT)
500 {
501 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
502 retval = vim_strsave(numbuf);
503 }
504#endif
505 else
506 retval = vim_strsave(tv_get_string(tv));
507 return retval;
508}
509
510/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200511 * Top level evaluation function, returning a string. Does not handle line
512 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000513 * When "convert" is TRUE convert a List into a sequence of lines and convert
514 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * Return pointer to allocated memory, or NULL for failure.
516 */
517 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100519 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 int convert,
521 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
Bram Moolenaar33570922005-01-25 22:26:29 +0000523 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100525 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
528 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 retval = NULL;
530 else
531 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100532 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000533 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 return retval;
538}
539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 char_u *
541eval_to_string(
542 char_u *arg,
543 int convert)
544{
545 return eval_to_string_eap(arg, convert, NULL);
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000549 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200550 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200551 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_string_safe(
555 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200559 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200560 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200563 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000564 if (use_sandbox)
565 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200566 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200567 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200625 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
626 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000627 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_vim_function(
631 char_u *func,
632 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200633 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200634 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000636 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200637 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100639 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200640 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200641 funcexe.firstline = curwin->w_cursor.lnum;
642 funcexe.lastline = curwin->w_cursor.lnum;
643 funcexe.evaluate = TRUE;
644 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000645 if (ret == FAIL)
646 clear_tv(rettv);
647
648 return ret;
649}
650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100651/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100652 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200654 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
655 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200657 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100658call_func_retnr(
659 char_u *func,
660 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662{
663 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200664 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 return -1;
668
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100669 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 clear_tv(&rettv);
671 return retval;
672}
673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100675 * Call Vim script function like call_func_retnr() and drop the result.
676 * Returns FAIL when calling the function fails.
677 */
678 int
679call_func_noret(
680 char_u *func,
681 int argc,
682 typval_T *argv)
683{
684 typval_T rettv;
685
686 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
687 return FAIL;
688 clear_tv(&rettv);
689 return OK;
690}
691
692/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100693 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100694 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000695 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696 */
697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100698call_func_retstr(
699 char_u *func,
700 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200701 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702{
703 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000704 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 return NULL;
708
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100709 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return retval;
712}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000714/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100715 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100716 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000717 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 */
719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100720call_func_retlist(
721 char_u *func,
722 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200723 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724{
725 typval_T rettv;
726
Bram Moolenaarded27a12018-08-01 19:06:03 +0200727 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728 return NULL;
729
730 if (rettv.v_type != VAR_LIST)
731 {
732 clear_tv(&rettv);
733 return NULL;
734 }
735
736 return rettv.vval.v_list;
737}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#ifdef FEAT_FOLDING
740/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100741 * Evaluate "arg", which is 'foldexpr'.
742 * Note: caller must set "curwin" to match "arg".
743 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
744 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 */
746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaar33570922005-01-25 22:26:29 +0000749 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200750 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000752 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
753 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000756 if (use_sandbox)
757 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200758 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200760 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 retval = 0;
762 else
763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100764 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000765 if (tv.v_type == VAR_NUMBER)
766 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000767 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 retval = 0;
769 else
770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 // If the result is a string, check if there is a non-digit before
772 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (!VIM_ISDIGIT(*s) && *s != '-')
775 *cp = *s++;
776 retval = atol((char *)s);
777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000781 if (use_sandbox)
782 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200783 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200784 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200786 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788#endif
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000791 * Get an lval: variable, Dict item or List item that can be assigned a value
792 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
793 * "name.key", "name.key[expr]" etc.
794 * Indexing only works if "name" is an existing List or Dictionary.
795 * "name" points to the start of the name.
796 * If "rettv" is not NULL it points to the value to be assigned.
797 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
798 * wrong; must end in space or cmd separator.
799 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100800 * flags:
801 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100802 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * GLV_NO_AUTOLOAD: do not use script autoloading
804 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000806 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100810get_lval(
811 char_u *name,
812 typval_T *rettv,
813 lval_T *lp,
814 int unlet,
815 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 int flags, // GLV_ values
817 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 char_u *expr_start, *expr_end;
821 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000822 dictitem_T *v;
823 typval_T var1;
824 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000826 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100829 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100830 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100831 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200834 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100836 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200840 lp->ll_name_end = find_name_end(name, NULL, NULL,
841 FNE_INCL_BR | fne_flags);
842 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 }
844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000846 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 if (expr_start != NULL)
849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100851 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 && *p != '[' && *p != '.')
853 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200854 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 return NULL;
856 }
857
858 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
859 if (lp->ll_exp_name == NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Report an invalid expression in braces, unless the
862 // expression evaluation has been cancelled due to an
863 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!aborting() && !quiet)
865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000866 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100867 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870 }
871 lp->ll_name = lp->ll_exp_name;
872 }
873 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_name = name;
876
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200877 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 // "a: type" is declaring variable "a" with a type, not "a:".
880 if (p == name + 2 && p[-1] == ':')
881 {
882 --p;
883 lp->ll_name_end = p;
884 }
885 if (*p == ':')
886 {
887 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
888 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100891 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
892 if (lp->ll_type == NULL && !quiet)
893 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200894 lp->ll_name_end = tp;
895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 }
897 }
898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100899 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
901 return p;
902
903 cc = *p;
904 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100905 // When we would write to the variable pass &ht and prevent autoload.
906 writing = !(flags & GLV_READ_ONLY);
907 v = find_var(lp->ll_name, writing ? &ht : NULL,
908 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200910 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 if (v == NULL)
913 return NULL;
914
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100915 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
916 {
917 if (!quiet)
918 semsg(_(e_variable_already_declared), lp->ll_name);
919 return NULL;
920 }
921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 /*
923 * Loop until no more [idx] or .key is following.
924 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100926 var1.v_type = VAR_UNKNOWN;
927 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200928 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200930 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
931 {
932 if (!quiet)
933 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
934 return NULL;
935 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200936 if (lp->ll_tv->v_type != VAR_LIST
937 && lp->ll_tv->v_type != VAR_DICT
938 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200944
945 // a NULL list/blob works like an empty list/blob, allocate one now.
946 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
947 rettv_list_alloc(lp->ll_tv);
948 else if (lp->ll_tv->v_type == VAR_BLOB
949 && lp->ll_tv->vval.v_blob == NULL)
950 rettv_blob_alloc(lp->ll_tv);
951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200963 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
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 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001178 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001180 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 return NULL;
1182 }
1183 if (lp->ll_range && !lp->ll_empty2)
1184 {
1185 lp->ll_n2 = (long)tv_get_number(&var2);
1186 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001187 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001193 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 else
1196 {
1197 /*
1198 * Get the number and item for the only or first index of the List.
1199 */
1200 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
1206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001209 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 if (lp->ll_li == NULL)
1211 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001212 // Vim9: Allow for adding an item at the end.
1213 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1214 && lp->ll_list->lv_lock == 0)
1215 {
1216 list_append_number(lp->ll_list, 0);
1217 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1218 }
1219 if (lp->ll_li == NULL)
1220 {
1221 clear_tv(&var2);
1222 if (!quiet)
1223 semsg(_(e_listidx), lp->ll_n1);
1224 return NULL;
1225 }
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 Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001247 {
1248 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001249 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 }
1254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 if (lp->ll_n1 < 0)
1257 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1258 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001259 {
1260 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001261 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001263 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
1265
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 }
1269
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001270 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 return p;
1273}
1274
1275/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001276 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280{
1281 vim_free(lp->ll_exp_name);
1282 vim_free(lp->ll_newkey);
1283}
1284
1285/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001286 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1289 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292set_var_lval(
1293 lval_T *lp,
1294 char_u *endp,
1295 typval_T *rettv,
1296 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001297 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1298 char_u *op,
1299 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300{
1301 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 listitem_T *ri;
1303 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304
1305 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001307 cc = *endp;
1308 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001309 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1310 return;
1311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (lp->ll_blob != NULL)
1313 {
1314 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 if (op != NULL && *op != '=')
1317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001318 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
1320 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001321 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001322 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323
1324 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1325 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001326 if (lp->ll_empty2)
1327 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1328
Bram Moolenaar68452172021-04-12 21:21:02 +02001329 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1330 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 else
1334 {
1335 val = (int)tv_get_number_chk(rettv, &error);
1336 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001337 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
1340 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001343
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001344 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1345 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001346 {
1347 emsg(_(e_cannot_mod));
1348 *endp = cc;
1349 return;
1350 }
1351
Bram Moolenaarff697e62019-02-12 22:28:33 +01001352 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001353 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001354 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001355 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 {
1357 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001358 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1359 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 && tv_op(&tv, rettv, op) == OK)
1361 set_var(lp->ll_name, &tv, FALSE);
1362 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001366 {
1367 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001368 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001369 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001370 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1371 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001372 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001375 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001376 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001377 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001378 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 else if (lp->ll_range)
1380 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001381 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001384 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1385 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001386 {
1387 emsg(_("E996: Cannot lock a range"));
1388 return;
1389 }
1390
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001391 /*
1392 * Check whether any of the list items is locked
1393 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001394 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001395 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001397 return;
1398 ri = ri->li_next;
1399 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1400 break;
1401 ll_li = ll_li->li_next;
1402 ++ll_n1;
1403 }
1404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 /*
1406 * Assign the List values to the list items.
1407 */
1408 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 if (op != NULL && *op != '=')
1411 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1412 else
1413 {
1414 clear_tv(&lp->ll_li->li_tv);
1415 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = ri->li_next;
1418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1419 break;
1420 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001422 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001423 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 ri = NULL;
1426 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 lp->ll_li = lp->ll_li->li_next;
1430 ++lp->ll_n1;
1431 }
1432 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001433 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else if (lp->ll_empty2
1435 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001437 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 }
1439 else
1440 {
1441 /*
1442 * Assign to a List or Dictionary item.
1443 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001444 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1445 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001446 {
1447 emsg(_("E996: Cannot lock a list or dict"));
1448 return;
1449 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001450
1451 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001452 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001453 return;
1454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 if (lp->ll_newkey != NULL)
1456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 if (op != NULL && *op != '=')
1458 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001459 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 return;
1461 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001462 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1463 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001464 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001466 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001467 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 if (di == NULL)
1469 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001470 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1471 {
1472 vim_free(di);
1473 return;
1474 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 else if (op != NULL && *op != '=')
1478 {
1479 tv_op(lp->ll_tv, rettv, op);
1480 return;
1481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 /*
1486 * Assign the value to the variable or list item.
1487 */
1488 if (copy)
1489 copy_tv(rettv, lp->ll_tv);
1490 else
1491 {
1492 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001493 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 }
1496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497}
1498
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1501 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 * Returns OK or FAIL.
1503 */
1504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001507 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 char_u numbuf[NUMBUFLEN];
1509 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001510 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001512 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001513 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 {
1516 switch (tv1->v_type)
1517 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001518 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001519 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 case VAR_DICT:
1522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001523 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001524 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001525 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001528 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 break;
1530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001531 case VAR_BLOB:
1532 if (*op != '+' || tv2->v_type != VAR_BLOB)
1533 break;
1534 // BLOB += BLOB
1535 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1536 {
1537 blob_T *b1 = tv1->vval.v_blob;
1538 blob_T *b2 = tv2->vval.v_blob;
1539 int i, len = blob_len(b2);
1540 for (i = 0; i < len; i++)
1541 ga_append(&b1->bv_ga, blob_get(b2, i));
1542 }
1543 return OK;
1544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 case VAR_LIST:
1546 if (*op != '+' || tv2->v_type != VAR_LIST)
1547 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001549 if (tv2->vval.v_list != NULL)
1550 {
1551 if (tv1->vval.v_list == NULL)
1552 {
1553 tv1->vval.v_list = tv2->vval.v_list;
1554 ++tv1->vval.v_list->lv_refcount;
1555 }
1556 else
1557 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1558 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 return OK;
1560
1561 case VAR_NUMBER:
1562 case VAR_STRING:
1563 if (tv2->v_type == VAR_LIST)
1564 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001565 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001567 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001568 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569#ifdef FEAT_FLOAT
1570 if (tv2->v_type == VAR_FLOAT)
1571 {
1572 float_T f = n;
1573
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 if (*op == '%')
1575 break;
1576 switch (*op)
1577 {
1578 case '+': f += tv2->vval.v_float; break;
1579 case '-': f -= tv2->vval.v_float; break;
1580 case '*': f *= tv2->vval.v_float; break;
1581 case '/': f /= tv2->vval.v_float; break;
1582 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583 clear_tv(tv1);
1584 tv1->v_type = VAR_FLOAT;
1585 tv1->vval.v_float = f;
1586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588#endif
1589 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 switch (*op)
1591 {
1592 case '+': n += tv_get_number(tv2); break;
1593 case '-': n -= tv_get_number(tv2); break;
1594 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001595 case '/': n = num_divide(n, tv_get_number(tv2),
1596 &failed); break;
1597 case '%': n = num_modulus(n, tv_get_number(tv2),
1598 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600 clear_tv(tv1);
1601 tv1->v_type = VAR_NUMBER;
1602 tv1->vval.v_number = n;
1603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 }
1605 else
1606 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607 if (tv2->v_type == VAR_FLOAT)
1608 break;
1609
Bram Moolenaarff697e62019-02-12 22:28:33 +01001610 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001611 s = tv_get_string(tv1);
1612 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 clear_tv(tv1);
1614 tv1->v_type = VAR_STRING;
1615 tv1->vval.v_string = s;
1616 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001617 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001620#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 {
1622 float_T f;
1623
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 if (*op == '%' || *op == '.'
1625 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 && tv2->v_type != VAR_NUMBER
1627 && tv2->v_type != VAR_STRING))
1628 break;
1629 if (tv2->v_type == VAR_FLOAT)
1630 f = tv2->vval.v_float;
1631 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001632 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 switch (*op)
1634 {
1635 case '+': tv1->vval.v_float += f; break;
1636 case '-': tv1->vval.v_float -= f; break;
1637 case '*': tv1->vval.v_float *= f; break;
1638 case '/': tv1->vval.v_float /= f; break;
1639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001642 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 }
1644 }
1645
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 return FAIL;
1648}
1649
1650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 * Evaluate the expression used in a ":for var in expr" command.
1652 * "arg" points to "var".
1653 * Set "*errp" to TRUE for an error, FALSE otherwise;
1654 * Return a pointer that holds the info. Null when there is an error.
1655 */
1656 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657eval_for_line(
1658 char_u *arg,
1659 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001660 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662{
Bram Moolenaar33570922005-01-25 22:26:29 +00001663 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001664 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T tv;
1667 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001668 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001670 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001672 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (fi == NULL)
1674 return NULL;
1675
Bram Moolenaar404557e2021-07-05 21:41:48 +02001676 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1677 &fi->fi_semicolon, FALSE);
1678 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 return fi;
1680
Bram Moolenaar404557e2021-07-05 21:41:48 +02001681 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001682 if (expr[0] != 'i' || expr[1] != 'n'
1683 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001685 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1686 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1687 else
1688 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 return fi;
1690 }
1691
1692 if (skip)
1693 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001694 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1695 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 {
1697 *errp = FALSE;
1698 if (!skip)
1699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 l = tv.vval.v_list;
1703 if (l == NULL)
1704 {
1705 // a null list is like an empty list: do nothing
1706 clear_tv(&tv);
1707 }
1708 else
1709 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001711 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 // No need to increment the refcount, it's already set for
1714 // the list being used in "tv".
1715 fi->fi_list = l;
1716 list_add_watch(l, &fi->fi_lw);
1717 fi->fi_lw.lw_item = l->lv_first;
1718 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001719 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001721 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 fi->fi_bi = 0;
1723 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001725 typval_T btv;
1726
1727 // Make a copy, so that the iteration still works when the
1728 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001733 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001734 else if (tv.v_type == VAR_STRING)
1735 {
1736 fi->fi_byte_idx = 0;
1737 fi->fi_string = tv.vval.v_string;
1738 tv.vval.v_string = NULL;
1739 if (fi->fi_string == NULL)
1740 fi->fi_string = vim_strsave((char_u *)"");
1741 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 else
1743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001744 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 }
1747 }
1748 }
1749 if (skip)
1750 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001751 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752
1753 return fi;
1754}
1755
1756/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001757 * Used when looping over a :for line, skip the "in expr" part.
1758 */
1759 void
1760skip_for_lines(void *fi_void, evalarg_T *evalarg)
1761{
1762 forinfo_T *fi = (forinfo_T *)fi_void;
1763 int i;
1764
1765 for (i = 0; i < fi->fi_break_count; ++i)
1766 eval_next_line(evalarg);
1767}
1768
1769/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 * Use the first item in a ":for" list. Advance to the next.
1771 * Assign the values to the variable (list). "arg" points to the first one.
1772 * Return TRUE when a valid item was found, FALSE when at end of list or
1773 * something wrong.
1774 */
1775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001778 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001780 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001781 ? (ASSIGN_FINAL
1782 // first round: error if variable exists
1783 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1784 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001785 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001788 if (fi->fi_blob != NULL)
1789 {
1790 typval_T tv;
1791
1792 if (fi->fi_bi >= blob_len(fi->fi_blob))
1793 return FALSE;
1794 tv.v_type = VAR_NUMBER;
1795 tv.v_lock = VAR_FIXED;
1796 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1797 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001798 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001799 fi->fi_varcount, flag, NULL) == OK;
1800 }
1801
1802 if (fi->fi_string != NULL)
1803 {
1804 typval_T tv;
1805 int len;
1806
1807 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1808 if (len == 0)
1809 return FALSE;
1810 tv.v_type = VAR_STRING;
1811 tv.v_lock = VAR_FIXED;
1812 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1813 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001814 ++fi->fi_bi;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001815 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001816 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001817 vim_free(tv.vval.v_string);
1818 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001819 }
1820
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 item = fi->fi_lw.lw_item;
1822 if (item == NULL)
1823 result = FALSE;
1824 else
1825 {
1826 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001827 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001828 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001829 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 }
1831 return result;
1832}
1833
1834/*
1835 * Free the structure used to store info used by ":for".
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839{
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001842 if (fi == NULL)
1843 return;
1844 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001845 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001847 list_unref(fi->fi_list);
1848 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001849 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001850 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001851 else
1852 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 vim_free(fi);
1854}
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857set_context_for_expression(
1858 expand_T *xp,
1859 char_u *arg,
1860 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001862 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 if (cmdidx == CMD_let || cmdidx == CMD_var
1867 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 {
1869 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001873 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 {
1875 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001876 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001877 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 break;
1879 }
1880 return;
1881 }
1882 }
1883 else
1884 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1885 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 while ((xp->xp_pattern = vim_strpbrk(arg,
1887 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1888 {
1889 c = *xp->xp_pattern;
1890 if (c == '&')
1891 {
1892 c = xp->xp_pattern[1];
1893 if (c == '&')
1894 {
1895 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001896 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001901 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1902 xp->xp_pattern += 2;
1903
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else if (c == '$')
1907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 xp->xp_context = EXPAND_ENV_VARS;
1910 }
1911 else if (c == '=')
1912 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 xp->xp_context = EXPAND_EXPRESSION;
1915 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001916 else if (c == '#'
1917 && xp->xp_context == EXPAND_EXPRESSION)
1918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001920 break;
1921 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001922 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 && xp->xp_context == EXPAND_FUNCTIONS
1924 && vim_strchr(xp->xp_pattern, '(') == NULL)
1925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 break;
1928 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001929 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1934 if (c == '\\' && xp->xp_pattern[1] != NUL)
1935 ++xp->xp_pattern;
1936 xp->xp_context = EXPAND_NOTHING;
1937 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001940 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1942 /* skip */ ;
1943 xp->xp_context = EXPAND_NOTHING;
1944 }
1945 else if (c == '|')
1946 {
1947 if (xp->xp_pattern[1] == '|')
1948 {
1949 ++xp->xp_pattern;
1950 xp->xp_context = EXPAND_EXPRESSION;
1951 }
1952 else
1953 xp->xp_context = EXPAND_COMMANDS;
1954 }
1955 else
1956 xp->xp_context = EXPAND_EXPRESSION;
1957 }
1958 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Doesn't look like something valid, expand as an expression
1960 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 arg = xp->xp_pattern;
1963 if (*arg != NUL)
1964 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1965 /* skip */ ;
1966 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001967
1968 // ":exe one two" completes "two"
1969 if ((cmdidx == CMD_execute
1970 || cmdidx == CMD_echo
1971 || cmdidx == CMD_echon
1972 || cmdidx == CMD_echomsg)
1973 && xp->xp_context == EXPAND_EXPRESSION)
1974 {
1975 for (;;)
1976 {
1977 char_u *n = skiptowhite(arg);
1978
1979 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1980 break;
1981 arg = skipwhite(n);
1982 }
1983 }
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 xp->xp_pattern = arg;
1986}
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 * Return TRUE if "pat" matches "text".
1990 * Does not use 'cpo' and always uses 'magic'.
1991 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001992 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001993pattern_match(char_u *pat, char_u *text, int ic)
1994{
1995 int matches = FALSE;
1996 char_u *save_cpo;
1997 regmatch_T regmatch;
1998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002000 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002001 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002002 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2003 if (regmatch.regprog != NULL)
2004 {
2005 regmatch.rm_ic = ic;
2006 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2007 vim_regfree(regmatch.regprog);
2008 }
2009 p_cpo = save_cpo;
2010 return matches;
2011}
2012
2013/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 * Handle a name followed by "(". Both for just "name(arg)" and for
2015 * "expr->name(arg)".
2016 * Returns OK or FAIL.
2017 */
2018 static int
2019eval_func(
2020 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002021 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 char_u *name,
2023 int name_len,
2024 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002025 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 typval_T *basetv) // "expr" for "expr->name(arg)"
2027{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002028 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029 char_u *s = name;
2030 int len = name_len;
2031 partial_T *partial;
2032 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002033 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034
2035 if (!evaluate)
2036 check_vars(s, len);
2037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002038 // If "s" is the name of a variable of type VAR_FUNC
2039 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002040 s = deref_func_name(s, &len, &partial,
2041 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Need to make a copy, in case evaluating the arguments makes
2044 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002046 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 ret = FAIL;
2048 else
2049 {
2050 funcexe_T funcexe;
2051
2052 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002053 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 funcexe.firstline = curwin->w_cursor.lnum;
2055 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 funcexe.evaluate = evaluate;
2057 funcexe.partial = partial;
2058 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002059 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002060 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002061 }
2062 vim_free(s);
2063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002064 // If evaluate is FALSE rettv->v_type was not set in
2065 // get_func_tv, but it's needed in handle_subscript() to parse
2066 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2068 {
2069 rettv->vval.v_string = NULL;
2070 rettv->v_type = VAR_FUNC;
2071 }
2072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002073 // Stop the expression evaluation when immediately
2074 // aborting on error, or when an interrupt occurred or
2075 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076 if (evaluate && aborting())
2077 {
2078 if (ret == OK)
2079 clear_tv(rettv);
2080 ret = FAIL;
2081 }
2082 return ret;
2083}
2084
2085/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002086 * Get the next line source line without advancing. But do skip over comment
2087 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002088 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002089 */
2090 static char_u *
2091getline_peek_skip_comments(evalarg_T *evalarg)
2092{
2093 for (;;)
2094 {
2095 char_u *next = getline_peek(evalarg->eval_getline,
2096 evalarg->eval_cookie);
2097 char_u *p;
2098
2099 if (next == NULL)
2100 break;
2101 p = skipwhite(next);
2102 if (*p != NUL && !vim9_comment_start(p))
2103 return next;
2104 (void)eval_next_line(evalarg);
2105 }
2106 return NULL;
2107}
2108
2109/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002110 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2111 * comment) and there is a next line, return the next line (skipping blanks)
2112 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002113 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2114 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 * "arg" must point somewhere inside a line, not at the start.
2116 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002117 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2119{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 char_u *p = skipwhite(arg);
2121
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002122 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002123 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002125 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002126 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002128 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129
2130 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002131 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002132 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002133 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136 {
2137 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002138 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 }
2140 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002141 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002142}
2143
2144/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002145 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002146 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002147 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002148 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002149eval_next_line(evalarg_T *evalarg)
2150{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002151 garray_T *gap = &evalarg->eval_ga;
2152 char_u *line;
2153
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002154 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002155 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2156 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002157 else
2158 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002159 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002160 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2161 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002162 char_u *p = skipwhite(line);
2163
2164 // Going to concatenate the lines after parsing. For an empty or
2165 // comment line use an empty string.
2166 if (*p == NUL || vim9_comment_start(p))
2167 {
2168 vim_free(line);
2169 line = vim_strsave((char_u *)"");
2170 }
2171
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002172 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2173 ++gap->ga_len;
2174 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002175 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002176 {
2177 vim_free(evalarg->eval_tofree);
2178 evalarg->eval_tofree = line;
2179 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002180
2181 // Advanced to the next line, "arg" no longer points into the previous
2182 // line.
2183 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2184
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002185 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002186}
2187
Bram Moolenaare6b53242020-07-01 17:28:33 +02002188/*
2189 * Call eval_next_non_blank() and get the next line if needed.
2190 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002191 char_u *
2192skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2193{
2194 int getnext;
2195 char_u *p = skipwhite(arg);
2196
Bram Moolenaar962d7212020-07-04 14:15:00 +02002197 if (evalarg == NULL)
2198 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002199 eval_next_non_blank(p, evalarg, &getnext);
2200 if (getnext)
2201 return eval_next_line(evalarg);
2202 return p;
2203}
2204
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002205/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002206 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002207 */
2208 void
2209clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2210{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002211 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002212 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002213 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002214 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002215 if (eap != NULL)
2216 {
2217 // We may need to keep the original command line, e.g. for
2218 // ":let" it has the variable names. But we may also need the
2219 // new one, "nextcmd" points into it. Keep both.
2220 vim_free(eap->cmdline_tofree);
2221 eap->cmdline_tofree = *eap->cmdlinep;
2222 *eap->cmdlinep = evalarg->eval_tofree;
2223 }
2224 else
2225 vim_free(evalarg->eval_tofree);
2226 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002227 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002228
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002229 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2230 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002231 }
2232}
2233
2234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2238 */
2239
2240/*
2241 * Handle zero level expression.
2242 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002244 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * Return OK or FAIL.
2247 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249eval0(
2250 char_u *arg,
2251 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002252 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 int ret;
2256 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002257 int did_emsg_before = did_emsg;
2258 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002260 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261
2262 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002264 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002265
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002266 if (ret != FAIL)
2267 end_error = !ends_excmd2(arg, p);
2268 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 /*
2273 * Report the invalid expression unless the expression evaluation has
2274 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002275 * exception, or we already gave a more specific error.
2276 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002278 if (!aborting()
2279 && did_emsg == did_emsg_before
2280 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002281 && (flags & EVAL_CONSTANT) == 0
2282 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002283 {
2284 if (end_error)
2285 semsg(_(e_trailing_arg), p);
2286 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002287 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002288 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002289
2290 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002291 // a next command to avoid more errors, unless "|" is following, which
2292 // could only be a command separator.
2293 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2294 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002297
2298 if (eap != NULL)
2299 eap->nextcmd = check_nextcmd(p);
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return ret;
2302}
2303
2304/*
2305 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002306 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002307 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 *
2309 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002310 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002312 * Note: "rettv.v_lock" is not set.
2313 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * Return OK or FAIL.
2315 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002316 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002317eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002319 char_u *p;
2320 int getnext;
2321
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002322 CLEAR_POINTER(rettv);
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /*
2325 * Get the first variable.
2326 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return FAIL;
2329
Bram Moolenaar793648f2020-06-26 21:28:25 +02002330 p = eval_next_non_blank(*arg, evalarg, &getnext);
2331 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002333 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002334 int result;
2335 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336 evalarg_T *evalarg_used = evalarg;
2337 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002338 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002339 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002340 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341
2342 if (evalarg == NULL)
2343 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002344 CLEAR_FIELD(local_evalarg);
2345 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002346 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002347 orig_flags = evalarg_used->eval_flags;
2348 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002350 if (getnext)
2351 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002352 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002353 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002354 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002355 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002356 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 clear_tv(rettv);
2358 return FAIL;
2359 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002360 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002361 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 int error = FALSE;
2367
Bram Moolenaar13106602020-10-04 16:06:05 +02002368 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002369 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002370 else if (vim9script)
2371 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002372 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002374 if (error || !op_falsy || !result)
2375 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 if (error)
2377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379
2380 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002381 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002383 if (op_falsy)
2384 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002385 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002386 {
mityu4ac198c2021-05-28 17:52:40 +02002387 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002388 clear_tv(rettv);
2389 return FAIL;
2390 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002391 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 evalarg_used->eval_flags = (op_falsy ? !result : result)
2393 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2394 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002395 {
2396 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002398 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002399 if (!op_falsy || !result)
2400 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002402 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002404 /*
2405 * Check for the ":".
2406 */
2407 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2408 if (*p != ':')
2409 {
2410 emsg(_(e_missing_colon));
2411 if (evaluate && result)
2412 clear_tv(rettv);
2413 evalarg_used->eval_flags = orig_flags;
2414 return FAIL;
2415 }
2416 if (getnext)
2417 *arg = eval_next_line(evalarg_used);
2418 else
2419 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002420 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002421 {
2422 error_white_both(p, 1);
2423 clear_tv(rettv);
2424 evalarg_used->eval_flags = orig_flags;
2425 return FAIL;
2426 }
2427 *arg = p;
2428 }
2429
2430 /*
2431 * Get the third variable. Recursive!
2432 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002433 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002434 {
mityu4ac198c2021-05-28 17:52:40 +02002435 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002436 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002437 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002438 return FAIL;
2439 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002440 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2441 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002443 if (eval1(arg, &var2, evalarg_used) == FAIL)
2444 {
2445 if (evaluate && result)
2446 clear_tv(rettv);
2447 evalarg_used->eval_flags = orig_flags;
2448 return FAIL;
2449 }
2450 if (evaluate && !result)
2451 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453
2454 if (evalarg == NULL)
2455 clear_evalarg(&local_evalarg, NULL);
2456 else
2457 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459
2460 return OK;
2461}
2462
2463/*
2464 * Handle first level expression:
2465 * expr2 || expr2 || expr2 logical OR
2466 *
2467 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002468 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 *
2470 * Return OK or FAIL.
2471 */
2472 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002475 char_u *p;
2476 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 /*
2479 * Get the first variable.
2480 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 return FAIL;
2483
2484 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002487 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490 evalarg_T *evalarg_used = evalarg;
2491 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 int evaluate;
2493 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 long result = FALSE;
2495 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002496 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002497 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002498
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 if (evalarg == NULL)
2500 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501 CLEAR_FIELD(local_evalarg);
2502 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 orig_flags = evalarg_used->eval_flags;
2505 evaluate = orig_flags & EVAL_EVALUATE;
2506 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002507 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002508 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002509 result = tv_get_bool_chk(rettv, &error);
2510 else if (tv_get_number_chk(rettv, &error) != 0)
2511 result = TRUE;
2512 clear_tv(rettv);
2513 if (error)
2514 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516
2517 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520 while (p[0] == '|' && p[1] == '|')
2521 {
2522 if (getnext)
2523 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002524 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002525 {
2526 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2527 {
2528 error_white_both(p, 2);
2529 clear_tv(rettv);
2530 return FAIL;
2531 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002532 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002533 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534
2535 /*
2536 * Get the second variable.
2537 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002538 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2539 {
mityu4ac198c2021-05-28 17:52:40 +02002540 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002541 clear_tv(rettv);
2542 return FAIL;
2543 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2545 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549
2550 /*
2551 * Compute the result.
2552 */
2553 if (evaluate && !result)
2554 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002555 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 result = tv_get_bool_chk(&var2, &error);
2557 else if (tv_get_number_chk(&var2, &error) != 0)
2558 result = TRUE;
2559 clear_tv(&var2);
2560 if (error)
2561 return FAIL;
2562 }
2563 if (evaluate)
2564 {
2565 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002566 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002567 rettv->v_type = VAR_BOOL;
2568 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002569 }
2570 else
2571 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002572 rettv->v_type = VAR_NUMBER;
2573 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002574 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576
2577 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002579
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 if (evalarg == NULL)
2581 clear_evalarg(&local_evalarg, NULL);
2582 else
2583 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586 return OK;
2587}
2588
2589/*
2590 * Handle second level expression:
2591 * expr3 && expr3 && expr3 logical AND
2592 *
2593 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002594 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 *
2596 * Return OK or FAIL.
2597 */
2598 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002601 char_u *p;
2602 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 /*
2605 * Get the first variable.
2606 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002607 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 return FAIL;
2609
2610 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002613 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616 evalarg_T *evalarg_used = evalarg;
2617 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 int orig_flags;
2619 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 long result = TRUE;
2621 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002622 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002623 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002624
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002625 if (evalarg == NULL)
2626 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 CLEAR_FIELD(local_evalarg);
2628 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002629 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 orig_flags = evalarg_used->eval_flags;
2631 evaluate = orig_flags & EVAL_EVALUATE;
2632 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002634 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002635 result = tv_get_bool_chk(rettv, &error);
2636 else if (tv_get_number_chk(rettv, &error) == 0)
2637 result = FALSE;
2638 clear_tv(rettv);
2639 if (error)
2640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642
2643 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002644 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002646 while (p[0] == '&' && p[1] == '&')
2647 {
2648 if (getnext)
2649 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002650 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002651 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002652 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002653 {
2654 error_white_both(p, 2);
2655 clear_tv(rettv);
2656 return FAIL;
2657 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002658 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002659 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660
2661 /*
2662 * Get the second variable.
2663 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002664 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2665 {
mityu4ac198c2021-05-28 17:52:40 +02002666 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002667 clear_tv(rettv);
2668 return FAIL;
2669 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2671 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002673 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676
2677 /*
2678 * Compute the result.
2679 */
2680 if (evaluate && result)
2681 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 result = tv_get_bool_chk(&var2, &error);
2684 else if (tv_get_number_chk(&var2, &error) == 0)
2685 result = FALSE;
2686 clear_tv(&var2);
2687 if (error)
2688 return FAIL;
2689 }
2690 if (evaluate)
2691 {
2692 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002693 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002694 rettv->v_type = VAR_BOOL;
2695 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002696 }
2697 else
2698 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002699 rettv->v_type = VAR_NUMBER;
2700 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002701 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002702 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703
2704 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002706
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707 if (evalarg == NULL)
2708 clear_evalarg(&local_evalarg, NULL);
2709 else
2710 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712
2713 return OK;
2714}
2715
2716/*
2717 * Handle third level expression:
2718 * var1 == var2
2719 * var1 =~ var2
2720 * var1 != var2
2721 * var1 !~ var2
2722 * var1 > var2
2723 * var1 >= var2
2724 * var1 < var2
2725 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002726 * var1 is var2
2727 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 *
2729 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002730 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 *
2732 * Return OK or FAIL.
2733 */
2734 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002738 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002739 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002741 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /*
2744 * Get the first variable.
2745 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return FAIL;
2748
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002749 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002750 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002755 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002757 typval_T var2;
2758 int ic;
2759 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002760 int evaluate = evalarg == NULL
2761 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002762
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002763 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002764 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002765 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002766 p = *arg;
2767 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002768 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2769 {
mityu4ac198c2021-05-28 17:52:40 +02002770 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002771 clear_tv(rettv);
2772 return FAIL;
2773 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002774
Bram Moolenaar696ba232020-07-29 21:20:41 +02002775 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2776 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002777 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002778 clear_tv(rettv);
2779 return FAIL;
2780 }
2781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002782 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (p[len] == '?')
2784 {
2785 ic = TRUE;
2786 ++len;
2787 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002788 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 else if (p[len] == '#')
2790 {
2791 ic = FALSE;
2792 ++len;
2793 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002794 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002796 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /*
2799 * Get the second variable.
2800 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002801 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2802 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002803 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002804 clear_tv(rettv);
2805 return FAIL;
2806 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002807 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002808 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002810 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 return FAIL;
2812 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002813 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002814 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002815 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002816
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002817 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002818 {
2819 ret = FAIL;
2820 clear_tv(rettv);
2821 }
2822 else
2823 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002824 clear_tv(&var2);
2825 return ret;
2826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
2829 return OK;
2830}
2831
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002832/*
2833 * Make a copy of blob "tv1" and append blob "tv2".
2834 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 void
2836eval_addblob(typval_T *tv1, typval_T *tv2)
2837{
2838 blob_T *b1 = tv1->vval.v_blob;
2839 blob_T *b2 = tv2->vval.v_blob;
2840 blob_T *b = blob_alloc();
2841 int i;
2842
2843 if (b != NULL)
2844 {
2845 for (i = 0; i < blob_len(b1); i++)
2846 ga_append(&b->bv_ga, blob_get(b1, i));
2847 for (i = 0; i < blob_len(b2); i++)
2848 ga_append(&b->bv_ga, blob_get(b2, i));
2849
2850 clear_tv(tv1);
2851 rettv_blob_set(tv1, b);
2852 }
2853}
2854
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002855/*
2856 * Make a copy of list "tv1" and append list "tv2".
2857 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 int
2859eval_addlist(typval_T *tv1, typval_T *tv2)
2860{
2861 typval_T var3;
2862
2863 // concatenate Lists
2864 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2865 {
2866 clear_tv(tv1);
2867 clear_tv(tv2);
2868 return FAIL;
2869 }
2870 clear_tv(tv1);
2871 *tv1 = var3;
2872 return OK;
2873}
2874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875/*
2876 * Handle fourth level expression:
2877 * + number addition
2878 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002879 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002880 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 *
2882 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002883 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * Return OK or FAIL.
2886 */
2887 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /*
2891 * Get the first variable.
2892 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 return FAIL;
2895
2896 /*
2897 * Repeat computing, until no '+', '-' or '.' is following.
2898 */
2899 for (;;)
2900 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002901 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 int getnext;
2903 char_u *p;
2904 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002905 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906 int concat;
2907 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002908 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002910 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002911 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002912 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 p = eval_next_non_blank(*arg, evalarg, &getnext);
2914 op = *p;
2915 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002916 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2917 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002919 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2920 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002921
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002922 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002923 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002925 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002926 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002927 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002928 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002929 {
mityu4ac198c2021-05-28 17:52:40 +02002930 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002931 clear_tv(rettv);
2932 return FAIL;
2933 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002934 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002935 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002936 if ((op != '+' || (rettv->v_type != VAR_LIST
2937 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002938#ifdef FEAT_FLOAT
2939 && (op == '.' || rettv->v_type != VAR_FLOAT)
2940#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002941 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002943 int error = FALSE;
2944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002945 // For "list + ...", an illegal use of the first operand as
2946 // a number cannot be determined before evaluating the 2nd
2947 // operand: if this is also a list, all is ok.
2948 // For "something . ...", "something - ..." or "non-list + ...",
2949 // we know that the first operand needs to be a string or number
2950 // without evaluating the 2nd operand. So check before to avoid
2951 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002952 if (op != '.')
2953 tv_get_number_chk(rettv, &error);
2954 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002955 {
2956 clear_tv(rettv);
2957 return FAIL;
2958 }
2959 }
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /*
2962 * Get the second variable.
2963 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002964 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002965 {
mityu89dcb4d2021-05-28 13:50:17 +02002966 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002967 clear_tv(rettv);
2968 return FAIL;
2969 }
2970 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002971 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 return FAIL;
2975 }
2976
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002977 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 /*
2980 * Compute the result.
2981 */
2982 if (op == '.')
2983 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2985 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002986 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987
Bram Moolenaar2e086612020-08-25 22:37:48 +02002988 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002989 || var2.v_type == VAR_CHANNEL
2990 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002991 semsg(_(e_using_invalid_value_as_string_str),
2992 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002993#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002994 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002995 {
2996 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2997 var2.vval.v_float);
2998 s2 = buf2;
2999 }
3000#endif
3001 else
3002 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003003 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004 {
3005 clear_tv(rettv);
3006 clear_tv(&var2);
3007 return FAIL;
3008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 clear_tv(rettv);
3011 rettv->v_type = VAR_STRING;
3012 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003014 else if (op == '+' && rettv->v_type == VAR_BLOB
3015 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003017 else if (op == '+' && rettv->v_type == VAR_LIST
3018 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003019 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003021 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
3024 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003025 int error = FALSE;
3026 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028 float_T f1 = 0, f2 = 0;
3029
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003030 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003031 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032 f1 = rettv->vval.v_float;
3033 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035 else
3036#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003038 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039 if (error)
3040 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003041 // This can only happen for "list + non-list" or
3042 // "blob + non-blob". For "non-list + ..." or
3043 // "something - ...", we returned before evaluating the
3044 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003045 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003046 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 return FAIL;
3048 }
3049#ifdef FEAT_FLOAT
3050 if (var2.v_type == VAR_FLOAT)
3051 f1 = n1;
3052#endif
3053 }
3054#ifdef FEAT_FLOAT
3055 if (var2.v_type == VAR_FLOAT)
3056 {
3057 f2 = var2.vval.v_float;
3058 n2 = 0;
3059 }
3060 else
3061#endif
3062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003063 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064 if (error)
3065 {
3066 clear_tv(rettv);
3067 clear_tv(&var2);
3068 return FAIL;
3069 }
3070#ifdef FEAT_FLOAT
3071 if (rettv->v_type == VAR_FLOAT)
3072 f2 = n2;
3073#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076
3077#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003078 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3080 {
3081 if (op == '+')
3082 f1 = f1 + f2;
3083 else
3084 f1 = f1 - f2;
3085 rettv->v_type = VAR_FLOAT;
3086 rettv->vval.v_float = f1;
3087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#endif
3090 {
3091 if (op == '+')
3092 n1 = n1 + n2;
3093 else
3094 n1 = n1 - n2;
3095 rettv->v_type = VAR_NUMBER;
3096 rettv->vval.v_number = n1;
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003099 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 }
3102 return OK;
3103}
3104
3105/*
3106 * Handle fifth level expression:
3107 * * number multiplication
3108 * / number division
3109 * % number modulo
3110 *
3111 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003112 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 *
3114 * Return OK or FAIL.
3115 */
3116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117eval6(
3118 char_u **arg,
3119 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003121 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003124 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /*
3128 * Get the first variable.
3129 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003130 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return FAIL;
3132
3133 /*
3134 * Repeat computing, until no '*', '/' or '%' is following.
3135 */
3136 for (;;)
3137 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003138 int evaluate;
3139 int getnext;
3140 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003141 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003142 int op;
3143 varnumber_T n1, n2;
3144#ifdef FEAT_FLOAT
3145 float_T f1, f2;
3146#endif
3147 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003148
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003149 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003150 p = eval_next_non_blank(*arg, evalarg, &getnext);
3151 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003152 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003154
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003155 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003156 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003157 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003158 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003159 {
3160 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3161 {
mityu4ac198c2021-05-28 17:52:40 +02003162 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003163 clear_tv(rettv);
3164 return FAIL;
3165 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003166 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003169#ifdef FEAT_FLOAT
3170 f1 = 0;
3171 f2 = 0;
3172#endif
3173 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003174 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176#ifdef FEAT_FLOAT
3177 if (rettv->v_type == VAR_FLOAT)
3178 {
3179 f1 = rettv->vval.v_float;
3180 use_float = TRUE;
3181 n1 = 0;
3182 }
3183 else
3184#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003185 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003187 if (error)
3188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190 else
3191 n1 = 0;
3192
3193 /*
3194 * Get the second variable.
3195 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003196 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3197 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003198 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003199 clear_tv(rettv);
3200 return FAIL;
3201 }
3202 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003203 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 return FAIL;
3205
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003206 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208#ifdef FEAT_FLOAT
3209 if (var2.v_type == VAR_FLOAT)
3210 {
3211 if (!use_float)
3212 {
3213 f1 = n1;
3214 use_float = TRUE;
3215 }
3216 f2 = var2.vval.v_float;
3217 n2 = 0;
3218 }
3219 else
3220#endif
3221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003222 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 clear_tv(&var2);
3224 if (error)
3225 return FAIL;
3226#ifdef FEAT_FLOAT
3227 if (use_float)
3228 f2 = n2;
3229#endif
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
3232 /*
3233 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003236#ifdef FEAT_FLOAT
3237 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 if (op == '*')
3240 f1 = f1 * f2;
3241 else if (op == '/')
3242 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003243# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003244 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003245 if (f2 == 0.0)
3246 {
3247 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003249 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003250 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003251 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003252 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003253 }
3254 else
3255 f1 = f1 / f2;
3256# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 // We rely on the floating point library to handle divide
3258 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003259 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003260# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003263 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003265 return FAIL;
3266 }
3267 rettv->v_type = VAR_FLOAT;
3268 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003273 int failed = FALSE;
3274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003275 if (op == '*')
3276 n1 = n1 * n2;
3277 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003278 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003280 n1 = num_modulus(n1, n2, &failed);
3281 if (failed)
3282 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003284 rettv->v_type = VAR_NUMBER;
3285 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 }
3289
3290 return OK;
3291}
3292
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003293/*
3294 * Handle a type cast before a base level expression.
3295 * "arg" must point to the first non-white of the expression.
3296 * "arg" is advanced to just after the recognized expression.
3297 * Return OK or FAIL.
3298 */
3299 static int
3300eval7t(
3301 char_u **arg,
3302 typval_T *rettv,
3303 evalarg_T *evalarg,
3304 int want_string) // after "." operator
3305{
3306 type_T *want_type = NULL;
3307 garray_T type_list; // list of pointers to allocated types
3308 int res;
3309 int evaluate = evalarg == NULL ? 0
3310 : (evalarg->eval_flags & EVAL_EVALUATE);
3311
3312 // Recognize <type> in Vim9 script only.
3313 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3314 {
3315 ++*arg;
3316 ga_init2(&type_list, sizeof(type_T *), 10);
3317 want_type = parse_type(arg, &type_list, TRUE);
3318 if (want_type == NULL && (evaluate || **arg != '>'))
3319 {
3320 clear_type_list(&type_list);
3321 return FAIL;
3322 }
3323
3324 if (**arg != '>')
3325 {
3326 if (*skipwhite(*arg) == '>')
3327 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3328 else
3329 emsg(_(e_missing_gt));
3330 clear_type_list(&type_list);
3331 return FAIL;
3332 }
3333 ++*arg;
3334 *arg = skipwhite_and_linebreak(*arg, evalarg);
3335 }
3336
3337 res = eval7(arg, rettv, evalarg, want_string);
3338
3339 if (want_type != NULL && evaluate)
3340 {
3341 if (res == OK)
3342 {
3343 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3344
3345 if (!equal_type(want_type, actual))
3346 {
3347 if (want_type == &t_bool && actual != &t_bool
3348 && (actual->tt_flags & TTFLAG_BOOL_OK))
3349 {
3350 int n = tv2bool(rettv);
3351
3352 // can use "0" and "1" for boolean in some places
3353 clear_tv(rettv);
3354 rettv->v_type = VAR_BOOL;
3355 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3356 }
3357 else
3358 {
3359 where_T where;
3360
3361 where.wt_index = 0;
3362 where.wt_variable = TRUE;
3363 res = check_type(want_type, actual, TRUE, where);
3364 }
3365 }
3366 }
3367 clear_type_list(&type_list);
3368 }
3369
3370 return res;
3371}
3372
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003373 int
3374eval_leader(char_u **arg, int vim9)
3375{
3376 char_u *s = *arg;
3377 char_u *p = *arg;
3378
3379 while (*p == '!' || *p == '-' || *p == '+')
3380 {
3381 char_u *n = skipwhite(p + 1);
3382
3383 // ++, --, -+ and +- are not accepted in Vim9 script
3384 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3385 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003386 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003387 return FAIL;
3388 }
3389 p = n;
3390 }
3391 *arg = p;
3392 return OK;
3393}
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395/*
3396 * Handle sixth level expression:
3397 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003398 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003399 * "string" string constant
3400 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * &option-name option value
3402 * @r register contents
3403 * identifier variable value
3404 * function() function call
3405 * $VAR environment variable
3406 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003409 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003410 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 *
3412 * Also handle:
3413 * ! in front logical NOT
3414 * - in front unary minus
3415 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 * trailing [] subscript in String or List
3417 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003418 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 *
3420 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003421 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 *
3423 * Return OK or FAIL.
3424 */
3425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426eval7(
3427 char_u **arg,
3428 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003432 int evaluate = evalarg != NULL
3433 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 int len;
3435 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 char_u *start_leader, *end_leader;
3437 int ret = OK;
3438 char_u *alias;
3439
3440 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003442 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003447 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003450 if (eval_leader(arg, in_vim9script()) == FAIL)
3451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 end_leader = *arg;
3453
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003454 if (**arg == '.' && (!isdigit(*(*arg + 1))
3455#ifdef FEAT_FLOAT
3456 || current_sctx.sc_version < 2
3457#endif
3458 ))
3459 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003460 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003461 ++*arg;
3462 return FAIL;
3463 }
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 switch (**arg)
3466 {
3467 /*
3468 * Number constant.
3469 */
3470 case '0':
3471 case '1':
3472 case '2':
3473 case '3':
3474 case '4':
3475 case '5':
3476 case '6':
3477 case '7':
3478 case '8':
3479 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003481
3482 // Apply prefixed "-" and "+" now. Matters especially when
3483 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003484 if (ret == OK && evaluate && end_leader > start_leader
3485 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003486 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 break;
3488
3489 /*
3490 * String constant: "string".
3491 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 break;
3494
3495 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003496 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003498 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003499 break;
3500
3501 /*
3502 * List: [expr, expr]
3503 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003504 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003508 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003509 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003510 case '#': if (in_vim9script())
3511 {
3512 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3513 }
3514 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003515 {
3516 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003517 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003518 }
3519 else
3520 ret = NOTDONE;
3521 break;
3522
3523 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003524 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003525 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003527 case '{': if (in_vim9script())
3528 ret = NOTDONE;
3529 else
3530 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003531 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003532 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003533 break;
3534
3535 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003536 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003538 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 break;
3540
3541 /*
3542 * Environment variable: $VAR.
3543 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003544 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 break;
3546
3547 /*
3548 * Register contents: @r.
3549 */
3550 case '@': ++*arg;
3551 if (evaluate)
3552 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003553 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3554 semsg(_(e_syntax_error_at_str), *arg);
3555 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3556 emsg_invreg(**arg);
3557 else
3558 {
3559 rettv->v_type = VAR_STRING;
3560 rettv->vval.v_string = get_reg_contents(**arg,
3561 GREG_EXPR_SRC);
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 if (**arg != NUL)
3565 ++*arg;
3566 break;
3567
3568 /*
3569 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003570 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003572 case '(': ret = NOTDONE;
3573 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003574 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003575 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003576 if (ret == OK && evaluate)
3577 {
3578 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3579
Bram Moolenaara9931532021-06-12 15:58:16 +02003580 // Compile it here to get the return type. The return
3581 // type is optional, when it's missing use t_unknown.
3582 // This is recognized in compile_return().
3583 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3584 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003585 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003586 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003587 {
3588 clear_tv(rettv);
3589 ret = FAIL;
3590 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003591 }
3592 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003593 if (ret == NOTDONE)
3594 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003595 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003596 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003597
Bram Moolenaar9215f012020-06-27 21:18:00 +02003598 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003599 if (**arg == ')')
3600 ++*arg;
3601 else if (ret == OK)
3602 {
3603 emsg(_(e_missing_close));
3604 clear_tv(rettv);
3605 ret = FAIL;
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 break;
3609
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 break;
3612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613
3614 if (ret == NOTDONE)
3615 {
3616 /*
3617 * Must be a variable or function name.
3618 * Can also be a curly-braces kind of name: {expr}.
3619 */
3620 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003621 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 if (alias != NULL)
3623 s = alias;
3624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003625 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 ret = FAIL;
3627 else
3628 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003629 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3630
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003631 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003632 {
3633 emsg(_(e_cannot_use_underscore_here));
3634 ret = FAIL;
3635 }
3636 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003637 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003638 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003639 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003640 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003641 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003642 else if (flags & EVAL_CONSTANT)
3643 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003645 {
3646 // get the value of "true", "false" or a variable
3647 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3648 {
3649 rettv->v_type = VAR_BOOL;
3650 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003651 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003652 }
3653 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003654 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003655 {
3656 rettv->v_type = VAR_BOOL;
3657 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003658 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003659 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003660 else if (len == 4 && in_vim9script()
3661 && STRNCMP(s, "null", 4) == 0)
3662 {
3663 rettv->v_type = VAR_SPECIAL;
3664 rettv->vval.v_number = VVAL_NULL;
3665 ret = OK;
3666 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003667 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003668 ret = eval_variable(s, len, rettv, NULL,
3669 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003670 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003671 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003672 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003673 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003674 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003675 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003678 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 }
3680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003681 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3682 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003683 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003684 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
3686 /*
3687 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3688 */
3689 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003690 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003691 return ret;
3692}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003694/*
3695 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003696 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003697 * Adjusts "end_leaderp" until it is at "start_leader".
3698 */
3699 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003700eval7_leader(
3701 typval_T *rettv,
3702 int numeric_only,
3703 char_u *start_leader,
3704 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003705{
3706 char_u *end_leader = *end_leaderp;
3707 int ret = OK;
3708 int error = FALSE;
3709 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003710 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003711#ifdef FEAT_FLOAT
3712 float_T f = 0.0;
3713
3714 if (rettv->v_type == VAR_FLOAT)
3715 f = rettv->vval.v_float;
3716 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003718 {
3719 while (VIM_ISWHITE(end_leader[-1]))
3720 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003721 if (in_vim9script() && end_leader[-1] == '!')
3722 val = tv2bool(rettv);
3723 else
3724 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003725 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003726 if (error)
3727 {
3728 clear_tv(rettv);
3729 ret = FAIL;
3730 }
3731 else
3732 {
3733 while (end_leader > start_leader)
3734 {
3735 --end_leader;
3736 if (*end_leader == '!')
3737 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003738 if (numeric_only)
3739 {
3740 ++end_leader;
3741 break;
3742 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003743#ifdef FEAT_FLOAT
3744 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003745 {
3746 if (in_vim9script())
3747 {
3748 rettv->v_type = VAR_BOOL;
3749 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3750 }
3751 else
3752 f = !f;
3753 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003754 else
3755#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003756 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003757 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003758 type = VAR_BOOL;
3759 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003760 }
3761 else if (*end_leader == '-')
3762 {
3763#ifdef FEAT_FLOAT
3764 if (rettv->v_type == VAR_FLOAT)
3765 f = -f;
3766 else
3767#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003768 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003769 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003770 type = VAR_NUMBER;
3771 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 }
3773 }
3774#ifdef FEAT_FLOAT
3775 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003778 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003781#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003783 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003784 if (in_vim9script())
3785 rettv->v_type = type;
3786 else
3787 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003788 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003791 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return ret;
3793}
3794
3795/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003796 * Call the function referred to in "rettv".
3797 */
3798 static int
3799call_func_rettv(
3800 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003801 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003802 typval_T *rettv,
3803 int evaluate,
3804 dict_T *selfdict,
3805 typval_T *basetv)
3806{
3807 partial_T *pt = NULL;
3808 funcexe_T funcexe;
3809 typval_T functv;
3810 char_u *s;
3811 int ret;
3812
3813 // need to copy the funcref so that we can clear rettv
3814 if (evaluate)
3815 {
3816 functv = *rettv;
3817 rettv->v_type = VAR_UNKNOWN;
3818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003819 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 if (functv.v_type == VAR_PARTIAL)
3821 {
3822 pt = functv.vval.v_partial;
3823 s = partial_name(pt);
3824 }
3825 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003826 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003827 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003828 if (s == NULL || *s == NUL)
3829 {
3830 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003831 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003832 goto theend;
3833 }
3834 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835 }
3836 else
3837 s = (char_u *)"";
3838
Bram Moolenaara80faa82020-04-12 19:37:17 +02003839 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003840 funcexe.firstline = curwin->w_cursor.lnum;
3841 funcexe.lastline = curwin->w_cursor.lnum;
3842 funcexe.evaluate = evaluate;
3843 funcexe.partial = pt;
3844 funcexe.selfdict = selfdict;
3845 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003846 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003847
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003848theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003849 // Clear the funcref afterwards, so that deleting it while
3850 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851 if (evaluate)
3852 clear_tv(&functv);
3853
3854 return ret;
3855}
3856
3857/*
3858 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003859 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003860 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3861 */
3862 static int
3863eval_lambda(
3864 char_u **arg,
3865 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003866 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003868{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003869 int evaluate = evalarg != NULL
3870 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871 typval_T base = *rettv;
3872 int ret;
3873
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003874 rettv->v_type = VAR_UNKNOWN;
3875
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003876 if (**arg == '{')
3877 {
3878 // ->{lambda}()
3879 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3880 }
3881 else
3882 {
3883 // ->(lambda)()
3884 ++*arg;
3885 ret = eval1(arg, rettv, evalarg);
3886 *arg = skipwhite_and_linebreak(*arg, evalarg);
3887 if (**arg != ')')
3888 {
3889 emsg(_(e_missing_close));
3890 ret = FAIL;
3891 }
3892 ++*arg;
3893 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003894 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003895 return FAIL;
3896 else if (**arg != '(')
3897 {
3898 if (verbose)
3899 {
3900 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003901 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003902 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003904 }
3905 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003906 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003907 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003908 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003909 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003910
3911 // Clear the funcref afterwards, so that deleting it while
3912 // evaluating the arguments is possible (see test55).
3913 if (evaluate)
3914 clear_tv(&base);
3915
3916 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003917}
3918
3919/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003921 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003922 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3923 */
3924 static int
3925eval_method(
3926 char_u **arg,
3927 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003928 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003929 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003930{
3931 char_u *name;
3932 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003933 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003935 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003936 int evaluate = evalarg != NULL
3937 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003939 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003940
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003942 len = get_name_len(arg, &alias, evaluate, TRUE);
3943 if (alias != NULL)
3944 name = alias;
3945
3946 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003947 {
3948 if (verbose)
3949 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003950 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003951 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003952 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003953 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003954 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003955 if (**arg != '(')
3956 {
3957 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003959 ret = FAIL;
3960 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003961 else if (VIM_ISWHITE((*arg)[-1]))
3962 {
3963 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003964 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003965 ret = FAIL;
3966 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003967 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003968 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003969 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003970 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003971
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003972 // Clear the funcref afterwards, so that deleting it while
3973 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003974 if (evaluate)
3975 clear_tv(&base);
3976
Bram Moolenaarac92e252019-08-03 21:58:38 +02003977 return ret;
3978}
3979
3980/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003981 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3982 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003983 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3984 */
3985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986eval_index(
3987 char_u **arg,
3988 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003989 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003990 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003992 int evaluate = evalarg != NULL
3993 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003996 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003998 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003999 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004001 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4002 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004004 init_tv(&var1);
4005 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004006 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004007 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004008 /*
4009 * dict.name
4010 */
4011 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004014 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004016 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017 }
4018 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 /*
4021 * something[idx]
4022 *
4023 * Get the (first) variable from inside the [].
4024 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004025 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026 if (**arg == ':')
4027 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004028 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004029 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004030 else if (vim9 && **arg == ':')
4031 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004032 semsg(_(e_white_space_required_before_and_after_str_at_str),
4033 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004034 clear_tv(&var1);
4035 return FAIL;
4036 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004037 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004039#ifdef FEAT_FLOAT
4040 // allow for indexing with float
4041 if (vim9 && rettv->v_type == VAR_DICT
4042 && var1.v_type == VAR_FLOAT)
4043 {
4044 var1.vval.v_string = typval_tostring(&var1, TRUE);
4045 var1.v_type = VAR_STRING;
4046 }
4047#endif
4048 if (tv_get_string_chk(&var1) == NULL)
4049 {
4050 // not a number or string
4051 clear_tv(&var1);
4052 return FAIL;
4053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055
4056 /*
4057 * Get the second variable from inside the [:].
4058 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004059 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 if (**arg == ':')
4061 {
4062 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004063 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004064 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004065 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004066 semsg(_(e_white_space_required_before_and_after_str_at_str),
4067 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004068 if (!empty1)
4069 clear_tv(&var1);
4070 return FAIL;
4071 }
4072 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 if (**arg == ']')
4074 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004075 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 if (!empty1)
4078 clear_tv(&var1);
4079 return FAIL;
4080 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004081 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004083 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 if (!empty1)
4085 clear_tv(&var1);
4086 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004087 return FAIL;
4088 }
4089 }
4090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004092 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004093 if (**arg != ']')
4094 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004095 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004096 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004097 clear_tv(&var1);
4098 if (range)
4099 clear_tv(&var2);
4100 return FAIL;
4101 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004102 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004103 }
4104
4105 if (evaluate)
4106 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004107 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004108 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004110
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004113 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004114 clear_tv(&var2);
4115 return res;
4116 }
4117 return OK;
4118}
4119
4120/*
4121 * Check if "rettv" can have an [index] or [sli:ce]
4122 */
4123 int
4124check_can_index(typval_T *rettv, int evaluate, int verbose)
4125{
4126 switch (rettv->v_type)
4127 {
4128 case VAR_FUNC:
4129 case VAR_PARTIAL:
4130 if (verbose)
4131 emsg(_("E695: Cannot index a Funcref"));
4132 return FAIL;
4133 case VAR_FLOAT:
4134#ifdef FEAT_FLOAT
4135 if (verbose)
4136 emsg(_(e_float_as_string));
4137 return FAIL;
4138#endif
4139 case VAR_BOOL:
4140 case VAR_SPECIAL:
4141 case VAR_JOB:
4142 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004143 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 if (verbose)
4145 emsg(_(e_cannot_index_special_variable));
4146 return FAIL;
4147 case VAR_UNKNOWN:
4148 case VAR_ANY:
4149 case VAR_VOID:
4150 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004151 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004152 emsg(_(e_cannot_index_special_variable));
4153 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004154 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004156
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 case VAR_STRING:
4158 case VAR_LIST:
4159 case VAR_DICT:
4160 case VAR_BLOB:
4161 break;
4162 case VAR_NUMBER:
4163 if (in_vim9script())
4164 emsg(_(e_cannot_index_number));
4165 break;
4166 }
4167 return OK;
4168}
4169
4170/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004171 * slice() function
4172 */
4173 void
4174f_slice(typval_T *argvars, typval_T *rettv)
4175{
4176 if (check_can_index(argvars, TRUE, FALSE) == OK)
4177 {
4178 copy_tv(argvars, rettv);
4179 eval_index_inner(rettv, TRUE, argvars + 1,
4180 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4181 TRUE, NULL, 0, FALSE);
4182 }
4183}
4184
4185/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004186 * Apply index or range to "rettv".
4187 * "var1" is the first index, NULL for [:expr].
4188 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004189 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4190 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004191 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4192 */
4193 int
4194eval_index_inner(
4195 typval_T *rettv,
4196 int is_range,
4197 typval_T *var1,
4198 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004199 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004200 char_u *key,
4201 int keylen,
4202 int verbose)
4203{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004204 varnumber_T n1, n2 = 0;
4205 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004206
4207 n1 = 0;
4208 if (var1 != NULL && rettv->v_type != VAR_DICT)
4209 n1 = tv_get_number(var1);
4210
4211 if (is_range)
4212 {
4213 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004215 if (verbose)
4216 emsg(_(e_cannot_slice_dictionary));
4217 return FAIL;
4218 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004219 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004220 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004221 else
4222 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004223 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004224
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004225 switch (rettv->v_type)
4226 {
4227 case VAR_UNKNOWN:
4228 case VAR_ANY:
4229 case VAR_VOID:
4230 case VAR_FUNC:
4231 case VAR_PARTIAL:
4232 case VAR_FLOAT:
4233 case VAR_BOOL:
4234 case VAR_SPECIAL:
4235 case VAR_JOB:
4236 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004237 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004238 break; // not evaluating, skipping over subscript
4239
4240 case VAR_NUMBER:
4241 case VAR_STRING:
4242 {
4243 char_u *s = tv_get_string(rettv);
4244
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004245 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004246 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004247 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004248 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004249 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004250 else
4251 s = char_from_string(s, n1);
4252 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004255 // The resulting variable is a substring. If the indexes
4256 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004257 if (n1 < 0)
4258 {
4259 n1 = len + n1;
4260 if (n1 < 0)
4261 n1 = 0;
4262 }
4263 if (n2 < 0)
4264 n2 = len + n2;
4265 else if (n2 >= len)
4266 n2 = len;
4267 if (n1 >= len || n2 < 0 || n1 > n2)
4268 s = NULL;
4269 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004270 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 }
4272 else
4273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004274 // The resulting variable is a string of a single
4275 // character. If the index is too big or negative the
4276 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 if (n1 >= len || n1 < 0)
4278 s = NULL;
4279 else
4280 s = vim_strnsave(s + n1, 1);
4281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
4283 rettv->v_type = VAR_STRING;
4284 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004285 }
4286 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004288 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004289 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4290 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 break;
4292
4293 case VAR_LIST:
4294 if (var1 == NULL)
4295 n1 = 0;
4296 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004297 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004298 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004299 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004300 return FAIL;
4301 break;
4302
4303 case VAR_DICT:
4304 {
4305 dictitem_T *item;
4306 typval_T tmp;
4307
4308 if (key == NULL)
4309 {
4310 key = tv_get_string_chk(var1);
4311 if (key == NULL)
4312 return FAIL;
4313 }
4314
4315 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4316
4317 if (item == NULL && verbose)
4318 semsg(_(e_dictkey), key);
4319 if (item == NULL)
4320 return FAIL;
4321
4322 copy_tv(&item->di_tv, &tmp);
4323 clear_tv(rettv);
4324 *rettv = tmp;
4325 }
4326 break;
4327 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004328 return OK;
4329}
4330
4331/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004332 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004333 */
4334 char_u *
4335partial_name(partial_T *pt)
4336{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004337 if (pt != NULL)
4338 {
4339 if (pt->pt_name != NULL)
4340 return pt->pt_name;
4341 if (pt->pt_func != NULL)
4342 return pt->pt_func->uf_name;
4343 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004344 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004345}
4346
Bram Moolenaarddecc252016-04-06 22:59:37 +02004347 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004348partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004349{
4350 int i;
4351
4352 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004353 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004354 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004355 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004356 if (pt->pt_name != NULL)
4357 {
4358 func_unref(pt->pt_name);
4359 vim_free(pt->pt_name);
4360 }
4361 else
4362 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004363
Bram Moolenaar54656012021-06-09 20:50:46 +02004364 // "out_up" is no longer used, decrement refcount on partial that owns it.
4365 partial_unref(pt->pt_outer.out_up_partial);
4366
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004367 // Decrease the reference count for the context of a closure. If down
4368 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004369 if (pt->pt_funcstack != NULL)
4370 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004371 --pt->pt_funcstack->fs_refcount;
4372 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004373 }
4374
Bram Moolenaarddecc252016-04-06 22:59:37 +02004375 vim_free(pt);
4376}
4377
4378/*
4379 * Unreference a closure: decrement the reference count and free it when it
4380 * becomes zero.
4381 */
4382 void
4383partial_unref(partial_T *pt)
4384{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004385 if (pt != NULL)
4386 {
4387 if (--pt->pt_refcount <= 0)
4388 partial_free(pt);
4389
4390 // If the reference count goes down to one, the funcstack may be the
4391 // only reference and can be freed if no other partials reference it.
4392 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4393 funcstack_check_refcount(pt->pt_funcstack);
4394 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004395}
4396
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004398 * Return the next (unique) copy ID.
4399 * Used for serializing nested structures.
4400 */
4401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004402get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004403{
4404 current_copyID += COPYID_INC;
4405 return current_copyID;
4406}
4407
4408/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004409 * Garbage collection for lists and dictionaries.
4410 *
4411 * We use reference counts to be able to free most items right away when they
4412 * are no longer used. But for composite items it's possible that it becomes
4413 * unused while the reference count is > 0: When there is a recursive
4414 * reference. Example:
4415 * :let l = [1, 2, 3]
4416 * :let d = {9: l}
4417 * :let l[1] = d
4418 *
4419 * Since this is quite unusual we handle this with garbage collection: every
4420 * once in a while find out which lists and dicts are not referenced from any
4421 * variable.
4422 *
4423 * Here is a good reference text about garbage collection (refers to Python
4424 * but it applies to all reference-counting mechanisms):
4425 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004426 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004427
4428/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004429 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004430 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004431 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004432 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004433 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004434garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004435{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004436 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004437 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004438 buf_T *buf;
4439 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004440 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004441 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004442
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004443 if (!testing)
4444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004445 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004446 want_garbage_collect = FALSE;
4447 may_garbage_collect = FALSE;
4448 garbage_collect_at_exit = FALSE;
4449 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004450
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004451 // The execution stack can grow big, limit the size.
4452 if (exestack.ga_maxlen - exestack.ga_len > 500)
4453 {
4454 size_t new_len;
4455 char_u *pp;
4456 int n;
4457
4458 // Keep 150% of the current size, with a minimum of the growth size.
4459 n = exestack.ga_len / 2;
4460 if (n < exestack.ga_growsize)
4461 n = exestack.ga_growsize;
4462
4463 // Don't make it bigger though.
4464 if (exestack.ga_len + n < exestack.ga_maxlen)
4465 {
4466 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4467 pp = vim_realloc(exestack.ga_data, new_len);
4468 if (pp == NULL)
4469 return FAIL;
4470 exestack.ga_maxlen = exestack.ga_len + n;
4471 exestack.ga_data = pp;
4472 }
4473 }
4474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // We advance by two because we add one for items referenced through
4476 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004477 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004478
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004479 /*
4480 * 1. Go through all accessible variables and mark all lists and dicts
4481 * with copyID.
4482 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 // Don't free variables in the previous_funccal list unless they are only
4485 // referenced through previous_funccal. This must be first, because if
4486 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004490 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004493 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004494 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4495 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004498 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004499 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4500 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004501 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004502 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4503 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004504#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004505 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004506 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4507 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004508 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004509 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004510 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4511 NULL, NULL);
4512#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004515 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004516 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4517 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004519 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004522 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004525 abort = abort || set_ref_in_functions(copyID);
4526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004527 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004531 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004532
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004533 // callbacks in buffers
4534 abort = abort || set_ref_in_buffers(copyID);
4535
Bram Moolenaar1dced572012-04-05 16:54:08 +02004536#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004538#endif
4539
Bram Moolenaardb913952012-06-29 12:54:53 +02004540#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004541 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004542#endif
4543
4544#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004545 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004546#endif
4547
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004548#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004549 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004550 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004551#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004552#ifdef FEAT_NETBEANS_INTG
4553 abort = abort || set_ref_in_nb_channel(copyID);
4554#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004555
Bram Moolenaare3188e22016-05-31 21:13:04 +02004556#ifdef FEAT_TIMERS
4557 abort = abort || set_ref_in_timer(copyID);
4558#endif
4559
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004560#ifdef FEAT_QUICKFIX
4561 abort = abort || set_ref_in_quickfix(copyID);
4562#endif
4563
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004564#ifdef FEAT_TERMINAL
4565 abort = abort || set_ref_in_term(copyID);
4566#endif
4567
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004568#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004569 abort = abort || set_ref_in_popups(copyID);
4570#endif
4571
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004573 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 /*
4575 * 2. Free lists and dictionaries that are not referenced.
4576 */
4577 did_free = free_unref_items(copyID);
4578
4579 /*
4580 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004582 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004583 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004584 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004585 else if (p_verbose > 0)
4586 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004587 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004589
4590 return did_free;
4591}
4592
4593/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004594 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004595 */
4596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004597free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004598{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004599 int did_free = FALSE;
4600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004601 // Let all "free" functions know that we are here. This means no
4602 // dictionaries, lists, channels or jobs are to be freed, because we will
4603 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004604 in_free_unref_items = TRUE;
4605
4606 /*
4607 * PASS 1: free the contents of the items. We don't free the items
4608 * themselves yet, so that it is possible to decrement refcount counters
4609 */
4610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004612 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004615 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004616
4617#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 // Go through the list of jobs and free items without the copyID. This
4619 // must happen before doing channels, because jobs refer to channels, but
4620 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004624 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4625#endif
4626
4627 /*
4628 * PASS 2: free the items themselves.
4629 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004630 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004631 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004632
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004634 // Go through the list of jobs and free items without the copyID. This
4635 // must happen before doing channels, because jobs refer to channels, but
4636 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004637 free_unused_jobs(copyID, COPYID_MASK);
4638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004639 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 free_unused_channels(copyID, COPYID_MASK);
4641#endif
4642
4643 in_free_unref_items = FALSE;
4644
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004645 return did_free;
4646}
4647
4648/*
4649 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004650 * "list_stack" is used to add lists to be marked. Can be NULL.
4651 *
4652 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004653 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004655set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004656{
4657 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004658 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004659 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 hashtab_T *cur_ht;
4661 ht_stack_T *ht_stack = NULL;
4662 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004663
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004664 cur_ht = ht;
4665 for (;;)
4666 {
4667 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // Mark each item in the hashtab. If the item contains a hashtab
4670 // it is added to ht_stack, if it contains a list it is added to
4671 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004672 todo = (int)cur_ht->ht_used;
4673 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4674 if (!HASHITEM_EMPTY(hi))
4675 {
4676 --todo;
4677 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4678 &ht_stack, list_stack);
4679 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681
4682 if (ht_stack == NULL)
4683 break;
4684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004685 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004686 cur_ht = ht_stack->ht;
4687 tempitem = ht_stack;
4688 ht_stack = ht_stack->prev;
4689 free(tempitem);
4690 }
4691
4692 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004693}
4694
4695/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004696 * Mark a dict and its items with "copyID".
4697 * Returns TRUE if setting references failed somehow.
4698 */
4699 int
4700set_ref_in_dict(dict_T *d, int copyID)
4701{
4702 if (d != NULL && d->dv_copyID != copyID)
4703 {
4704 d->dv_copyID = copyID;
4705 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4706 }
4707 return FALSE;
4708}
4709
4710/*
4711 * Mark a list and its items with "copyID".
4712 * Returns TRUE if setting references failed somehow.
4713 */
4714 int
4715set_ref_in_list(list_T *ll, int copyID)
4716{
4717 if (ll != NULL && ll->lv_copyID != copyID)
4718 {
4719 ll->lv_copyID = copyID;
4720 return set_ref_in_list_items(ll, copyID, NULL);
4721 }
4722 return FALSE;
4723}
4724
4725/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004726 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004727 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4728 *
4729 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004730 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004731 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004732set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004733{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004734 listitem_T *li;
4735 int abort = FALSE;
4736 list_T *cur_l;
4737 list_stack_T *list_stack = NULL;
4738 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004739
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004740 cur_l = l;
4741 for (;;)
4742 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004743 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004744 // Mark each item in the list. If the item contains a hashtab
4745 // it is added to ht_stack, if it contains a list it is added to
4746 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004747 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4748 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4749 ht_stack, &list_stack);
4750 if (list_stack == NULL)
4751 break;
4752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 cur_l = list_stack->list;
4755 tempitem = list_stack;
4756 list_stack = list_stack->prev;
4757 free(tempitem);
4758 }
4759
4760 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004761}
4762
4763/*
4764 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004765 * "list_stack" is used to add lists to be marked. Can be NULL.
4766 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4767 *
4768 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004769 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004771set_ref_in_item(
4772 typval_T *tv,
4773 int copyID,
4774 ht_stack_T **ht_stack,
4775 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004776{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004777 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004778
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004779 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004780 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004781 dict_T *dd = tv->vval.v_dict;
4782
Bram Moolenaara03f2332016-02-06 18:09:59 +01004783 if (dd != NULL && dd->dv_copyID != copyID)
4784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004786 dd->dv_copyID = copyID;
4787 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004788 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004789 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4790 }
4791 else
4792 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004793 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4794
Bram Moolenaara03f2332016-02-06 18:09:59 +01004795 if (newitem == NULL)
4796 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004797 else
4798 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004799 newitem->ht = &dd->dv_hashtab;
4800 newitem->prev = *ht_stack;
4801 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004802 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004803 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004804 }
4805 }
4806 else if (tv->v_type == VAR_LIST)
4807 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004808 list_T *ll = tv->vval.v_list;
4809
Bram Moolenaara03f2332016-02-06 18:09:59 +01004810 if (ll != NULL && ll->lv_copyID != copyID)
4811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004813 ll->lv_copyID = copyID;
4814 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004815 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004816 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004817 }
4818 else
4819 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004820 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4821
Bram Moolenaara03f2332016-02-06 18:09:59 +01004822 if (newitem == NULL)
4823 abort = TRUE;
4824 else
4825 {
4826 newitem->list = ll;
4827 newitem->prev = *list_stack;
4828 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004829 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004830 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004831 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004832 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004833 else if (tv->v_type == VAR_FUNC)
4834 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004835 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004836 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004837 else if (tv->v_type == VAR_PARTIAL)
4838 {
4839 partial_T *pt = tv->vval.v_partial;
4840 int i;
4841
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004842 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004844 // Didn't see this partial yet.
4845 pt->pt_copyID = copyID;
4846
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004847 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004848
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 if (pt->pt_dict != NULL)
4850 {
4851 typval_T dtv;
4852
4853 dtv.v_type = VAR_DICT;
4854 dtv.vval.v_dict = pt->pt_dict;
4855 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4856 }
4857
4858 for (i = 0; i < pt->pt_argc; ++i)
4859 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4860 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004861 if (pt->pt_funcstack != NULL)
4862 {
4863 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4864
4865 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4866 abort = abort || set_ref_in_item(stack + i, copyID,
4867 ht_stack, list_stack);
4868 }
4869
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 }
4871 }
4872#ifdef FEAT_JOB_CHANNEL
4873 else if (tv->v_type == VAR_JOB)
4874 {
4875 job_T *job = tv->vval.v_job;
4876 typval_T dtv;
4877
4878 if (job != NULL && job->jv_copyID != copyID)
4879 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004880 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 if (job->jv_channel != NULL)
4882 {
4883 dtv.v_type = VAR_CHANNEL;
4884 dtv.vval.v_channel = job->jv_channel;
4885 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4886 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004887 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 {
4889 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004890 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4892 }
4893 }
4894 }
4895 else if (tv->v_type == VAR_CHANNEL)
4896 {
4897 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004898 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004899 typval_T dtv;
4900 jsonq_T *jq;
4901 cbq_T *cq;
4902
4903 if (ch != NULL && ch->ch_copyID != copyID)
4904 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004905 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004906 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004907 {
4908 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4909 jq = jq->jq_next)
4910 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4911 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4912 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004913 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004914 {
4915 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004916 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004917 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4918 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004919 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 {
4921 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004922 dtv.vval.v_partial =
4923 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004924 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4925 }
4926 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004927 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004928 {
4929 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004930 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004931 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4932 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004933 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 {
4935 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004936 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004937 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4938 }
4939 }
4940 }
4941#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004942 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004943}
4944
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946 * Return a string with the string representation of a variable.
4947 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004948 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004950 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004951 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004952 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004953 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4954 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004955 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004956 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004957 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004959 typval_T *tv,
4960 char_u **tofree,
4961 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004962 int copyID,
4963 int echo_style,
4964 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004965 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004967 static int recurse = 0;
4968 char_u *r = NULL;
4969
Bram Moolenaar33570922005-01-25 22:26:29 +00004970 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004972 if (!did_echo_string_emsg)
4973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // Only give this message once for a recursive call to avoid
4975 // flooding the user with errors. And stop iterating over lists
4976 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004977 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004978 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004980 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004981 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004982 }
4983 ++recurse;
4984
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 switch (tv->v_type)
4986 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004987 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004988 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004989 {
4990 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004991 r = tv->vval.v_string;
4992 if (r == NULL)
4993 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004994 }
4995 else
4996 {
4997 *tofree = string_quote(tv->vval.v_string, FALSE);
4998 r = *tofree;
4999 }
5000 break;
5001
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005003 if (echo_style)
5004 {
5005 *tofree = NULL;
5006 r = tv->vval.v_string;
5007 }
5008 else
5009 {
5010 *tofree = string_quote(tv->vval.v_string, TRUE);
5011 r = *tofree;
5012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005013 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005014
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005015 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005016 {
5017 partial_T *pt = tv->vval.v_partial;
5018 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005019 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005020 garray_T ga;
5021 int i;
5022 char_u *tf;
5023
5024 ga_init2(&ga, 1, 100);
5025 ga_concat(&ga, (char_u *)"function(");
5026 if (fname != NULL)
5027 {
5028 ga_concat(&ga, fname);
5029 vim_free(fname);
5030 }
5031 if (pt != NULL && pt->pt_argc > 0)
5032 {
5033 ga_concat(&ga, (char_u *)", [");
5034 for (i = 0; i < pt->pt_argc; ++i)
5035 {
5036 if (i > 0)
5037 ga_concat(&ga, (char_u *)", ");
5038 ga_concat(&ga,
5039 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5040 vim_free(tf);
5041 }
5042 ga_concat(&ga, (char_u *)"]");
5043 }
5044 if (pt != NULL && pt->pt_dict != NULL)
5045 {
5046 typval_T dtv;
5047
5048 ga_concat(&ga, (char_u *)", ");
5049 dtv.v_type = VAR_DICT;
5050 dtv.vval.v_dict = pt->pt_dict;
5051 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5052 vim_free(tf);
5053 }
5054 ga_concat(&ga, (char_u *)")");
5055
5056 *tofree = ga.ga_data;
5057 r = *tofree;
5058 break;
5059 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005060
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005061 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005062 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005063 break;
5064
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005065 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005066 if (tv->vval.v_list == NULL)
5067 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005068 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005069 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005070 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005071 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005072 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5073 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005074 {
5075 *tofree = NULL;
5076 r = (char_u *)"[...]";
5077 }
5078 else
5079 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005080 int old_copyID = tv->vval.v_list->lv_copyID;
5081
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005082 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005083 *tofree = list2string(tv, copyID, restore_copyID);
5084 if (restore_copyID)
5085 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005086 r = *tofree;
5087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005089
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005091 if (tv->vval.v_dict == NULL)
5092 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005093 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005094 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005095 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005096 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005097 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5098 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005099 {
5100 *tofree = NULL;
5101 r = (char_u *)"{...}";
5102 }
5103 else
5104 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005105 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005106
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005107 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005108 *tofree = dict2string(tv, copyID, restore_copyID);
5109 if (restore_copyID)
5110 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005111 r = *tofree;
5112 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005113 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005116 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005117 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005119 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005120 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005121 break;
5122
Bram Moolenaar835dc632016-02-07 14:27:38 +01005123 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005124 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005125#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005126 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005127 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5128 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005129 if (composite_val)
5130 {
5131 *tofree = string_quote(r, FALSE);
5132 r = *tofree;
5133 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005134#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005136
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005137 case VAR_INSTR:
5138 *tofree = NULL;
5139 r = (char_u *)"instructions";
5140 break;
5141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005142 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005143#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 *tofree = NULL;
5145 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5146 r = numbuf;
5147 break;
5148#endif
5149
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005150 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005151 case VAR_SPECIAL:
5152 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005153 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005154 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005156
Bram Moolenaar8502c702014-06-17 12:51:16 +02005157 if (--recurse == 0)
5158 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005160}
5161
5162/*
5163 * Return a string with the string representation of a variable.
5164 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5165 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005166 * Does not put quotes around strings, as ":echo" displays values.
5167 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5168 * May return NULL.
5169 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005170 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005171echo_string(
5172 typval_T *tv,
5173 char_u **tofree,
5174 char_u *numbuf,
5175 int copyID)
5176{
5177 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5178}
5179
5180/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005181 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5182 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005183 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005184 */
5185 int
5186buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5187{
5188 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005189 char_u *t;
5190 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005191
5192 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5193 return -1;
5194
5195 if (lnum > buf->b_ml.ml_line_count)
5196 lnum = buf->b_ml.ml_line_count;
5197
5198 str = ml_get_buf(buf, lnum, FALSE);
5199 if (str == NULL)
5200 return -1;
5201
5202 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005203 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005204
Bram Moolenaar91458462021-01-13 20:08:38 +01005205 // count the number of characters
5206 t = str;
5207 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5208 t += mb_ptr2len(t);
5209
5210 // In insert mode, when the cursor is at the end of a non-empty line,
5211 // byteidx points to the NUL character immediately past the end of the
5212 // string. In this case, add one to the character count.
5213 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5214 count++;
5215
5216 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005217}
5218
5219/*
5220 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005221 * byte index. Works only for loaded buffers. Returns -1 on failure.
5222 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005223 */
5224 int
5225buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5226{
5227 char_u *str;
5228 char_u *t;
5229
5230 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5231 return -1;
5232
5233 if (lnum > buf->b_ml.ml_line_count)
5234 lnum = buf->b_ml.ml_line_count;
5235
5236 str = ml_get_buf(buf, lnum, FALSE);
5237 if (str == NULL)
5238 return -1;
5239
5240 // Convert the character offset to a byte offset
5241 t = str;
5242 while (*t != NUL && --charidx > 0)
5243 t += mb_ptr2len(t);
5244
Bram Moolenaar91458462021-01-13 20:08:38 +01005245 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005246}
5247
5248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005250 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253var2fpos(
5254 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 int *fnum, // set to fnum for '0, 'A, etc.
5257 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005259 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005261 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005264 if (varp->v_type == VAR_LIST)
5265 {
5266 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005267 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005268 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005269 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005270
5271 l = varp->vval.v_list;
5272 if (l == NULL)
5273 return NULL;
5274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005276 pos.lnum = list_find_nr(l, 0L, &error);
5277 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005279 if (charcol)
5280 len = (long)mb_charlen(ml_get(pos.lnum));
5281 else
5282 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005283
Bram Moolenaarec65d772020-08-20 22:29:12 +02005284 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005286 li = list_find(l, 1L);
5287 if (li != NULL && li->li_tv.v_type == VAR_STRING
5288 && li->li_tv.vval.v_string != NULL
5289 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005290 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005291 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005292 }
5293 else
5294 {
5295 pos.col = list_find_nr(l, 1L, &error);
5296 if (error)
5297 return NULL;
5298 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005300 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005301 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005303 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005305 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005306 pos.coladd = list_find_nr(l, 2L, &error);
5307 if (error)
5308 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005309
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005310 return &pos;
5311 }
5312
Bram Moolenaarc5809432021-03-27 21:23:30 +01005313 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5314 return NULL;
5315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005316 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 if (name == NULL)
5318 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005320 {
5321 pos = curwin->w_cursor;
5322 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005323 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005324 return &pos;
5325 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005327 {
5328 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005329 pos = VIsual;
5330 else
5331 pos = curwin->w_cursor;
5332 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005333 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005334 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005335 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005338 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5340 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005341 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005342 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return pp;
5344 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005345
Bram Moolenaara5525202006-03-02 22:52:09 +00005346 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005347
Bram Moolenaar477933c2007-07-17 14:32:23 +00005348 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005349 {
5350 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005352 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005353 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 // In silent Ex mode topline is zero, but that's not a valid line
5355 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005356 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005357 return &pos;
5358 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005360 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005361 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005363 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005364 return &pos;
5365 }
5366 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005369 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
5371 pos.lnum = curbuf->b_ml.ml_line_count;
5372 pos.col = 0;
5373 }
5374 else
5375 {
5376 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005377 if (charcol)
5378 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5379 else
5380 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 return &pos;
5383 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005384 if (in_vim9script())
5385 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return NULL;
5387}
5388
5389/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005390 * Convert list in "arg" into a position and optional file number.
5391 * When "fnump" is NULL there is no file number, only 3 items.
5392 * Note that the column is passed on as-is, the caller may want to decrement
5393 * it to use 1 for the first column.
5394 * Return FAIL when conversion is not possible, doesn't check the position for
5395 * validity.
5396 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398list2fpos(
5399 typval_T *arg,
5400 pos_T *posp,
5401 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005402 colnr_T *curswantp,
5403 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005404{
5405 list_T *l = arg->vval.v_list;
5406 long i = 0;
5407 long n;
5408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5410 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005411 if (arg->v_type != VAR_LIST
5412 || l == NULL
5413 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005414 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005415 return FAIL;
5416
5417 if (fnump != NULL)
5418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005419 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005420 if (n < 0)
5421 return FAIL;
5422 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005423 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005424 *fnump = n;
5425 }
5426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005428 if (n < 0)
5429 return FAIL;
5430 posp->lnum = n;
5431
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005433 if (n < 0)
5434 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005435 // If character position is specified, then convert to byte position
5436 if (charcol)
5437 {
5438 buf_T *buf;
5439
5440 // Get the text for the specified line in a loaded buffer
5441 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5442 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5443 return FAIL;
5444
Bram Moolenaar91458462021-01-13 20:08:38 +01005445 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005446 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005447 posp->col = n;
5448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005450 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005451 posp->coladd = 0;
5452 else
5453 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005454
Bram Moolenaar493c1782014-05-28 14:34:46 +02005455 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005457
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005458 return OK;
5459}
5460
5461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 * Get the length of an environment variable name.
5463 * Advance "arg" to the first character after the name.
5464 * Return 0 for error.
5465 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005467get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 char_u *p;
5470 int len;
5471
5472 for (p = *arg; vim_isIDc(*p); ++p)
5473 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 return 0;
5476
5477 len = (int)(p - *arg);
5478 *arg = p;
5479 return len;
5480}
5481
5482/*
5483 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005484 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 * Return 0 if something is wrong.
5486 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005488get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 char_u *p;
5491 int len;
5492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005493 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005495 {
5496 if (*p == ':')
5497 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005498 // "s:" is start of "s:var", but "n:" is not and can be used in
5499 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005500 len = (int)(p - *arg);
5501 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5502 || len > 1)
5503 break;
5504 }
5505 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return 0;
5508
5509 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005510 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512 return len;
5513}
5514
5515/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005516 * Get the length of the name of a variable or function.
5517 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005519 * Return -1 if curly braces expansion failed.
5520 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * If the name contains 'magic' {}'s, expand them and return the
5522 * expanded name in an allocated string via 'alias' - caller must free.
5523 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005525get_name_len(
5526 char_u **arg,
5527 char_u **alias,
5528 int evaluate,
5529 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 char_u *p;
5533 char_u *expr_start;
5534 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5539 && (*arg)[2] == (int)KE_SNR)
5540 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 *arg += 3;
5543 return get_id_len(arg) + 3;
5544 }
5545 len = eval_fname_script(*arg);
5546 if (len > 0)
5547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 *arg += len;
5550 }
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005555 p = find_name_end(*arg, &expr_start, &expr_end,
5556 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (expr_start != NULL)
5558 {
5559 char_u *temp_string;
5560
5561 if (!evaluate)
5562 {
5563 len += (int)(p - *arg);
5564 *arg = skipwhite(p);
5565 return len;
5566 }
5567
5568 /*
5569 * Include any <SID> etc in the expanded string:
5570 * Thus the -len here.
5571 */
5572 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5573 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005574 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 *alias = temp_string;
5576 *arg = skipwhite(p);
5577 return (int)STRLEN(temp_string);
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005581 // Only give an error when there is something, otherwise it will be
5582 // reported at a higher level.
5583 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005584 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 return len;
5587}
5588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589/*
5590 * Find the end of a variable or function name, taking care of magic braces.
5591 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5592 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005593 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 * Return a pointer to just after the name. Equal to "arg" if there is no
5595 * valid name.
5596 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005597 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005598find_name_end(
5599 char_u *arg,
5600 char_u **expr_start,
5601 char_u **expr_end,
5602 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 int mb_nest = 0;
5605 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005607 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005608 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 *expr_start = NULL;
5613 *expr_end = NULL;
5614 }
5615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005616 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005617 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5618 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005619 return arg;
5620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 for (p = arg; *p != NUL
5622 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005623 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005624 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005625 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005627 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005629 if (*p == '\'')
5630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005631 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005632 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005633 ;
5634 if (*p == NUL)
5635 break;
5636 }
5637 else if (*p == '"')
5638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005640 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005641 if (*p == '\\' && p[1] != NUL)
5642 ++p;
5643 if (*p == NUL)
5644 break;
5645 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005646 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // "s:" is start of "s:var", but "n:" is not and can be used in
5649 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005650 len = (int)(p - arg);
5651 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005652 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005653 break;
5654 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005655
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 if (*p == '[')
5659 ++br_nest;
5660 else if (*p == ']')
5661 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005663
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005664 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 if (*p == '{')
5667 {
5668 mb_nest++;
5669 if (expr_start != NULL && *expr_start == NULL)
5670 *expr_start = p;
5671 }
5672 else if (*p == '}')
5673 {
5674 mb_nest--;
5675 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5676 *expr_end = p;
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680
5681 return p;
5682}
5683
5684/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 * Expands out the 'magic' {}'s in a variable/function name.
5686 * Note that this can call itself recursively, to deal with
5687 * constructs like foo{bar}{baz}{bam}
5688 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5689 * "in_start" ^
5690 * "expr_start" ^
5691 * "expr_end" ^
5692 * "in_end" ^
5693 *
5694 * Returns a new allocated string, which the caller must free.
5695 * Returns NULL for failure.
5696 */
5697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005698make_expanded_name(
5699 char_u *in_start,
5700 char_u *expr_start,
5701 char_u *expr_end,
5702 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005703{
5704 char_u c1;
5705 char_u *retval = NULL;
5706 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005707
5708 if (expr_end == NULL || in_end == NULL)
5709 return NULL;
5710 *expr_start = NUL;
5711 *expr_end = NUL;
5712 c1 = *in_end;
5713 *in_end = NUL;
5714
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005715 temp_result = eval_to_string(expr_start + 1, FALSE);
5716 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005717 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005718 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5719 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005720 if (retval != NULL)
5721 {
5722 STRCPY(retval, in_start);
5723 STRCAT(retval, temp_result);
5724 STRCAT(retval, expr_end + 1);
5725 }
5726 }
5727 vim_free(temp_result);
5728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730 *expr_start = '{';
5731 *expr_end = '}';
5732
5733 if (retval != NULL)
5734 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005735 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005736 if (expr_start != NULL)
5737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005739 temp_result = make_expanded_name(retval, expr_start,
5740 expr_end, temp_result);
5741 vim_free(retval);
5742 retval = temp_result;
5743 }
5744 }
5745
5746 return retval;
5747}
5748
5749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005751 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005754eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005756 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005757}
5758
5759/*
5760 * Return TRUE if character "c" can be used as the first character in a
5761 * variable or function name (excluding '{' and '}').
5762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005765{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005766 return ASCII_ISALPHA(c) || c == '_';
5767}
5768
5769/*
5770 * Return TRUE if character "c" can be used as the first character of a
5771 * dictionary key.
5772 */
5773 int
5774eval_isdictc(int c)
5775{
5776 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777}
5778
5779/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005780 * Handle:
5781 * - expr[expr], expr[expr:expr] subscript
5782 * - ".name" lookup
5783 * - function call with Funcref variable: func(expr)
5784 * - method call: var->method()
5785 *
5786 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005787 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005789handle_subscript(
5790 char_u **arg,
5791 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005792 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005793 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005794{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005795 int evaluate = evalarg != NULL
5796 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005797 int ret = OK;
5798 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005799 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005800 int getnext;
5801 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005802
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005803 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005804 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005805 // When at the end of the line and ".name" or "->{" or "->X" follows in
5806 // the next line then consume the line break.
5807 p = eval_next_non_blank(*arg, evalarg, &getnext);
5808 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005809 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005810 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5811 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5812 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005813 {
5814 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005815 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005816 check_white = FALSE;
5817 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005819 if (rettv->v_type == VAR_ANY)
5820 {
5821 char_u *exp_name;
5822 int cc;
5823 int idx;
5824 ufunc_T *ufunc;
5825 type_T *type;
5826
5827 // Found script from "import * as {name}", script item name must
5828 // follow.
5829 if (**arg != '.')
5830 {
5831 if (verbose)
5832 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5833 ret = FAIL;
5834 break;
5835 }
5836 ++*arg;
5837 if (IS_WHITE_OR_NUL(**arg))
5838 {
5839 if (verbose)
5840 emsg(_(e_no_white_space_allowed_after_dot));
5841 ret = FAIL;
5842 break;
5843 }
5844
5845 // isolate the name
5846 exp_name = *arg;
5847 while (eval_isnamec(**arg))
5848 ++*arg;
5849 cc = **arg;
5850 **arg = NUL;
5851
5852 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5853 evalarg->eval_cctx, verbose);
5854 **arg = cc;
5855 *arg = skipwhite(*arg);
5856
5857 if (idx < 0 && ufunc == NULL)
5858 {
5859 ret = FAIL;
5860 break;
5861 }
5862 if (idx >= 0)
5863 {
5864 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5865 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5866
5867 copy_tv(sv->sv_tv, rettv);
5868 }
5869 else
5870 {
5871 rettv->v_type = VAR_FUNC;
5872 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5873 }
5874 }
5875
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005876 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5877 || rettv->v_type == VAR_PARTIAL))
5878 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005879 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005880 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5881 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005882
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005883 // Stop the expression evaluation when immediately aborting on
5884 // error, or when an interrupt occurred or an exception was thrown
5885 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005886 if (aborting())
5887 {
5888 if (ret == OK)
5889 clear_tv(rettv);
5890 ret = FAIL;
5891 }
5892 dict_unref(selfdict);
5893 selfdict = NULL;
5894 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005895 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005896 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005897 if (in_vim9script())
5898 *arg = skipwhite(p + 2);
5899 else
5900 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005901 if (ret == OK)
5902 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005903 if (VIM_ISWHITE(**arg))
5904 {
5905 emsg(_(e_nowhitespace));
5906 ret = FAIL;
5907 }
5908 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005909 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005910 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005911 else
5912 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005913 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005914 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005915 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005916 // "." is ".name" lookup when we found a dict or when evaluating and
5917 // scriptversion is at least 2, where string concatenation is "..".
5918 else if (**arg == '['
5919 || (**arg == '.' && (rettv->v_type == VAR_DICT
5920 || (!evaluate
5921 && (*arg)[1] != '.'
5922 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005923 {
5924 dict_unref(selfdict);
5925 if (rettv->v_type == VAR_DICT)
5926 {
5927 selfdict = rettv->vval.v_dict;
5928 if (selfdict != NULL)
5929 ++selfdict->dv_refcount;
5930 }
5931 else
5932 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005933 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005934 {
5935 clear_tv(rettv);
5936 ret = FAIL;
5937 }
5938 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005939 else
5940 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005941 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5944 // Don't do this when "Func" is already a partial that was bound
5945 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005946 if (selfdict != NULL
5947 && (rettv->v_type == VAR_FUNC
5948 || (rettv->v_type == VAR_PARTIAL
5949 && (rettv->vval.v_partial->pt_auto
5950 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005951 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005952
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005953 dict_unref(selfdict);
5954 return ret;
5955}
5956
5957/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005958 * Make a copy of an item.
5959 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5961 * reference to an already copied list/dict can be used.
5962 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005963 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005965item_copy(
5966 typval_T *from,
5967 typval_T *to,
5968 int deep,
5969 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970{
5971 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005972 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005976 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005977 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005978 }
5979 ++recurse;
5980
5981 switch (from->v_type)
5982 {
5983 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005984 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985 case VAR_STRING:
5986 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005987 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005988 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005989 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005990 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005991 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005992 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993 copy_tv(from, to);
5994 break;
5995 case VAR_LIST:
5996 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005997 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 if (from->vval.v_list == NULL)
5999 to->vval.v_list = NULL;
6000 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006002 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 to->vval.v_list = from->vval.v_list->lv_copylist;
6004 ++to->vval.v_list->lv_refcount;
6005 }
6006 else
6007 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6008 if (to->vval.v_list == NULL)
6009 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006010 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006011 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006013 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 case VAR_DICT:
6015 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006016 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006017 if (from->vval.v_dict == NULL)
6018 to->vval.v_dict = NULL;
6019 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006021 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6023 ++to->vval.v_dict->dv_refcount;
6024 }
6025 else
6026 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6027 if (to->vval.v_dict == NULL)
6028 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006030 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006031 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006033 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006035 }
6036 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006037 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038}
6039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 void
6041echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6042{
6043 char_u *tofree;
6044 char_u numbuf[NUMBUFLEN];
6045 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6046
6047 if (*atstart)
6048 {
6049 *atstart = FALSE;
6050 // Call msg_start() after eval1(), evaluating the expression
6051 // may cause a message to appear.
6052 if (with_space)
6053 {
6054 // Mark the saved text as finishing the line, so that what
6055 // follows is displayed on a new line when scrolling back
6056 // at the more prompt.
6057 msg_sb_eol();
6058 msg_start();
6059 }
6060 }
6061 else if (with_space)
6062 msg_puts_attr(" ", echo_attr);
6063
6064 if (p != NULL)
6065 for ( ; *p != NUL && !got_int; ++p)
6066 {
6067 if (*p == '\n' || *p == '\r' || *p == TAB)
6068 {
6069 if (*p != TAB && *needclr)
6070 {
6071 // remove any text still there from the command
6072 msg_clr_eos();
6073 *needclr = FALSE;
6074 }
6075 msg_putchar_attr(*p, echo_attr);
6076 }
6077 else
6078 {
6079 if (has_mbyte)
6080 {
6081 int i = (*mb_ptr2len)(p);
6082
6083 (void)msg_outtrans_len_attr(p, i, echo_attr);
6084 p += i - 1;
6085 }
6086 else
6087 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6088 }
6089 }
6090 vim_free(tofree);
6091}
6092
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 * ":echo expr1 ..." print each argument separated with a space, add a
6095 * newline at the end.
6096 * ":echon expr1 ..." print each argument plain.
6097 */
6098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006103 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 int needclr = TRUE;
6105 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006106 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006107 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006108 evalarg_T evalarg;
6109
Bram Moolenaare707c882020-07-01 13:07:37 +02006110 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 if (eap->skip)
6113 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006114 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006116 // If eval1() causes an error message the text from the command may
6117 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006118 need_clr_eos = needclr;
6119
Bram Moolenaar68db9962021-05-09 23:19:22 +02006120 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006121 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
6123 /*
6124 * Report the invalid expression unless the expression evaluation
6125 * has been cancelled due to an aborting error, an interrupt, or an
6126 * exception.
6127 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006128 if (!aborting() && did_emsg == did_emsg_before
6129 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006130 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006131 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 break;
6133 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006134 need_clr_eos = FALSE;
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006137 {
6138 if (rettv.v_type == VAR_VOID)
6139 {
6140 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6141 break;
6142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006144 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 arg = skipwhite(arg);
6148 }
6149 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006150 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 if (eap->skip)
6153 --emsg_skip;
6154 else
6155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006156 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (needclr)
6158 msg_clr_eos();
6159 if (eap->cmdidx == CMD_echo)
6160 msg_end();
6161 }
6162}
6163
6164/*
6165 * ":echohl {name}".
6166 */
6167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006170 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
6173/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006174 * Returns the :echo attribute
6175 */
6176 int
6177get_echo_attr(void)
6178{
6179 return echo_attr;
6180}
6181
6182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 * ":execute expr1 ..." execute the result of an expression.
6184 * ":echomsg expr1 ..." Print a message
6185 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006186 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 * Each gets spaces around each argument and a newline at the end for
6188 * echo commands
6189 */
6190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006191ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 int ret = OK;
6196 char_u *p;
6197 garray_T ga;
6198 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006199 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 ga_init2(&ga, 1, 80);
6202
6203 if (eap->skip)
6204 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006205 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006207 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006208 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 if (!eap->skip)
6212 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006213 char_u buf[NUMBUFLEN];
6214
6215 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006216 {
6217 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6218 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006219 semsg(_(e_using_invalid_value_as_string_str),
6220 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006221 p = NULL;
6222 }
6223 else
6224 p = tv_get_string_buf(&rettv, buf);
6225 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006226 else
6227 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006228 if (p == NULL)
6229 {
6230 clear_tv(&rettv);
6231 ret = FAIL;
6232 break;
6233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 len = (int)STRLEN(p);
6235 if (ga_grow(&ga, len + 2) == FAIL)
6236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 ret = FAIL;
6239 break;
6240 }
6241 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 ga.ga_len += len;
6245 }
6246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 arg = skipwhite(arg);
6249 }
6250
6251 if (ret != FAIL && ga.ga_data != NULL)
6252 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006253 // use the first line of continuation lines for messages
6254 SOURCING_LNUM = start_lnum;
6255
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006256 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006258 // Mark the already saved text as finishing the line, so that what
6259 // follows is displayed on a new line when scrolling back at the
6260 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006261 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006262 }
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006265 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006266 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006267 out_flush();
6268 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006269 else if (eap->cmdidx == CMD_echoconsole)
6270 {
6271 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6272 ui_write((char_u *)"\r\n", 2, TRUE);
6273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 else if (eap->cmdidx == CMD_echoerr)
6275 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006276 int save_did_emsg = did_emsg;
6277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006278 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006279 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (!force_abort)
6281 did_emsg = save_did_emsg;
6282 }
6283 else if (eap->cmdidx == CMD_execute)
6284 do_cmdline((char_u *)ga.ga_data,
6285 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6286 }
6287
6288 ga_clear(&ga);
6289
6290 if (eap->skip)
6291 --emsg_skip;
6292
6293 eap->nextcmd = check_nextcmd(arg);
6294}
6295
6296/*
6297 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6298 * "arg" points to the "&" or '+' when called, to "option" when returning.
6299 * Returns NULL when no option name found. Otherwise pointer to the char
6300 * after the option name.
6301 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006302 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006303find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 char_u *p = *arg;
6306
6307 ++p;
6308 if (*p == 'g' && p[1] == ':')
6309 {
6310 *opt_flags = OPT_GLOBAL;
6311 p += 2;
6312 }
6313 else if (*p == 'l' && p[1] == ':')
6314 {
6315 *opt_flags = OPT_LOCAL;
6316 p += 2;
6317 }
6318 else
6319 *opt_flags = 0;
6320
6321 if (!ASCII_ISALPHA(*p))
6322 return NULL;
6323 *arg = p;
6324
6325 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 else
6328 while (ASCII_ISALPHA(*p))
6329 ++p;
6330 return p;
6331}
6332
6333/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006334 * Display script name where an item was last set.
6335 * Should only be invoked when 'verbose' is non-zero.
6336 */
6337 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006338last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006339{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006340 char_u *p;
6341
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006342 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006343 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006344 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006345 if (p != NULL)
6346 {
6347 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006348 msg_puts(_("\n\tLast set from "));
6349 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006350 if (script_ctx.sc_lnum > 0)
6351 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006352 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006353 msg_outnum((long)script_ctx.sc_lnum);
6354 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006355 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006356 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006357 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006358 }
6359}
6360
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006361#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363/*
6364 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006365 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 * "flags" can be "g" to do a global substitute.
6367 * Returns an allocated string, NULL for error.
6368 */
6369 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370do_string_sub(
6371 char_u *str,
6372 char_u *pat,
6373 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006374 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 int sublen;
6378 regmatch_T regmatch;
6379 int i;
6380 int do_all;
6381 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006382 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 garray_T ga;
6384 char_u *ret;
6385 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006386 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006388 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006390 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392 ga_init2(&ga, 1, 200);
6393
6394 do_all = (flags[0] == 'g');
6395
6396 regmatch.rm_ic = p_ic;
6397 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6398 if (regmatch.regprog != NULL)
6399 {
6400 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006401 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006404 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006405 if (regmatch.startp[0] == regmatch.endp[0])
6406 {
6407 if (zero_width == regmatch.startp[0])
6408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006409 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006410 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006411 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6412 (size_t)i);
6413 ga.ga_len += i;
6414 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006415 continue;
6416 }
6417 zero_width = regmatch.startp[0];
6418 }
6419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 /*
6421 * Get some space for a temporary buffer to do the substitution
6422 * into. It will contain:
6423 * - The text up to where the match is.
6424 * - The substituted text.
6425 * - The text after the match.
6426 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006427 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006428 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6430 {
6431 ga_clear(&ga);
6432 break;
6433 }
6434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006435 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 i = (int)(regmatch.startp[0] - tail);
6437 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006438 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006439 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 + ga.ga_len + i, TRUE, TRUE, FALSE);
6441 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006442 tail = regmatch.endp[0];
6443 if (*tail == NUL)
6444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (!do_all)
6446 break;
6447 }
6448
6449 if (ga.ga_data != NULL)
6450 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6451
Bram Moolenaar473de612013-06-08 18:19:48 +02006452 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 }
6454
6455 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6456 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006457 if (p_cpo == empty_option)
6458 p_cpo = save_cpo;
6459 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006460 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006461 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006462 // If it's still empty it was changed and restored, need to restore in
6463 // the complicated way.
6464 if (*p_cpo == NUL)
6465 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006466 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
6469 return ret;
6470}