blob: d369df5e01d17ee36e703f7c6b6dca59bf8854dd [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 Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
69 else
70 result = n1 / n2;
71
72 return result;
73}
74
75/*
76 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010077 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020079 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010080num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010081{
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010084 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085 if (failed != NULL)
86 *failed = TRUE;
87 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010088 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaar33570922005-01-25 22:26:29 +000091/*
92 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000093 */
94 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010095eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020097 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020098 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000099}
100
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000101#if defined(EXITFREE) || defined(PROTO)
102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100103eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000104{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200105 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100106 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200107 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000108
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200109 // autoloaded script names
110 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000111
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200112 // unreferenced lists and dicts
113 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200114
115 // functions not garbage collected
116 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117}
118#endif
119
Bram Moolenaare6b53242020-07-01 17:28:33 +0200120 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200121fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
122{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100123 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200124 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200125 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200126 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200127 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100128 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200129 {
130 evalarg->eval_getline = eap->getline;
131 evalarg->eval_cookie = eap->cookie;
132 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200133 }
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Top level evaluation function, returning a boolean.
138 * Sets "error" to TRUE if there was an error.
139 * Return TRUE or FALSE.
140 */
141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_to_bool(
143 char_u *arg,
144 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200145 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100146 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
151
Bram Moolenaar37c83712020-06-30 21:18:36 +0200152 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 if (skip)
155 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200156 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159 {
160 *error = FALSE;
161 if (!skip)
162 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200163 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200164 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200165 else
166 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000167 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
169 }
170 if (skip)
171 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200174 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175}
176
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100177/*
178 * Call eval1() and give an error message if not done at a lower level.
179 */
180 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200181eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100183 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100184 int ret;
185 int did_emsg_before = did_emsg;
186 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200187 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100188
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200189 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
190
191 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192 if (ret == FAIL)
193 {
194 // Report the invalid expression unless the expression evaluation has
195 // been cancelled due to an aborting error, an interrupt, or an
196 // exception, or we already gave a more specific error.
197 // Also check called_emsg for when using assert_fails().
198 if (!aborting() && did_emsg == did_emsg_before
199 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200200 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 return ret;
204}
205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200207 * Return whether a typval is a valid expression to pass to eval_expr_typval()
208 * or eval_expr_to_bool(). An empty string returns FALSE;
209 */
210 int
211eval_expr_valid_arg(typval_T *tv)
212{
213 return tv->v_type != VAR_UNKNOWN
214 && (tv->v_type != VAR_STRING
215 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
216}
217
218/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000236 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar822ba242020-05-24 23:00:18 +0200247 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200248 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 {
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100250 funccall_T *fc = create_funccal(partial->pt_func, rettv);
251 int r;
252
253 if (fc == NULL)
254 return FAIL;
255
Bram Moolenaar69082912022-09-22 21:35:19 +0100256 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100257 r = call_def_function(partial->pt_func, argc, argv,
258 DEF_USE_PT_ARGV, partial, fc, rettv);
259 remove_funccal();
260 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 return FAIL;
262 }
263 else
264 {
265 s = partial_name(partial);
266 if (s == NULL || *s == NUL)
267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000269 funcexe.fe_evaluate = TRUE;
270 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
272 return FAIL;
273 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100274 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200275 else if (expr->v_type == VAR_INSTR)
276 {
277 return exe_typval_instr(expr, rettv);
278 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100279 else
280 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000281 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100282 if (s == NULL)
283 return FAIL;
284 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200285 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200287 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200289 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200290 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100291 return FAIL;
292 }
293 }
294 return OK;
295}
296
297/*
298 * Like eval_to_bool() but using a typval_T instead of a string.
299 * Works for string, funcref and partial.
300 */
301 int
302eval_expr_to_bool(typval_T *expr, int *error)
303{
304 typval_T rettv;
305 int res;
306
307 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
308 {
309 *error = TRUE;
310 return FALSE;
311 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200312 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100313 clear_tv(&rettv);
314 return res;
315}
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/*
318 * Top level evaluation function, returning a string. If "skip" is TRUE,
319 * only parsing to "nextcmd" is done, without reporting errors. Return
320 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
321 */
322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323eval_to_string_skip(
324 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200325 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100326 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
Bram Moolenaar33570922005-01-25 22:26:29 +0000328 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200330 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
Bram Moolenaar37c83712020-06-30 21:18:36 +0200332 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (skip)
334 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200335 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 retval = NULL;
337 else
338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100339 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 if (skip)
343 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346 return retval;
347}
348
349/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100350 * Initialize "evalarg" for use.
351 */
352 void
353init_evalarg(evalarg_T *evalarg)
354{
355 CLEAR_POINTER(evalarg);
356 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
357}
358
359/*
360 * If "evalarg->eval_tofree" is not NULL free it later.
361 * Caller is expected to overwrite "evalarg->eval_tofree" next.
362 */
363 static void
364free_eval_tofree_later(evalarg_T *evalarg)
365{
366 if (evalarg->eval_tofree != NULL)
367 {
368 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
369 ((char_u **)evalarg->eval_tofree_ga.ga_data)
370 [evalarg->eval_tofree_ga.ga_len++]
371 = evalarg->eval_tofree;
372 else
373 vim_free(evalarg->eval_tofree);
374 }
375}
376
377/*
378 * After using "evalarg" filled from "eap": free the memory.
379 */
380 void
381clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
382{
383 if (evalarg != NULL)
384 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100385 garray_T *etga = &evalarg->eval_tofree_ga;
386
387 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100388 {
389 if (eap != NULL)
390 {
391 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100392 // ":let" it has the variable names. But we may also need
393 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100394 vim_free(eap->cmdline_tofree);
395 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100396
397 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
398 {
399 // "nextcmd" points into the last line in eval_tofree_ga,
400 // need to keep it around.
401 --etga->ga_len;
402 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
Bram Moolenaar86fb3f82022-09-23 13:27:57 +0100403 vim_free(evalarg->eval_tofree);
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100404 }
405 else
406 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100407 }
408 else
409 vim_free(evalarg->eval_tofree);
410 evalarg->eval_tofree = NULL;
411 }
412
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100413 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100414 VIM_CLEAR(evalarg->eval_tofree_lambda);
415 }
416}
417
418/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000419 * Skip over an expression at "*pp".
420 * Return FAIL for an error, OK otherwise.
421 */
422 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200423skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000424{
Bram Moolenaar33570922005-01-25 22:26:29 +0000425 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000426
427 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200428 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000429}
430
431/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200432 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433 * If in Vim9 script and line breaks are encountered, the lines are
434 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200435 * "arg" is advanced to just after the expression.
436 * "start" is set to the start of the expression, "end" to just after the end.
437 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200438 * Return FAIL for an error, OK otherwise.
439 */
440 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441skip_expr_concatenate(
442 char_u **arg,
443 char_u **start,
444 char_u **end,
445 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200446{
447 typval_T rettv;
448 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200449 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200450 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200451 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200452 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200453 int evaluate = evalarg == NULL
454 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200455
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200456 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200458 {
459 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200460 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200463 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466
467 // Don't evaluate the expression.
468 if (evalarg != NULL)
469 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200470 *arg = skipwhite(*arg);
471 res = eval1(arg, &rettv, evalarg);
472 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200473 if (evalarg != NULL)
474 evalarg->eval_flags = save_flags;
475
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200476 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200478 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200479 if (evalarg->eval_ga.ga_len == 1)
480 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200481 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200482 ga_clear(gap);
483 gap->ga_itemsize = 0;
484 }
485 else
486 {
487 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200488 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200489
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200490 // Line breaks encountered, concatenate all the lines.
491 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200492 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200493
494 // free the lines only when using getsourceline()
495 if (evalarg->eval_cookie != NULL)
496 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200497 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200498 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200499 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100500 // later. Also free "eval_tofree" later if needed.
501 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200502 evalarg->eval_tofree =
503 ((char_u **)gap->ga_data)[gap->ga_len - 1];
504 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200505 ga_clear_strings(gap);
506 }
507 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200508 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200509 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200510
511 // free lines that were explicitly marked for freeing
512 ga_clear_strings(freegap);
513 }
514
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200515 gap->ga_itemsize = 0;
516 if (p == NULL)
517 return FAIL;
518 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200519 vim_free(evalarg->eval_tofree_lambda);
520 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200521 // Compute "end" relative to the end.
522 *end = *start + STRLEN(*start) - endoff;
523 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 }
525
526 return res;
527}
528
529/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100530 * Convert "tv" to a string.
531 * When "convert" is TRUE convert a List into a sequence of lines and convert
532 * a Float to a String.
533 * Returns an allocated string (NULL when out of memory).
534 */
535 char_u *
536typval2string(typval_T *tv, int convert)
537{
538 garray_T ga;
539 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100540 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100541
542 if (convert && tv->v_type == VAR_LIST)
543 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000544 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100545 if (tv->vval.v_list != NULL)
546 {
547 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
548 if (tv->vval.v_list->lv_len > 0)
549 ga_append(&ga, NL);
550 }
551 ga_append(&ga, NUL);
552 retval = (char_u *)ga.ga_data;
553 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100554 else if (convert && tv->v_type == VAR_FLOAT)
555 {
556 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
557 retval = vim_strsave(numbuf);
558 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100559 else
560 retval = vim_strsave(tv_get_string(tv));
561 return retval;
562}
563
564/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200565 * Top level evaluation function, returning a string. Does not handle line
566 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000567 * When "convert" is TRUE convert a List into a sequence of lines and convert
568 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 * Return pointer to allocated memory, or NULL for failure.
570 */
571 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100572eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100573 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100574 int convert,
575 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100579 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100581 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
582 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 retval = NULL;
584 else
585 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000587 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100589 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 return retval;
592}
593
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100594 char_u *
595eval_to_string(
596 char_u *arg,
597 int convert)
598{
599 return eval_to_string_eap(arg, convert, NULL);
600}
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100604 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200605 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 */
607 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_to_string_safe(
609 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000610 int use_sandbox,
611 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200614 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200615 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200616 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar9530b582022-01-22 13:39:08 +0000618 if (!keep_script_version)
619 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200620 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000621 if (use_sandbox)
622 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100623 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200624 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200625 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626 if (use_sandbox)
627 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100628 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200629 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200630 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200631 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return retval;
633}
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635/*
636 * Top level evaluation function, returning a number.
637 * Evaluates "expr" silently.
638 * Returns -1 for an error.
639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar33570922005-01-25 22:26:29 +0000643 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200644 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000645 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647 ++emsg_off;
648
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200649 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 retval = -1;
651 else
652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100653 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 --emsg_off;
657
658 return retval;
659}
660
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000661/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662 * Top level evaluation function.
663 * Returns an allocated typval_T with the result.
664 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665 */
666 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200667eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000668{
669 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200670 evalarg_T evalarg;
671
672 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200674 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200675 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100676 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000677
Bram Moolenaar37c83712020-06-30 21:18:36 +0200678 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000679 return tv;
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000683 * "*arg" points to what can be a function name in the form of "import.Name" or
684 * "Funcref". Return the name of the function. Set "tofree" to something that
685 * was allocated.
686 * If "verbose" is FALSE no errors are given.
687 * Return NULL for any failure.
688 */
689 static char_u *
690deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000691 char_u **arg,
692 char_u **tofree,
693 evalarg_T *evalarg,
694 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000695{
696 typval_T ref;
697 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100698 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000699
700 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100701 if (evalarg != NULL)
702 {
703 // need to evaluate this to get an import, like in "a.Func"
704 save_flags = evalarg->eval_flags;
705 evalarg->eval_flags |= EVAL_EVALUATE;
706 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100707 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100708 {
709 dictitem_T *v;
710
711 // If <SID>VarName was used it would not be found, try another way.
712 v = find_var_also_in_script(name, NULL, FALSE);
713 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100714 {
715 name = NULL;
716 goto theend;
717 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100718 copy_tv(&v->di_tv, &ref);
719 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000720 if (*skipwhite(*arg) != NUL)
721 {
722 if (verbose)
723 semsg(_(e_trailing_characters_str), *arg);
724 name = NULL;
725 }
726 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
727 {
728 name = ref.vval.v_string;
729 ref.vval.v_string = NULL;
730 *tofree = name;
731 }
732 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
733 {
734 if (ref.vval.v_partial->pt_argc > 0
735 || ref.vval.v_partial->pt_dict != NULL)
736 {
737 if (verbose)
738 emsg(_(e_cannot_use_partial_here));
739 name = NULL;
740 }
741 else
742 {
743 name = vim_strsave(partial_name(ref.vval.v_partial));
744 *tofree = name;
745 }
746 }
747 else
748 {
749 if (verbose)
750 semsg(_(e_not_callable_type_str), name);
751 name = NULL;
752 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100753
754theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000755 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100756 if (evalarg != NULL)
757 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000758 return name;
759}
760
761/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100762 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200763 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
764 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000765 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100768call_vim_function(
769 char_u *func,
770 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200771 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200772 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000774 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200775 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000776 char_u *arg;
777 char_u *name;
778 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000779 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200782 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000783 funcexe.fe_firstline = curwin->w_cursor.lnum;
784 funcexe.fe_lastline = curwin->w_cursor.lnum;
785 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000786
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000787 // The name might be "import.Func" or "Funcref". We don't know, we need to
788 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000789 // autoload script has errors. Guess that when there is a dot in the name
790 // showing errors is the right choice.
791 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000792 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000793 if (ignore_errors)
794 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000795 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000796 if (ignore_errors)
797 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000798 if (name == NULL)
799 name = func;
800
801 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
802
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000803 if (ret == FAIL)
804 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000805 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806
807 return ret;
808}
809
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100810/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100811 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000812 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
813 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000814 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000815 */
816 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100817call_func_retstr(
818 char_u *func,
819 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200820 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000821{
822 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000823 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000824
Bram Moolenaarded27a12018-08-01 19:06:03 +0200825 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000826 return NULL;
827
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100828 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000829 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 return retval;
831}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000833/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100834 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000835 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000836 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100837 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000838 */
839 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100840call_func_retlist(
841 char_u *func,
842 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200843 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000844{
845 typval_T rettv;
846
Bram Moolenaarded27a12018-08-01 19:06:03 +0200847 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000848 return NULL;
849
850 if (rettv.v_type != VAR_LIST)
851 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100852 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
853 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000854 clear_tv(&rettv);
855 return NULL;
856 }
857
858 return rettv.vval.v_list;
859}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100863 * Evaluate "arg", which is 'foldexpr'.
864 * Note: caller must set "curwin" to match "arg".
865 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
866 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 */
868 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000869eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000871 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200873 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000875 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000876 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
877 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaare70dd112022-01-21 16:31:11 +0000879 arg = wp->w_p_fde;
880 current_sctx = wp->w_p_script_ctx[WV_FDE];
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000883 if (use_sandbox)
884 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100885 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200887 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 retval = 0;
889 else
890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 if (tv.v_type == VAR_NUMBER)
893 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000894 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = 0;
896 else
897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100898 // If the result is a string, check if there is a non-digit before
899 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (!VIM_ISDIGIT(*s) && *s != '-')
902 *cp = *s++;
903 retval = atol((char *)s);
904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000905 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000908 if (use_sandbox)
909 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100910 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200911 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000912 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200914 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915}
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 * Get an lval: variable, Dict item or List item that can be assigned a value
920 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
921 * "name.key", "name.key[expr]" etc.
922 * Indexing only works if "name" is an existing List or Dictionary.
923 * "name" points to the start of the name.
924 * If "rettv" is not NULL it points to the value to be assigned.
925 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
926 * wrong; must end in space or cmd separator.
927 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100928 * flags:
929 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100930 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100931 * GLV_NO_AUTOLOAD: do not use script autoloading
932 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000934 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000936 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200937 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100938get_lval(
939 char_u *name,
940 typval_T *rettv,
941 lval_T *lp,
942 int unlet,
943 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100944 int flags, // GLV_ values
945 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 char_u *expr_start, *expr_end;
949 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 dictitem_T *v;
951 typval_T var1;
952 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100956 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100957 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100958 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000959 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200962 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100964 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100966 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200968 lp->ll_name_end = find_name_end(name, NULL, NULL,
969 FNE_INCL_BR | fne_flags);
970 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000971 }
972
Bram Moolenaara749a422022-02-12 19:52:25 +0000973 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000974 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000975 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
976 {
977 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
978 return NULL;
979 }
980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000982 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 if (expr_start != NULL)
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100987 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 && *p != '[' && *p != '.')
989 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000990 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 return NULL;
992 }
993
994 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
995 if (lp->ll_exp_name == NULL)
996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // Report an invalid expression in braces, unless the
998 // expression evaluation has been cancelled due to an
999 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 if (!aborting() && !quiet)
1001 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001002 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001003 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return NULL;
1005 }
1006 }
1007 lp->ll_name = lp->ll_exp_name;
1008 }
1009 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 lp->ll_name = name;
1012
Bram Moolenaar4525a572022-02-13 11:57:33 +00001013 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001015 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001016 // However, "g:[key]" is indexing a dictionary.
1017 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001018 {
1019 --p;
1020 lp->ll_name_end = p;
1021 }
1022 if (*p == ':')
1023 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001024 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001025
Bram Moolenaar9510d222022-09-11 15:14:05 +01001026 if (is_scoped_variable(name))
1027 {
1028 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1029 return NULL;
1030 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001031 if (tp == p + 1 && !quiet)
1032 {
1033 semsg(_(e_white_space_required_after_str_str), ":", p);
1034 return NULL;
1035 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001036 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001037 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001038 semsg(_(e_using_type_not_in_script_context_str), p);
1039 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001040 }
1041
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001042 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001043 lp->ll_type = parse_type(&tp,
1044 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1045 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001046 if (lp->ll_type == NULL && !quiet)
1047 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001048 lp->ll_name_end = tp;
1049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 }
1051 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001052 if (lp->ll_name == NULL)
1053 return p;
1054
Bram Moolenaar62aec932022-01-29 21:45:34 +00001055 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001056 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001057 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001058
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001059 if (import != NULL)
1060 {
1061 ufunc_T *ufunc;
1062 type_T *type;
1063
Bram Moolenaar753885b2022-08-24 16:30:36 +01001064 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001065 lp->ll_sid = import->imp_sid;
1066 lp->ll_name = skipwhite(p + 1);
1067 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1068 lp->ll_name_end = p;
1069
1070 // check the item is exported
1071 cc = *p;
1072 *p = NUL;
1073 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001074 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001075 {
1076 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001077 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001078 }
1079 *p = cc;
1080 }
1081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001084 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return p;
1086
Bram Moolenaar4525a572022-02-13 11:57:33 +00001087 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001088 {
1089 // using local variable
1090 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001091 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001092 }
1093 else
1094 {
1095 cc = *p;
1096 *p = NUL;
1097 // When we would write to the variable pass &ht and prevent autoload.
1098 writing = !(flags & GLV_READ_ONLY);
1099 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001100 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001101 if (v == NULL && !quiet)
1102 semsg(_(e_undefined_variable_str), lp->ll_name);
1103 *p = cc;
1104 if (v == NULL)
1105 return NULL;
1106 lp->ll_tv = &v->di_tv;
1107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108
Bram Moolenaar4525a572022-02-13 11:57:33 +00001109 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001110 {
1111 if (!quiet)
1112 semsg(_(e_variable_already_declared), lp->ll_name);
1113 return NULL;
1114 }
1115
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 /*
1117 * Loop until no more [idx] or .key is following.
1118 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001119 var1.v_type = VAR_UNKNOWN;
1120 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001121 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001123 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001124
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001125 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1126 {
1127 if (!quiet)
1128 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1129 return NULL;
1130 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001131 if (lp->ll_tv->v_type != VAR_LIST
1132 && lp->ll_tv->v_type != VAR_DICT
1133 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001136 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001139
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001140 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001141 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001142 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001143 else if (lp->ll_tv->v_type == VAR_BLOB
1144 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001145 r = rettv_blob_alloc(lp->ll_tv);
1146 if (r == FAIL)
1147 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001148
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001152 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001155
Bram Moolenaar4525a572022-02-13 11:57:33 +00001156 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001157 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001158 && lp->ll_tv == &v->di_tv
1159 && ht != NULL && ht == get_script_local_ht())
1160 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001161 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001162
1163 // Vim9 script local variable: get the type
1164 if (sv != NULL)
1165 lp->ll_valtype = sv->sv_type;
1166 }
1167
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 len = -1;
1169 if (*p == '.')
1170 {
1171 key = p + 1;
1172 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1173 ;
1174 if (len == 0)
1175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001177 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 }
1180 p = key + len;
1181 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001182 else
1183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001184 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001185 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 if (*p == ':')
1187 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001188 else
1189 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001191 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001193 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001195 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001196 clear_tv(&var1);
1197 return NULL;
1198 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001199 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 }
1201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001203 if (*p == ':')
1204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001208 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001209 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001211 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001212 if (rettv != NULL
1213 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001214 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001215 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001216 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001219 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001220 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 }
1223 p = skipwhite(p + 1);
1224 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 else
1227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001229 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001230 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001232 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001235 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001237 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001238 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001239 clear_tv(&var2);
1240 return NULL;
1241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001247
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001251 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001252 clear_tv(&var1);
1253 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 ++p;
1259 }
1260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 {
1263 if (len == -1)
1264 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001265 // "[key]": get key from "var1"
1266 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001267 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001271 }
1272 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001273 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001274
1275 // a NULL dict is equivalent with an empty dict
1276 if (lp->ll_tv->vval.v_dict == NULL)
1277 {
1278 lp->ll_tv->vval.v_dict = dict_alloc();
1279 if (lp->ll_tv->vval.v_dict == NULL)
1280 {
1281 clear_tv(&var1);
1282 return NULL;
1283 }
1284 ++lp->ll_tv->vval.v_dict->dv_refcount;
1285 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001286 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001287
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001288 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001290 // When assigning to a scope dictionary check that a function and
1291 // variable name is valid (only variable name unless it is l: or
1292 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001293 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001294 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001295 int prevval;
1296 int wrong;
1297
1298 if (len != -1)
1299 {
1300 prevval = key[len];
1301 key[len] = NUL;
1302 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001303 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001304 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001305 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1306 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001307 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001308 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001309 if (len != -1)
1310 key[len] = prevval;
1311 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001312 {
1313 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001314 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001315 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001316 }
1317
Bram Moolenaar10c65862020-10-08 21:16:42 +02001318 if (lp->ll_valtype != NULL)
1319 // use the type of the member
1320 lp->ll_valtype = lp->ll_valtype->tt_member;
1321
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001324 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001325 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001326 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001327 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001328 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001329 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001330 return NULL;
1331 }
1332
Bram Moolenaar31b81602019-02-10 22:14:27 +01001333 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001337 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001338 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 }
1341 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001343 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001345 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 p = NULL;
1348 break;
1349 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001350 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001351 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001352 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1353 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001354 {
1355 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001356 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001357 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001358
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001359 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001360 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001361 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001362 else if (lp->ll_tv->v_type == VAR_BLOB)
1363 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001364 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1365
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 /*
1367 * Get the number and item for the only or first index of the List.
1368 */
1369 if (empty1)
1370 lp->ll_n1 = 0;
1371 else
1372 // is number or string
1373 lp->ll_n1 = (long)tv_get_number(&var1);
1374 clear_tv(&var1);
1375
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001376 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001377 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001378 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001379 return NULL;
1380 }
1381 if (lp->ll_range && !lp->ll_empty2)
1382 {
1383 lp->ll_n2 = (long)tv_get_number(&var2);
1384 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001385 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1386 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001387 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001388 }
1389 lp->ll_blob = lp->ll_tv->vval.v_blob;
1390 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001391 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001392 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001393 else
1394 {
1395 /*
1396 * Get the number and item for the only or first index of the List.
1397 */
1398 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001400 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001401 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001403 clear_tv(&var1);
1404
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001407 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1408 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001409 if (lp->ll_li == NULL)
1410 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001411 clear_tv(&var2);
1412 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001413 }
1414
Bram Moolenaar10c65862020-10-08 21:16:42 +02001415 if (lp->ll_valtype != NULL)
1416 // use the type of the member
1417 lp->ll_valtype = lp->ll_valtype->tt_member;
1418
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 /*
1420 * May need to find the item or absolute index for the second
1421 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 * When no index given: "lp->ll_empty2" is TRUE.
1423 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001424 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001427 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001428 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001429 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001430 if (check_range_index_two(lp->ll_list,
1431 &lp->ll_n1, lp->ll_li,
1432 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 }
1435
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 }
1439
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001440 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 return p;
1443}
1444
1445/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001446 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001449clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450{
1451 vim_free(lp->ll_exp_name);
1452 vim_free(lp->ll_newkey);
1453}
1454
1455/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001456 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001458 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1459 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462set_var_lval(
1463 lval_T *lp,
1464 char_u *endp,
1465 typval_T *rettv,
1466 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001467 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1468 char_u *op,
1469 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470{
1471 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001472 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473
1474 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001475 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001476 cc = *endp;
1477 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001478 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1479 return;
1480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 if (lp->ll_blob != NULL)
1482 {
1483 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001484
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001485 if (op != NULL && *op != '=')
1486 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001487 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001488 return;
1489 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001490 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001491 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001492
1493 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1494 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001495 if (lp->ll_empty2)
1496 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1497
Bram Moolenaar68452172021-04-12 21:21:02 +02001498 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1499 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001500 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001501 }
1502 else
1503 {
1504 val = (int)tv_get_number_chk(rettv, &error);
1505 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001506 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001507 }
1508 }
1509 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001511 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001513 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1514 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001515 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001516 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001517 *endp = cc;
1518 return;
1519 }
1520
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001522 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001523 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001524 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001525 {
1526 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001527 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1528 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001529 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001530 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001531 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001532 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001535 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001536 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001537 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1538 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001539 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001540 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001541 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001542 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001543 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001545 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001546 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001547 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001548 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001549 else if (lp->ll_range)
1550 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001551 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1552 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001553 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001554 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001555 return;
1556 }
1557
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001558 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1559 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001560 }
1561 else
1562 {
1563 /*
1564 * Assign to a List or Dictionary item.
1565 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001566 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1567 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001568 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001569 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001570 return;
1571 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001572
1573 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001574 && check_typval_arg_type(lp->ll_valtype, rettv,
1575 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001576 return;
1577
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001578 if (lp->ll_newkey != NULL)
1579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 if (op != NULL && *op != '=')
1581 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001582 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001583 return;
1584 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001585 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1586 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001587 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001589 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001590 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001591 if (di == NULL)
1592 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001593 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1594 {
1595 vim_free(di);
1596 return;
1597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001599 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 else if (op != NULL && *op != '=')
1601 {
1602 tv_op(lp->ll_tv, rettv, op);
1603 return;
1604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001606 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001608 /*
1609 * Assign the value to the variable or list item.
1610 */
1611 if (copy)
1612 copy_tv(rettv, lp->ll_tv);
1613 else
1614 {
1615 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001616 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001617 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 }
1619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620}
1621
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1624 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 * Returns OK or FAIL.
1626 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001628tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001630 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 char_u numbuf[NUMBUFLEN];
1632 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001633 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001635 // Can't do anything with a Funcref or Dict on the right.
1636 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001637 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001638 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1639 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 {
1641 switch (tv1->v_type)
1642 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001643 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001644 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 case VAR_DICT:
1647 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001648 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001649 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001650 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001651 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001652 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001653 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 break;
1655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 case VAR_BLOB:
1657 if (*op != '+' || tv2->v_type != VAR_BLOB)
1658 break;
1659 // BLOB += BLOB
1660 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1661 {
1662 blob_T *b1 = tv1->vval.v_blob;
1663 blob_T *b2 = tv2->vval.v_blob;
1664 int i, len = blob_len(b2);
1665 for (i = 0; i < len; i++)
1666 ga_append(&b1->bv_ga, blob_get(b2, i));
1667 }
1668 return OK;
1669
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 case VAR_LIST:
1671 if (*op != '+' || tv2->v_type != VAR_LIST)
1672 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001674 if (tv2->vval.v_list != NULL)
1675 {
1676 if (tv1->vval.v_list == NULL)
1677 {
1678 tv1->vval.v_list = tv2->vval.v_list;
1679 ++tv1->vval.v_list->lv_refcount;
1680 }
1681 else
1682 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1683 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 return OK;
1685
1686 case VAR_NUMBER:
1687 case VAR_STRING:
1688 if (tv2->v_type == VAR_LIST)
1689 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001690 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001692 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001693 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001694 if (tv2->v_type == VAR_FLOAT)
1695 {
1696 float_T f = n;
1697
Bram Moolenaarff697e62019-02-12 22:28:33 +01001698 if (*op == '%')
1699 break;
1700 switch (*op)
1701 {
1702 case '+': f += tv2->vval.v_float; break;
1703 case '-': f -= tv2->vval.v_float; break;
1704 case '*': f *= tv2->vval.v_float; break;
1705 case '/': f /= tv2->vval.v_float; break;
1706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001707 clear_tv(tv1);
1708 tv1->v_type = VAR_FLOAT;
1709 tv1->vval.v_float = f;
1710 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001712 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001713 switch (*op)
1714 {
1715 case '+': n += tv_get_number(tv2); break;
1716 case '-': n -= tv_get_number(tv2); break;
1717 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001718 case '/': n = num_divide(n, tv_get_number(tv2),
1719 &failed); break;
1720 case '%': n = num_modulus(n, tv_get_number(tv2),
1721 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001722 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001723 clear_tv(tv1);
1724 tv1->v_type = VAR_NUMBER;
1725 tv1->vval.v_number = n;
1726 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 }
1728 else
1729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001730 if (tv2->v_type == VAR_FLOAT)
1731 break;
1732
Bram Moolenaarff697e62019-02-12 22:28:33 +01001733 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001734 s = tv_get_string(tv1);
1735 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 clear_tv(tv1);
1737 tv1->v_type = VAR_STRING;
1738 tv1->vval.v_string = s;
1739 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001740 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001741
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001742 case VAR_FLOAT:
1743 {
1744 float_T f;
1745
Bram Moolenaarff697e62019-02-12 22:28:33 +01001746 if (*op == '%' || *op == '.'
1747 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001748 && tv2->v_type != VAR_NUMBER
1749 && tv2->v_type != VAR_STRING))
1750 break;
1751 if (tv2->v_type == VAR_FLOAT)
1752 f = tv2->vval.v_float;
1753 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001754 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001755 switch (*op)
1756 {
1757 case '+': tv1->vval.v_float += f; break;
1758 case '-': tv1->vval.v_float -= f; break;
1759 case '*': tv1->vval.v_float *= f; break;
1760 case '/': tv1->vval.v_float /= f; break;
1761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001762 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001763 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 }
1765 }
1766
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001767 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 return FAIL;
1769}
1770
1771/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 * Evaluate the expression used in a ":for var in expr" command.
1773 * "arg" points to "var".
1774 * Set "*errp" to TRUE for an error, FALSE otherwise;
1775 * Return a pointer that holds the info. Null when there is an error.
1776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778eval_for_line(
1779 char_u *arg,
1780 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001781 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001782 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001785 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001787 typval_T tv;
1788 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001789 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001791 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001793 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 if (fi == NULL)
1795 return NULL;
1796
Bram Moolenaar404557e2021-07-05 21:41:48 +02001797 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1798 &fi->fi_semicolon, FALSE);
1799 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 return fi;
1801
Bram Moolenaar404557e2021-07-05 21:41:48 +02001802 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001803 if (expr[0] != 'i' || expr[1] != 'n'
1804 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001806 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1807 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1808 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001809 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 return fi;
1811 }
1812
1813 if (skip)
1814 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001815 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1816 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 {
1818 *errp = FALSE;
1819 if (!skip)
1820 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001821 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001822 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001823 l = tv.vval.v_list;
1824 if (l == NULL)
1825 {
1826 // a null list is like an empty list: do nothing
1827 clear_tv(&tv);
1828 }
1829 else
1830 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001832 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001834 // No need to increment the refcount, it's already set for
1835 // the list being used in "tv".
1836 fi->fi_list = l;
1837 list_add_watch(l, &fi->fi_lw);
1838 fi->fi_lw.lw_item = l->lv_first;
1839 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001840 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001841 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001842 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001843 fi->fi_bi = 0;
1844 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001845 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001846 typval_T btv;
1847
1848 // Make a copy, so that the iteration still works when the
1849 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001851 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001853 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001854 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001855 else if (tv.v_type == VAR_STRING)
1856 {
1857 fi->fi_byte_idx = 0;
1858 fi->fi_string = tv.vval.v_string;
1859 tv.vval.v_string = NULL;
1860 if (fi->fi_string == NULL)
1861 fi->fi_string = vim_strsave((char_u *)"");
1862 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 else
1864 {
Sean Dewar80d73952021-08-04 19:25:54 +02001865 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001866 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 }
1868 }
1869 }
1870 if (skip)
1871 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001872 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873
1874 return fi;
1875}
1876
1877/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001878 * Used when looping over a :for line, skip the "in expr" part.
1879 */
1880 void
1881skip_for_lines(void *fi_void, evalarg_T *evalarg)
1882{
1883 forinfo_T *fi = (forinfo_T *)fi_void;
1884 int i;
1885
1886 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001887 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001888}
1889
1890/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 * Use the first item in a ":for" list. Advance to the next.
1892 * Assign the values to the variable (list). "arg" points to the first one.
1893 * Return TRUE when a valid item was found, FALSE when at end of list or
1894 * something wrong.
1895 */
1896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001899 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001901 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001902 ? (ASSIGN_FINAL
1903 // first round: error if variable exists
1904 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001905 | ASSIGN_NO_MEMBER_TYPE
1906 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001907 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001909 int skip_assign = in_vim9script() && arg[0] == '_'
1910 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001912 if (fi->fi_blob != NULL)
1913 {
1914 typval_T tv;
1915
1916 if (fi->fi_bi >= blob_len(fi->fi_blob))
1917 return FALSE;
1918 tv.v_type = VAR_NUMBER;
1919 tv.v_lock = VAR_FIXED;
1920 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1921 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001922 if (skip_assign)
1923 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001924 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001925 fi->fi_varcount, flag, NULL) == OK;
1926 }
1927
1928 if (fi->fi_string != NULL)
1929 {
1930 typval_T tv;
1931 int len;
1932
1933 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1934 if (len == 0)
1935 return FALSE;
1936 tv.v_type = VAR_STRING;
1937 tv.v_lock = VAR_FIXED;
1938 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1939 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001940 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001941 if (skip_assign)
1942 result = TRUE;
1943 else
1944 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001945 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001946 vim_free(tv.vval.v_string);
1947 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001948 }
1949
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 item = fi->fi_lw.lw_item;
1951 if (item == NULL)
1952 result = FALSE;
1953 else
1954 {
1955 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001956 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001957 if (skip_assign)
1958 result = TRUE;
1959 else
1960 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001961 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 }
1963 return result;
1964}
1965
1966/*
1967 * Free the structure used to store info used by ":for".
1968 */
1969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971{
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001974 if (fi == NULL)
1975 return;
1976 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001977 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001979 list_unref(fi->fi_list);
1980 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001981 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001982 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001983 else
1984 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 vim_free(fi);
1986}
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001989set_context_for_expression(
1990 expand_T *xp,
1991 char_u *arg,
1992 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001994 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001998 if (cmdidx == CMD_let || cmdidx == CMD_var
1999 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 {
2001 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 {
2007 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002008 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002009 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 break;
2011 }
2012 return;
2013 }
2014 }
2015 else
2016 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2017 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 while ((xp->xp_pattern = vim_strpbrk(arg,
2019 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2020 {
2021 c = *xp->xp_pattern;
2022 if (c == '&')
2023 {
2024 c = xp->xp_pattern[1];
2025 if (c == '&')
2026 {
2027 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002028 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002033 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2034 xp->xp_pattern += 2;
2035
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else if (c == '$')
2039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 xp->xp_context = EXPAND_ENV_VARS;
2042 }
2043 else if (c == '=')
2044 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002045 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 xp->xp_context = EXPAND_EXPRESSION;
2047 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002048 else if (c == '#'
2049 && xp->xp_context == EXPAND_EXPRESSION)
2050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002052 break;
2053 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002054 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 && xp->xp_context == EXPAND_FUNCTIONS
2056 && vim_strchr(xp->xp_pattern, '(') == NULL)
2057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002058 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
2060 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002061 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2066 if (c == '\\' && xp->xp_pattern[1] != NUL)
2067 ++xp->xp_pattern;
2068 xp->xp_context = EXPAND_NOTHING;
2069 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002070 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002072 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2074 /* skip */ ;
2075 xp->xp_context = EXPAND_NOTHING;
2076 }
2077 else if (c == '|')
2078 {
2079 if (xp->xp_pattern[1] == '|')
2080 {
2081 ++xp->xp_pattern;
2082 xp->xp_context = EXPAND_EXPRESSION;
2083 }
2084 else
2085 xp->xp_context = EXPAND_COMMANDS;
2086 }
2087 else
2088 xp->xp_context = EXPAND_EXPRESSION;
2089 }
2090 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002091 // Doesn't look like something valid, expand as an expression
2092 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 arg = xp->xp_pattern;
2095 if (*arg != NUL)
2096 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2097 /* skip */ ;
2098 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002099
2100 // ":exe one two" completes "two"
2101 if ((cmdidx == CMD_execute
2102 || cmdidx == CMD_echo
2103 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002104 || cmdidx == CMD_echomsg
2105 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002106 && xp->xp_context == EXPAND_EXPRESSION)
2107 {
2108 for (;;)
2109 {
2110 char_u *n = skiptowhite(arg);
2111
2112 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2113 break;
2114 arg = skipwhite(n);
2115 }
2116 }
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 xp->xp_pattern = arg;
2119}
2120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002122 * Return TRUE if "pat" matches "text".
2123 * Does not use 'cpo' and always uses 'magic'.
2124 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002125 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002126pattern_match(char_u *pat, char_u *text, int ic)
2127{
2128 int matches = FALSE;
2129 char_u *save_cpo;
2130 regmatch_T regmatch;
2131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002133 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002134 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2136 if (regmatch.regprog != NULL)
2137 {
2138 regmatch.rm_ic = ic;
2139 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2140 vim_regfree(regmatch.regprog);
2141 }
2142 p_cpo = save_cpo;
2143 return matches;
2144}
2145
2146/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002147 * Handle a name followed by "(". Both for just "name(arg)" and for
2148 * "expr->name(arg)".
2149 * Returns OK or FAIL.
2150 */
2151 static int
2152eval_func(
2153 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002154 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002155 char_u *name,
2156 int name_len,
2157 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002158 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002159 typval_T *basetv) // "expr" for "expr->name(arg)"
2160{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002161 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002162 char_u *s = name;
2163 int len = name_len;
2164 partial_T *partial;
2165 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002166 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002167 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002168
2169 if (!evaluate)
2170 check_vars(s, len);
2171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002172 // If "s" is the name of a variable of type VAR_FUNC
2173 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002174 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002175 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002177 // Need to make a copy, in case evaluating the arguments makes
2178 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002179 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002180 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002181 ret = FAIL;
2182 else
2183 {
2184 funcexe_T funcexe;
2185
2186 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002187 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002188 funcexe.fe_firstline = curwin->w_cursor.lnum;
2189 funcexe.fe_lastline = curwin->w_cursor.lnum;
2190 funcexe.fe_evaluate = evaluate;
2191 funcexe.fe_partial = partial;
2192 funcexe.fe_basetv = basetv;
2193 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002194 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002195 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002196 }
2197 vim_free(s);
2198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002199 // If evaluate is FALSE rettv->v_type was not set in
2200 // get_func_tv, but it's needed in handle_subscript() to parse
2201 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002202 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2203 {
2204 rettv->vval.v_string = NULL;
2205 rettv->v_type = VAR_FUNC;
2206 }
2207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002208 // Stop the expression evaluation when immediately
2209 // aborting on error, or when an interrupt occurred or
2210 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002211 if (evaluate && aborting())
2212 {
2213 if (ret == OK)
2214 clear_tv(rettv);
2215 ret = FAIL;
2216 }
2217 return ret;
2218}
2219
2220/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002221 * After a NL, skip over empty lines and comment-only lines.
2222 */
2223 static char_u *
2224newline_skip_comments(char_u *arg)
2225{
2226 char_u *p = arg + 1;
2227
2228 for (;;)
2229 {
2230 p = skipwhite(p);
2231
2232 if (*p == NUL)
2233 break;
2234 if (vim9_comment_start(p))
2235 {
2236 char_u *nl = vim_strchr(p, NL);
2237
2238 if (nl == NULL)
2239 break;
2240 p = nl;
2241 }
2242 if (*p != NL)
2243 break;
2244 ++p; // skip another NL
2245 }
2246 return p;
2247}
2248
2249/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002250 * Get the next line source line without advancing. But do skip over comment
2251 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002252 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002253 */
2254 static char_u *
2255getline_peek_skip_comments(evalarg_T *evalarg)
2256{
2257 for (;;)
2258 {
2259 char_u *next = getline_peek(evalarg->eval_getline,
2260 evalarg->eval_cookie);
2261 char_u *p;
2262
2263 if (next == NULL)
2264 break;
2265 p = skipwhite(next);
2266 if (*p != NUL && !vim9_comment_start(p))
2267 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002268 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002269 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002270 }
2271 return NULL;
2272}
2273
2274/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002275 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2276 * comment) and there is a next line, return the next line (skipping blanks)
2277 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002278 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2279 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 * "arg" must point somewhere inside a line, not at the start.
2281 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002282 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2284{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002285 char_u *p = skipwhite(arg);
2286
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002288 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002290 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2291 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002292 && (*p == NUL || *p == NL
2293 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002295 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002296
Bram Moolenaare442d592022-05-05 12:20:28 +01002297 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002298 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002299 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002300 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002301 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002306 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002307 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 }
2309 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002310 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311}
2312
2313/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002314 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002315 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002316 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002317 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002318eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002319{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002320 garray_T *gap = &evalarg->eval_ga;
2321 char_u *line;
2322
Bram Moolenaar39be4982022-05-06 21:51:50 +01002323 if (arg != NULL)
2324 {
2325 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002326 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002327 // Truncate before a trailing comment, so that concatenating the lines
2328 // won't turn the rest into a comment.
2329 if (*skipwhite(arg) == '#')
2330 *arg = NUL;
2331 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002332
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002333 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002334 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2335 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002336 else
2337 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002338 if (line == NULL)
2339 return NULL;
2340
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002341 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002342 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2343 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002344 char_u *p = skipwhite(line);
2345
2346 // Going to concatenate the lines after parsing. For an empty or
2347 // comment line use an empty string.
2348 if (*p == NUL || vim9_comment_start(p))
2349 {
2350 vim_free(line);
2351 line = vim_strsave((char_u *)"");
2352 }
2353
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002354 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2355 ++gap->ga_len;
2356 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002357 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002358 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002359 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002360 evalarg->eval_tofree = line;
2361 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002362
2363 // Advanced to the next line, "arg" no longer points into the previous
2364 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002365 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002366 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002367}
2368
Bram Moolenaare6b53242020-07-01 17:28:33 +02002369/*
2370 * Call eval_next_non_blank() and get the next line if needed.
2371 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002372 char_u *
2373skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2374{
2375 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002376 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002377
Bram Moolenaar962d7212020-07-04 14:15:00 +02002378 if (evalarg == NULL)
2379 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002380 eval_next_non_blank(p, evalarg, &getnext);
2381 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002382 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002383 return p;
2384}
2385
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2390 */
2391
2392/*
2393 * Handle zero level expression.
2394 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002396 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002397 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * Return OK or FAIL.
2399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002401eval0(
2402 char_u *arg,
2403 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002404 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002407 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2408}
2409
2410/*
2411 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2412 * expression and don't check what comes after the expression.
2413 */
2414 int
2415eval0_retarg(
2416 char_u *arg,
2417 typval_T *rettv,
2418 exarg_T *eap,
2419 evalarg_T *evalarg,
2420 char_u **retarg)
2421{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 int ret;
2423 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002424 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002425 int did_emsg_before = did_emsg;
2426 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002428 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002429 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002433
Bram Moolenaar79481362022-06-27 20:15:10 +01002434 if (ret != FAIL)
2435 {
2436 expr_end = p;
2437 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002438
Bram Moolenaar79481362022-06-27 20:15:10 +01002439 // In Vim9 script a command block is not split at NL characters for
2440 // commands using an expression argument. Skip over a '#' comment to
2441 // check for a following NL. Require white space before the '#'.
2442 if (in_vim9script() && p > expr_end && retarg == NULL)
2443 while (*p == '#')
2444 {
2445 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002446
Bram Moolenaar79481362022-06-27 20:15:10 +01002447 if (nl == NULL)
2448 break;
2449 p = skipwhite(nl + 1);
2450 if (eap != NULL && *p != NUL)
2451 eap->nextcmd = p;
2452 check_for_end = FALSE;
2453 }
2454
2455 if (check_for_end)
2456 end_error = !ends_excmd2(arg, p);
2457 }
2458
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002459 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 /*
2464 * Report the invalid expression unless the expression evaluation has
2465 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002466 * exception, or we already gave a more specific error.
2467 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002469 if (!aborting()
2470 && did_emsg == did_emsg_before
2471 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002472 && (flags & EVAL_CONSTANT) == 0
2473 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002474 {
2475 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002476 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002477 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002478 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002479 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002480
2481 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002482 // a next command to avoid more errors, unless "|" is following, which
2483 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002484 if (eap != NULL && p != NULL
2485 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002486 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002489
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002490 if (retarg != NULL)
2491 *retarg = p;
2492 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002493 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return ret;
2496}
2497
2498/*
2499 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002500 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002501 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 *
2503 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002504 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002506 * Note: "rettv.v_lock" is not set.
2507 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * Return OK or FAIL.
2509 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002510 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002513 char_u *p;
2514 int getnext;
2515
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002516 CLEAR_POINTER(rettv);
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * Get the first variable.
2520 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002521 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 return FAIL;
2523
Bram Moolenaar793648f2020-06-26 21:28:25 +02002524 p = eval_next_non_blank(*arg, evalarg, &getnext);
2525 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002527 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 int result;
2529 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 evalarg_T *evalarg_used = evalarg;
2531 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002533 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002534 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535
2536 if (evalarg == NULL)
2537 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002538 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002539 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 orig_flags = evalarg_used->eval_flags;
2542 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002545 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002546 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002547 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002548 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002549 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002550 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002551 clear_tv(rettv);
2552 return FAIL;
2553 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002554 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002555 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 int error = FALSE;
2561
Bram Moolenaar13106602020-10-04 16:06:05 +02002562 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002563 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002564 else if (vim9script)
2565 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002566 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002568 if (error || !op_falsy || !result)
2569 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 if (error)
2571 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
2574 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002575 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002577 if (op_falsy)
2578 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002579 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002580 {
mityu4ac198c2021-05-28 17:52:40 +02002581 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002582 clear_tv(rettv);
2583 return FAIL;
2584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002586 evalarg_used->eval_flags = (op_falsy ? !result : result)
2587 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2588 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002589 {
2590 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002592 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002593 if (!op_falsy || !result)
2594 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002596 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002598 /*
2599 * Check for the ":".
2600 */
2601 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2602 if (*p != ':')
2603 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002604 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002605 if (evaluate && result)
2606 clear_tv(rettv);
2607 evalarg_used->eval_flags = orig_flags;
2608 return FAIL;
2609 }
2610 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002611 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002612 else
2613 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002614 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002615 {
2616 error_white_both(p, 1);
2617 clear_tv(rettv);
2618 evalarg_used->eval_flags = orig_flags;
2619 return FAIL;
2620 }
2621 *arg = p;
2622 }
2623
2624 /*
2625 * Get the third variable. Recursive!
2626 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002627 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002628 {
mityu4ac198c2021-05-28 17:52:40 +02002629 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002630 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002631 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002632 return FAIL;
2633 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002634 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2635 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002637 if (eval1(arg, &var2, evalarg_used) == FAIL)
2638 {
2639 if (evaluate && result)
2640 clear_tv(rettv);
2641 evalarg_used->eval_flags = orig_flags;
2642 return FAIL;
2643 }
2644 if (evaluate && !result)
2645 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 if (evalarg == NULL)
2649 clear_evalarg(&local_evalarg, NULL);
2650 else
2651 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 return OK;
2655}
2656
2657/*
2658 * Handle first level expression:
2659 * expr2 || expr2 || expr2 logical OR
2660 *
2661 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002662 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 *
2664 * Return OK or FAIL.
2665 */
2666 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002669 char_u *p;
2670 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002673 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return FAIL;
2677
2678 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002681 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002682 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 evalarg_T *evalarg_used = evalarg;
2685 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 int evaluate;
2687 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 long result = FALSE;
2689 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002690 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002691 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002692
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 if (evalarg == NULL)
2694 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002695 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002698 orig_flags = evalarg_used->eval_flags;
2699 evaluate = orig_flags & EVAL_EVALUATE;
2700 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002701 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002702 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002703 result = tv_get_bool_chk(rettv, &error);
2704 else if (tv_get_number_chk(rettv, &error) != 0)
2705 result = TRUE;
2706 clear_tv(rettv);
2707 if (error)
2708 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710
2711 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 while (p[0] == '|' && p[1] == '|')
2715 {
2716 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002717 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002718 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002719 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002720 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002721 {
2722 error_white_both(p, 2);
2723 clear_tv(rettv);
2724 return FAIL;
2725 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002726 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002727 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728
2729 /*
2730 * Get the second variable.
2731 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002732 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002733 {
mityu4ac198c2021-05-28 17:52:40 +02002734 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002735 clear_tv(rettv);
2736 return FAIL;
2737 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2739 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002741 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743
2744 /*
2745 * Compute the result.
2746 */
2747 if (evaluate && !result)
2748 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002749 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002750 result = tv_get_bool_chk(&var2, &error);
2751 else if (tv_get_number_chk(&var2, &error) != 0)
2752 result = TRUE;
2753 clear_tv(&var2);
2754 if (error)
2755 return FAIL;
2756 }
2757 if (evaluate)
2758 {
2759 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002760 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002761 rettv->v_type = VAR_BOOL;
2762 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002763 }
2764 else
2765 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002766 rettv->v_type = VAR_NUMBER;
2767 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002768 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002769 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002770
2771 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002773
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002774 if (evalarg == NULL)
2775 clear_evalarg(&local_evalarg, NULL);
2776 else
2777 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
2780 return OK;
2781}
2782
2783/*
2784 * Handle second level expression:
2785 * expr3 && expr3 && expr3 logical AND
2786 *
2787 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002788 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 *
2790 * Return OK or FAIL.
2791 */
2792 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002793eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002795 char_u *p;
2796 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002799 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002801 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return FAIL;
2803
2804 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002807 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002808 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810 evalarg_T *evalarg_used = evalarg;
2811 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 int orig_flags;
2813 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 long result = TRUE;
2815 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002816 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002817 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002818
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 if (evalarg == NULL)
2820 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002821 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002822 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002824 orig_flags = evalarg_used->eval_flags;
2825 evaluate = orig_flags & EVAL_EVALUATE;
2826 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002828 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002829 result = tv_get_bool_chk(rettv, &error);
2830 else if (tv_get_number_chk(rettv, &error) == 0)
2831 result = FALSE;
2832 clear_tv(rettv);
2833 if (error)
2834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836
2837 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002838 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002840 while (p[0] == '&' && p[1] == '&')
2841 {
2842 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002843 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002844 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002845 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002846 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002847 {
2848 error_white_both(p, 2);
2849 clear_tv(rettv);
2850 return FAIL;
2851 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002852 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002853 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002854
2855 /*
2856 * Get the second variable.
2857 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002858 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002859 {
mityu4ac198c2021-05-28 17:52:40 +02002860 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002861 clear_tv(rettv);
2862 return FAIL;
2863 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002864 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2865 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002866 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002867 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002868 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002869 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002870
2871 /*
2872 * Compute the result.
2873 */
2874 if (evaluate && result)
2875 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002876 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002877 result = tv_get_bool_chk(&var2, &error);
2878 else if (tv_get_number_chk(&var2, &error) == 0)
2879 result = FALSE;
2880 clear_tv(&var2);
2881 if (error)
2882 return FAIL;
2883 }
2884 if (evaluate)
2885 {
2886 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002887 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002888 rettv->v_type = VAR_BOOL;
2889 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002890 }
2891 else
2892 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002893 rettv->v_type = VAR_NUMBER;
2894 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002895 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002896 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002897
2898 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002900
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002901 if (evalarg == NULL)
2902 clear_evalarg(&local_evalarg, NULL);
2903 else
2904 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907 return OK;
2908}
2909
2910/*
2911 * Handle third level expression:
2912 * var1 == var2
2913 * var1 =~ var2
2914 * var1 != var2
2915 * var1 !~ var2
2916 * var1 > var2
2917 * var1 >= var2
2918 * var1 < var2
2919 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002920 * var1 is var2
2921 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002924 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 *
2926 * Return OK or FAIL.
2927 */
2928 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002932 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002933 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002935 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002938 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return FAIL;
2942
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002943 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002944
Bram Moolenaar696ba232020-07-29 21:20:41 +02002945 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002950 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002952 typval_T var2;
2953 int ic;
2954 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002955 int evaluate = evalarg == NULL
2956 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002957 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002958
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002959 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002960 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002961 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002962 p = *arg;
2963 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002964 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2965 {
mityu4ac198c2021-05-28 17:52:40 +02002966 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002967 clear_tv(rettv);
2968 return FAIL;
2969 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002970
Bram Moolenaar696ba232020-07-29 21:20:41 +02002971 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2972 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002973 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002974 clear_tv(rettv);
2975 return FAIL;
2976 }
2977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002978 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (p[len] == '?')
2980 {
2981 ic = TRUE;
2982 ++len;
2983 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002984 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else if (p[len] == '#')
2986 {
2987 ic = FALSE;
2988 ++len;
2989 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002990 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002992 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 /*
2995 * Get the second variable.
2996 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002997 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2998 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002999 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003000 clear_tv(rettv);
3001 return FAIL;
3002 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003003 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 return FAIL;
3008 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003009 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003010 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003011 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003012
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003013 // use the line of the comparison for messages
3014 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003015 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003016 {
3017 ret = FAIL;
3018 clear_tv(rettv);
3019 }
3020 else
3021 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003022 clear_tv(&var2);
3023 return ret;
3024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026
3027 return OK;
3028}
3029
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003030/*
3031 * Make a copy of blob "tv1" and append blob "tv2".
3032 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 void
3034eval_addblob(typval_T *tv1, typval_T *tv2)
3035{
3036 blob_T *b1 = tv1->vval.v_blob;
3037 blob_T *b2 = tv2->vval.v_blob;
3038 blob_T *b = blob_alloc();
3039 int i;
3040
3041 if (b != NULL)
3042 {
3043 for (i = 0; i < blob_len(b1); i++)
3044 ga_append(&b->bv_ga, blob_get(b1, i));
3045 for (i = 0; i < blob_len(b2); i++)
3046 ga_append(&b->bv_ga, blob_get(b2, i));
3047
3048 clear_tv(tv1);
3049 rettv_blob_set(tv1, b);
3050 }
3051}
3052
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003053/*
3054 * Make a copy of list "tv1" and append list "tv2".
3055 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 int
3057eval_addlist(typval_T *tv1, typval_T *tv2)
3058{
3059 typval_T var3;
3060
3061 // concatenate Lists
3062 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3063 {
3064 clear_tv(tv1);
3065 clear_tv(tv2);
3066 return FAIL;
3067 }
3068 clear_tv(tv1);
3069 *tv1 = var3;
3070 return OK;
3071}
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003074 * Handle the bitwise left/right shift operator expression:
3075 * var1 << var2
3076 * var1 >> var2
3077 *
3078 * "arg" must point to the first non-white of the expression.
3079 * "arg" is advanced to just after the recognized expression.
3080 *
3081 * Return OK or FAIL.
3082 */
3083 static int
3084eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3085{
3086 /*
3087 * Get the first expression.
3088 */
3089 if (eval6(arg, rettv, evalarg) == FAIL)
3090 return FAIL;
3091
3092 /*
3093 * Repeat computing, until no '<<' or '>>' is following.
3094 */
3095 for (;;)
3096 {
3097 char_u *p;
3098 int getnext;
3099 exprtype_T type;
3100 int evaluate;
3101 typval_T var2;
3102 int vim9script;
3103
3104 p = eval_next_non_blank(*arg, evalarg, &getnext);
3105 if (p[0] == '<' && p[1] == '<')
3106 type = EXPR_LSHIFT;
3107 else if (p[0] == '>' && p[1] == '>')
3108 type = EXPR_RSHIFT;
3109 else
3110 return OK;
3111
3112 // Handle a bitwise left or right shift operator
3113 if (rettv->v_type != VAR_NUMBER)
3114 {
3115 // left operand should be a number
3116 emsg(_(e_bitshift_ops_must_be_number));
3117 clear_tv(rettv);
3118 return FAIL;
3119 }
3120
3121 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3122 vim9script = in_vim9script();
3123 if (getnext)
3124 {
3125 *arg = eval_next_line(*arg, evalarg);
3126 p = *arg;
3127 }
3128 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3129 {
3130 error_white_both(*arg, 2);
3131 clear_tv(rettv);
3132 return FAIL;
3133 }
3134
3135 /*
3136 * Get the second variable.
3137 */
3138 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3139 {
3140 error_white_both(p, 2);
3141 clear_tv(rettv);
3142 return FAIL;
3143 }
3144 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3145 if (eval6(arg, &var2, evalarg) == FAIL)
3146 {
3147 clear_tv(rettv);
3148 return FAIL;
3149 }
3150
3151 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3152 {
3153 // right operand should be a positive number
3154 if (var2.v_type != VAR_NUMBER)
3155 emsg(_(e_bitshift_ops_must_be_number));
3156 else
3157 emsg(_(e_bitshift_ops_must_be_postive));
3158 clear_tv(rettv);
3159 clear_tv(&var2);
3160 return FAIL;
3161 }
3162
3163 if (evaluate)
3164 {
3165 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3166 // shifting more bits than we have always results in zero
3167 rettv->vval.v_number = 0;
3168 else if (type == EXPR_LSHIFT)
3169 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003170 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003171 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003172 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003173 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003174 }
3175
3176 clear_tv(&var2);
3177 }
3178
3179 return OK;
3180}
3181
3182/*
3183 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003184 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003186 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003187 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 *
3189 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003190 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 *
3192 * Return OK or FAIL.
3193 */
3194 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003195eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003198 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003200 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 return FAIL;
3202
3203 /*
3204 * Repeat computing, until no '+', '-' or '.' is following.
3205 */
3206 for (;;)
3207 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003208 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 int getnext;
3210 char_u *p;
3211 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003212 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 int concat;
3214 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003215 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003217 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003218 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003219 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 p = eval_next_non_blank(*arg, evalarg, &getnext);
3221 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003222 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003223 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3224 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003226 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3227 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003228
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003229 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003230 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003232 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003233 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003234 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003235 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003236 {
mityu4ac198c2021-05-28 17:52:40 +02003237 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003238 clear_tv(rettv);
3239 return FAIL;
3240 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003241 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003242 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003243 if ((op != '+' || (rettv->v_type != VAR_LIST
3244 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003246 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003247 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003248 int error = FALSE;
3249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003250 // For "list + ...", an illegal use of the first operand as
3251 // a number cannot be determined before evaluating the 2nd
3252 // operand: if this is also a list, all is ok.
3253 // For "something . ...", "something - ..." or "non-list + ...",
3254 // we know that the first operand needs to be a string or number
3255 // without evaluating the 2nd operand. So check before to avoid
3256 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003257 if (op != '.')
3258 tv_get_number_chk(rettv, &error);
3259 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003260 {
3261 clear_tv(rettv);
3262 return FAIL;
3263 }
3264 }
3265
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 /*
3267 * Get the second variable.
3268 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003269 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003270 {
mityu89dcb4d2021-05-28 13:50:17 +02003271 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003272 clear_tv(rettv);
3273 return FAIL;
3274 }
3275 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003276 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003278 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 return FAIL;
3280 }
3281
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003282 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 /*
3285 * Compute the result.
3286 */
3287 if (op == '.')
3288 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003289 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3290 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003291 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003292
Bram Moolenaar2e086612020-08-25 22:37:48 +02003293 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003294 || var2.v_type == VAR_CHANNEL
3295 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003296 semsg(_(e_using_invalid_value_as_string_str),
3297 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003298 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003299 {
3300 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3301 var2.vval.v_float);
3302 s2 = buf2;
3303 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003304 else
3305 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307 {
3308 clear_tv(rettv);
3309 clear_tv(&var2);
3310 return FAIL;
3311 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003312 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313 clear_tv(rettv);
3314 rettv->v_type = VAR_STRING;
3315 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003317 else if (op == '+' && rettv->v_type == VAR_BLOB
3318 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003320 else if (op == '+' && rettv->v_type == VAR_LIST
3321 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003324 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
3327 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328 int error = FALSE;
3329 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003330 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003331
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003332 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003333 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334 f1 = rettv->vval.v_float;
3335 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003339 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340 if (error)
3341 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003342 // This can only happen for "list + non-list" or
3343 // "blob + non-blob". For "non-list + ..." or
3344 // "something - ...", we returned before evaluating the
3345 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003347 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003348 return FAIL;
3349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 if (var2.v_type == VAR_FLOAT)
3351 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003353 if (var2.v_type == VAR_FLOAT)
3354 {
3355 f2 = var2.vval.v_float;
3356 n2 = 0;
3357 }
3358 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003360 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 if (error)
3362 {
3363 clear_tv(rettv);
3364 clear_tv(&var2);
3365 return FAIL;
3366 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003367 if (rettv->v_type == VAR_FLOAT)
3368 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003372 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003373 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3374 {
3375 if (op == '+')
3376 f1 = f1 + f2;
3377 else
3378 f1 = f1 - f2;
3379 rettv->v_type = VAR_FLOAT;
3380 rettv->vval.v_float = f1;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003383 {
3384 if (op == '+')
3385 n1 = n1 + n2;
3386 else
3387 n1 = n1 - n2;
3388 rettv->v_type = VAR_NUMBER;
3389 rettv->vval.v_number = n1;
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003392 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394 }
3395 return OK;
3396}
3397
3398/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003399 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * * number multiplication
3401 * / number division
3402 * % number modulo
3403 *
3404 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003405 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * Return OK or FAIL.
3408 */
3409 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003410eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003411 char_u **arg,
3412 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003413 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003414 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003416 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003419 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003421 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return FAIL;
3423
3424 /*
3425 * Repeat computing, until no '*', '/' or '%' is following.
3426 */
3427 for (;;)
3428 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003429 int evaluate;
3430 int getnext;
3431 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003432 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003433 int op;
3434 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003435 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003436 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003438 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003439 p = eval_next_non_blank(*arg, evalarg, &getnext);
3440 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003441 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003443
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003444 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003445 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003446 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003447 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003448 {
3449 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3450 {
mityu4ac198c2021-05-28 17:52:40 +02003451 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003452 clear_tv(rettv);
3453 return FAIL;
3454 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003455 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003458 f1 = 0;
3459 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003460 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003461 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003463 if (rettv->v_type == VAR_FLOAT)
3464 {
3465 f1 = rettv->vval.v_float;
3466 use_float = TRUE;
3467 n1 = 0;
3468 }
3469 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003470 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003472 if (error)
3473 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 else
3476 n1 = 0;
3477
3478 /*
3479 * Get the second variable.
3480 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003481 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3482 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003483 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003484 clear_tv(rettv);
3485 return FAIL;
3486 }
3487 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003488 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return FAIL;
3490
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003491 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003493 if (var2.v_type == VAR_FLOAT)
3494 {
3495 if (!use_float)
3496 {
3497 f1 = n1;
3498 use_float = TRUE;
3499 }
3500 f2 = var2.vval.v_float;
3501 n2 = 0;
3502 }
3503 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003506 clear_tv(&var2);
3507 if (error)
3508 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003509 if (use_float)
3510 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 /*
3514 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003515 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003517 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519 if (op == '*')
3520 f1 = f1 * f2;
3521 else if (op == '/')
3522 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003523#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003524 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003525 if (f2 == 0.0)
3526 {
3527 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003528 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003529 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003530 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003531 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003532 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003533 }
3534 else
3535 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003536#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003537 // We rely on the floating point library to handle divide
3538 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003540#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003544 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003545 return FAIL;
3546 }
3547 rettv->v_type = VAR_FLOAT;
3548 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 else
3551 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003552 int failed = FALSE;
3553
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554 if (op == '*')
3555 n1 = n1 * n2;
3556 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003557 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003559 n1 = num_modulus(n1, n2, &failed);
3560 if (failed)
3561 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 rettv->v_type = VAR_NUMBER;
3564 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 }
3568
3569 return OK;
3570}
3571
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003572/*
3573 * Handle a type cast before a base level expression.
3574 * "arg" must point to the first non-white of the expression.
3575 * "arg" is advanced to just after the recognized expression.
3576 * Return OK or FAIL.
3577 */
3578 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003579eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003580 char_u **arg,
3581 typval_T *rettv,
3582 evalarg_T *evalarg,
3583 int want_string) // after "." operator
3584{
3585 type_T *want_type = NULL;
3586 garray_T type_list; // list of pointers to allocated types
3587 int res;
3588 int evaluate = evalarg == NULL ? 0
3589 : (evalarg->eval_flags & EVAL_EVALUATE);
3590
3591 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003592 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3593 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003594 {
3595 ++*arg;
3596 ga_init2(&type_list, sizeof(type_T *), 10);
3597 want_type = parse_type(arg, &type_list, TRUE);
3598 if (want_type == NULL && (evaluate || **arg != '>'))
3599 {
3600 clear_type_list(&type_list);
3601 return FAIL;
3602 }
3603
3604 if (**arg != '>')
3605 {
3606 if (*skipwhite(*arg) == '>')
3607 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3608 else
3609 emsg(_(e_missing_gt));
3610 clear_type_list(&type_list);
3611 return FAIL;
3612 }
3613 ++*arg;
3614 *arg = skipwhite_and_linebreak(*arg, evalarg);
3615 }
3616
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003617 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003618
3619 if (want_type != NULL && evaluate)
3620 {
3621 if (res == OK)
3622 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003623 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3624 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003625
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003626 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003627 {
3628 if (want_type == &t_bool && actual != &t_bool
3629 && (actual->tt_flags & TTFLAG_BOOL_OK))
3630 {
3631 int n = tv2bool(rettv);
3632
3633 // can use "0" and "1" for boolean in some places
3634 clear_tv(rettv);
3635 rettv->v_type = VAR_BOOL;
3636 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3637 }
3638 else
3639 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003640 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003641
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003642 where.wt_variable = TRUE;
3643 res = check_type(want_type, actual, TRUE, where);
3644 }
3645 }
3646 }
3647 clear_type_list(&type_list);
3648 }
3649
3650 return res;
3651}
3652
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003653 int
3654eval_leader(char_u **arg, int vim9)
3655{
3656 char_u *s = *arg;
3657 char_u *p = *arg;
3658
3659 while (*p == '!' || *p == '-' || *p == '+')
3660 {
3661 char_u *n = skipwhite(p + 1);
3662
3663 // ++, --, -+ and +- are not accepted in Vim9 script
3664 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3665 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003666 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003667 return FAIL;
3668 }
3669 p = n;
3670 }
3671 *arg = p;
3672 return OK;
3673}
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003676 * Check for a predefined value "true", "false" and "null.*".
3677 * Return OK when recognized.
3678 */
3679 int
3680handle_predefined(char_u *s, int len, typval_T *rettv)
3681{
3682 switch (len)
3683 {
3684 case 4: if (STRNCMP(s, "true", 4) == 0)
3685 {
3686 rettv->v_type = VAR_BOOL;
3687 rettv->vval.v_number = VVAL_TRUE;
3688 return OK;
3689 }
3690 if (STRNCMP(s, "null", 4) == 0)
3691 {
3692 rettv->v_type = VAR_SPECIAL;
3693 rettv->vval.v_number = VVAL_NULL;
3694 return OK;
3695 }
3696 break;
3697 case 5: if (STRNCMP(s, "false", 5) == 0)
3698 {
3699 rettv->v_type = VAR_BOOL;
3700 rettv->vval.v_number = VVAL_FALSE;
3701 return OK;
3702 }
3703 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003704 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3705 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003706#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003707 rettv->v_type = VAR_JOB;
3708 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003709#else
3710 rettv->v_type = VAR_SPECIAL;
3711 rettv->vval.v_number = VVAL_NULL;
3712#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003713 return OK;
3714 }
3715 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003716 case 9:
3717 if (STRNCMP(s, "null_", 5) != 0)
3718 break;
3719 if (STRNCMP(s + 5, "list", 4) == 0)
3720 {
3721 rettv->v_type = VAR_LIST;
3722 rettv->vval.v_list = NULL;
3723 return OK;
3724 }
3725 if (STRNCMP(s + 5, "dict", 4) == 0)
3726 {
3727 rettv->v_type = VAR_DICT;
3728 rettv->vval.v_dict = NULL;
3729 return OK;
3730 }
3731 if (STRNCMP(s + 5, "blob", 4) == 0)
3732 {
3733 rettv->v_type = VAR_BLOB;
3734 rettv->vval.v_blob = NULL;
3735 return OK;
3736 }
3737 break;
3738 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3739 {
3740 rettv->v_type = VAR_STRING;
3741 rettv->vval.v_string = NULL;
3742 return OK;
3743 }
3744 break;
3745 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003746 if (STRNCMP(s, "null_channel", 12) == 0)
3747 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003748#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003749 rettv->v_type = VAR_CHANNEL;
3750 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003751#else
3752 rettv->v_type = VAR_SPECIAL;
3753 rettv->vval.v_number = VVAL_NULL;
3754#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003755 return OK;
3756 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003757 if (STRNCMP(s, "null_partial", 12) == 0)
3758 {
3759 rettv->v_type = VAR_PARTIAL;
3760 rettv->vval.v_partial = NULL;
3761 return OK;
3762 }
3763 break;
3764 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3765 {
3766 rettv->v_type = VAR_FUNC;
3767 rettv->vval.v_string = NULL;
3768 return OK;
3769 }
3770 break;
3771 }
3772 return FAIL;
3773}
3774
3775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 * Handle sixth level expression:
3777 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003778 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003779 * "string" string constant
3780 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * &option-name option value
3782 * @r register contents
3783 * identifier variable value
3784 * function() function call
3785 * $VAR environment variable
3786 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003789 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003790 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 *
3792 * Also handle:
3793 * ! in front logical NOT
3794 * - in front unary minus
3795 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003796 * trailing [] subscript in String or List
3797 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003798 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 *
3800 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003801 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 *
3803 * Return OK or FAIL.
3804 */
3805 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003806eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003807 char_u **arg,
3808 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003809 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003812 int evaluate = evalarg != NULL
3813 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int len;
3815 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003816 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 char_u *start_leader, *end_leader;
3818 int ret = OK;
3819 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003820 static int recurse = 0;
3821 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003825 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003830 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 */
3832 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003833 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 end_leader = *arg;
3836
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003837 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003838 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003839 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003840 ++*arg;
3841 return FAIL;
3842 }
3843
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003844 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003845 // and crash. With MSVC the stack is smaller.
3846 if (recurse ==
3847#ifdef _MSC_VER
3848 300
3849#else
3850 1000
3851#endif
3852 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003853 {
3854 semsg(_(e_expression_too_recursive_str), *arg);
3855 return FAIL;
3856 }
3857 ++recurse;
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 switch (**arg)
3860 {
3861 /*
3862 * Number constant.
3863 */
3864 case '0':
3865 case '1':
3866 case '2':
3867 case '3':
3868 case '4':
3869 case '5':
3870 case '6':
3871 case '7':
3872 case '8':
3873 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003874 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003875
3876 // Apply prefixed "-" and "+" now. Matters especially when
3877 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003878 if (ret == OK && evaluate && end_leader > start_leader
3879 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003880 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 break;
3882
3883 /*
3884 * String constant: "string".
3885 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003886 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 break;
3888
3889 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003890 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003892 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003893 break;
3894
3895 /*
3896 * List: [expr, expr]
3897 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003898 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 break;
3900
3901 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003902 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003903 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003904 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003905 {
3906 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3907 }
3908 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003909 {
3910 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003911 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003912 }
3913 else
3914 ret = NOTDONE;
3915 break;
3916
3917 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003918 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003919 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003920 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003921 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003922 ret = NOTDONE;
3923 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003924 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003925 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003926 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003927 break;
3928
3929 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003930 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003932 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 break;
3934
3935 /*
3936 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003937 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 */
LemonBoy2eaef102022-05-06 13:14:50 +01003939 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3940 ret = eval_interp_string(arg, rettv, evaluate);
3941 else
3942 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 break;
3944
3945 /*
3946 * Register contents: @r.
3947 */
3948 case '@': ++*arg;
3949 if (evaluate)
3950 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003951 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003952 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003953 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003954 emsg_invreg(**arg);
3955 else
3956 {
3957 rettv->v_type = VAR_STRING;
3958 rettv->vval.v_string = get_reg_contents(**arg,
3959 GREG_EXPR_SRC);
3960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 if (**arg != NUL)
3963 ++*arg;
3964 break;
3965
3966 /*
3967 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003968 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003970 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003971 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003972 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003973 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003974 if (ret == OK && evaluate)
3975 {
3976 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3977
Bram Moolenaara9931532021-06-12 15:58:16 +02003978 // Compile it here to get the return type. The return
3979 // type is optional, when it's missing use t_unknown.
3980 // This is recognized in compile_return().
3981 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3982 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003983 if (compile_def_function(ufunc, FALSE,
3984 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003985 {
3986 clear_tv(rettv);
3987 ret = FAIL;
3988 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003989 }
3990 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003991 if (ret == NOTDONE)
3992 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003993 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003994 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003995
Bram Moolenaar9215f012020-06-27 21:18:00 +02003996 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003997 if (**arg == ')')
3998 ++*arg;
3999 else if (ret == OK)
4000 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004001 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004002 clear_tv(rettv);
4003 ret = FAIL;
4004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006 break;
4007
Bram Moolenaar8c711452005-01-14 21:53:12 +00004008 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 break;
4010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004011
4012 if (ret == NOTDONE)
4013 {
4014 /*
4015 * Must be a variable or function name.
4016 * Can also be a curly-braces kind of name: {expr}.
4017 */
4018 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004019 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 if (alias != NULL)
4021 s = alias;
4022
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004023 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 ret = FAIL;
4025 else
4026 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004027 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4028
Bram Moolenaar4525a572022-02-13 11:57:33 +00004029 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004030 {
4031 emsg(_(e_cannot_use_underscore_here));
4032 ret = FAIL;
4033 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004034 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004035 && s[0] == 's' && s[1] == ':')
4036 {
4037 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4038 ret = FAIL;
4039 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004040 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004041 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004042 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004043 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004044 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004045 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004046 else if (flags & EVAL_CONSTANT)
4047 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004049 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004050 // get the value of "true", "false", etc. or a variable
4051 ret = FAIL;
4052 if (vim9script)
4053 ret = handle_predefined(s, len, rettv);
4054 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004055 {
4056 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004057 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004058 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004059 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004061 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004062 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004063 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004064 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004065 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004068 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004069 }
4070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004071 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4072 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004073 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004074 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 /*
4077 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4078 */
4079 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004080 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004081
4082 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004083 return ret;
4084}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004086/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004087 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004088 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004089 * Adjusts "end_leaderp" until it is at "start_leader".
4090 */
4091 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004092eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004093 typval_T *rettv,
4094 int numeric_only,
4095 char_u *start_leader,
4096 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004097{
4098 char_u *end_leader = *end_leaderp;
4099 int ret = OK;
4100 int error = FALSE;
4101 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004102 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004103 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004104 float_T f = 0.0;
4105
4106 if (rettv->v_type == VAR_FLOAT)
4107 f = rettv->vval.v_float;
4108 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004109 {
4110 while (VIM_ISWHITE(end_leader[-1]))
4111 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004112 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004113 val = tv2bool(rettv);
4114 else
4115 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004116 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004117 if (error)
4118 {
4119 clear_tv(rettv);
4120 ret = FAIL;
4121 }
4122 else
4123 {
4124 while (end_leader > start_leader)
4125 {
4126 --end_leader;
4127 if (*end_leader == '!')
4128 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004129 if (numeric_only)
4130 {
4131 ++end_leader;
4132 break;
4133 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004134 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004135 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004136 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004137 {
4138 rettv->v_type = VAR_BOOL;
4139 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4140 }
4141 else
4142 f = !f;
4143 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004144 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004145 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004146 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004147 type = VAR_BOOL;
4148 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004149 }
4150 else if (*end_leader == '-')
4151 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004152 if (rettv->v_type == VAR_FLOAT)
4153 f = -f;
4154 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004155 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004156 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004157 type = VAR_NUMBER;
4158 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004159 }
4160 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004161 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004164 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 else
4167 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004168 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004169 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004170 rettv->v_type = type;
4171 else
4172 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004173 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004176 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return ret;
4178}
4179
4180/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004181 * Call the function referred to in "rettv".
4182 */
4183 static int
4184call_func_rettv(
4185 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004186 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004187 typval_T *rettv,
4188 int evaluate,
4189 dict_T *selfdict,
4190 typval_T *basetv)
4191{
4192 partial_T *pt = NULL;
4193 funcexe_T funcexe;
4194 typval_T functv;
4195 char_u *s;
4196 int ret;
4197
4198 // need to copy the funcref so that we can clear rettv
4199 if (evaluate)
4200 {
4201 functv = *rettv;
4202 rettv->v_type = VAR_UNKNOWN;
4203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004205 if (functv.v_type == VAR_PARTIAL)
4206 {
4207 pt = functv.vval.v_partial;
4208 s = partial_name(pt);
4209 }
4210 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004211 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004212 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004213 if (s == NULL || *s == NUL)
4214 {
4215 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004216 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004217 goto theend;
4218 }
4219 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004220 }
4221 else
4222 s = (char_u *)"";
4223
Bram Moolenaara80faa82020-04-12 19:37:17 +02004224 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004225 funcexe.fe_firstline = curwin->w_cursor.lnum;
4226 funcexe.fe_lastline = curwin->w_cursor.lnum;
4227 funcexe.fe_evaluate = evaluate;
4228 funcexe.fe_partial = pt;
4229 funcexe.fe_selfdict = selfdict;
4230 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004231 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004232
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004233theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004234 // Clear the funcref afterwards, so that deleting it while
4235 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004236 if (evaluate)
4237 clear_tv(&functv);
4238
4239 return ret;
4240}
4241
4242/*
4243 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004244 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004245 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4246 */
4247 static int
4248eval_lambda(
4249 char_u **arg,
4250 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004251 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004252 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004253{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004254 int evaluate = evalarg != NULL
4255 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 typval_T base = *rettv;
4257 int ret;
4258
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004259 rettv->v_type = VAR_UNKNOWN;
4260
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004261 if (**arg == '{')
4262 {
4263 // ->{lambda}()
4264 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4265 }
4266 else
4267 {
4268 // ->(lambda)()
4269 ++*arg;
4270 ret = eval1(arg, rettv, evalarg);
4271 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004272 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004273 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004274 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004275 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004276 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004277 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4278 && rettv->v_type != VAR_PARTIAL)
4279 {
4280 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4281 return FAIL;
4282 }
4283 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004284 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004285 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004286 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004287
4288 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004289 {
4290 if (verbose)
4291 {
4292 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004293 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004294 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004295 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 }
4297 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004298 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004299 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004300 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004301 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004302
4303 // Clear the funcref afterwards, so that deleting it while
4304 // evaluating the arguments is possible (see test55).
4305 if (evaluate)
4306 clear_tv(&base);
4307
4308 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004309}
4310
4311/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004312 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004313 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004314 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4315 */
4316 static int
4317eval_method(
4318 char_u **arg,
4319 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004320 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004321 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004322{
4323 char_u *name;
4324 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004325 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004326 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004327 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004328 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004329 int evaluate = evalarg != NULL
4330 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004331
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004332 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004333
Bram Moolenaarac92e252019-08-03 21:58:38 +02004334 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004335 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004336 if (alias != NULL)
4337 name = alias;
4338
4339 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004340 {
4341 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004342 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004343 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004344 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004345 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004346 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004347 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004348
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004349 // If there is no "(" immediately following, but there is further on,
4350 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4351 // Does not handle anything where "(" is part of the expression.
4352 *arg = skipwhite(*arg);
4353
4354 if (**arg != '(' && alias == NULL
4355 && (paren = vim_strchr(*arg, '(')) != NULL)
4356 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004357 char_u *deref;
4358
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004359 *arg = name;
4360 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004361 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4362 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004363 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004364 *arg = name + len;
4365 ret = FAIL;
4366 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004367 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004368 {
4369 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004370 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004371 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004372 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004373 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004374
4375 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004376 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004377 *arg = skipwhite(*arg);
4378
4379 if (**arg != '(')
4380 {
4381 if (verbose)
4382 semsg(_(e_missing_parenthesis_str), name);
4383 ret = FAIL;
4384 }
4385 else if (VIM_ISWHITE((*arg)[-1]))
4386 {
4387 if (verbose)
4388 emsg(_(e_no_white_space_allowed_before_parenthesis));
4389 ret = FAIL;
4390 }
4391 else
4392 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004393 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004394 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004395 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004396
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004397 // Clear the funcref afterwards, so that deleting it while
4398 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004399 if (evaluate)
4400 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004401 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004402
Bram Moolenaarac92e252019-08-03 21:58:38 +02004403 return ret;
4404}
4405
4406/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004407 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4408 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4410 */
4411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004412eval_index(
4413 char_u **arg,
4414 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004415 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004416 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004418 int evaluate = evalarg != NULL
4419 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004421 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004422 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004424 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004425 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004427 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4428 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004429
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004430 init_tv(&var1);
4431 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004432 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434 /*
4435 * dict.name
4436 */
4437 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004438 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004440 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004442 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443 }
4444 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 /*
4447 * something[idx]
4448 *
4449 * Get the (first) variable from inside the [].
4450 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004451 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 if (**arg == ':')
4453 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004454 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004456 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004457 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004458 semsg(_(e_white_space_required_before_and_after_str_at_str),
4459 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004460 clear_tv(&var1);
4461 return FAIL;
4462 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004463 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004465 int error = FALSE;
4466
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004467 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004468 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004469 && var1.v_type == VAR_FLOAT)
4470 {
4471 var1.vval.v_string = typval_tostring(&var1, TRUE);
4472 var1.v_type = VAR_STRING;
4473 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004474
Bram Moolenaar4525a572022-02-13 11:57:33 +00004475 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004476 tv_get_number_chk(&var1, &error);
4477 else
4478 error = tv_get_string_chk(&var1) == NULL;
4479 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004480 {
4481 // not a number or string
4482 clear_tv(&var1);
4483 return FAIL;
4484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486
4487 /*
4488 * Get the second variable from inside the [:].
4489 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004490 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 if (**arg == ':')
4492 {
4493 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004494 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004495 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004496 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004497 semsg(_(e_white_space_required_before_and_after_str_at_str),
4498 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004499 if (!empty1)
4500 clear_tv(&var1);
4501 return FAIL;
4502 }
4503 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 if (**arg == ']')
4505 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004506 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 if (!empty1)
4509 clear_tv(&var1);
4510 return FAIL;
4511 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004512 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004513 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004515 if (!empty1)
4516 clear_tv(&var1);
4517 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 return FAIL;
4519 }
4520 }
4521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004523 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 if (**arg != ']')
4525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004526 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004527 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528 clear_tv(&var1);
4529 if (range)
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004533 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004534 }
4535
4536 if (evaluate)
4537 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004538 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004539 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004540 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004541
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004542 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004545 clear_tv(&var2);
4546 return res;
4547 }
4548 return OK;
4549}
4550
4551/*
4552 * Check if "rettv" can have an [index] or [sli:ce]
4553 */
4554 int
4555check_can_index(typval_T *rettv, int evaluate, int verbose)
4556{
4557 switch (rettv->v_type)
4558 {
4559 case VAR_FUNC:
4560 case VAR_PARTIAL:
4561 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004562 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004563 return FAIL;
4564 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004565 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004566 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004567 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004568 case VAR_BOOL:
4569 case VAR_SPECIAL:
4570 case VAR_JOB:
4571 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004572 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004573 if (verbose)
4574 emsg(_(e_cannot_index_special_variable));
4575 return FAIL;
4576 case VAR_UNKNOWN:
4577 case VAR_ANY:
4578 case VAR_VOID:
4579 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004581 emsg(_(e_cannot_index_special_variable));
4582 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 case VAR_STRING:
4587 case VAR_LIST:
4588 case VAR_DICT:
4589 case VAR_BLOB:
4590 break;
4591 case VAR_NUMBER:
4592 if (in_vim9script())
4593 emsg(_(e_cannot_index_number));
4594 break;
4595 }
4596 return OK;
4597}
4598
4599/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004600 * slice() function
4601 */
4602 void
4603f_slice(typval_T *argvars, typval_T *rettv)
4604{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004605 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004606 && ((argvars[0].v_type != VAR_STRING
4607 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004608 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004609 && check_for_list_arg(argvars, 0) == FAIL)
4610 || check_for_number_arg(argvars, 1) == FAIL
4611 || check_for_opt_number_arg(argvars, 2) == FAIL))
4612 return;
4613
Bram Moolenaar6601b622021-01-13 21:47:15 +01004614 if (check_can_index(argvars, TRUE, FALSE) == OK)
4615 {
4616 copy_tv(argvars, rettv);
4617 eval_index_inner(rettv, TRUE, argvars + 1,
4618 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4619 TRUE, NULL, 0, FALSE);
4620 }
4621}
4622
4623/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004624 * Apply index or range to "rettv".
4625 * "var1" is the first index, NULL for [:expr].
4626 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004627 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4628 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004629 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4630 */
4631 int
4632eval_index_inner(
4633 typval_T *rettv,
4634 int is_range,
4635 typval_T *var1,
4636 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004637 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004638 char_u *key,
4639 int keylen,
4640 int verbose)
4641{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004642 varnumber_T n1, n2 = 0;
4643 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004644
4645 n1 = 0;
4646 if (var1 != NULL && rettv->v_type != VAR_DICT)
4647 n1 = tv_get_number(var1);
4648
4649 if (is_range)
4650 {
4651 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 if (verbose)
4654 emsg(_(e_cannot_slice_dictionary));
4655 return FAIL;
4656 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004659 else
4660 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004661 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 switch (rettv->v_type)
4664 {
4665 case VAR_UNKNOWN:
4666 case VAR_ANY:
4667 case VAR_VOID:
4668 case VAR_FUNC:
4669 case VAR_PARTIAL:
4670 case VAR_FLOAT:
4671 case VAR_BOOL:
4672 case VAR_SPECIAL:
4673 case VAR_JOB:
4674 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004675 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 break; // not evaluating, skipping over subscript
4677
4678 case VAR_NUMBER:
4679 case VAR_STRING:
4680 {
4681 char_u *s = tv_get_string(rettv);
4682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004684 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004685 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004686 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004687 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004688 else
4689 s = char_from_string(s, n1);
4690 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004691 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // The resulting variable is a substring. If the indexes
4694 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 if (n1 < 0)
4696 {
4697 n1 = len + n1;
4698 if (n1 < 0)
4699 n1 = 0;
4700 }
4701 if (n2 < 0)
4702 n2 = len + n2;
4703 else if (n2 >= len)
4704 n2 = len;
4705 if (n1 >= len || n2 < 0 || n1 > n2)
4706 s = NULL;
4707 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004708 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 }
4710 else
4711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // The resulting variable is a string of a single
4713 // character. If the index is too big or negative the
4714 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 if (n1 >= len || n1 < 0)
4716 s = NULL;
4717 else
4718 s = vim_strnsave(s + n1, 1);
4719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 clear_tv(rettv);
4721 rettv->v_type = VAR_STRING;
4722 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723 }
4724 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004726 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004727 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4728 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004729 break;
4730
4731 case VAR_LIST:
4732 if (var1 == NULL)
4733 n1 = 0;
4734 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004735 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004736 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004737 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004738 return FAIL;
4739 break;
4740
4741 case VAR_DICT:
4742 {
4743 dictitem_T *item;
4744 typval_T tmp;
4745
4746 if (key == NULL)
4747 {
4748 key = tv_get_string_chk(var1);
4749 if (key == NULL)
4750 return FAIL;
4751 }
4752
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004753 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004754
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004755 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004756 {
4757 if (verbose)
4758 {
4759 if (keylen > 0)
4760 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004761 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004762 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004763 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004764 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765
4766 copy_tv(&item->di_tv, &tmp);
4767 clear_tv(rettv);
4768 *rettv = tmp;
4769 }
4770 break;
4771 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 return OK;
4773}
4774
4775/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004776 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004777 */
4778 char_u *
4779partial_name(partial_T *pt)
4780{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004781 if (pt != NULL)
4782 {
4783 if (pt->pt_name != NULL)
4784 return pt->pt_name;
4785 if (pt->pt_func != NULL)
4786 return pt->pt_func->uf_name;
4787 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004788 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004789}
4790
Bram Moolenaarddecc252016-04-06 22:59:37 +02004791 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004793{
4794 int i;
4795
4796 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004798 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004800 if (pt->pt_name != NULL)
4801 {
4802 func_unref(pt->pt_name);
4803 vim_free(pt->pt_name);
4804 }
4805 else
4806 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004807
Bram Moolenaar54656012021-06-09 20:50:46 +02004808 // "out_up" is no longer used, decrement refcount on partial that owns it.
4809 partial_unref(pt->pt_outer.out_up_partial);
4810
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004811 // Using pt_outer from another partial.
4812 partial_unref(pt->pt_outer_partial);
4813
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004814 // Decrease the reference count for the context of a closure. If down
4815 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004816 if (pt->pt_funcstack != NULL)
4817 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004818 --pt->pt_funcstack->fs_refcount;
4819 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004820 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004821 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004822 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4823 if (pt->pt_loopvars[i] != NULL)
4824 {
4825 --pt->pt_loopvars[i]->lvs_refcount;
4826 loopvars_check_refcount(pt->pt_loopvars[i]);
4827 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004828
Bram Moolenaarddecc252016-04-06 22:59:37 +02004829 vim_free(pt);
4830}
4831
4832/*
4833 * Unreference a closure: decrement the reference count and free it when it
4834 * becomes zero.
4835 */
4836 void
4837partial_unref(partial_T *pt)
4838{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004839 if (pt != NULL)
4840 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004841 int done = FALSE;
4842
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004843 if (--pt->pt_refcount <= 0)
4844 partial_free(pt);
4845
4846 // If the reference count goes down to one, the funcstack may be the
4847 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004848 else if (pt->pt_refcount == 1)
4849 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004850 // careful: if the funcstack is freed it may contain this partial
4851 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004852 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004853 done = funcstack_check_refcount(pt->pt_funcstack);
4854
Bram Moolenaarcc341812022-09-19 15:54:34 +01004855 if (!done)
4856 {
4857 int depth;
4858
4859 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4860 if (pt->pt_loopvars[depth] != NULL
4861 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4862 break;
4863 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004864 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004865 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004866}
4867
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004868/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004869 * Return the next (unique) copy ID.
4870 * Used for serializing nested structures.
4871 */
4872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004874{
4875 current_copyID += COPYID_INC;
4876 return current_copyID;
4877}
4878
4879/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004880 * Garbage collection for lists and dictionaries.
4881 *
4882 * We use reference counts to be able to free most items right away when they
4883 * are no longer used. But for composite items it's possible that it becomes
4884 * unused while the reference count is > 0: When there is a recursive
4885 * reference. Example:
4886 * :let l = [1, 2, 3]
4887 * :let d = {9: l}
4888 * :let l[1] = d
4889 *
4890 * Since this is quite unusual we handle this with garbage collection: every
4891 * once in a while find out which lists and dicts are not referenced from any
4892 * variable.
4893 *
4894 * Here is a good reference text about garbage collection (refers to Python
4895 * but it applies to all reference-counting mechanisms):
4896 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004897 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004898
4899/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004900 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004901 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004902 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004903 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004904 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004905garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004906{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004907 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004908 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004909 buf_T *buf;
4910 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004911 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004913
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004914 if (!testing)
4915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004917 want_garbage_collect = FALSE;
4918 may_garbage_collect = FALSE;
4919 garbage_collect_at_exit = FALSE;
4920 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004921
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004922 // The execution stack can grow big, limit the size.
4923 if (exestack.ga_maxlen - exestack.ga_len > 500)
4924 {
4925 size_t new_len;
4926 char_u *pp;
4927 int n;
4928
4929 // Keep 150% of the current size, with a minimum of the growth size.
4930 n = exestack.ga_len / 2;
4931 if (n < exestack.ga_growsize)
4932 n = exestack.ga_growsize;
4933
4934 // Don't make it bigger though.
4935 if (exestack.ga_len + n < exestack.ga_maxlen)
4936 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004937 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004938 pp = vim_realloc(exestack.ga_data, new_len);
4939 if (pp == NULL)
4940 return FAIL;
4941 exestack.ga_maxlen = exestack.ga_len + n;
4942 exestack.ga_data = pp;
4943 }
4944 }
4945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 // We advance by two because we add one for items referenced through
4947 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004948 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004949
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004950 /*
4951 * 1. Go through all accessible variables and mark all lists and dicts
4952 * with copyID.
4953 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // Don't free variables in the previous_funccal list unless they are only
4956 // referenced through previous_funccal. This must be first, because if
4957 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004961 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004964 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004965 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4966 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004969 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004970 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4971 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004972 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004973 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4974 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004975#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004976 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004977 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4978 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004979 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004980 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004981 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4982 NULL, NULL);
4983#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004986 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004987 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4988 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004990 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004996 abort = abort || set_ref_in_functions(copyID);
4997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004999 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005000
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005001 // funcstacks keep variables for closures
5002 abort = abort || set_ref_in_funcstacks(copyID);
5003
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005004 // loopvars keep variables for loop blocks
5005 abort = abort || set_ref_in_loopvars(copyID);
5006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005008 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005009
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005010 // callbacks in buffers
5011 abort = abort || set_ref_in_buffers(copyID);
5012
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005013 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5014 abort = abort || set_ref_in_insexpand_funcs(copyID);
5015
5016 // 'operatorfunc' callback
5017 abort = abort || set_ref_in_opfunc(copyID);
5018
5019 // 'tagfunc' callback
5020 abort = abort || set_ref_in_tagfunc(copyID);
5021
5022 // 'imactivatefunc' and 'imstatusfunc' callbacks
5023 abort = abort || set_ref_in_im_funcs(copyID);
5024
Bram Moolenaar1dced572012-04-05 16:54:08 +02005025#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005026 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005027#endif
5028
Bram Moolenaardb913952012-06-29 12:54:53 +02005029#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005030 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005031#endif
5032
5033#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005034 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005035#endif
5036
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005037#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005038 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005039 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005040#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005041#ifdef FEAT_NETBEANS_INTG
5042 abort = abort || set_ref_in_nb_channel(copyID);
5043#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005044
Bram Moolenaare3188e22016-05-31 21:13:04 +02005045#ifdef FEAT_TIMERS
5046 abort = abort || set_ref_in_timer(copyID);
5047#endif
5048
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005049#ifdef FEAT_QUICKFIX
5050 abort = abort || set_ref_in_quickfix(copyID);
5051#endif
5052
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005053#ifdef FEAT_TERMINAL
5054 abort = abort || set_ref_in_term(copyID);
5055#endif
5056
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005057#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005058 abort = abort || set_ref_in_popups(copyID);
5059#endif
5060
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005061 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005063 /*
5064 * 2. Free lists and dictionaries that are not referenced.
5065 */
5066 did_free = free_unref_items(copyID);
5067
5068 /*
5069 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005070 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005071 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005073 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005074 else if (p_verbose > 0)
5075 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005076 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005077 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005078
5079 return did_free;
5080}
5081
5082/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005084 */
5085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005087{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005088 int did_free = FALSE;
5089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 // Let all "free" functions know that we are here. This means no
5091 // dictionaries, lists, channels or jobs are to be freed, because we will
5092 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093 in_free_unref_items = TRUE;
5094
5095 /*
5096 * PASS 1: free the contents of the items. We don't free the items
5097 * themselves yet, so that it is possible to decrement refcount counters
5098 */
5099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005101 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005104 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105
5106#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // Go through the list of jobs and free items without the copyID. This
5108 // must happen before doing channels, because jobs refer to channels, but
5109 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005112 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005113 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5114#endif
5115
5116 /*
5117 * PASS 2: free the items themselves.
5118 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005119 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005120 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005121
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005123 // Go through the list of jobs and free items without the copyID. This
5124 // must happen before doing channels, because jobs refer to channels, but
5125 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005126 free_unused_jobs(copyID, COPYID_MASK);
5127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129 free_unused_channels(copyID, COPYID_MASK);
5130#endif
5131
5132 in_free_unref_items = FALSE;
5133
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005134 return did_free;
5135}
5136
5137/*
5138 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005139 * "list_stack" is used to add lists to be marked. Can be NULL.
5140 *
5141 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005142 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005144set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005145{
5146 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005147 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005148 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005149 hashtab_T *cur_ht;
5150 ht_stack_T *ht_stack = NULL;
5151 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005152
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005153 cur_ht = ht;
5154 for (;;)
5155 {
5156 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005158 // Mark each item in the hashtab. If the item contains a hashtab
5159 // it is added to ht_stack, if it contains a list it is added to
5160 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161 todo = (int)cur_ht->ht_used;
5162 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5163 if (!HASHITEM_EMPTY(hi))
5164 {
5165 --todo;
5166 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5167 &ht_stack, list_stack);
5168 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005169 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170
5171 if (ht_stack == NULL)
5172 break;
5173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005174 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005175 cur_ht = ht_stack->ht;
5176 tempitem = ht_stack;
5177 ht_stack = ht_stack->prev;
5178 free(tempitem);
5179 }
5180
5181 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005182}
5183
Dominique Pelle748b3082022-01-08 12:41:16 +00005184#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5185 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005186/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005187 * Mark a dict and its items with "copyID".
5188 * Returns TRUE if setting references failed somehow.
5189 */
5190 int
5191set_ref_in_dict(dict_T *d, int copyID)
5192{
5193 if (d != NULL && d->dv_copyID != copyID)
5194 {
5195 d->dv_copyID = copyID;
5196 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5197 }
5198 return FALSE;
5199}
Dominique Pelle748b3082022-01-08 12:41:16 +00005200#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005201
5202/*
5203 * Mark a list and its items with "copyID".
5204 * Returns TRUE if setting references failed somehow.
5205 */
5206 int
5207set_ref_in_list(list_T *ll, int copyID)
5208{
5209 if (ll != NULL && ll->lv_copyID != copyID)
5210 {
5211 ll->lv_copyID = copyID;
5212 return set_ref_in_list_items(ll, copyID, NULL);
5213 }
5214 return FALSE;
5215}
5216
5217/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5220 *
5221 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005223 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005224set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005226 listitem_T *li;
5227 int abort = FALSE;
5228 list_T *cur_l;
5229 list_stack_T *list_stack = NULL;
5230 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 cur_l = l;
5233 for (;;)
5234 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005235 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // Mark each item in the list. If the item contains a hashtab
5237 // it is added to ht_stack, if it contains a list it is added to
5238 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5240 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5241 ht_stack, &list_stack);
5242 if (list_stack == NULL)
5243 break;
5244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 cur_l = list_stack->list;
5247 tempitem = list_stack;
5248 list_stack = list_stack->prev;
5249 free(tempitem);
5250 }
5251
5252 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005253}
5254
5255/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005256 * Mark the partial in callback 'cb' with "copyID".
5257 */
5258 int
5259set_ref_in_callback(callback_T *cb, int copyID)
5260{
5261 typval_T tv;
5262
5263 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5264 return FALSE;
5265
5266 tv.v_type = VAR_PARTIAL;
5267 tv.vval.v_partial = cb->cb_partial;
5268 return set_ref_in_item(&tv, copyID, NULL, NULL);
5269}
5270
5271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 * "list_stack" is used to add lists to be marked. Can be NULL.
5274 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5275 *
5276 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005277 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279set_ref_in_item(
5280 typval_T *tv,
5281 int copyID,
5282 ht_stack_T **ht_stack,
5283 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005286
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005287 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005288 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005289 dict_T *dd = tv->vval.v_dict;
5290
Bram Moolenaara03f2332016-02-06 18:09:59 +01005291 if (dd != NULL && dd->dv_copyID != copyID)
5292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005294 dd->dv_copyID = copyID;
5295 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005296 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005297 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5298 }
5299 else
5300 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005301 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5302
Bram Moolenaara03f2332016-02-06 18:09:59 +01005303 if (newitem == NULL)
5304 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 else
5306 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005307 newitem->ht = &dd->dv_hashtab;
5308 newitem->prev = *ht_stack;
5309 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005310 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005312 }
5313 }
5314 else if (tv->v_type == VAR_LIST)
5315 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316 list_T *ll = tv->vval.v_list;
5317
Bram Moolenaara03f2332016-02-06 18:09:59 +01005318 if (ll != NULL && ll->lv_copyID != copyID)
5319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005321 ll->lv_copyID = copyID;
5322 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005323 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005324 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005325 }
5326 else
5327 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005328 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5329
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 if (newitem == NULL)
5331 abort = TRUE;
5332 else
5333 {
5334 newitem->list = ll;
5335 newitem->prev = *list_stack;
5336 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005337 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005338 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005339 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005341 else if (tv->v_type == VAR_FUNC)
5342 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005343 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005344 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 else if (tv->v_type == VAR_PARTIAL)
5346 {
5347 partial_T *pt = tv->vval.v_partial;
5348 int i;
5349
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005350 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005351 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005352 // Didn't see this partial yet.
5353 pt->pt_copyID = copyID;
5354
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005355 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005356
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005357 if (pt->pt_dict != NULL)
5358 {
5359 typval_T dtv;
5360
5361 dtv.v_type = VAR_DICT;
5362 dtv.vval.v_dict = pt->pt_dict;
5363 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5364 }
5365
5366 for (i = 0; i < pt->pt_argc; ++i)
5367 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5368 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005369 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005370 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005371 }
5372 }
5373#ifdef FEAT_JOB_CHANNEL
5374 else if (tv->v_type == VAR_JOB)
5375 {
5376 job_T *job = tv->vval.v_job;
5377 typval_T dtv;
5378
5379 if (job != NULL && job->jv_copyID != copyID)
5380 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005381 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005382 if (job->jv_channel != NULL)
5383 {
5384 dtv.v_type = VAR_CHANNEL;
5385 dtv.vval.v_channel = job->jv_channel;
5386 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5387 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005388 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 {
5390 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005391 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005392 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5393 }
5394 }
5395 }
5396 else if (tv->v_type == VAR_CHANNEL)
5397 {
5398 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005399 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005400 typval_T dtv;
5401 jsonq_T *jq;
5402 cbq_T *cq;
5403
5404 if (ch != NULL && ch->ch_copyID != copyID)
5405 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005406 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005407 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 {
5409 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5410 jq = jq->jq_next)
5411 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5412 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5413 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005414 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 {
5416 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005417 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005418 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5419 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005420 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005421 {
5422 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005423 dtv.vval.v_partial =
5424 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5426 }
5427 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005428 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429 {
5430 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005431 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5433 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005434 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005435 {
5436 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005437 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005438 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5439 }
5440 }
5441 }
5442#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005443 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005444}
5445
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 * Return a string with the string representation of a variable.
5448 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005449 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005450 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005451 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005452 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005453 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005454 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5455 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005456 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005458 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005459echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005460 typval_T *tv,
5461 char_u **tofree,
5462 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005463 int copyID,
5464 int echo_style,
5465 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005466 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005468 static int recurse = 0;
5469 char_u *r = NULL;
5470
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005473 if (!did_echo_string_emsg)
5474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 // Only give this message once for a recursive call to avoid
5476 // flooding the user with errors. And stop iterating over lists
5477 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005478 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005479 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005481 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005482 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005483 }
5484 ++recurse;
5485
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 switch (tv->v_type)
5487 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005488 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005489 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005490 {
5491 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005492 r = tv->vval.v_string;
5493 if (r == NULL)
5494 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005495 }
5496 else
5497 {
5498 *tofree = string_quote(tv->vval.v_string, FALSE);
5499 r = *tofree;
5500 }
5501 break;
5502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005504 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005505 char_u buf[MAX_FUNC_NAME_LEN];
5506
5507 if (echo_style)
5508 {
LemonBoya5d35902022-04-29 21:15:02 +01005509 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5510 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005511 buf, MAX_FUNC_NAME_LEN);
5512 if (r == buf)
5513 {
5514 r = vim_strsave(buf);
5515 *tofree = r;
5516 }
5517 else
5518 *tofree = NULL;
5519 }
5520 else
5521 {
5522 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5523 : make_ufunc_name_readable(
5524 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5525 TRUE);
5526 r = *tofree;
5527 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005529 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005530
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005531 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005532 {
5533 partial_T *pt = tv->vval.v_partial;
5534 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005535 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005536 garray_T ga;
5537 int i;
5538 char_u *tf;
5539
5540 ga_init2(&ga, 1, 100);
5541 ga_concat(&ga, (char_u *)"function(");
5542 if (fname != NULL)
5543 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005544 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005545 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005546 && vim_isupper(fname[1]))
5547 {
5548 ga_concat(&ga, (char_u *)"'g:");
5549 ga_concat(&ga, fname + 1);
5550 }
5551 else
5552 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005553 vim_free(fname);
5554 }
5555 if (pt != NULL && pt->pt_argc > 0)
5556 {
5557 ga_concat(&ga, (char_u *)", [");
5558 for (i = 0; i < pt->pt_argc; ++i)
5559 {
5560 if (i > 0)
5561 ga_concat(&ga, (char_u *)", ");
5562 ga_concat(&ga,
5563 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5564 vim_free(tf);
5565 }
5566 ga_concat(&ga, (char_u *)"]");
5567 }
5568 if (pt != NULL && pt->pt_dict != NULL)
5569 {
5570 typval_T dtv;
5571
5572 ga_concat(&ga, (char_u *)", ");
5573 dtv.v_type = VAR_DICT;
5574 dtv.vval.v_dict = pt->pt_dict;
5575 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5576 vim_free(tf);
5577 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005578 // terminate with ')' and a NUL
5579 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005580
5581 *tofree = ga.ga_data;
5582 r = *tofree;
5583 break;
5584 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005585
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005586 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005587 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005588 break;
5589
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005591 if (tv->vval.v_list == NULL)
5592 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005593 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005594 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005595 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005596 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005597 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5598 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005599 {
5600 *tofree = NULL;
5601 r = (char_u *)"[...]";
5602 }
5603 else
5604 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005605 int old_copyID = tv->vval.v_list->lv_copyID;
5606
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005607 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005608 *tofree = list2string(tv, copyID, restore_copyID);
5609 if (restore_copyID)
5610 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005611 r = *tofree;
5612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005613 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005614
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005616 if (tv->vval.v_dict == NULL)
5617 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005618 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005619 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005620 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005621 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005622 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5623 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005624 {
5625 *tofree = NULL;
5626 r = (char_u *)"{...}";
5627 }
5628 else
5629 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005630 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005631
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005632 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005633 *tofree = dict2string(tv, copyID, restore_copyID);
5634 if (restore_copyID)
5635 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636 r = *tofree;
5637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005641 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005642 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005644 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005645 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005646 break;
5647
Bram Moolenaar835dc632016-02-07 14:27:38 +01005648 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005649 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005650#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005651 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005652 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5653 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005654 if (composite_val)
5655 {
5656 *tofree = string_quote(r, FALSE);
5657 r = *tofree;
5658 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005659#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005661
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005662 case VAR_INSTR:
5663 *tofree = NULL;
5664 r = (char_u *)"instructions";
5665 break;
5666
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005667 case VAR_FLOAT:
5668 *tofree = NULL;
5669 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5670 r = numbuf;
5671 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005672
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005673 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005674 case VAR_SPECIAL:
5675 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005676 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005677 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005679
Bram Moolenaar8502c702014-06-17 12:51:16 +02005680 if (--recurse == 0)
5681 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005682 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683}
5684
5685/*
5686 * Return a string with the string representation of a variable.
5687 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5688 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005689 * Does not put quotes around strings, as ":echo" displays values.
5690 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5691 * May return NULL.
5692 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005694echo_string(
5695 typval_T *tv,
5696 char_u **tofree,
5697 char_u *numbuf,
5698 int copyID)
5699{
5700 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5701}
5702
5703/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005704 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5705 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005706 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005707 */
5708 int
5709buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5710{
5711 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005712 char_u *t;
5713 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005714
5715 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5716 return -1;
5717
5718 if (lnum > buf->b_ml.ml_line_count)
5719 lnum = buf->b_ml.ml_line_count;
5720
5721 str = ml_get_buf(buf, lnum, FALSE);
5722 if (str == NULL)
5723 return -1;
5724
5725 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005726 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005727
Bram Moolenaar91458462021-01-13 20:08:38 +01005728 // count the number of characters
5729 t = str;
5730 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5731 t += mb_ptr2len(t);
5732
5733 // In insert mode, when the cursor is at the end of a non-empty line,
5734 // byteidx points to the NUL character immediately past the end of the
5735 // string. In this case, add one to the character count.
5736 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5737 count++;
5738
5739 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005740}
5741
5742/*
5743 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005744 * byte index. Works only for loaded buffers. Returns -1 on failure.
5745 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005746 */
5747 int
5748buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5749{
5750 char_u *str;
5751 char_u *t;
5752
5753 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5754 return -1;
5755
5756 if (lnum > buf->b_ml.ml_line_count)
5757 lnum = buf->b_ml.ml_line_count;
5758
5759 str = ml_get_buf(buf, lnum, FALSE);
5760 if (str == NULL)
5761 return -1;
5762
5763 // Convert the character offset to a byte offset
5764 t = str;
5765 while (*t != NUL && --charidx > 0)
5766 t += mb_ptr2len(t);
5767
Bram Moolenaar91458462021-01-13 20:08:38 +01005768 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005769}
5770
5771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005773 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005776var2fpos(
5777 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005778 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005779 int *fnum, // set to fnum for '0, 'A, etc.
5780 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005782 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005784 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005786 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005787 if (varp->v_type == VAR_LIST)
5788 {
5789 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005790 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005791 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005792 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005793
5794 l = varp->vval.v_list;
5795 if (l == NULL)
5796 return NULL;
5797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005798 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005799 pos.lnum = list_find_nr(l, 0L, &error);
5800 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005802 if (charcol)
5803 len = (long)mb_charlen(ml_get(pos.lnum));
5804 else
5805 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005806
Bram Moolenaarec65d772020-08-20 22:29:12 +02005807 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005808 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005809 li = list_find(l, 1L);
5810 if (li != NULL && li->li_tv.v_type == VAR_STRING
5811 && li->li_tv.vval.v_string != NULL
5812 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005813 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005814 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005815 }
5816 else
5817 {
5818 pos.col = list_find_nr(l, 1L, &error);
5819 if (error)
5820 return NULL;
5821 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005823 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005824 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005826 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005829 pos.coladd = list_find_nr(l, 2L, &error);
5830 if (error)
5831 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005832
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005833 return &pos;
5834 }
5835
Bram Moolenaarc5809432021-03-27 21:23:30 +01005836 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5837 return NULL;
5838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005839 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005840 if (name == NULL)
5841 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005842
5843 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005844 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005845 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005846 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005847 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005848 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005849 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005850 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005851 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005852 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005853 pos = VIsual;
5854 else
5855 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005856 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005857 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005858 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005860 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005861 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5863 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005864 pos = *pp;
5865 }
5866 if (pos.lnum != 0)
5867 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005868 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005869 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5870 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005872
Bram Moolenaara5525202006-03-02 22:52:09 +00005873 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005874
Bram Moolenaar477933c2007-07-17 14:32:23 +00005875 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005876 {
5877 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005879 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005880 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005881 // In silent Ex mode topline is zero, but that's not a valid line
5882 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005883 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005884 return &pos;
5885 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005886 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005887 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005888 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005890 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005891 return &pos;
5892 }
5893 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005894 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005896 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
5898 pos.lnum = curbuf->b_ml.ml_line_count;
5899 pos.col = 0;
5900 }
5901 else
5902 {
5903 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005904 if (charcol)
5905 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5906 else
5907 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
5909 return &pos;
5910 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005911 if (in_vim9script())
5912 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 return NULL;
5914}
5915
5916/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005917 * Convert list in "arg" into a position and optional file number.
5918 * When "fnump" is NULL there is no file number, only 3 items.
5919 * Note that the column is passed on as-is, the caller may want to decrement
5920 * it to use 1 for the first column.
5921 * Return FAIL when conversion is not possible, doesn't check the position for
5922 * validity.
5923 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925list2fpos(
5926 typval_T *arg,
5927 pos_T *posp,
5928 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005929 colnr_T *curswantp,
5930 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005931{
5932 list_T *l = arg->vval.v_list;
5933 long i = 0;
5934 long n;
5935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5937 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005938 if (arg->v_type != VAR_LIST
5939 || l == NULL
5940 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005941 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005942 return FAIL;
5943
5944 if (fnump != NULL)
5945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005946 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005947 if (n < 0)
5948 return FAIL;
5949 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005950 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005951 *fnump = n;
5952 }
5953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005955 if (n < 0)
5956 return FAIL;
5957 posp->lnum = n;
5958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005960 if (n < 0)
5961 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005962 // If character position is specified, then convert to byte position
5963 if (charcol)
5964 {
5965 buf_T *buf;
5966
5967 // Get the text for the specified line in a loaded buffer
5968 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5969 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5970 return FAIL;
5971
Bram Moolenaar91458462021-01-13 20:08:38 +01005972 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005973 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005974 posp->col = n;
5975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005977 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005978 posp->coladd = 0;
5979 else
5980 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005981
Bram Moolenaar493c1782014-05-28 14:34:46 +02005982 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005984
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005985 return OK;
5986}
5987
5988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 * Get the length of an environment variable name.
5990 * Advance "arg" to the first character after the name.
5991 * Return 0 for error.
5992 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005993 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005994get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 char_u *p;
5997 int len;
5998
5999 for (p = *arg; vim_isIDc(*p); ++p)
6000 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 return 0;
6003
6004 len = (int)(p - *arg);
6005 *arg = p;
6006 return len;
6007}
6008
6009/*
6010 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006011 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 * Return 0 if something is wrong.
6013 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006015get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 char_u *p;
6018 int len;
6019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006020 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006022 {
6023 if (*p == ':')
6024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006025 // "s:" is start of "s:var", but "n:" is not and can be used in
6026 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006027 len = (int)(p - *arg);
6028 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6029 || len > 1)
6030 break;
6031 }
6032 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return 0;
6035
6036 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006037 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 return len;
6040}
6041
6042/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006043 * Get the length of the name of a variable or function.
6044 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006046 * Return -1 if curly braces expansion failed.
6047 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 * If the name contains 'magic' {}'s, expand them and return the
6049 * expanded name in an allocated string via 'alias' - caller must free.
6050 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052get_name_len(
6053 char_u **arg,
6054 char_u **alias,
6055 int evaluate,
6056 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 char_u *p;
6060 char_u *expr_start;
6061 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
6065 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6066 && (*arg)[2] == (int)KE_SNR)
6067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 *arg += 3;
6070 return get_id_len(arg) + 3;
6071 }
6072 len = eval_fname_script(*arg);
6073 if (len > 0)
6074 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 *arg += len;
6077 }
6078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006080 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006082 p = find_name_end(*arg, &expr_start, &expr_end,
6083 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (expr_start != NULL)
6085 {
6086 char_u *temp_string;
6087
6088 if (!evaluate)
6089 {
6090 len += (int)(p - *arg);
6091 *arg = skipwhite(p);
6092 return len;
6093 }
6094
6095 /*
6096 * Include any <SID> etc in the expanded string:
6097 * Thus the -len here.
6098 */
6099 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6100 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006101 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 *alias = temp_string;
6103 *arg = skipwhite(p);
6104 return (int)STRLEN(temp_string);
6105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006108 // Only give an error when there is something, otherwise it will be
6109 // reported at a higher level.
6110 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006111 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113 return len;
6114}
6115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006116/*
6117 * Find the end of a variable or function name, taking care of magic braces.
6118 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6119 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006120 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006121 * Return a pointer to just after the name. Equal to "arg" if there is no
6122 * valid name.
6123 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006124 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125find_name_end(
6126 char_u *arg,
6127 char_u **expr_start,
6128 char_u **expr_end,
6129 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 int mb_nest = 0;
6132 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006134 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006135 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006137 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006139 *expr_start = NULL;
6140 *expr_end = NULL;
6141 }
6142
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006143 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006144 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6145 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006146 return arg;
6147
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006148 for (p = arg; *p != NUL
6149 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006150 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006151 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006152 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006154 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006156 if (*p == '\'')
6157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006158 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006159 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006160 ;
6161 if (*p == NUL)
6162 break;
6163 }
6164 else if (*p == '"')
6165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006166 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006167 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006168 if (*p == '\\' && p[1] != NUL)
6169 ++p;
6170 if (*p == NUL)
6171 break;
6172 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006173 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 // "s:" is start of "s:var", but "n:" is not and can be used in
6176 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006177 len = (int)(p - arg);
6178 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006179 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006180 break;
6181 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006182
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006183 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006185 if (*p == '[')
6186 ++br_nest;
6187 else if (*p == ']')
6188 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006190
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006191 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 if (*p == '{')
6194 {
6195 mb_nest++;
6196 if (expr_start != NULL && *expr_start == NULL)
6197 *expr_start = p;
6198 }
6199 else if (*p == '}')
6200 {
6201 mb_nest--;
6202 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6203 *expr_end = p;
6204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
6207
6208 return p;
6209}
6210
6211/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006212 * Expands out the 'magic' {}'s in a variable/function name.
6213 * Note that this can call itself recursively, to deal with
6214 * constructs like foo{bar}{baz}{bam}
6215 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6216 * "in_start" ^
6217 * "expr_start" ^
6218 * "expr_end" ^
6219 * "in_end" ^
6220 *
6221 * Returns a new allocated string, which the caller must free.
6222 * Returns NULL for failure.
6223 */
6224 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006225make_expanded_name(
6226 char_u *in_start,
6227 char_u *expr_start,
6228 char_u *expr_end,
6229 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006230{
6231 char_u c1;
6232 char_u *retval = NULL;
6233 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006234
6235 if (expr_end == NULL || in_end == NULL)
6236 return NULL;
6237 *expr_start = NUL;
6238 *expr_end = NUL;
6239 c1 = *in_end;
6240 *in_end = NUL;
6241
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006242 temp_result = eval_to_string(expr_start + 1, FALSE);
6243 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006244 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006245 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6246 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006247 if (retval != NULL)
6248 {
6249 STRCPY(retval, in_start);
6250 STRCAT(retval, temp_result);
6251 STRCAT(retval, expr_end + 1);
6252 }
6253 }
6254 vim_free(temp_result);
6255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006256 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006257 *expr_start = '{';
6258 *expr_end = '}';
6259
6260 if (retval != NULL)
6261 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006262 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006263 if (expr_start != NULL)
6264 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006266 temp_result = make_expanded_name(retval, expr_start,
6267 expr_end, temp_result);
6268 vim_free(retval);
6269 retval = temp_result;
6270 }
6271 }
6272
6273 return retval;
6274}
6275
6276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006278 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006281eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006283 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006284}
6285
6286/*
6287 * Return TRUE if character "c" can be used as the first character in a
6288 * variable or function name (excluding '{' and '}').
6289 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006292{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006293 return ASCII_ISALPHA(c) || c == '_';
6294}
6295
6296/*
6297 * Return TRUE if character "c" can be used as the first character of a
6298 * dictionary key.
6299 */
6300 int
6301eval_isdictc(int c)
6302{
6303 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304}
6305
6306/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006307 * Handle:
6308 * - expr[expr], expr[expr:expr] subscript
6309 * - ".name" lookup
6310 * - function call with Funcref variable: func(expr)
6311 * - method call: var->method()
6312 *
6313 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006314 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006315 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006317handle_subscript(
6318 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006319 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006320 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006321 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006322 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006323{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006324 int evaluate = evalarg != NULL
6325 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006326 int ret = OK;
6327 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006328 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006329 int getnext;
6330 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006331
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006332 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006333 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006334 // When at the end of the line and ".name" or "->{" or "->X" follows in
6335 // the next line then consume the line break.
6336 p = eval_next_non_blank(*arg, evalarg, &getnext);
6337 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006338 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006339 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6340 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6341 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006342 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006343 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006344 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006345 check_white = FALSE;
6346 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006347
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006348 if (rettv->v_type == VAR_ANY)
6349 {
6350 char_u *exp_name;
6351 int cc;
6352 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006353 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006354 type_T *type;
6355
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006356 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006357 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006358 if (**arg != '.')
6359 {
6360 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006361 semsg(_(e_expected_dot_after_name_str),
6362 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006363 ret = FAIL;
6364 break;
6365 }
6366 ++*arg;
6367 if (IS_WHITE_OR_NUL(**arg))
6368 {
6369 if (verbose)
6370 emsg(_(e_no_white_space_allowed_after_dot));
6371 ret = FAIL;
6372 break;
6373 }
6374
6375 // isolate the name
6376 exp_name = *arg;
6377 while (eval_isnamec(**arg))
6378 ++*arg;
6379 cc = **arg;
6380 **arg = NUL;
6381
6382 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006383 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006384 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006385
6386 if (idx < 0 && ufunc == NULL)
6387 {
6388 ret = FAIL;
6389 break;
6390 }
6391 if (idx >= 0)
6392 {
6393 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6394 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6395
6396 copy_tv(sv->sv_tv, rettv);
6397 }
6398 else
6399 {
6400 rettv->v_type = VAR_FUNC;
6401 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6402 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006403 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006404 }
6405
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006406 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6407 || rettv->v_type == VAR_PARTIAL))
6408 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006409 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006410 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6411 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006412
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006413 // Stop the expression evaluation when immediately aborting on
6414 // error, or when an interrupt occurred or an exception was thrown
6415 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006416 if (aborting())
6417 {
6418 if (ret == OK)
6419 clear_tv(rettv);
6420 ret = FAIL;
6421 }
6422 dict_unref(selfdict);
6423 selfdict = NULL;
6424 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006425 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006426 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006427 if (in_vim9script())
6428 *arg = skipwhite(p + 2);
6429 else
6430 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006431 if (ret == OK)
6432 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006433 if (VIM_ISWHITE(**arg))
6434 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006435 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006436 ret = FAIL;
6437 }
6438 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006439 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006440 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006441 else
6442 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006443 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006444 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006445 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006446 // "." is ".name" lookup when we found a dict or when evaluating and
6447 // scriptversion is at least 2, where string concatenation is "..".
6448 else if (**arg == '['
6449 || (**arg == '.' && (rettv->v_type == VAR_DICT
6450 || (!evaluate
6451 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006452 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006453 {
6454 dict_unref(selfdict);
6455 if (rettv->v_type == VAR_DICT)
6456 {
6457 selfdict = rettv->vval.v_dict;
6458 if (selfdict != NULL)
6459 ++selfdict->dv_refcount;
6460 }
6461 else
6462 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006463 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006464 {
6465 clear_tv(rettv);
6466 ret = FAIL;
6467 }
6468 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006469 else
6470 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006471 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6474 // Don't do this when "Func" is already a partial that was bound
6475 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006476 if (selfdict != NULL
6477 && (rettv->v_type == VAR_FUNC
6478 || (rettv->v_type == VAR_PARTIAL
6479 && (rettv->vval.v_partial->pt_auto
6480 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006481 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006482
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006483 dict_unref(selfdict);
6484 return ret;
6485}
6486
6487/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006488 * Make a copy of an item.
6489 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006490 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6492 * reference to an already copied list/dict can be used.
6493 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496item_copy(
6497 typval_T *from,
6498 typval_T *to,
6499 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006500 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502{
6503 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006507 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006508 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 }
6511 ++recurse;
6512
6513 switch (from->v_type)
6514 {
6515 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006516 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 case VAR_STRING:
6518 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006519 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006520 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006521 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006522 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006523 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006524 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 copy_tv(from, to);
6526 break;
6527 case VAR_LIST:
6528 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006529 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 if (from->vval.v_list == NULL)
6531 to->vval.v_list = NULL;
6532 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6533 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006534 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 to->vval.v_list = from->vval.v_list->lv_copylist;
6536 ++to->vval.v_list->lv_refcount;
6537 }
6538 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006539 to->vval.v_list = list_copy(from->vval.v_list,
6540 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (to->vval.v_list == NULL)
6542 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006544 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006546 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 case VAR_DICT:
6548 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006549 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006550 if (from->vval.v_dict == NULL)
6551 to->vval.v_dict = NULL;
6552 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6553 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6556 ++to->vval.v_dict->dv_refcount;
6557 }
6558 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006559 to->vval.v_dict = dict_copy(from->vval.v_dict,
6560 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 if (to->vval.v_dict == NULL)
6562 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006564 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006565 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006567 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 }
6570 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572}
6573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 void
6575echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6576{
6577 char_u *tofree;
6578 char_u numbuf[NUMBUFLEN];
6579 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6580
6581 if (*atstart)
6582 {
6583 *atstart = FALSE;
6584 // Call msg_start() after eval1(), evaluating the expression
6585 // may cause a message to appear.
6586 if (with_space)
6587 {
6588 // Mark the saved text as finishing the line, so that what
6589 // follows is displayed on a new line when scrolling back
6590 // at the more prompt.
6591 msg_sb_eol();
6592 msg_start();
6593 }
6594 }
6595 else if (with_space)
6596 msg_puts_attr(" ", echo_attr);
6597
6598 if (p != NULL)
6599 for ( ; *p != NUL && !got_int; ++p)
6600 {
6601 if (*p == '\n' || *p == '\r' || *p == TAB)
6602 {
6603 if (*p != TAB && *needclr)
6604 {
6605 // remove any text still there from the command
6606 msg_clr_eos();
6607 *needclr = FALSE;
6608 }
6609 msg_putchar_attr(*p, echo_attr);
6610 }
6611 else
6612 {
6613 if (has_mbyte)
6614 {
6615 int i = (*mb_ptr2len)(p);
6616
6617 (void)msg_outtrans_len_attr(p, i, echo_attr);
6618 p += i - 1;
6619 }
6620 else
6621 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6622 }
6623 }
6624 vim_free(tofree);
6625}
6626
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 * ":echo expr1 ..." print each argument separated with a space, add a
6629 * newline at the end.
6630 * ":echon expr1 ..." print each argument plain.
6631 */
6632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006633ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006637 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 int needclr = TRUE;
6639 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006640 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006641 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006642 evalarg_T evalarg;
6643
Bram Moolenaare707c882020-07-01 13:07:37 +02006644 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 if (eap->skip)
6647 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006648 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // If eval1() causes an error message the text from the command may
6651 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006652 need_clr_eos = needclr;
6653
Bram Moolenaar68db9962021-05-09 23:19:22 +02006654 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006655 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
6657 /*
6658 * Report the invalid expression unless the expression evaluation
6659 * has been cancelled due to an aborting error, an interrupt, or an
6660 * exception.
6661 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006662 if (!aborting() && did_emsg == did_emsg_before
6663 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006664 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006665 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 break;
6667 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006668 need_clr_eos = FALSE;
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006671 {
6672 if (rettv.v_type == VAR_VOID)
6673 {
6674 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6675 break;
6676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006678 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 arg = skipwhite(arg);
6682 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006683 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006684 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 if (eap->skip)
6687 --emsg_skip;
6688 else
6689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (needclr)
6692 msg_clr_eos();
6693 if (eap->cmdidx == CMD_echo)
6694 msg_end();
6695 }
6696}
6697
6698/*
6699 * ":echohl {name}".
6700 */
6701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006704 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706
6707/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006708 * Returns the :echo attribute
6709 */
6710 int
6711get_echo_attr(void)
6712{
6713 return echo_attr;
6714}
6715
6716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 * ":execute expr1 ..." execute the result of an expression.
6718 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006719 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006721 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 * Each gets spaces around each argument and a newline at the end for
6723 * echo commands
6724 */
6725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 int ret = OK;
6731 char_u *p;
6732 garray_T ga;
6733 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006734 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 ga_init2(&ga, 1, 80);
6737
6738 if (eap->skip)
6739 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006740 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006742 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006743 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746 if (!eap->skip)
6747 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006748 char_u buf[NUMBUFLEN];
6749
6750 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006751 {
6752 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6753 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006754 semsg(_(e_using_invalid_value_as_string_str),
6755 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006756 p = NULL;
6757 }
6758 else
6759 p = tv_get_string_buf(&rettv, buf);
6760 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006761 else
6762 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006763 if (p == NULL)
6764 {
6765 clear_tv(&rettv);
6766 ret = FAIL;
6767 break;
6768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 len = (int)STRLEN(p);
6770 if (ga_grow(&ga, len + 2) == FAIL)
6771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006772 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 ret = FAIL;
6774 break;
6775 }
6776 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 ga.ga_len += len;
6780 }
6781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006782 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 arg = skipwhite(arg);
6784 }
6785
6786 if (ret != FAIL && ga.ga_data != NULL)
6787 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006788 // use the first line of continuation lines for messages
6789 SOURCING_LNUM = start_lnum;
6790
Bram Moolenaar37fef162022-08-29 18:16:32 +01006791 if (eap->cmdidx == CMD_echomsg
6792 || eap->cmdidx == CMD_echowindow
6793 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006795 // Mark the already saved text as finishing the line, so that what
6796 // follows is displayed on a new line when scrolling back at the
6797 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006798 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006799 }
6800
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006801 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006802 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006803 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006804 out_flush();
6805 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006806 else if (eap->cmdidx == CMD_echowindow)
6807 {
6808#ifdef HAS_MESSAGE_WINDOW
6809 start_echowindow();
6810#endif
6811 msg_attr(ga.ga_data, echo_attr);
6812#ifdef HAS_MESSAGE_WINDOW
6813 end_echowindow();
6814#endif
6815 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006816 else if (eap->cmdidx == CMD_echoconsole)
6817 {
6818 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6819 ui_write((char_u *)"\r\n", 2, TRUE);
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 else if (eap->cmdidx == CMD_echoerr)
6822 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006823 int save_did_emsg = did_emsg;
6824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006825 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006826 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (!force_abort)
6828 did_emsg = save_did_emsg;
6829 }
6830 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006831 {
6832 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6833
6834 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6835 sticky_cmdmod_flags = cmdmod.cmod_flags
6836 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 do_cmdline((char_u *)ga.ga_data,
6838 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006839 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842
6843 ga_clear(&ga);
6844
6845 if (eap->skip)
6846 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006847 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848}
6849
6850/*
6851 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6852 * "arg" points to the "&" or '+' when called, to "option" when returning.
6853 * Returns NULL when no option name found. Otherwise pointer to the char
6854 * after the option name.
6855 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006856 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006857find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 char_u *p = *arg;
6860
6861 ++p;
6862 if (*p == 'g' && p[1] == ':')
6863 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006864 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 p += 2;
6866 }
6867 else if (*p == 'l' && p[1] == ':')
6868 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006869 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 p += 2;
6871 }
6872 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006873 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875 if (!ASCII_ISALPHA(*p))
6876 return NULL;
6877 *arg = p;
6878
6879 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006880 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 else
6882 while (ASCII_ISALPHA(*p))
6883 ++p;
6884 return p;
6885}
6886
6887/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006888 * Display script name where an item was last set.
6889 * Should only be invoked when 'verbose' is non-zero.
6890 */
6891 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006892last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006893{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006894 char_u *p;
6895
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006896 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006897 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006898 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006899 if (p != NULL)
6900 {
6901 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006902 msg_puts(_("\n\tLast set from "));
6903 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006904 if (script_ctx.sc_lnum > 0)
6905 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006906 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006907 msg_outnum((long)script_ctx.sc_lnum);
6908 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006909 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006910 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006911 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006912 }
6913}
6914
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006915#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917/*
6918 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006919 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 * "flags" can be "g" to do a global substitute.
6921 * Returns an allocated string, NULL for error.
6922 */
6923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924do_string_sub(
6925 char_u *str,
6926 char_u *pat,
6927 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006928 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006929 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930{
6931 int sublen;
6932 regmatch_T regmatch;
6933 int i;
6934 int do_all;
6935 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006936 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 garray_T ga;
6938 char_u *ret;
6939 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006940 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006942 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006944 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 ga_init2(&ga, 1, 200);
6947
6948 do_all = (flags[0] == 'g');
6949
6950 regmatch.rm_ic = p_ic;
6951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6952 if (regmatch.regprog != NULL)
6953 {
6954 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006955 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006958 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006959 if (regmatch.startp[0] == regmatch.endp[0])
6960 {
6961 if (zero_width == regmatch.startp[0])
6962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006963 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006964 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006965 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6966 (size_t)i);
6967 ga.ga_len += i;
6968 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006969 continue;
6970 }
6971 zero_width = regmatch.startp[0];
6972 }
6973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 /*
6975 * Get some space for a temporary buffer to do the substitution
6976 * into. It will contain:
6977 * - The text up to where the match is.
6978 * - The substituted text.
6979 * - The text after the match.
6980 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006981 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006982 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6984 {
6985 ga_clear(&ga);
6986 break;
6987 }
6988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006989 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 i = (int)(regmatch.startp[0] - tail);
6991 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006992 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006993 (void)vim_regsub(&regmatch, sub, expr,
6994 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6995 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006997 tail = regmatch.endp[0];
6998 if (*tail == NUL)
6999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (!do_all)
7001 break;
7002 }
7003
7004 if (ga.ga_data != NULL)
7005 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7006
Bram Moolenaar473de612013-06-08 18:19:48 +02007007 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009
7010 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7011 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007012 if (p_cpo == empty_option)
7013 p_cpo = save_cpo;
7014 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007016 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007017 // If it's still empty it was changed and restored, need to restore in
7018 // the complicated way.
7019 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007020 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007021 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 return ret;
7025}