blob: c57f6a4a05b9b0a2f001f285317fba88e85dfb7d [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 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200131 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200132 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200133 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100134 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200135 {
136 evalarg->eval_getline = eap->getline;
137 evalarg->eval_cookie = eap->cookie;
138 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 }
140}
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
143 * Top level evaluation function, returning a boolean.
144 * Sets "error" to TRUE if there was an error.
145 * Return TRUE or FALSE.
146 */
147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100148eval_to_bool(
149 char_u *arg,
150 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200151 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100152 int skip, // only parse, don't execute
153 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
Bram Moolenaar33570922005-01-25 22:26:29 +0000155 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200156 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200157 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100158 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200159
Bram Moolenaar37c83712020-06-30 21:18:36 +0200160 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
162 if (skip)
163 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100164 if (use_simple_function)
165 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
166 else
167 r = eval0(arg, &tv, eap, &evalarg);
168 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 else
171 {
172 *error = FALSE;
173 if (!skip)
174 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200175 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200176 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200177 else
178 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 }
181 }
182 if (skip)
183 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200184 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200193eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200199 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200201 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
202
203 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204 if (ret == FAIL)
205 {
206 // Report the invalid expression unless the expression evaluation has
207 // been cancelled due to an aborting error, an interrupt, or an
208 // exception, or we already gave a more specific error.
209 // Also check called_emsg for when using assert_fails().
210 if (!aborting() && did_emsg == did_emsg_before
211 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200212 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200214 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215 return ret;
216}
217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200219 * Return whether a typval is a valid expression to pass to eval_expr_typval()
220 * or eval_expr_to_bool(). An empty string returns FALSE;
221 */
222 int
223eval_expr_valid_arg(typval_T *tv)
224{
225 return tv->v_type != VAR_UNKNOWN
226 && (tv->v_type != VAR_STRING
227 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
228}
229
230/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100231 * When calling eval_expr_typval() many times we only need one funccall_T.
232 * Returns NULL when no funccall_T is to be used.
233 * When returning non-NULL remove_funccal() must be called later.
234 */
235 funccall_T *
236eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
237{
238 if (expr->v_type != VAR_PARTIAL)
239 return NULL;
240
241 partial_T *partial = expr->vval.v_partial;
242 if (partial == NULL)
243 return NULL;
244 if (partial->pt_func == NULL
245 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
246 return NULL;
247
248 return create_funccal(partial->pt_func, rettv);
249}
250
251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 * Evaluate an expression, which can be a function, partial or string.
253 * Pass arguments "argv[argc]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100254 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Return the result in "rettv" and OK or FAIL.
256 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200257 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100258eval_expr_typval(
259 typval_T *expr,
260 typval_T *argv,
261 int argc,
262 funccall_T *fc_arg,
263 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100264{
265 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100266 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200267 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100268
269 if (expr->v_type == VAR_FUNC)
270 {
271 s = expr->vval.v_string;
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200276 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 return FAIL;
278 }
279 else if (expr->v_type == VAR_PARTIAL)
280 {
281 partial_T *partial = expr->vval.v_partial;
282
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200283 if (partial == NULL)
284 return FAIL;
285
Bram Moolenaar822ba242020-05-24 23:00:18 +0200286 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200287 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100289 funccall_T *fc = fc_arg != NULL ? fc_arg
290 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100291 int r;
292
293 if (fc == NULL)
294 return FAIL;
295
Bram Moolenaar69082912022-09-22 21:35:19 +0100296 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100297 r = call_def_function(partial->pt_func, argc, argv,
298 DEF_USE_PT_ARGV, partial, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100299 if (fc_arg == NULL)
300 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100301 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 return FAIL;
303 }
304 else
305 {
306 s = partial_name(partial);
307 if (s == NULL || *s == NUL)
308 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200309 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000310 funcexe.fe_evaluate = TRUE;
311 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
313 return FAIL;
314 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100315 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200316 else if (expr->v_type == VAR_INSTR)
317 {
318 return exe_typval_instr(expr, rettv);
319 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 else
321 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000322 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 if (s == NULL)
324 return FAIL;
325 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200326 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100327 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200328 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200330 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200331 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
333 }
334 }
335 return OK;
336}
337
338/*
339 * Like eval_to_bool() but using a typval_T instead of a string.
340 * Works for string, funcref and partial.
341 */
342 int
343eval_expr_to_bool(typval_T *expr, int *error)
344{
345 typval_T rettv;
346 int res;
347
Bram Moolenaar82418262022-09-28 16:16:15 +0100348 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100349 {
350 *error = TRUE;
351 return FALSE;
352 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200353 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 clear_tv(&rettv);
355 return res;
356}
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
359 * Top level evaluation function, returning a string. If "skip" is TRUE,
360 * only parsing to "nextcmd" is done, without reporting errors. Return
361 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
362 */
363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100364eval_to_string_skip(
365 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200366 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100367 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200371 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaar37c83712020-06-30 21:18:36 +0200373 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (skip)
375 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 retval = NULL;
378 else
379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100380 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 }
383 if (skip)
384 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200385 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387 return retval;
388}
389
390/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100391 * Initialize "evalarg" for use.
392 */
393 void
394init_evalarg(evalarg_T *evalarg)
395{
396 CLEAR_POINTER(evalarg);
397 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
398}
399
400/*
401 * If "evalarg->eval_tofree" is not NULL free it later.
402 * Caller is expected to overwrite "evalarg->eval_tofree" next.
403 */
404 static void
405free_eval_tofree_later(evalarg_T *evalarg)
406{
407 if (evalarg->eval_tofree != NULL)
408 {
409 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
410 ((char_u **)evalarg->eval_tofree_ga.ga_data)
411 [evalarg->eval_tofree_ga.ga_len++]
412 = evalarg->eval_tofree;
413 else
414 vim_free(evalarg->eval_tofree);
415 }
416}
417
418/*
419 * After using "evalarg" filled from "eap": free the memory.
420 */
421 void
422clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
423{
424 if (evalarg != NULL)
425 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100426 garray_T *etga = &evalarg->eval_tofree_ga;
427
428 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100429 {
430 if (eap != NULL)
431 {
432 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100433 // ":let" it has the variable names. But we may also need
434 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 vim_free(eap->cmdline_tofree);
436 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100437
438 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
439 {
440 // "nextcmd" points into the last line in eval_tofree_ga,
441 // need to keep it around.
442 --etga->ga_len;
443 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
Bram Moolenaar86fb3f82022-09-23 13:27:57 +0100444 vim_free(evalarg->eval_tofree);
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100445 }
446 else
447 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100448 }
449 else
450 vim_free(evalarg->eval_tofree);
451 evalarg->eval_tofree = NULL;
452 }
453
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100454 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100455 VIM_CLEAR(evalarg->eval_tofree_lambda);
456 }
457}
458
459/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000460 * Skip over an expression at "*pp".
461 * Return FAIL for an error, OK otherwise.
462 */
463 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200464skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465{
Bram Moolenaar33570922005-01-25 22:26:29 +0000466 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000467
468 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470}
471
472/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200473 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200474 * If in Vim9 script and line breaks are encountered, the lines are
475 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200476 * "arg" is advanced to just after the expression.
477 * "start" is set to the start of the expression, "end" to just after the end.
478 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * Return FAIL for an error, OK otherwise.
480 */
481 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200482skip_expr_concatenate(
483 char_u **arg,
484 char_u **start,
485 char_u **end,
486 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200487{
488 typval_T rettv;
489 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200490 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200491 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200492 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200493 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200494 int evaluate = evalarg == NULL
495 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200496
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200497 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200498 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200499 {
500 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200501 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200502 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200503 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200504 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200505 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200506 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507
508 // Don't evaluate the expression.
509 if (evalarg != NULL)
510 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *arg = skipwhite(*arg);
512 res = eval1(arg, &rettv, evalarg);
513 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200514 if (evalarg != NULL)
515 evalarg->eval_flags = save_flags;
516
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200517 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200518 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200520 if (evalarg->eval_ga.ga_len == 1)
521 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200522 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 ga_clear(gap);
524 gap->ga_itemsize = 0;
525 }
526 else
527 {
528 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200529 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200530
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200531 // Line breaks encountered, concatenate all the lines.
532 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200533 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200534
535 // free the lines only when using getsourceline()
536 if (evalarg->eval_cookie != NULL)
537 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200538 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200540 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100541 // later. Also free "eval_tofree" later if needed.
542 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 evalarg->eval_tofree =
544 ((char_u **)gap->ga_data)[gap->ga_len - 1];
545 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200546 ga_clear_strings(gap);
547 }
548 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200549 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200550 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200551
552 // free lines that were explicitly marked for freeing
553 ga_clear_strings(freegap);
554 }
555
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 gap->ga_itemsize = 0;
557 if (p == NULL)
558 return FAIL;
559 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200560 vim_free(evalarg->eval_tofree_lambda);
561 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200562 // Compute "end" relative to the end.
563 *end = *start + STRLEN(*start) - endoff;
564 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200565 }
566
567 return res;
568}
569
570/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100571 * Convert "tv" to a string.
572 * When "convert" is TRUE convert a List into a sequence of lines and convert
573 * a Float to a String.
574 * Returns an allocated string (NULL when out of memory).
575 */
576 char_u *
577typval2string(typval_T *tv, int convert)
578{
579 garray_T ga;
580 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100581 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100582
583 if (convert && tv->v_type == VAR_LIST)
584 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000585 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586 if (tv->vval.v_list != NULL)
587 {
588 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
589 if (tv->vval.v_list->lv_len > 0)
590 ga_append(&ga, NL);
591 }
592 ga_append(&ga, NUL);
593 retval = (char_u *)ga.ga_data;
594 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100595 else if (convert && tv->v_type == VAR_FLOAT)
596 {
597 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
598 retval = vim_strsave(numbuf);
599 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100600 else
601 retval = vim_strsave(tv_get_string(tv));
602 return retval;
603}
604
605/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200606 * Top level evaluation function, returning a string. Does not handle line
607 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000608 * When "convert" is TRUE convert a List into a sequence of lines and convert
609 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * Return pointer to allocated memory, or NULL for failure.
611 */
612 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100613eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100614 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100615 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100616 exarg_T *eap,
617 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar33570922005-01-25 22:26:29 +0000619 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100625 if (use_simple_function)
626 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
627 else
628 r = eval0(arg, &tv, NULL, &evalarg);
629 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 retval = NULL;
631 else
632 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000634 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100636 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 return retval;
639}
640
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100641 char_u *
642eval_to_string(
643 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 int convert,
645 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100646{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100647 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100648}
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000651 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100652 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200653 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 */
655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656eval_to_string_safe(
657 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000658 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100659 int keep_script_version,
660 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200663 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200664 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200665 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
Bram Moolenaar9530b582022-01-22 13:39:08 +0000667 if (!keep_script_version)
668 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200669 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000670 if (use_sandbox)
671 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100672 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200673 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100674 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 if (use_sandbox)
676 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100677 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200678 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200679 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200680 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 return retval;
682}
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684/*
685 * Top level evaluation function, returning a number.
686 * Evaluates "expr" silently.
687 * Returns -1 for an error.
688 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200689 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100690eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar33570922005-01-25 22:26:29 +0000692 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200693 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000694 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100695 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697 ++emsg_off;
698
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100699 if (use_simple_function)
700 r = may_call_simple_func(expr, &rettv);
701 if (r == NOTDONE)
702 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
703 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 retval = -1;
705 else
706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100707 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000708 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710 --emsg_off;
711
712 return retval;
713}
714
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000715/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000716 * Top level evaluation function.
717 * Returns an allocated typval_T with the result.
718 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719 */
720 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200721eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000722{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100723 return eval_expr_ext(arg, eap, FALSE);
724}
725
726 typval_T *
727eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
728{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000729 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200730 evalarg_T evalarg;
731
732 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200734 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100735 if (tv != NULL)
736 {
737 int r = NOTDONE;
738
739 if (use_simple_function)
740 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
741 if (r == NOTDONE)
742 r = eval0(arg, tv, eap, &evalarg);
743
744 if (r == FAIL)
745 VIM_CLEAR(tv);
746 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000747
Bram Moolenaar37c83712020-06-30 21:18:36 +0200748 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000749 return tv;
750}
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000753 * "*arg" points to what can be a function name in the form of "import.Name" or
754 * "Funcref". Return the name of the function. Set "tofree" to something that
755 * was allocated.
756 * If "verbose" is FALSE no errors are given.
757 * Return NULL for any failure.
758 */
759 static char_u *
760deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000761 char_u **arg,
762 char_u **tofree,
763 evalarg_T *evalarg,
764 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000765{
766 typval_T ref;
767 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100768 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000769
770 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100771 if (evalarg != NULL)
772 {
773 // need to evaluate this to get an import, like in "a.Func"
774 save_flags = evalarg->eval_flags;
775 evalarg->eval_flags |= EVAL_EVALUATE;
776 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100777 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100778 {
779 dictitem_T *v;
780
781 // If <SID>VarName was used it would not be found, try another way.
782 v = find_var_also_in_script(name, NULL, FALSE);
783 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100784 {
785 name = NULL;
786 goto theend;
787 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100788 copy_tv(&v->di_tv, &ref);
789 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 if (*skipwhite(*arg) != NUL)
791 {
792 if (verbose)
793 semsg(_(e_trailing_characters_str), *arg);
794 name = NULL;
795 }
796 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
797 {
798 name = ref.vval.v_string;
799 ref.vval.v_string = NULL;
800 *tofree = name;
801 }
802 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
803 {
804 if (ref.vval.v_partial->pt_argc > 0
805 || ref.vval.v_partial->pt_dict != NULL)
806 {
807 if (verbose)
808 emsg(_(e_cannot_use_partial_here));
809 name = NULL;
810 }
811 else
812 {
813 name = vim_strsave(partial_name(ref.vval.v_partial));
814 *tofree = name;
815 }
816 }
817 else
818 {
819 if (verbose)
820 semsg(_(e_not_callable_type_str), name);
821 name = NULL;
822 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823
824theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100826 if (evalarg != NULL)
827 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000828 return name;
829}
830
831/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100832 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200833 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
834 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000835 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838call_vim_function(
839 char_u *func,
840 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200841 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200842 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000844 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200845 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000846 char_u *arg;
847 char_u *name;
848 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000849 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200852 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000853 funcexe.fe_firstline = curwin->w_cursor.lnum;
854 funcexe.fe_lastline = curwin->w_cursor.lnum;
855 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000856
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000857 // The name might be "import.Func" or "Funcref". We don't know, we need to
858 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000859 // autoload script has errors. Guess that when there is a dot in the name
860 // showing errors is the right choice.
861 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000866 if (ignore_errors)
867 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000868 if (name == NULL)
869 name = func;
870
871 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
872
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873 if (ret == FAIL)
874 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000875 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000876
877 return ret;
878}
879
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100880/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100881 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000882 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
883 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000884 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000885 */
886 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100887call_func_retstr(
888 char_u *func,
889 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200890 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891{
892 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000893 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000894
Bram Moolenaarded27a12018-08-01 19:06:03 +0200895 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 return NULL;
897
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100898 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 return retval;
901}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000902
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000903/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100904 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000905 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000906 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100907 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000908 */
909 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910call_func_retlist(
911 char_u *func,
912 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200913 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000914{
915 typval_T rettv;
916
Bram Moolenaarded27a12018-08-01 19:06:03 +0200917 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000918 return NULL;
919
920 if (rettv.v_type != VAR_LIST)
921 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100922 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
923 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000924 clear_tv(&rettv);
925 return NULL;
926 }
927
928 return rettv.vval.v_list;
929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaare70dd112022-01-21 16:31:11 +0000931#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100933 * Evaluate "arg", which is 'foldexpr'.
934 * Note: caller must set "curwin" to match "arg".
935 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
936 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 */
938 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000939eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000941 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200943 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000945 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000946 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100947 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100949 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000950 current_sctx = wp->w_p_script_ctx[WV_FDE];
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000953 if (use_sandbox)
954 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100955 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100957
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100958 // Evaluate the expression. If the expression is "FuncName()" call the
959 // function directly.
960 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 retval = 0;
962 else
963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 if (tv.v_type == VAR_NUMBER)
966 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000967 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 retval = 0;
969 else
970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // If the result is a string, check if there is a non-digit before
972 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (!VIM_ISDIGIT(*s) && *s != '-')
975 *cp = *s++;
976 retval = atol((char *)s);
977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000978 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000981 if (use_sandbox)
982 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100983 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200984 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000985 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200987 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988}
989#endif
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 * Get an lval: variable, Dict item or List item that can be assigned a value
993 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
994 * "name.key", "name.key[expr]" etc.
995 * Indexing only works if "name" is an existing List or Dictionary.
996 * "name" points to the start of the name.
997 * If "rettv" is not NULL it points to the value to be assigned.
998 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
999 * wrong; must end in space or cmd separator.
1000 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 * flags:
1002 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001003 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001004 * GLV_NO_AUTOLOAD: do not use script autoloading
1005 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001007 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001010 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001011get_lval(
1012 char_u *name,
1013 typval_T *rettv,
1014 lval_T *lp,
1015 int unlet,
1016 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 int flags, // GLV_ values
1018 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 char_u *expr_start, *expr_end;
1022 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001023 dictitem_T *v;
1024 typval_T var1;
1025 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001029 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001030 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001031 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001032 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001035 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001037 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001039 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001041 lp->ll_name_end = find_name_end(name, NULL, NULL,
1042 FNE_INCL_BR | fne_flags);
1043 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 }
1045
Bram Moolenaara749a422022-02-12 19:52:25 +00001046 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001047 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001048 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1049 {
1050 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1051 return NULL;
1052 }
1053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001054 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (expr_start != NULL)
1058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001060 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 && *p != '[' && *p != '.')
1062 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001063 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 return NULL;
1065 }
1066
1067 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1068 if (lp->ll_exp_name == NULL)
1069 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001070 // Report an invalid expression in braces, unless the
1071 // expression evaluation has been cancelled due to an
1072 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!aborting() && !quiet)
1074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001075 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001076 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
1078 }
1079 }
1080 lp->ll_name = lp->ll_exp_name;
1081 }
1082 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 lp->ll_name = name;
1085
Bram Moolenaar4525a572022-02-13 11:57:33 +00001086 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001088 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001089 // However, "g:[key]" is indexing a dictionary.
1090 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001091 {
1092 --p;
1093 lp->ll_name_end = p;
1094 }
1095 if (*p == ':')
1096 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001097 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001098
Bram Moolenaar9510d222022-09-11 15:14:05 +01001099 if (is_scoped_variable(name))
1100 {
1101 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1102 return NULL;
1103 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001104 if (tp == p + 1 && !quiet)
1105 {
1106 semsg(_(e_white_space_required_after_str_str), ":", p);
1107 return NULL;
1108 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001109 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001110 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001111 semsg(_(e_using_type_not_in_script_context_str), p);
1112 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001113 }
1114
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001115 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001116 lp->ll_type = parse_type(&tp,
1117 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1118 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001119 if (lp->ll_type == NULL && !quiet)
1120 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001121 lp->ll_name_end = tp;
1122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 }
1124 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001125 if (lp->ll_name == NULL)
1126 return p;
1127
Bram Moolenaar62aec932022-01-29 21:45:34 +00001128 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001129 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001130 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001131
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001132 if (import != NULL)
1133 {
1134 ufunc_T *ufunc;
1135 type_T *type;
1136
Bram Moolenaar753885b2022-08-24 16:30:36 +01001137 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001138 lp->ll_sid = import->imp_sid;
1139 lp->ll_name = skipwhite(p + 1);
1140 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1141 lp->ll_name_end = p;
1142
1143 // check the item is exported
1144 cc = *p;
1145 *p = NUL;
1146 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001147 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001148 {
1149 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001150 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001151 }
1152 *p = cc;
1153 }
1154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001157 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 return p;
1159
Bram Moolenaar4525a572022-02-13 11:57:33 +00001160 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001161 {
1162 // using local variable
1163 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001164 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001165 }
1166 else
1167 {
1168 cc = *p;
1169 *p = NUL;
1170 // When we would write to the variable pass &ht and prevent autoload.
1171 writing = !(flags & GLV_READ_ONLY);
1172 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001173 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001174 if (v == NULL && !quiet)
1175 semsg(_(e_undefined_variable_str), lp->ll_name);
1176 *p = cc;
1177 if (v == NULL)
1178 return NULL;
1179 lp->ll_tv = &v->di_tv;
1180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181
Bram Moolenaar4525a572022-02-13 11:57:33 +00001182 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001183 {
1184 if (!quiet)
1185 semsg(_(e_variable_already_declared), lp->ll_name);
1186 return NULL;
1187 }
1188
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 /*
1190 * Loop until no more [idx] or .key is following.
1191 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001192 var1.v_type = VAR_UNKNOWN;
1193 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001194 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001196 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001197
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001198 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1199 {
1200 if (!quiet)
1201 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1202 return NULL;
1203 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001204 if (lp->ll_tv->v_type != VAR_LIST
1205 && lp->ll_tv->v_type != VAR_DICT
1206 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001209 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001212
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001213 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001214 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001215 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001216 else if (lp->ll_tv->v_type == VAR_BLOB
1217 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001218 r = rettv_blob_alloc(lp->ll_tv);
1219 if (r == FAIL)
1220 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001221
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001225 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001227 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001228
Bram Moolenaar4525a572022-02-13 11:57:33 +00001229 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001230 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001231 && lp->ll_tv == &v->di_tv
1232 && ht != NULL && ht == get_script_local_ht())
1233 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001234 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001235
1236 // Vim9 script local variable: get the type
1237 if (sv != NULL)
1238 lp->ll_valtype = sv->sv_type;
1239 }
1240
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 len = -1;
1242 if (*p == '.')
1243 {
1244 key = p + 1;
1245 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1246 ;
1247 if (len == 0)
1248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001250 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253 p = key + len;
1254 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255 else
1256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 if (*p == ':')
1260 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 else
1262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001264 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001266 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001268 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001269 clear_tv(&var1);
1270 return NULL;
1271 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001272 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001273 }
1274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001275 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 if (*p == ':')
1277 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001278 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001281 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001282 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001284 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 if (rettv != NULL
1286 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001287 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001289 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001292 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001293 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001295 }
1296 p = skipwhite(p + 1);
1297 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001299 else
1300 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001302 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001303 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001304 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001305 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001307 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001308 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001310 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001311 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 clear_tv(&var2);
1313 return NULL;
1314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001320
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001322 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001324 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001325 clear_tv(&var1);
1326 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001330 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 ++p;
1332 }
1333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 {
1336 if (len == -1)
1337 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001338 // "[key]": get key from "var1"
1339 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001340 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 }
1345 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001346 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001347
1348 // a NULL dict is equivalent with an empty dict
1349 if (lp->ll_tv->vval.v_dict == NULL)
1350 {
1351 lp->ll_tv->vval.v_dict = dict_alloc();
1352 if (lp->ll_tv->vval.v_dict == NULL)
1353 {
1354 clear_tv(&var1);
1355 return NULL;
1356 }
1357 ++lp->ll_tv->vval.v_dict->dv_refcount;
1358 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001360
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001361 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001363 // When assigning to a scope dictionary check that a function and
1364 // variable name is valid (only variable name unless it is l: or
1365 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001366 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001367 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001368 int prevval;
1369 int wrong;
1370
1371 if (len != -1)
1372 {
1373 prevval = key[len];
1374 key[len] = NUL;
1375 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001376 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001377 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001378 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001379 && (rettv->v_type == VAR_FUNC
1380 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001381 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001382 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001383 if (len != -1)
1384 key[len] = prevval;
1385 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001386 {
1387 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001388 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001389 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001390 }
1391
Bram Moolenaar10c65862020-10-08 21:16:42 +02001392 if (lp->ll_valtype != NULL)
1393 // use the type of the member
1394 lp->ll_valtype = lp->ll_valtype->tt_member;
1395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001398 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001399 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001400 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001401 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001402 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001403 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001404 return NULL;
1405 }
1406
Bram Moolenaar31b81602019-02-10 22:14:27 +01001407 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001411 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001412 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 }
1415 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001417 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001419 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 p = NULL;
1422 break;
1423 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001424 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001425 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001426 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1427 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001428 {
1429 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001430 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001431 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001432
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001433 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001436 else if (lp->ll_tv->v_type == VAR_BLOB)
1437 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001438 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1439
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001440 /*
1441 * Get the number and item for the only or first index of the List.
1442 */
1443 if (empty1)
1444 lp->ll_n1 = 0;
1445 else
1446 // is number or string
1447 lp->ll_n1 = (long)tv_get_number(&var1);
1448 clear_tv(&var1);
1449
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001450 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001451 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001452 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 return NULL;
1454 }
1455 if (lp->ll_range && !lp->ll_empty2)
1456 {
1457 lp->ll_n2 = (long)tv_get_number(&var2);
1458 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001459 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1460 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001461 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001462 }
1463 lp->ll_blob = lp->ll_tv->vval.v_blob;
1464 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001465 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001467 else
1468 {
1469 /*
1470 * Get the number and item for the only or first index of the List.
1471 */
1472 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001474 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001475 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001476 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001477 clear_tv(&var1);
1478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001481 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1482 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001483 if (lp->ll_li == NULL)
1484 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001485 clear_tv(&var2);
1486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001487 }
1488
Bram Moolenaar10c65862020-10-08 21:16:42 +02001489 if (lp->ll_valtype != NULL)
1490 // use the type of the member
1491 lp->ll_valtype = lp->ll_valtype->tt_member;
1492
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 /*
1494 * May need to find the item or absolute index for the second
1495 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 * When no index given: "lp->ll_empty2" is TRUE.
1497 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001501 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001502 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001504 if (check_range_index_two(lp->ll_list,
1505 &lp->ll_n1, lp->ll_li,
1506 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001508 }
1509
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 }
1513
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001514 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001516 return p;
1517}
1518
1519/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524{
1525 vim_free(lp->ll_exp_name);
1526 vim_free(lp->ll_newkey);
1527}
1528
1529/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001530 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001531 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1533 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001534 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536set_var_lval(
1537 lval_T *lp,
1538 char_u *endp,
1539 typval_T *rettv,
1540 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001541 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1542 char_u *op,
1543 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001544{
1545 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001546 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001547
1548 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001550 cc = *endp;
1551 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001552 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1553 return;
1554
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001555 if (lp->ll_blob != NULL)
1556 {
1557 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001558
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001559 if (op != NULL && *op != '=')
1560 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001561 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001562 return;
1563 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001564 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001565 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001566
1567 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1568 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001569 if (lp->ll_empty2)
1570 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1571
Bram Moolenaar68452172021-04-12 21:21:02 +02001572 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1573 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001575 }
1576 else
1577 {
1578 val = (int)tv_get_number_chk(rettv, &error);
1579 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001580 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001581 }
1582 }
1583 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001585 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001587 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1588 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001589 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001590 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001591 *endp = cc;
1592 return;
1593 }
1594
Bram Moolenaarff697e62019-02-12 22:28:33 +01001595 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001596 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001597 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001598 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001599 {
1600 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001601 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1602 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001603 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001604 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001605 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001606 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001609 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001610 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001611 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1612 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001613 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001614 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001615 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001616 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001617 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001619 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001620 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001621 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001622 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001623 else if (lp->ll_range)
1624 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001625 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1626 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001627 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001628 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001629 return;
1630 }
1631
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001632 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1633 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001634 }
1635 else
1636 {
1637 /*
1638 * Assign to a List or Dictionary item.
1639 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001640 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1641 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001642 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001643 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001644 return;
1645 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001646
1647 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001648 && check_typval_arg_type(lp->ll_valtype, rettv,
1649 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001650 return;
1651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001652 if (lp->ll_newkey != NULL)
1653 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 if (op != NULL && *op != '=')
1655 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001656 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 return;
1658 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001659 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1660 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001661 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001663 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001664 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001665 if (di == NULL)
1666 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001667 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1668 {
1669 vim_free(di);
1670 return;
1671 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001672 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001673 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 else if (op != NULL && *op != '=')
1675 {
1676 tv_op(lp->ll_tv, rettv, op);
1677 return;
1678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001680 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001681
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001682 /*
1683 * Assign the value to the variable or list item.
1684 */
1685 if (copy)
1686 copy_tv(rettv, lp->ll_tv);
1687 else
1688 {
1689 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001690 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692 }
1693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694}
1695
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001697 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1698 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 * Returns OK or FAIL.
1700 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001704 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001705 char_u numbuf[NUMBUFLEN];
1706 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001707 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001709 // Can't do anything with a Funcref or Dict on the right.
1710 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001711 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001712 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1713 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714 {
1715 switch (tv1->v_type)
1716 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001717 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001718 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 case VAR_DICT:
1721 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001722 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001723 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001724 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001725 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001726 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001727 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 break;
1729
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 case VAR_BLOB:
1731 if (*op != '+' || tv2->v_type != VAR_BLOB)
1732 break;
1733 // BLOB += BLOB
1734 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1735 {
1736 blob_T *b1 = tv1->vval.v_blob;
1737 blob_T *b2 = tv2->vval.v_blob;
1738 int i, len = blob_len(b2);
1739 for (i = 0; i < len; i++)
1740 ga_append(&b1->bv_ga, blob_get(b2, i));
1741 }
1742 return OK;
1743
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744 case VAR_LIST:
1745 if (*op != '+' || tv2->v_type != VAR_LIST)
1746 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001747 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001748 if (tv2->vval.v_list != NULL)
1749 {
1750 if (tv1->vval.v_list == NULL)
1751 {
1752 tv1->vval.v_list = tv2->vval.v_list;
1753 ++tv1->vval.v_list->lv_refcount;
1754 }
1755 else
1756 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 return OK;
1759
1760 case VAR_NUMBER:
1761 case VAR_STRING:
1762 if (tv2->v_type == VAR_LIST)
1763 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001764 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001766 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001767 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001768 if (tv2->v_type == VAR_FLOAT)
1769 {
1770 float_T f = n;
1771
Bram Moolenaarff697e62019-02-12 22:28:33 +01001772 if (*op == '%')
1773 break;
1774 switch (*op)
1775 {
1776 case '+': f += tv2->vval.v_float; break;
1777 case '-': f -= tv2->vval.v_float; break;
1778 case '*': f *= tv2->vval.v_float; break;
1779 case '/': f /= tv2->vval.v_float; break;
1780 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001781 clear_tv(tv1);
1782 tv1->v_type = VAR_FLOAT;
1783 tv1->vval.v_float = f;
1784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001786 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001787 switch (*op)
1788 {
1789 case '+': n += tv_get_number(tv2); break;
1790 case '-': n -= tv_get_number(tv2); break;
1791 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001792 case '/': n = num_divide(n, tv_get_number(tv2),
1793 &failed); break;
1794 case '%': n = num_modulus(n, tv_get_number(tv2),
1795 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001797 clear_tv(tv1);
1798 tv1->v_type = VAR_NUMBER;
1799 tv1->vval.v_number = n;
1800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 }
1802 else
1803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001804 if (tv2->v_type == VAR_FLOAT)
1805 break;
1806
Bram Moolenaarff697e62019-02-12 22:28:33 +01001807 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001808 s = tv_get_string(tv1);
1809 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001810 clear_tv(tv1);
1811 tv1->v_type = VAR_STRING;
1812 tv1->vval.v_string = s;
1813 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001814 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001815
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001816 case VAR_FLOAT:
1817 {
1818 float_T f;
1819
Bram Moolenaarff697e62019-02-12 22:28:33 +01001820 if (*op == '%' || *op == '.'
1821 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001822 && tv2->v_type != VAR_NUMBER
1823 && tv2->v_type != VAR_STRING))
1824 break;
1825 if (tv2->v_type == VAR_FLOAT)
1826 f = tv2->vval.v_float;
1827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001828 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001829 switch (*op)
1830 {
1831 case '+': tv1->vval.v_float += f; break;
1832 case '-': tv1->vval.v_float -= f; break;
1833 case '*': tv1->vval.v_float *= f; break;
1834 case '/': tv1->vval.v_float /= f; break;
1835 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001836 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001837 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001838 }
1839 }
1840
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001841 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 return FAIL;
1843}
1844
1845/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 * Evaluate the expression used in a ":for var in expr" command.
1847 * "arg" points to "var".
1848 * Set "*errp" to TRUE for an error, FALSE otherwise;
1849 * Return a pointer that holds the info. Null when there is an error.
1850 */
1851 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852eval_for_line(
1853 char_u *arg,
1854 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001855 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001856 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857{
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001859 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 typval_T tv;
1862 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001863 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001867 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 if (fi == NULL)
1869 return NULL;
1870
Bram Moolenaar404557e2021-07-05 21:41:48 +02001871 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1872 &fi->fi_semicolon, FALSE);
1873 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 return fi;
1875
Bram Moolenaar404557e2021-07-05 21:41:48 +02001876 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001877 if (expr[0] != 'i' || expr[1] != 'n'
1878 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001880 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1881 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1882 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001883 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return fi;
1885 }
1886
1887 if (skip)
1888 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001889 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1890 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 {
1892 *errp = FALSE;
1893 if (!skip)
1894 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001895 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001896 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001897 l = tv.vval.v_list;
1898 if (l == NULL)
1899 {
1900 // a null list is like an empty list: do nothing
1901 clear_tv(&tv);
1902 }
1903 else
1904 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001906 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001908 // No need to increment the refcount, it's already set for
1909 // the list being used in "tv".
1910 fi->fi_list = l;
1911 list_add_watch(l, &fi->fi_lw);
1912 fi->fi_lw.lw_item = l->lv_first;
1913 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001914 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001915 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001916 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001917 fi->fi_bi = 0;
1918 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001919 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001920 typval_T btv;
1921
1922 // Make a copy, so that the iteration still works when the
1923 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001925 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001926 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001927 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001928 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001929 else if (tv.v_type == VAR_STRING)
1930 {
1931 fi->fi_byte_idx = 0;
1932 fi->fi_string = tv.vval.v_string;
1933 tv.vval.v_string = NULL;
1934 if (fi->fi_string == NULL)
1935 fi->fi_string = vim_strsave((char_u *)"");
1936 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 else
1938 {
Sean Dewar80d73952021-08-04 19:25:54 +02001939 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 }
1942 }
1943 }
1944 if (skip)
1945 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001946 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947
1948 return fi;
1949}
1950
1951/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001952 * Used when looping over a :for line, skip the "in expr" part.
1953 */
1954 void
1955skip_for_lines(void *fi_void, evalarg_T *evalarg)
1956{
1957 forinfo_T *fi = (forinfo_T *)fi_void;
1958 int i;
1959
1960 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001961 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001962}
1963
1964/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 * Use the first item in a ":for" list. Advance to the next.
1966 * Assign the values to the variable (list). "arg" points to the first one.
1967 * Return TRUE when a valid item was found, FALSE when at end of list or
1968 * something wrong.
1969 */
1970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001973 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001975 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001976 ? (ASSIGN_FINAL
1977 // first round: error if variable exists
1978 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001979 | ASSIGN_NO_MEMBER_TYPE
1980 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001981 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001983 int skip_assign = in_vim9script() && arg[0] == '_'
1984 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001986 if (fi->fi_blob != NULL)
1987 {
1988 typval_T tv;
1989
1990 if (fi->fi_bi >= blob_len(fi->fi_blob))
1991 return FALSE;
1992 tv.v_type = VAR_NUMBER;
1993 tv.v_lock = VAR_FIXED;
1994 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1995 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001996 if (skip_assign)
1997 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001998 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001999 fi->fi_varcount, flag, NULL) == OK;
2000 }
2001
2002 if (fi->fi_string != NULL)
2003 {
2004 typval_T tv;
2005 int len;
2006
2007 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2008 if (len == 0)
2009 return FALSE;
2010 tv.v_type = VAR_STRING;
2011 tv.v_lock = VAR_FIXED;
2012 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2013 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002014 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002015 if (skip_assign)
2016 result = TRUE;
2017 else
2018 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002019 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002020 vim_free(tv.vval.v_string);
2021 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002022 }
2023
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 item = fi->fi_lw.lw_item;
2025 if (item == NULL)
2026 result = FALSE;
2027 else
2028 {
2029 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002030 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002031 if (skip_assign)
2032 result = TRUE;
2033 else
2034 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002035 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 }
2037 return result;
2038}
2039
2040/*
2041 * Free the structure used to store info used by ":for".
2042 */
2043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002044free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045{
Bram Moolenaar33570922005-01-25 22:26:29 +00002046 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002048 if (fi == NULL)
2049 return;
2050 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002051 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002053 list_unref(fi->fi_list);
2054 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002055 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002056 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002057 else
2058 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 vim_free(fi);
2060}
2061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002063set_context_for_expression(
2064 expand_T *xp,
2065 char_u *arg,
2066 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002068 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002072 if (cmdidx == CMD_let || cmdidx == CMD_var
2073 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074 {
2075 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002078 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002079 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080 {
2081 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002082 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002083 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 break;
2085 }
2086 return;
2087 }
2088 }
2089 else
2090 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2091 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 while ((xp->xp_pattern = vim_strpbrk(arg,
2093 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2094 {
2095 c = *xp->xp_pattern;
2096 if (c == '&')
2097 {
2098 c = xp->xp_pattern[1];
2099 if (c == '&')
2100 {
2101 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002102 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002107 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2108 xp->xp_pattern += 2;
2109
2110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
2112 else if (c == '$')
2113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002114 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 xp->xp_context = EXPAND_ENV_VARS;
2116 }
2117 else if (c == '=')
2118 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002119 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 xp->xp_context = EXPAND_EXPRESSION;
2121 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002122 else if (c == '#'
2123 && xp->xp_context == EXPAND_EXPRESSION)
2124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002126 break;
2127 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002128 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 && xp->xp_context == EXPAND_FUNCTIONS
2130 && vim_strchr(xp->xp_pattern, '(') == NULL)
2131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 break;
2134 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002135 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002137 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2140 if (c == '\\' && xp->xp_pattern[1] != NUL)
2141 ++xp->xp_pattern;
2142 xp->xp_context = EXPAND_NOTHING;
2143 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002146 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2148 /* skip */ ;
2149 xp->xp_context = EXPAND_NOTHING;
2150 }
2151 else if (c == '|')
2152 {
2153 if (xp->xp_pattern[1] == '|')
2154 {
2155 ++xp->xp_pattern;
2156 xp->xp_context = EXPAND_EXPRESSION;
2157 }
2158 else
2159 xp->xp_context = EXPAND_COMMANDS;
2160 }
2161 else
2162 xp->xp_context = EXPAND_EXPRESSION;
2163 }
2164 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002165 // Doesn't look like something valid, expand as an expression
2166 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 arg = xp->xp_pattern;
2169 if (*arg != NUL)
2170 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2171 /* skip */ ;
2172 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002173
2174 // ":exe one two" completes "two"
2175 if ((cmdidx == CMD_execute
2176 || cmdidx == CMD_echo
2177 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002178 || cmdidx == CMD_echomsg
2179 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002180 && xp->xp_context == EXPAND_EXPRESSION)
2181 {
2182 for (;;)
2183 {
2184 char_u *n = skiptowhite(arg);
2185
2186 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2187 break;
2188 arg = skipwhite(n);
2189 }
2190 }
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 xp->xp_pattern = arg;
2193}
2194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002196 * Return TRUE if "pat" matches "text".
2197 * Does not use 'cpo' and always uses 'magic'.
2198 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002199 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002200pattern_match(char_u *pat, char_u *text, int ic)
2201{
2202 int matches = FALSE;
2203 char_u *save_cpo;
2204 regmatch_T regmatch;
2205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002206 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002207 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002208 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002209 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2210 if (regmatch.regprog != NULL)
2211 {
2212 regmatch.rm_ic = ic;
2213 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2214 vim_regfree(regmatch.regprog);
2215 }
2216 p_cpo = save_cpo;
2217 return matches;
2218}
2219
2220/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002221 * Handle a name followed by "(". Both for just "name(arg)" and for
2222 * "expr->name(arg)".
2223 * Returns OK or FAIL.
2224 */
2225 static int
2226eval_func(
2227 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002228 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002229 char_u *name,
2230 int name_len,
2231 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002232 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002233 typval_T *basetv) // "expr" for "expr->name(arg)"
2234{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002235 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002236 char_u *s = name;
2237 int len = name_len;
2238 partial_T *partial;
2239 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002240 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002241 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002242
2243 if (!evaluate)
2244 check_vars(s, len);
2245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002246 // If "s" is the name of a variable of type VAR_FUNC
2247 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002248 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002249 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002251 // Need to make a copy, in case evaluating the arguments makes
2252 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002253 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002254 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002255 ret = FAIL;
2256 else
2257 {
2258 funcexe_T funcexe;
2259
2260 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002261 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002262 funcexe.fe_firstline = curwin->w_cursor.lnum;
2263 funcexe.fe_lastline = curwin->w_cursor.lnum;
2264 funcexe.fe_evaluate = evaluate;
2265 funcexe.fe_partial = partial;
2266 funcexe.fe_basetv = basetv;
2267 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002268 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002269 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002270 }
2271 vim_free(s);
2272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002273 // If evaluate is FALSE rettv->v_type was not set in
2274 // get_func_tv, but it's needed in handle_subscript() to parse
2275 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002276 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2277 {
2278 rettv->vval.v_string = NULL;
2279 rettv->v_type = VAR_FUNC;
2280 }
2281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002282 // Stop the expression evaluation when immediately
2283 // aborting on error, or when an interrupt occurred or
2284 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002285 if (evaluate && aborting())
2286 {
2287 if (ret == OK)
2288 clear_tv(rettv);
2289 ret = FAIL;
2290 }
2291 return ret;
2292}
2293
2294/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002295 * After a NL, skip over empty lines and comment-only lines.
2296 */
2297 static char_u *
2298newline_skip_comments(char_u *arg)
2299{
2300 char_u *p = arg + 1;
2301
2302 for (;;)
2303 {
2304 p = skipwhite(p);
2305
2306 if (*p == NUL)
2307 break;
2308 if (vim9_comment_start(p))
2309 {
2310 char_u *nl = vim_strchr(p, NL);
2311
2312 if (nl == NULL)
2313 break;
2314 p = nl;
2315 }
2316 if (*p != NL)
2317 break;
2318 ++p; // skip another NL
2319 }
2320 return p;
2321}
2322
2323/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002324 * Get the next line source line without advancing. But do skip over comment
2325 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002326 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002327 */
2328 static char_u *
2329getline_peek_skip_comments(evalarg_T *evalarg)
2330{
2331 for (;;)
2332 {
2333 char_u *next = getline_peek(evalarg->eval_getline,
2334 evalarg->eval_cookie);
2335 char_u *p;
2336
2337 if (next == NULL)
2338 break;
2339 p = skipwhite(next);
2340 if (*p != NUL && !vim9_comment_start(p))
2341 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002342 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002343 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002344 }
2345 return NULL;
2346}
2347
2348/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002349 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2350 * comment) and there is a next line, return the next line (skipping blanks)
2351 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002352 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2353 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002354 * "arg" must point somewhere inside a line, not at the start.
2355 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002356 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002357eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2358{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002359 char_u *p = skipwhite(arg);
2360
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002361 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002362 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002363 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002364 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2365 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002366 && (*p == NUL || *p == NL
2367 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002368 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002369 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002370
Bram Moolenaare442d592022-05-05 12:20:28 +01002371 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002372 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002373 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002374 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002375 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002376 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002377
Bram Moolenaar9d489562020-07-30 20:08:50 +02002378 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002379 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002380 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002381 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002382 }
2383 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002384 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385}
2386
2387/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002388 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002389 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002390 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002391 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002392eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002393{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002394 garray_T *gap = &evalarg->eval_ga;
2395 char_u *line;
2396
Bram Moolenaar39be4982022-05-06 21:51:50 +01002397 if (arg != NULL)
2398 {
2399 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002400 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002401 // Truncate before a trailing comment, so that concatenating the lines
2402 // won't turn the rest into a comment.
2403 if (*skipwhite(arg) == '#')
2404 *arg = NUL;
2405 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002406
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002407 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002408 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2409 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002410 else
2411 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002412 if (line == NULL)
2413 return NULL;
2414
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002415 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002416 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2417 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002418 char_u *p = skipwhite(line);
2419
2420 // Going to concatenate the lines after parsing. For an empty or
2421 // comment line use an empty string.
2422 if (*p == NUL || vim9_comment_start(p))
2423 {
2424 vim_free(line);
2425 line = vim_strsave((char_u *)"");
2426 }
2427
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002428 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2429 ++gap->ga_len;
2430 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002431 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002432 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002433 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002434 evalarg->eval_tofree = line;
2435 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002436
2437 // Advanced to the next line, "arg" no longer points into the previous
2438 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002439 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002440 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002441}
2442
Bram Moolenaare6b53242020-07-01 17:28:33 +02002443/*
2444 * Call eval_next_non_blank() and get the next line if needed.
2445 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002446 char_u *
2447skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2448{
2449 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002450 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002451
Bram Moolenaar962d7212020-07-04 14:15:00 +02002452 if (evalarg == NULL)
2453 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002454 eval_next_non_blank(p, evalarg, &getnext);
2455 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002456 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002457 return p;
2458}
2459
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002460/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002461 * The "eval" functions have an "evalarg" argument: When NULL or
2462 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2463 * parsed but not executed. The functions may return OK, but the rettv will be
2464 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
2466
2467/*
2468 * Handle zero level expression.
2469 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002471 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 * Return OK or FAIL.
2474 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002476eval0(
2477 char_u *arg,
2478 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002479 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002482 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2483}
2484
2485/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002486 * If "arg" is a simple function call without arguments then call it and return
2487 * the result. Otherwise return NOTDONE.
2488 */
2489 int
2490may_call_simple_func(
2491 char_u *arg,
2492 typval_T *rettv)
2493{
2494 char_u *parens = (char_u *)strstr((char *)arg, "()");
2495 int r = NOTDONE;
2496
2497 // If the expression is "FuncName()" then we can skip a lot of overhead.
2498 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2499 {
2500 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2501
2502 if (to_name_end(p, TRUE) == parens)
2503 r = call_simple_func(arg, (int)(parens - arg), rettv);
2504 }
2505 return r;
2506}
2507
2508/*
2509 * Handle zero level expression with optimization for a simple function call.
2510 * Same arguments and return value as eval0().
2511 */
2512 int
2513eval0_simple_funccal(
2514 char_u *arg,
2515 typval_T *rettv,
2516 exarg_T *eap,
2517 evalarg_T *evalarg)
2518{
2519 int r = may_call_simple_func(arg, rettv);
2520
2521 if (r == NOTDONE)
2522 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2523 return r;
2524}
2525
2526/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002527 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2528 * expression and don't check what comes after the expression.
2529 */
2530 int
2531eval0_retarg(
2532 char_u *arg,
2533 typval_T *rettv,
2534 exarg_T *eap,
2535 evalarg_T *evalarg,
2536 char_u **retarg)
2537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 int ret;
2539 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002540 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002541 int did_emsg_before = did_emsg;
2542 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002544 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002545 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002548 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002549
Bram Moolenaar79481362022-06-27 20:15:10 +01002550 if (ret != FAIL)
2551 {
2552 expr_end = p;
2553 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002554
Bram Moolenaar79481362022-06-27 20:15:10 +01002555 // In Vim9 script a command block is not split at NL characters for
2556 // commands using an expression argument. Skip over a '#' comment to
2557 // check for a following NL. Require white space before the '#'.
2558 if (in_vim9script() && p > expr_end && retarg == NULL)
2559 while (*p == '#')
2560 {
2561 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002562
Bram Moolenaar79481362022-06-27 20:15:10 +01002563 if (nl == NULL)
2564 break;
2565 p = skipwhite(nl + 1);
2566 if (eap != NULL && *p != NUL)
2567 eap->nextcmd = p;
2568 check_for_end = FALSE;
2569 }
2570
2571 if (check_for_end)
2572 end_error = !ends_excmd2(arg, p);
2573 }
2574
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002575 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 /*
2580 * Report the invalid expression unless the expression evaluation has
2581 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002582 * exception, or we already gave a more specific error.
2583 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002585 if (!aborting()
2586 && did_emsg == did_emsg_before
2587 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002588 && (flags & EVAL_CONSTANT) == 0
2589 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002590 {
2591 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002592 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002593 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002594 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002595 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002596
2597 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002598 // a next command to avoid more errors, unless "|" is following, which
2599 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002600 if (eap != NULL && p != NULL
2601 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002602 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002605
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002606 if (retarg != NULL)
2607 *retarg = p;
2608 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002609 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return ret;
2612}
2613
2614/*
2615 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002616 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002617 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 *
2619 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002620 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002622 * Note: "rettv.v_lock" is not set.
2623 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 * Return OK or FAIL.
2625 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002626 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002629 char_u *p;
2630 int getnext;
2631
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002632 CLEAR_POINTER(rettv);
2633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 /*
2635 * Get the first variable.
2636 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002637 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return FAIL;
2639
Bram Moolenaar793648f2020-06-26 21:28:25 +02002640 p = eval_next_non_blank(*arg, evalarg, &getnext);
2641 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002643 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 int result;
2645 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002646 evalarg_T *evalarg_used = evalarg;
2647 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002649 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002650 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651
2652 if (evalarg == NULL)
2653 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002654 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 orig_flags = evalarg_used->eval_flags;
2658 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002661 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002662 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002663 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002664 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002665 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002666 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002667 clear_tv(rettv);
2668 return FAIL;
2669 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002670 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002671 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 int error = FALSE;
2677
Bram Moolenaar13106602020-10-04 16:06:05 +02002678 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002679 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002680 else if (vim9script)
2681 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002682 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002684 if (error || !op_falsy || !result)
2685 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (error)
2687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002691 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002693 if (op_falsy)
2694 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002695 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002696 {
mityu4ac198c2021-05-28 17:52:40 +02002697 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002698 clear_tv(rettv);
2699 return FAIL;
2700 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002701 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002702 evalarg_used->eval_flags = (op_falsy ? !result : result)
2703 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2704 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002705 {
2706 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002708 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002709 if (!op_falsy || !result)
2710 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002712 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002714 /*
2715 * Check for the ":".
2716 */
2717 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2718 if (*p != ':')
2719 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002720 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002721 if (evaluate && result)
2722 clear_tv(rettv);
2723 evalarg_used->eval_flags = orig_flags;
2724 return FAIL;
2725 }
2726 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002727 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002728 else
2729 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002730 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002731 {
2732 error_white_both(p, 1);
2733 clear_tv(rettv);
2734 evalarg_used->eval_flags = orig_flags;
2735 return FAIL;
2736 }
2737 *arg = p;
2738 }
2739
2740 /*
2741 * Get the third variable. Recursive!
2742 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002743 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002744 {
mityu4ac198c2021-05-28 17:52:40 +02002745 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002746 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002747 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002748 return FAIL;
2749 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002750 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2751 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002753 if (eval1(arg, &var2, evalarg_used) == FAIL)
2754 {
2755 if (evaluate && result)
2756 clear_tv(rettv);
2757 evalarg_used->eval_flags = orig_flags;
2758 return FAIL;
2759 }
2760 if (evaluate && !result)
2761 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002763
2764 if (evalarg == NULL)
2765 clear_evalarg(&local_evalarg, NULL);
2766 else
2767 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769
2770 return OK;
2771}
2772
2773/*
2774 * Handle first level expression:
2775 * expr2 || expr2 || expr2 logical OR
2776 *
2777 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002778 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 *
2780 * Return OK or FAIL.
2781 */
2782 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002783eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002785 char_u *p;
2786 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
2788 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002789 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002791 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 return FAIL;
2793
2794 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002795 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002797 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002798 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002800 evalarg_T *evalarg_used = evalarg;
2801 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 int evaluate;
2803 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002804 long result = FALSE;
2805 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002806 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002807 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002808
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002809 if (evalarg == NULL)
2810 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002811 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002812 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002813 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 orig_flags = evalarg_used->eval_flags;
2815 evaluate = orig_flags & EVAL_EVALUATE;
2816 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002818 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002819 result = tv_get_bool_chk(rettv, &error);
2820 else if (tv_get_number_chk(rettv, &error) != 0)
2821 result = TRUE;
2822 clear_tv(rettv);
2823 if (error)
2824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
2827 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002828 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002830 while (p[0] == '|' && p[1] == '|')
2831 {
2832 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002833 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002834 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002835 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002836 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002837 {
2838 error_white_both(p, 2);
2839 clear_tv(rettv);
2840 return FAIL;
2841 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002842 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002843 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002844
2845 /*
2846 * Get the second variable.
2847 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002848 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002849 {
mityu4ac198c2021-05-28 17:52:40 +02002850 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002851 clear_tv(rettv);
2852 return FAIL;
2853 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002854 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2855 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002857 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002859
2860 /*
2861 * Compute the result.
2862 */
2863 if (evaluate && !result)
2864 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002865 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002866 result = tv_get_bool_chk(&var2, &error);
2867 else if (tv_get_number_chk(&var2, &error) != 0)
2868 result = TRUE;
2869 clear_tv(&var2);
2870 if (error)
2871 return FAIL;
2872 }
2873 if (evaluate)
2874 {
2875 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002876 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002877 rettv->v_type = VAR_BOOL;
2878 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002879 }
2880 else
2881 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002882 rettv->v_type = VAR_NUMBER;
2883 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002884 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002885 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002886
2887 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002889
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002890 if (evalarg == NULL)
2891 clear_evalarg(&local_evalarg, NULL);
2892 else
2893 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895
2896 return OK;
2897}
2898
2899/*
2900 * Handle second level expression:
2901 * expr3 && expr3 && expr3 logical AND
2902 *
2903 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002904 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 *
2906 * Return OK or FAIL.
2907 */
2908 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002911 char_u *p;
2912 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002915 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return FAIL;
2919
2920 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002921 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002923 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002924 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002926 evalarg_T *evalarg_used = evalarg;
2927 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002928 int orig_flags;
2929 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002930 long result = TRUE;
2931 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002932 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002933 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002934
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935 if (evalarg == NULL)
2936 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002937 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002938 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002939 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002940 orig_flags = evalarg_used->eval_flags;
2941 evaluate = orig_flags & EVAL_EVALUATE;
2942 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002943 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002944 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002945 result = tv_get_bool_chk(rettv, &error);
2946 else if (tv_get_number_chk(rettv, &error) == 0)
2947 result = FALSE;
2948 clear_tv(rettv);
2949 if (error)
2950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952
2953 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002954 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002956 while (p[0] == '&' && p[1] == '&')
2957 {
2958 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002959 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002960 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002961 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002962 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002963 {
2964 error_white_both(p, 2);
2965 clear_tv(rettv);
2966 return FAIL;
2967 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002968 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002969 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002970
2971 /*
2972 * Get the second variable.
2973 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002974 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002975 {
mityu4ac198c2021-05-28 17:52:40 +02002976 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002977 clear_tv(rettv);
2978 return FAIL;
2979 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002980 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2981 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002983 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002984 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002985 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002986
2987 /*
2988 * Compute the result.
2989 */
2990 if (evaluate && result)
2991 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002992 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002993 result = tv_get_bool_chk(&var2, &error);
2994 else if (tv_get_number_chk(&var2, &error) == 0)
2995 result = FALSE;
2996 clear_tv(&var2);
2997 if (error)
2998 return FAIL;
2999 }
3000 if (evaluate)
3001 {
3002 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003003 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003004 rettv->v_type = VAR_BOOL;
3005 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003006 }
3007 else
3008 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003009 rettv->v_type = VAR_NUMBER;
3010 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003011 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003012 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003013
3014 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003016
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003017 if (evalarg == NULL)
3018 clear_evalarg(&local_evalarg, NULL);
3019 else
3020 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022
3023 return OK;
3024}
3025
3026/*
3027 * Handle third level expression:
3028 * var1 == var2
3029 * var1 =~ var2
3030 * var1 != var2
3031 * var1 !~ var2
3032 * var1 > var2
3033 * var1 >= var2
3034 * var1 < var2
3035 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003036 * var1 is var2
3037 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 *
3039 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003040 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 *
3042 * Return OK or FAIL.
3043 */
3044 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003045eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003048 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003049 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003051 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003054 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 return FAIL;
3058
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003059 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003060
Bram Moolenaar696ba232020-07-29 21:20:41 +02003061 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003066 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003068 typval_T var2;
3069 int ic;
3070 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003071 int evaluate = evalarg == NULL
3072 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003073 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003074
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003075 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003076 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003077 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003078 p = *arg;
3079 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003080 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3081 {
mityu4ac198c2021-05-28 17:52:40 +02003082 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003083 clear_tv(rettv);
3084 return FAIL;
3085 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003086
Bram Moolenaar696ba232020-07-29 21:20:41 +02003087 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3088 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003089 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003090 clear_tv(rettv);
3091 return FAIL;
3092 }
3093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003094 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (p[len] == '?')
3096 {
3097 ic = TRUE;
3098 ++len;
3099 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003100 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else if (p[len] == '#')
3102 {
3103 ic = FALSE;
3104 ++len;
3105 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003106 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003108 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 /*
3111 * Get the second variable.
3112 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003113 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3114 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003115 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003116 clear_tv(rettv);
3117 return FAIL;
3118 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003119 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return FAIL;
3124 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003125 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003126 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003127 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003128
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003129 // use the line of the comparison for messages
3130 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003131 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003132 {
3133 ret = FAIL;
3134 clear_tv(rettv);
3135 }
3136 else
3137 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003138 clear_tv(&var2);
3139 return ret;
3140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142
3143 return OK;
3144}
3145
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003146/*
3147 * Make a copy of blob "tv1" and append blob "tv2".
3148 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 void
3150eval_addblob(typval_T *tv1, typval_T *tv2)
3151{
3152 blob_T *b1 = tv1->vval.v_blob;
3153 blob_T *b2 = tv2->vval.v_blob;
3154 blob_T *b = blob_alloc();
3155 int i;
3156
3157 if (b != NULL)
3158 {
3159 for (i = 0; i < blob_len(b1); i++)
3160 ga_append(&b->bv_ga, blob_get(b1, i));
3161 for (i = 0; i < blob_len(b2); i++)
3162 ga_append(&b->bv_ga, blob_get(b2, i));
3163
3164 clear_tv(tv1);
3165 rettv_blob_set(tv1, b);
3166 }
3167}
3168
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003169/*
3170 * Make a copy of list "tv1" and append list "tv2".
3171 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 int
3173eval_addlist(typval_T *tv1, typval_T *tv2)
3174{
3175 typval_T var3;
3176
3177 // concatenate Lists
3178 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3179 {
3180 clear_tv(tv1);
3181 clear_tv(tv2);
3182 return FAIL;
3183 }
3184 clear_tv(tv1);
3185 *tv1 = var3;
3186 return OK;
3187}
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003190 * Handle the bitwise left/right shift operator expression:
3191 * var1 << var2
3192 * var1 >> var2
3193 *
3194 * "arg" must point to the first non-white of the expression.
3195 * "arg" is advanced to just after the recognized expression.
3196 *
3197 * Return OK or FAIL.
3198 */
3199 static int
3200eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3201{
3202 /*
3203 * Get the first expression.
3204 */
3205 if (eval6(arg, rettv, evalarg) == FAIL)
3206 return FAIL;
3207
3208 /*
3209 * Repeat computing, until no '<<' or '>>' is following.
3210 */
3211 for (;;)
3212 {
3213 char_u *p;
3214 int getnext;
3215 exprtype_T type;
3216 int evaluate;
3217 typval_T var2;
3218 int vim9script;
3219
3220 p = eval_next_non_blank(*arg, evalarg, &getnext);
3221 if (p[0] == '<' && p[1] == '<')
3222 type = EXPR_LSHIFT;
3223 else if (p[0] == '>' && p[1] == '>')
3224 type = EXPR_RSHIFT;
3225 else
3226 return OK;
3227
3228 // Handle a bitwise left or right shift operator
3229 if (rettv->v_type != VAR_NUMBER)
3230 {
3231 // left operand should be a number
3232 emsg(_(e_bitshift_ops_must_be_number));
3233 clear_tv(rettv);
3234 return FAIL;
3235 }
3236
3237 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3238 vim9script = in_vim9script();
3239 if (getnext)
3240 {
3241 *arg = eval_next_line(*arg, evalarg);
3242 p = *arg;
3243 }
3244 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3245 {
3246 error_white_both(*arg, 2);
3247 clear_tv(rettv);
3248 return FAIL;
3249 }
3250
3251 /*
3252 * Get the second variable.
3253 */
3254 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3255 {
3256 error_white_both(p, 2);
3257 clear_tv(rettv);
3258 return FAIL;
3259 }
3260 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3261 if (eval6(arg, &var2, evalarg) == FAIL)
3262 {
3263 clear_tv(rettv);
3264 return FAIL;
3265 }
3266
3267 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3268 {
3269 // right operand should be a positive number
3270 if (var2.v_type != VAR_NUMBER)
3271 emsg(_(e_bitshift_ops_must_be_number));
3272 else
dundargocc57b5bc2022-11-02 13:30:51 +00003273 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003274 clear_tv(rettv);
3275 clear_tv(&var2);
3276 return FAIL;
3277 }
3278
3279 if (evaluate)
3280 {
3281 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3282 // shifting more bits than we have always results in zero
3283 rettv->vval.v_number = 0;
3284 else if (type == EXPR_LSHIFT)
3285 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003286 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003287 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003288 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003289 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003290 }
3291
3292 clear_tv(&var2);
3293 }
3294
3295 return OK;
3296}
3297
3298/*
3299 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003300 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003302 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003303 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 *
3305 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003306 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 *
3308 * Return OK or FAIL.
3309 */
3310 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003311eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003314 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003316 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return FAIL;
3318
3319 /*
3320 * Repeat computing, until no '+', '-' or '.' is following.
3321 */
3322 for (;;)
3323 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003324 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003325 int getnext;
3326 char_u *p;
3327 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003328 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003329 int concat;
3330 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003331 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003332
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003333 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003334 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003335 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336 p = eval_next_non_blank(*arg, evalarg, &getnext);
3337 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003338 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003339 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3340 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003342 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3343 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003344
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003345 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003346 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003347 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003348 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003349 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003350 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003351 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003352 {
mityu4ac198c2021-05-28 17:52:40 +02003353 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003354 clear_tv(rettv);
3355 return FAIL;
3356 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003357 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003358 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003359 if ((op != '+' || (rettv->v_type != VAR_LIST
3360 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003362 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003364 int error = FALSE;
3365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 // For "list + ...", an illegal use of the first operand as
3367 // a number cannot be determined before evaluating the 2nd
3368 // operand: if this is also a list, all is ok.
3369 // For "something . ...", "something - ..." or "non-list + ...",
3370 // we know that the first operand needs to be a string or number
3371 // without evaluating the 2nd operand. So check before to avoid
3372 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003373 if (op != '.')
3374 tv_get_number_chk(rettv, &error);
3375 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 {
3377 clear_tv(rettv);
3378 return FAIL;
3379 }
3380 }
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 /*
3383 * Get the second variable.
3384 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003385 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003386 {
mityu89dcb4d2021-05-28 13:50:17 +02003387 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003388 clear_tv(rettv);
3389 return FAIL;
3390 }
3391 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003392 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return FAIL;
3396 }
3397
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003398 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 /*
3401 * Compute the result.
3402 */
3403 if (op == '.')
3404 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003405 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3406 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003407 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003408
Bram Moolenaar2e086612020-08-25 22:37:48 +02003409 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003410 || var2.v_type == VAR_CHANNEL
3411 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003412 semsg(_(e_using_invalid_value_as_string_str),
3413 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003414 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003415 {
3416 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3417 var2.vval.v_float);
3418 s2 = buf2;
3419 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003420 else
3421 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 {
3424 clear_tv(rettv);
3425 clear_tv(&var2);
3426 return FAIL;
3427 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 clear_tv(rettv);
3430 rettv->v_type = VAR_STRING;
3431 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003433 else if (op == '+' && rettv->v_type == VAR_BLOB
3434 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003436 else if (op == '+' && rettv->v_type == VAR_LIST
3437 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003438 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003440 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 else
3443 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444 int error = FALSE;
3445 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003446 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003448 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003449 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003450 f1 = rettv->vval.v_float;
3451 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003452 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003453 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003456 if (error)
3457 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003458 // This can only happen for "list + non-list" or
3459 // "blob + non-blob". For "non-list + ..." or
3460 // "something - ...", we returned before evaluating the
3461 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003462 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003463 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003464 return FAIL;
3465 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003466 if (var2.v_type == VAR_FLOAT)
3467 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003468 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003469 if (var2.v_type == VAR_FLOAT)
3470 {
3471 f2 = var2.vval.v_float;
3472 n2 = 0;
3473 }
3474 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003476 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003477 if (error)
3478 {
3479 clear_tv(rettv);
3480 clear_tv(&var2);
3481 return FAIL;
3482 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003483 if (rettv->v_type == VAR_FLOAT)
3484 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003488 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003489 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3490 {
3491 if (op == '+')
3492 f1 = f1 + f2;
3493 else
3494 f1 = f1 - f2;
3495 rettv->v_type = VAR_FLOAT;
3496 rettv->vval.v_float = f1;
3497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003499 {
3500 if (op == '+')
3501 n1 = n1 + n2;
3502 else
3503 n1 = n1 - n2;
3504 rettv->v_type = VAR_NUMBER;
3505 rettv->vval.v_number = n1;
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003508 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 }
3511 return OK;
3512}
3513
3514/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003515 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 * * number multiplication
3517 * / number division
3518 * % number modulo
3519 *
3520 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003521 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 *
3523 * Return OK or FAIL.
3524 */
3525 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003526eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003527 char_u **arg,
3528 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003529 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003532 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003535 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003537 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return FAIL;
3539
3540 /*
3541 * Repeat computing, until no '*', '/' or '%' is following.
3542 */
3543 for (;;)
3544 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003545 int evaluate;
3546 int getnext;
3547 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003548 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003549 int op;
3550 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003551 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003552 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003553
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003554 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003555 p = eval_next_non_blank(*arg, evalarg, &getnext);
3556 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003557 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003559
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003560 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003561 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003562 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003563 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003564 {
3565 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3566 {
mityu4ac198c2021-05-28 17:52:40 +02003567 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003568 clear_tv(rettv);
3569 return FAIL;
3570 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003571 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003574 f1 = 0;
3575 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003576 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003577 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003579 if (rettv->v_type == VAR_FLOAT)
3580 {
3581 f1 = rettv->vval.v_float;
3582 use_float = TRUE;
3583 n1 = 0;
3584 }
3585 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003586 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003587 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003588 if (error)
3589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 else
3592 n1 = 0;
3593
3594 /*
3595 * Get the second variable.
3596 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003597 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3598 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003599 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003600 clear_tv(rettv);
3601 return FAIL;
3602 }
3603 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003604 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return FAIL;
3606
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003607 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003609 if (var2.v_type == VAR_FLOAT)
3610 {
3611 if (!use_float)
3612 {
3613 f1 = n1;
3614 use_float = TRUE;
3615 }
3616 f2 = var2.vval.v_float;
3617 n2 = 0;
3618 }
3619 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003621 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003622 clear_tv(&var2);
3623 if (error)
3624 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003625 if (use_float)
3626 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 /*
3630 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003631 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003633 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003635 if (op == '*')
3636 f1 = f1 * f2;
3637 else if (op == '/')
3638 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003639#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003640 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003641 if (f2 == 0.0)
3642 {
3643 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003644 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003645 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003646 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003647 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003648 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003649 }
3650 else
3651 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003652#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003653 // We rely on the floating point library to handle divide
3654 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003655 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003656#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003659 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003660 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003661 return FAIL;
3662 }
3663 rettv->v_type = VAR_FLOAT;
3664 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 else
3667 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003668 int failed = FALSE;
3669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003670 if (op == '*')
3671 n1 = n1 * n2;
3672 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003673 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003675 n1 = num_modulus(n1, n2, &failed);
3676 if (failed)
3677 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003678
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003679 rettv->v_type = VAR_NUMBER;
3680 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683 }
3684
3685 return OK;
3686}
3687
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003688/*
3689 * Handle a type cast before a base level expression.
3690 * "arg" must point to the first non-white of the expression.
3691 * "arg" is advanced to just after the recognized expression.
3692 * Return OK or FAIL.
3693 */
3694 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003695eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003696 char_u **arg,
3697 typval_T *rettv,
3698 evalarg_T *evalarg,
3699 int want_string) // after "." operator
3700{
3701 type_T *want_type = NULL;
3702 garray_T type_list; // list of pointers to allocated types
3703 int res;
3704 int evaluate = evalarg == NULL ? 0
3705 : (evalarg->eval_flags & EVAL_EVALUATE);
3706
3707 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003708 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3709 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003710 {
3711 ++*arg;
3712 ga_init2(&type_list, sizeof(type_T *), 10);
3713 want_type = parse_type(arg, &type_list, TRUE);
3714 if (want_type == NULL && (evaluate || **arg != '>'))
3715 {
3716 clear_type_list(&type_list);
3717 return FAIL;
3718 }
3719
3720 if (**arg != '>')
3721 {
3722 if (*skipwhite(*arg) == '>')
3723 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3724 else
3725 emsg(_(e_missing_gt));
3726 clear_type_list(&type_list);
3727 return FAIL;
3728 }
3729 ++*arg;
3730 *arg = skipwhite_and_linebreak(*arg, evalarg);
3731 }
3732
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003733 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003734
3735 if (want_type != NULL && evaluate)
3736 {
3737 if (res == OK)
3738 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003739 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3740 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003741
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003742 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003743 {
3744 if (want_type == &t_bool && actual != &t_bool
3745 && (actual->tt_flags & TTFLAG_BOOL_OK))
3746 {
3747 int n = tv2bool(rettv);
3748
3749 // can use "0" and "1" for boolean in some places
3750 clear_tv(rettv);
3751 rettv->v_type = VAR_BOOL;
3752 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3753 }
3754 else
3755 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003756 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003757
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003758 where.wt_variable = TRUE;
3759 res = check_type(want_type, actual, TRUE, where);
3760 }
3761 }
3762 }
3763 clear_type_list(&type_list);
3764 }
3765
3766 return res;
3767}
3768
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003769 int
3770eval_leader(char_u **arg, int vim9)
3771{
3772 char_u *s = *arg;
3773 char_u *p = *arg;
3774
3775 while (*p == '!' || *p == '-' || *p == '+')
3776 {
3777 char_u *n = skipwhite(p + 1);
3778
3779 // ++, --, -+ and +- are not accepted in Vim9 script
3780 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3781 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003782 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003783 return FAIL;
3784 }
3785 p = n;
3786 }
3787 *arg = p;
3788 return OK;
3789}
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003792 * Check for a predefined value "true", "false" and "null.*".
3793 * Return OK when recognized.
3794 */
3795 int
3796handle_predefined(char_u *s, int len, typval_T *rettv)
3797{
3798 switch (len)
3799 {
3800 case 4: if (STRNCMP(s, "true", 4) == 0)
3801 {
3802 rettv->v_type = VAR_BOOL;
3803 rettv->vval.v_number = VVAL_TRUE;
3804 return OK;
3805 }
3806 if (STRNCMP(s, "null", 4) == 0)
3807 {
3808 rettv->v_type = VAR_SPECIAL;
3809 rettv->vval.v_number = VVAL_NULL;
3810 return OK;
3811 }
3812 break;
3813 case 5: if (STRNCMP(s, "false", 5) == 0)
3814 {
3815 rettv->v_type = VAR_BOOL;
3816 rettv->vval.v_number = VVAL_FALSE;
3817 return OK;
3818 }
3819 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003820 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3821 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003822#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003823 rettv->v_type = VAR_JOB;
3824 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003825#else
3826 rettv->v_type = VAR_SPECIAL;
3827 rettv->vval.v_number = VVAL_NULL;
3828#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003829 return OK;
3830 }
3831 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003832 case 9:
3833 if (STRNCMP(s, "null_", 5) != 0)
3834 break;
3835 if (STRNCMP(s + 5, "list", 4) == 0)
3836 {
3837 rettv->v_type = VAR_LIST;
3838 rettv->vval.v_list = NULL;
3839 return OK;
3840 }
3841 if (STRNCMP(s + 5, "dict", 4) == 0)
3842 {
3843 rettv->v_type = VAR_DICT;
3844 rettv->vval.v_dict = NULL;
3845 return OK;
3846 }
3847 if (STRNCMP(s + 5, "blob", 4) == 0)
3848 {
3849 rettv->v_type = VAR_BLOB;
3850 rettv->vval.v_blob = NULL;
3851 return OK;
3852 }
3853 break;
3854 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3855 {
3856 rettv->v_type = VAR_STRING;
3857 rettv->vval.v_string = NULL;
3858 return OK;
3859 }
3860 break;
3861 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003862 if (STRNCMP(s, "null_channel", 12) == 0)
3863 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003864#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003865 rettv->v_type = VAR_CHANNEL;
3866 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003867#else
3868 rettv->v_type = VAR_SPECIAL;
3869 rettv->vval.v_number = VVAL_NULL;
3870#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003871 return OK;
3872 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003873 if (STRNCMP(s, "null_partial", 12) == 0)
3874 {
3875 rettv->v_type = VAR_PARTIAL;
3876 rettv->vval.v_partial = NULL;
3877 return OK;
3878 }
3879 break;
3880 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3881 {
3882 rettv->v_type = VAR_FUNC;
3883 rettv->vval.v_string = NULL;
3884 return OK;
3885 }
3886 break;
3887 }
3888 return FAIL;
3889}
3890
3891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 * Handle sixth level expression:
3893 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003894 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003895 * "string" string constant
3896 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 * &option-name option value
3898 * @r register contents
3899 * identifier variable value
3900 * function() function call
3901 * $VAR environment variable
3902 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003905 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003906 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 *
3908 * Also handle:
3909 * ! in front logical NOT
3910 * - in front unary minus
3911 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003912 * trailing [] subscript in String or List
3913 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 *
3916 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003917 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 *
3919 * Return OK or FAIL.
3920 */
3921 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003922eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923 char_u **arg,
3924 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003925 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003928 int evaluate = evalarg != NULL
3929 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 int len;
3931 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003932 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 char_u *start_leader, *end_leader;
3934 int ret = OK;
3935 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003936 static int recurse = 0;
3937 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003946 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 */
3948 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003949 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 end_leader = *arg;
3952
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003953 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003954 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003955 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003956 ++*arg;
3957 return FAIL;
3958 }
3959
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003960 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003961 // and crash. With MSVC the stack is smaller.
3962 if (recurse ==
3963#ifdef _MSC_VER
3964 300
3965#else
3966 1000
3967#endif
3968 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003969 {
3970 semsg(_(e_expression_too_recursive_str), *arg);
3971 return FAIL;
3972 }
3973 ++recurse;
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 switch (**arg)
3976 {
3977 /*
3978 * Number constant.
3979 */
3980 case '0':
3981 case '1':
3982 case '2':
3983 case '3':
3984 case '4':
3985 case '5':
3986 case '6':
3987 case '7':
3988 case '8':
3989 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003990 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003991
3992 // Apply prefixed "-" and "+" now. Matters especially when
3993 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003994 if (ret == OK && evaluate && end_leader > start_leader
3995 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003996 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 /*
4000 * String constant: "string".
4001 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004002 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 break;
4004
4005 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004006 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004008 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004009 break;
4010
4011 /*
4012 * List: [expr, expr]
4013 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004014 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 break;
4016
4017 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004018 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004019 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004020 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004021 {
4022 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4023 }
4024 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004025 {
4026 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004027 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004028 }
4029 else
4030 ret = NOTDONE;
4031 break;
4032
4033 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004034 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004035 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004037 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004038 ret = NOTDONE;
4039 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004040 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004041 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004042 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 break;
4044
4045 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004046 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004048 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 break;
4050
4051 /*
4052 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004053 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
LemonBoy2eaef102022-05-06 13:14:50 +01004055 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4056 ret = eval_interp_string(arg, rettv, evaluate);
4057 else
4058 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 break;
4060
4061 /*
4062 * Register contents: @r.
4063 */
4064 case '@': ++*arg;
4065 if (evaluate)
4066 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004067 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004068 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004069 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004070 emsg_invreg(**arg);
4071 else
4072 {
4073 rettv->v_type = VAR_STRING;
4074 rettv->vval.v_string = get_reg_contents(**arg,
4075 GREG_EXPR_SRC);
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 if (**arg != NUL)
4079 ++*arg;
4080 break;
4081
4082 /*
4083 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004084 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004086 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004087 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004088 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004089 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004090 if (ret == OK && evaluate)
4091 {
4092 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4093
Bram Moolenaara9931532021-06-12 15:58:16 +02004094 // Compile it here to get the return type. The return
4095 // type is optional, when it's missing use t_unknown.
4096 // This is recognized in compile_return().
4097 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4098 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004099 if (compile_def_function(ufunc, FALSE,
4100 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004101 {
4102 clear_tv(rettv);
4103 ret = FAIL;
4104 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004105 }
4106 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004107 if (ret == NOTDONE)
4108 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004109 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004110 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004111
Bram Moolenaar9215f012020-06-27 21:18:00 +02004112 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004113 if (**arg == ')')
4114 ++*arg;
4115 else if (ret == OK)
4116 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004117 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004118 clear_tv(rettv);
4119 ret = FAIL;
4120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 break;
4123
Bram Moolenaar8c711452005-01-14 21:53:12 +00004124 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 break;
4126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004127
4128 if (ret == NOTDONE)
4129 {
4130 /*
4131 * Must be a variable or function name.
4132 * Can also be a curly-braces kind of name: {expr}.
4133 */
4134 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004135 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004136 if (alias != NULL)
4137 s = alias;
4138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004139 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004140 ret = FAIL;
4141 else
4142 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004143 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4144
Bram Moolenaar4525a572022-02-13 11:57:33 +00004145 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004146 {
4147 emsg(_(e_cannot_use_underscore_here));
4148 ret = FAIL;
4149 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004150 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004151 && s[0] == 's' && s[1] == ':')
4152 {
4153 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4154 ret = FAIL;
4155 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004156 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004157 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004158 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004159 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004160 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004161 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004162 else if (flags & EVAL_CONSTANT)
4163 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004164 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004165 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004166 // get the value of "true", "false", etc. or a variable
4167 ret = FAIL;
4168 if (vim9script)
4169 ret = handle_predefined(s, len, rettv);
4170 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004171 {
4172 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004173 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004174 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004175 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004176 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004177 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004178 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004179 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004180 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004181 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004182 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004183 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004184 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004185 }
4186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004187 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4188 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004189 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004190 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 /*
4193 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4194 */
4195 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004196 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004197
4198 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004199 return ret;
4200}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004202/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004203 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004204 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004205 * Adjusts "end_leaderp" until it is at "start_leader".
4206 */
4207 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004208eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004209 typval_T *rettv,
4210 int numeric_only,
4211 char_u *start_leader,
4212 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004213{
4214 char_u *end_leader = *end_leaderp;
4215 int ret = OK;
4216 int error = FALSE;
4217 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004218 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004219 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004220 float_T f = 0.0;
4221
4222 if (rettv->v_type == VAR_FLOAT)
4223 f = rettv->vval.v_float;
4224 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004225 {
4226 while (VIM_ISWHITE(end_leader[-1]))
4227 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004228 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004229 val = tv2bool(rettv);
4230 else
4231 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004232 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004233 if (error)
4234 {
4235 clear_tv(rettv);
4236 ret = FAIL;
4237 }
4238 else
4239 {
4240 while (end_leader > start_leader)
4241 {
4242 --end_leader;
4243 if (*end_leader == '!')
4244 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004245 if (numeric_only)
4246 {
4247 ++end_leader;
4248 break;
4249 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004250 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004251 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004252 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004253 {
4254 rettv->v_type = VAR_BOOL;
4255 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4256 }
4257 else
4258 f = !f;
4259 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004260 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004261 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004262 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004263 type = VAR_BOOL;
4264 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004265 }
4266 else if (*end_leader == '-')
4267 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004268 if (rettv->v_type == VAR_FLOAT)
4269 f = -f;
4270 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004271 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004272 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004273 type = VAR_NUMBER;
4274 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004275 }
4276 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004277 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004280 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 else
4283 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004284 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004285 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004286 rettv->v_type = type;
4287 else
4288 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004289 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004292 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return ret;
4294}
4295
4296/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004297 * Call the function referred to in "rettv".
4298 */
4299 static int
4300call_func_rettv(
4301 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004302 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004303 typval_T *rettv,
4304 int evaluate,
4305 dict_T *selfdict,
4306 typval_T *basetv)
4307{
4308 partial_T *pt = NULL;
4309 funcexe_T funcexe;
4310 typval_T functv;
4311 char_u *s;
4312 int ret;
4313
4314 // need to copy the funcref so that we can clear rettv
4315 if (evaluate)
4316 {
4317 functv = *rettv;
4318 rettv->v_type = VAR_UNKNOWN;
4319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004321 if (functv.v_type == VAR_PARTIAL)
4322 {
4323 pt = functv.vval.v_partial;
4324 s = partial_name(pt);
4325 }
4326 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004327 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004328 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004329 if (s == NULL || *s == NUL)
4330 {
4331 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004332 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004333 goto theend;
4334 }
4335 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004336 }
4337 else
4338 s = (char_u *)"";
4339
Bram Moolenaara80faa82020-04-12 19:37:17 +02004340 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004341 funcexe.fe_firstline = curwin->w_cursor.lnum;
4342 funcexe.fe_lastline = curwin->w_cursor.lnum;
4343 funcexe.fe_evaluate = evaluate;
4344 funcexe.fe_partial = pt;
4345 funcexe.fe_selfdict = selfdict;
4346 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004347 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004348
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004349theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // Clear the funcref afterwards, so that deleting it while
4351 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004352 if (evaluate)
4353 clear_tv(&functv);
4354
4355 return ret;
4356}
4357
4358/*
4359 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004360 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004361 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4362 */
4363 static int
4364eval_lambda(
4365 char_u **arg,
4366 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004367 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004369{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004370 int evaluate = evalarg != NULL
4371 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004372 typval_T base = *rettv;
4373 int ret;
4374
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004375 rettv->v_type = VAR_UNKNOWN;
4376
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004377 if (**arg == '{')
4378 {
4379 // ->{lambda}()
4380 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4381 }
4382 else
4383 {
4384 // ->(lambda)()
4385 ++*arg;
4386 ret = eval1(arg, rettv, evalarg);
4387 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004388 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004389 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004390 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004391 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004392 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004393 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4394 && rettv->v_type != VAR_PARTIAL)
4395 {
4396 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4397 return FAIL;
4398 }
4399 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004400 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004401 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004402 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004403
4404 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004405 {
4406 if (verbose)
4407 {
4408 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004409 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004410 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004411 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004412 }
4413 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004414 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004415 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004416 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004417 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004418
4419 // Clear the funcref afterwards, so that deleting it while
4420 // evaluating the arguments is possible (see test55).
4421 if (evaluate)
4422 clear_tv(&base);
4423
4424 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004425}
4426
4427/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004428 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004429 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004430 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4431 */
4432 static int
4433eval_method(
4434 char_u **arg,
4435 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004436 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004437 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004438{
4439 char_u *name;
4440 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004441 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004442 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004444 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004445 int evaluate = evalarg != NULL
4446 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004447
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004448 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004449
Bram Moolenaarac92e252019-08-03 21:58:38 +02004450 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004451 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004452 if (alias != NULL)
4453 name = alias;
4454
4455 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004456 {
4457 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004458 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004459 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004460 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004461 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004462 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004463 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004464
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004465 // If there is no "(" immediately following, but there is further on,
4466 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4467 // Does not handle anything where "(" is part of the expression.
4468 *arg = skipwhite(*arg);
4469
4470 if (**arg != '(' && alias == NULL
4471 && (paren = vim_strchr(*arg, '(')) != NULL)
4472 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004473 char_u *deref;
4474
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004475 *arg = name;
4476 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004477 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4478 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004479 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004480 *arg = name + len;
4481 ret = FAIL;
4482 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004483 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004484 {
4485 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004486 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004487 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004488 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004489 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004490
4491 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004492 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004493 *arg = skipwhite(*arg);
4494
4495 if (**arg != '(')
4496 {
4497 if (verbose)
4498 semsg(_(e_missing_parenthesis_str), name);
4499 ret = FAIL;
4500 }
4501 else if (VIM_ISWHITE((*arg)[-1]))
4502 {
4503 if (verbose)
4504 emsg(_(e_no_white_space_allowed_before_parenthesis));
4505 ret = FAIL;
4506 }
4507 else
4508 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004509 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004510 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004511 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004512
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004513 // Clear the funcref afterwards, so that deleting it while
4514 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004515 if (evaluate)
4516 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004517 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004518
Bram Moolenaarac92e252019-08-03 21:58:38 +02004519 return ret;
4520}
4521
4522/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004523 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4524 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4526 */
4527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004528eval_index(
4529 char_u **arg,
4530 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004531 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004534 int evaluate = evalarg != NULL
4535 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004536 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004537 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004540 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004541 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004543 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4544 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004546 init_tv(&var1);
4547 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 /*
4551 * dict.name
4552 */
4553 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004554 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004556 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004558 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 }
4560 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 /*
4563 * something[idx]
4564 *
4565 * Get the (first) variable from inside the [].
4566 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004567 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 if (**arg == ':')
4569 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004570 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004571 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004572 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004573 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004574 semsg(_(e_white_space_required_before_and_after_str_at_str),
4575 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004576 clear_tv(&var1);
4577 return FAIL;
4578 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004579 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004581 int error = FALSE;
4582
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004583 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004584 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004585 && var1.v_type == VAR_FLOAT)
4586 {
4587 var1.vval.v_string = typval_tostring(&var1, TRUE);
4588 var1.v_type = VAR_STRING;
4589 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004590
Bram Moolenaar4525a572022-02-13 11:57:33 +00004591 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004592 tv_get_number_chk(&var1, &error);
4593 else
4594 error = tv_get_string_chk(&var1) == NULL;
4595 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004596 {
4597 // not a number or string
4598 clear_tv(&var1);
4599 return FAIL;
4600 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602
4603 /*
4604 * Get the second variable from inside the [:].
4605 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004606 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 if (**arg == ':')
4608 {
4609 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004610 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004611 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004612 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004613 semsg(_(e_white_space_required_before_and_after_str_at_str),
4614 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004615 if (!empty1)
4616 clear_tv(&var1);
4617 return FAIL;
4618 }
4619 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 if (**arg == ']')
4621 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004622 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 if (!empty1)
4625 clear_tv(&var1);
4626 return FAIL;
4627 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004630 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 if (!empty1)
4632 clear_tv(&var1);
4633 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634 return FAIL;
4635 }
4636 }
4637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004639 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 if (**arg != ']')
4641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004642 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004643 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 clear_tv(&var1);
4645 if (range)
4646 clear_tv(&var2);
4647 return FAIL;
4648 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004649 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 }
4651
4652 if (evaluate)
4653 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004654 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004655 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004656 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004657
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004661 clear_tv(&var2);
4662 return res;
4663 }
4664 return OK;
4665}
4666
4667/*
4668 * Check if "rettv" can have an [index] or [sli:ce]
4669 */
4670 int
4671check_can_index(typval_T *rettv, int evaluate, int verbose)
4672{
4673 switch (rettv->v_type)
4674 {
4675 case VAR_FUNC:
4676 case VAR_PARTIAL:
4677 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004678 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679 return FAIL;
4680 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004682 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004683 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004684 case VAR_BOOL:
4685 case VAR_SPECIAL:
4686 case VAR_JOB:
4687 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004688 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004689 if (verbose)
4690 emsg(_(e_cannot_index_special_variable));
4691 return FAIL;
4692 case VAR_UNKNOWN:
4693 case VAR_ANY:
4694 case VAR_VOID:
4695 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004697 emsg(_(e_cannot_index_special_variable));
4698 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004700 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004702 case VAR_STRING:
4703 case VAR_LIST:
4704 case VAR_DICT:
4705 case VAR_BLOB:
4706 break;
4707 case VAR_NUMBER:
4708 if (in_vim9script())
4709 emsg(_(e_cannot_index_number));
4710 break;
4711 }
4712 return OK;
4713}
4714
4715/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004716 * slice() function
4717 */
4718 void
4719f_slice(typval_T *argvars, typval_T *rettv)
4720{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004721 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004722 && ((argvars[0].v_type != VAR_STRING
4723 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004724 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004725 && check_for_list_arg(argvars, 0) == FAIL)
4726 || check_for_number_arg(argvars, 1) == FAIL
4727 || check_for_opt_number_arg(argvars, 2) == FAIL))
4728 return;
4729
Bram Moolenaar6601b622021-01-13 21:47:15 +01004730 if (check_can_index(argvars, TRUE, FALSE) == OK)
4731 {
4732 copy_tv(argvars, rettv);
4733 eval_index_inner(rettv, TRUE, argvars + 1,
4734 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4735 TRUE, NULL, 0, FALSE);
4736 }
4737}
4738
4739/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004740 * Apply index or range to "rettv".
4741 * "var1" is the first index, NULL for [:expr].
4742 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004743 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4744 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004745 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4746 */
4747 int
4748eval_index_inner(
4749 typval_T *rettv,
4750 int is_range,
4751 typval_T *var1,
4752 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004753 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004754 char_u *key,
4755 int keylen,
4756 int verbose)
4757{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004758 varnumber_T n1, n2 = 0;
4759 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004760
4761 n1 = 0;
4762 if (var1 != NULL && rettv->v_type != VAR_DICT)
4763 n1 = tv_get_number(var1);
4764
4765 if (is_range)
4766 {
4767 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004769 if (verbose)
4770 emsg(_(e_cannot_slice_dictionary));
4771 return FAIL;
4772 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004773 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004775 else
4776 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004777 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004779 switch (rettv->v_type)
4780 {
4781 case VAR_UNKNOWN:
4782 case VAR_ANY:
4783 case VAR_VOID:
4784 case VAR_FUNC:
4785 case VAR_PARTIAL:
4786 case VAR_FLOAT:
4787 case VAR_BOOL:
4788 case VAR_SPECIAL:
4789 case VAR_JOB:
4790 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004791 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004792 break; // not evaluating, skipping over subscript
4793
4794 case VAR_NUMBER:
4795 case VAR_STRING:
4796 {
4797 char_u *s = tv_get_string(rettv);
4798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004800 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004801 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004802 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004803 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004804 else
4805 s = char_from_string(s, n1);
4806 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004807 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // The resulting variable is a substring. If the indexes
4810 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 if (n1 < 0)
4812 {
4813 n1 = len + n1;
4814 if (n1 < 0)
4815 n1 = 0;
4816 }
4817 if (n2 < 0)
4818 n2 = len + n2;
4819 else if (n2 >= len)
4820 n2 = len;
4821 if (n1 >= len || n2 < 0 || n1 > n2)
4822 s = NULL;
4823 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004824 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 }
4826 else
4827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // The resulting variable is a string of a single
4829 // character. If the index is too big or negative the
4830 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 if (n1 >= len || n1 < 0)
4832 s = NULL;
4833 else
4834 s = vim_strnsave(s + n1, 1);
4835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
4837 rettv->v_type = VAR_STRING;
4838 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004839 }
4840 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004842 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004843 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4844 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004845 break;
4846
4847 case VAR_LIST:
4848 if (var1 == NULL)
4849 n1 = 0;
4850 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004851 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004852 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004853 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004854 return FAIL;
4855 break;
4856
4857 case VAR_DICT:
4858 {
4859 dictitem_T *item;
4860 typval_T tmp;
4861
4862 if (key == NULL)
4863 {
4864 key = tv_get_string_chk(var1);
4865 if (key == NULL)
4866 return FAIL;
4867 }
4868
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004869 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004870
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004871 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004872 {
4873 if (verbose)
4874 {
4875 if (keylen > 0)
4876 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004877 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004878 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004879 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004880 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004881
4882 copy_tv(&item->di_tv, &tmp);
4883 clear_tv(rettv);
4884 *rettv = tmp;
4885 }
4886 break;
4887 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 return OK;
4889}
4890
4891/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004892 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004893 */
4894 char_u *
4895partial_name(partial_T *pt)
4896{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004897 if (pt != NULL)
4898 {
4899 if (pt->pt_name != NULL)
4900 return pt->pt_name;
4901 if (pt->pt_func != NULL)
4902 return pt->pt_func->uf_name;
4903 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004904 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004905}
4906
Bram Moolenaarddecc252016-04-06 22:59:37 +02004907 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004909{
4910 int i;
4911
4912 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004913 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004914 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004916 if (pt->pt_name != NULL)
4917 {
4918 func_unref(pt->pt_name);
4919 vim_free(pt->pt_name);
4920 }
4921 else
4922 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004923
Bram Moolenaar54656012021-06-09 20:50:46 +02004924 // "out_up" is no longer used, decrement refcount on partial that owns it.
4925 partial_unref(pt->pt_outer.out_up_partial);
4926
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004927 // Using pt_outer from another partial.
4928 partial_unref(pt->pt_outer_partial);
4929
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004930 // Decrease the reference count for the context of a closure. If down
4931 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004932 if (pt->pt_funcstack != NULL)
4933 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004934 --pt->pt_funcstack->fs_refcount;
4935 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004936 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004937 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004938 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4939 if (pt->pt_loopvars[i] != NULL)
4940 {
4941 --pt->pt_loopvars[i]->lvs_refcount;
4942 loopvars_check_refcount(pt->pt_loopvars[i]);
4943 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004944
Bram Moolenaarddecc252016-04-06 22:59:37 +02004945 vim_free(pt);
4946}
4947
4948/*
4949 * Unreference a closure: decrement the reference count and free it when it
4950 * becomes zero.
4951 */
4952 void
4953partial_unref(partial_T *pt)
4954{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004955 if (pt != NULL)
4956 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004957 int done = FALSE;
4958
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004959 if (--pt->pt_refcount <= 0)
4960 partial_free(pt);
4961
4962 // If the reference count goes down to one, the funcstack may be the
4963 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004964 else if (pt->pt_refcount == 1)
4965 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004966 // careful: if the funcstack is freed it may contain this partial
4967 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004968 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004969 done = funcstack_check_refcount(pt->pt_funcstack);
4970
Bram Moolenaarcc341812022-09-19 15:54:34 +01004971 if (!done)
4972 {
4973 int depth;
4974
4975 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4976 if (pt->pt_loopvars[depth] != NULL
4977 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4978 break;
4979 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004980 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004981 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004982}
4983
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004984/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004985 * Return the next (unique) copy ID.
4986 * Used for serializing nested structures.
4987 */
4988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004989get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004990{
4991 current_copyID += COPYID_INC;
4992 return current_copyID;
4993}
4994
4995/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004996 * Garbage collection for lists and dictionaries.
4997 *
4998 * We use reference counts to be able to free most items right away when they
4999 * are no longer used. But for composite items it's possible that it becomes
5000 * unused while the reference count is > 0: When there is a recursive
5001 * reference. Example:
5002 * :let l = [1, 2, 3]
5003 * :let d = {9: l}
5004 * :let l[1] = d
5005 *
5006 * Since this is quite unusual we handle this with garbage collection: every
5007 * once in a while find out which lists and dicts are not referenced from any
5008 * variable.
5009 *
5010 * Here is a good reference text about garbage collection (refers to Python
5011 * but it applies to all reference-counting mechanisms):
5012 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005013 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005014
5015/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005016 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005017 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005018 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005019 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005020 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005021garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005022{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005023 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005024 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005025 buf_T *buf;
5026 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005027 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005028 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005029
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005030 if (!testing)
5031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005033 want_garbage_collect = FALSE;
5034 may_garbage_collect = FALSE;
5035 garbage_collect_at_exit = FALSE;
5036 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005037
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005038 // The execution stack can grow big, limit the size.
5039 if (exestack.ga_maxlen - exestack.ga_len > 500)
5040 {
5041 size_t new_len;
5042 char_u *pp;
5043 int n;
5044
5045 // Keep 150% of the current size, with a minimum of the growth size.
5046 n = exestack.ga_len / 2;
5047 if (n < exestack.ga_growsize)
5048 n = exestack.ga_growsize;
5049
5050 // Don't make it bigger though.
5051 if (exestack.ga_len + n < exestack.ga_maxlen)
5052 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005053 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005054 pp = vim_realloc(exestack.ga_data, new_len);
5055 if (pp == NULL)
5056 return FAIL;
5057 exestack.ga_maxlen = exestack.ga_len + n;
5058 exestack.ga_data = pp;
5059 }
5060 }
5061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005062 // We advance by two because we add one for items referenced through
5063 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005064 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005065
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005066 /*
5067 * 1. Go through all accessible variables and mark all lists and dicts
5068 * with copyID.
5069 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005071 // Don't free variables in the previous_funccal list unless they are only
5072 // referenced through previous_funccal. This must be first, because if
5073 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005077 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005080 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5082 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005084 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005085 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005086 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5087 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005088 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005089 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5090 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005091#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005092 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005093 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5094 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005095 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005096 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005097 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5098 NULL, NULL);
5099#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005102 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005103 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5104 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005106 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005112 abort = abort || set_ref_in_functions(copyID);
5113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005115 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005116
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005117 // funcstacks keep variables for closures
5118 abort = abort || set_ref_in_funcstacks(copyID);
5119
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005120 // loopvars keep variables for loop blocks
5121 abort = abort || set_ref_in_loopvars(copyID);
5122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005123 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005124 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005125
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005126 // callbacks in buffers
5127 abort = abort || set_ref_in_buffers(copyID);
5128
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005129 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5130 abort = abort || set_ref_in_insexpand_funcs(copyID);
5131
5132 // 'operatorfunc' callback
5133 abort = abort || set_ref_in_opfunc(copyID);
5134
5135 // 'tagfunc' callback
5136 abort = abort || set_ref_in_tagfunc(copyID);
5137
5138 // 'imactivatefunc' and 'imstatusfunc' callbacks
5139 abort = abort || set_ref_in_im_funcs(copyID);
5140
Bram Moolenaar1dced572012-04-05 16:54:08 +02005141#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005142 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005143#endif
5144
Bram Moolenaardb913952012-06-29 12:54:53 +02005145#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005147#endif
5148
5149#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005150 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005151#endif
5152
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005153#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005154 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005155 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005156#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005157#ifdef FEAT_NETBEANS_INTG
5158 abort = abort || set_ref_in_nb_channel(copyID);
5159#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005160
Bram Moolenaare3188e22016-05-31 21:13:04 +02005161#ifdef FEAT_TIMERS
5162 abort = abort || set_ref_in_timer(copyID);
5163#endif
5164
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005165#ifdef FEAT_QUICKFIX
5166 abort = abort || set_ref_in_quickfix(copyID);
5167#endif
5168
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005169#ifdef FEAT_TERMINAL
5170 abort = abort || set_ref_in_term(copyID);
5171#endif
5172
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005173#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005174 abort = abort || set_ref_in_popups(copyID);
5175#endif
5176
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005177 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005178 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005179 /*
5180 * 2. Free lists and dictionaries that are not referenced.
5181 */
5182 did_free = free_unref_items(copyID);
5183
5184 /*
5185 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005187 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005189 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005190 else if (p_verbose > 0)
5191 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005192 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005193 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005194
5195 return did_free;
5196}
5197
5198/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005199 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005200 */
5201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005203{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005204 int did_free = FALSE;
5205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005206 // Let all "free" functions know that we are here. This means no
5207 // dictionaries, lists, channels or jobs are to be freed, because we will
5208 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005209 in_free_unref_items = TRUE;
5210
5211 /*
5212 * PASS 1: free the contents of the items. We don't free the items
5213 * themselves yet, so that it is possible to decrement refcount counters
5214 */
5215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005217 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005220 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005221
5222#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // Go through the list of jobs and free items without the copyID. This
5224 // must happen before doing channels, because jobs refer to channels, but
5225 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005226 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005229 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5230#endif
5231
5232 /*
5233 * PASS 2: free the items themselves.
5234 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005235 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005236 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005237
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005238#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 // Go through the list of jobs and free items without the copyID. This
5240 // must happen before doing channels, because jobs refer to channels, but
5241 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005242 free_unused_jobs(copyID, COPYID_MASK);
5243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005245 free_unused_channels(copyID, COPYID_MASK);
5246#endif
5247
5248 in_free_unref_items = FALSE;
5249
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005250 return did_free;
5251}
5252
5253/*
5254 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005255 * "list_stack" is used to add lists to be marked. Can be NULL.
5256 *
5257 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005258 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005261{
5262 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005263 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005264 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 hashtab_T *cur_ht;
5266 ht_stack_T *ht_stack = NULL;
5267 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005268
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 cur_ht = ht;
5270 for (;;)
5271 {
5272 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005274 // Mark each item in the hashtab. If the item contains a hashtab
5275 // it is added to ht_stack, if it contains a list it is added to
5276 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 todo = (int)cur_ht->ht_used;
5278 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5279 if (!HASHITEM_EMPTY(hi))
5280 {
5281 --todo;
5282 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5283 &ht_stack, list_stack);
5284 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286
5287 if (ht_stack == NULL)
5288 break;
5289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005291 cur_ht = ht_stack->ht;
5292 tempitem = ht_stack;
5293 ht_stack = ht_stack->prev;
5294 free(tempitem);
5295 }
5296
5297 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298}
5299
Dominique Pelle748b3082022-01-08 12:41:16 +00005300#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5301 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005302/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005303 * Mark a dict and its items with "copyID".
5304 * Returns TRUE if setting references failed somehow.
5305 */
5306 int
5307set_ref_in_dict(dict_T *d, int copyID)
5308{
5309 if (d != NULL && d->dv_copyID != copyID)
5310 {
5311 d->dv_copyID = copyID;
5312 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5313 }
5314 return FALSE;
5315}
Dominique Pelle748b3082022-01-08 12:41:16 +00005316#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005317
5318/*
5319 * Mark a list and its items with "copyID".
5320 * Returns TRUE if setting references failed somehow.
5321 */
5322 int
5323set_ref_in_list(list_T *ll, int copyID)
5324{
5325 if (ll != NULL && ll->lv_copyID != copyID)
5326 {
5327 ll->lv_copyID = copyID;
5328 return set_ref_in_list_items(ll, copyID, NULL);
5329 }
5330 return FALSE;
5331}
5332
5333/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005334 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5336 *
5337 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005338 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005339 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005340set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005341{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005342 listitem_T *li;
5343 int abort = FALSE;
5344 list_T *cur_l;
5345 list_stack_T *list_stack = NULL;
5346 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 cur_l = l;
5349 for (;;)
5350 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005351 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 // Mark each item in the list. If the item contains a hashtab
5353 // it is added to ht_stack, if it contains a list it is added to
5354 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005355 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5356 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5357 ht_stack, &list_stack);
5358 if (list_stack == NULL)
5359 break;
5360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 cur_l = list_stack->list;
5363 tempitem = list_stack;
5364 list_stack = list_stack->prev;
5365 free(tempitem);
5366 }
5367
5368 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005369}
5370
5371/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005372 * Mark the partial in callback 'cb' with "copyID".
5373 */
5374 int
5375set_ref_in_callback(callback_T *cb, int copyID)
5376{
5377 typval_T tv;
5378
5379 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5380 return FALSE;
5381
5382 tv.v_type = VAR_PARTIAL;
5383 tv.vval.v_partial = cb->cb_partial;
5384 return set_ref_in_item(&tv, copyID, NULL, NULL);
5385}
5386
5387/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 * "list_stack" is used to add lists to be marked. Can be NULL.
5390 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5391 *
5392 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005395set_ref_in_item(
5396 typval_T *tv,
5397 int copyID,
5398 ht_stack_T **ht_stack,
5399 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005402
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005403 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005404 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005405 dict_T *dd = tv->vval.v_dict;
5406
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 if (dd != NULL && dd->dv_copyID != copyID)
5408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005410 dd->dv_copyID = copyID;
5411 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005412 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005413 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5414 }
5415 else
5416 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005417 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5418
Bram Moolenaara03f2332016-02-06 18:09:59 +01005419 if (newitem == NULL)
5420 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 else
5422 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005423 newitem->ht = &dd->dv_hashtab;
5424 newitem->prev = *ht_stack;
5425 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005427 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 }
5429 }
5430 else if (tv->v_type == VAR_LIST)
5431 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432 list_T *ll = tv->vval.v_list;
5433
Bram Moolenaara03f2332016-02-06 18:09:59 +01005434 if (ll != NULL && ll->lv_copyID != copyID)
5435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005437 ll->lv_copyID = copyID;
5438 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005439 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005440 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005441 }
5442 else
5443 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005444 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5445
Bram Moolenaara03f2332016-02-06 18:09:59 +01005446 if (newitem == NULL)
5447 abort = TRUE;
5448 else
5449 {
5450 newitem->list = ll;
5451 newitem->prev = *list_stack;
5452 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005453 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005454 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005455 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005456 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005457 else if (tv->v_type == VAR_FUNC)
5458 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005459 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005460 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005461 else if (tv->v_type == VAR_PARTIAL)
5462 {
5463 partial_T *pt = tv->vval.v_partial;
5464 int i;
5465
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005466 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005468 // Didn't see this partial yet.
5469 pt->pt_copyID = copyID;
5470
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005471 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005473 if (pt->pt_dict != NULL)
5474 {
5475 typval_T dtv;
5476
5477 dtv.v_type = VAR_DICT;
5478 dtv.vval.v_dict = pt->pt_dict;
5479 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5480 }
5481
5482 for (i = 0; i < pt->pt_argc; ++i)
5483 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5484 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005485 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005486 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005487 }
5488 }
5489#ifdef FEAT_JOB_CHANNEL
5490 else if (tv->v_type == VAR_JOB)
5491 {
5492 job_T *job = tv->vval.v_job;
5493 typval_T dtv;
5494
5495 if (job != NULL && job->jv_copyID != copyID)
5496 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005497 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005498 if (job->jv_channel != NULL)
5499 {
5500 dtv.v_type = VAR_CHANNEL;
5501 dtv.vval.v_channel = job->jv_channel;
5502 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5503 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005504 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005505 {
5506 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005507 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005508 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5509 }
5510 }
5511 }
5512 else if (tv->v_type == VAR_CHANNEL)
5513 {
5514 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005515 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005516 typval_T dtv;
5517 jsonq_T *jq;
5518 cbq_T *cq;
5519
5520 if (ch != NULL && ch->ch_copyID != copyID)
5521 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005522 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005523 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 {
5525 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5526 jq = jq->jq_next)
5527 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5528 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5529 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005530 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005531 {
5532 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005533 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005534 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5535 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005536 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005537 {
5538 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005539 dtv.vval.v_partial =
5540 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005541 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5542 }
5543 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005544 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005545 {
5546 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005547 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005548 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5549 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005550 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005551 {
5552 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005553 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005554 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5555 }
5556 }
5557 }
5558#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005559 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005560}
5561
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 * Return a string with the string representation of a variable.
5564 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005566 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005567 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005568 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005569 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005570 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5571 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005572 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005574 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005575echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576 typval_T *tv,
5577 char_u **tofree,
5578 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005579 int copyID,
5580 int echo_style,
5581 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005582 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005584 static int recurse = 0;
5585 char_u *r = NULL;
5586
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005588 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005589 if (!did_echo_string_emsg)
5590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005591 // Only give this message once for a recursive call to avoid
5592 // flooding the user with errors. And stop iterating over lists
5593 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005594 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005595 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005596 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005597 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005598 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005599 }
5600 ++recurse;
5601
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 switch (tv->v_type)
5603 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005604 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005605 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005606 {
5607 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005608 r = tv->vval.v_string;
5609 if (r == NULL)
5610 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005611 }
5612 else
5613 {
5614 *tofree = string_quote(tv->vval.v_string, FALSE);
5615 r = *tofree;
5616 }
5617 break;
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005620 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005621 char_u buf[MAX_FUNC_NAME_LEN];
5622
5623 if (echo_style)
5624 {
LemonBoya5d35902022-04-29 21:15:02 +01005625 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5626 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005627 buf, MAX_FUNC_NAME_LEN);
5628 if (r == buf)
5629 {
5630 r = vim_strsave(buf);
5631 *tofree = r;
5632 }
5633 else
5634 *tofree = NULL;
5635 }
5636 else
5637 {
5638 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5639 : make_ufunc_name_readable(
5640 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5641 TRUE);
5642 r = *tofree;
5643 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005646
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005647 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005648 {
5649 partial_T *pt = tv->vval.v_partial;
5650 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005651 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005652 garray_T ga;
5653 int i;
5654 char_u *tf;
5655
5656 ga_init2(&ga, 1, 100);
5657 ga_concat(&ga, (char_u *)"function(");
5658 if (fname != NULL)
5659 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005660 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005661 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005662 && vim_isupper(fname[1]))
5663 {
5664 ga_concat(&ga, (char_u *)"'g:");
5665 ga_concat(&ga, fname + 1);
5666 }
5667 else
5668 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005669 vim_free(fname);
5670 }
5671 if (pt != NULL && pt->pt_argc > 0)
5672 {
5673 ga_concat(&ga, (char_u *)", [");
5674 for (i = 0; i < pt->pt_argc; ++i)
5675 {
5676 if (i > 0)
5677 ga_concat(&ga, (char_u *)", ");
5678 ga_concat(&ga,
5679 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5680 vim_free(tf);
5681 }
5682 ga_concat(&ga, (char_u *)"]");
5683 }
5684 if (pt != NULL && pt->pt_dict != NULL)
5685 {
5686 typval_T dtv;
5687
5688 ga_concat(&ga, (char_u *)", ");
5689 dtv.v_type = VAR_DICT;
5690 dtv.vval.v_dict = pt->pt_dict;
5691 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5692 vim_free(tf);
5693 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005694 // terminate with ')' and a NUL
5695 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005696
5697 *tofree = ga.ga_data;
5698 r = *tofree;
5699 break;
5700 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005701
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005702 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005703 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005704 break;
5705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005707 if (tv->vval.v_list == NULL)
5708 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005709 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005710 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005711 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005712 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005713 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5714 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005715 {
5716 *tofree = NULL;
5717 r = (char_u *)"[...]";
5718 }
5719 else
5720 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005721 int old_copyID = tv->vval.v_list->lv_copyID;
5722
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005723 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005724 *tofree = list2string(tv, copyID, restore_copyID);
5725 if (restore_copyID)
5726 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005727 r = *tofree;
5728 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005729 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005730
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005732 if (tv->vval.v_dict == NULL)
5733 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005734 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005735 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005736 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005737 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005738 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5739 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005740 {
5741 *tofree = NULL;
5742 r = (char_u *)"{...}";
5743 }
5744 else
5745 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005746 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005747
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005748 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005749 *tofree = dict2string(tv, copyID, restore_copyID);
5750 if (restore_copyID)
5751 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005752 r = *tofree;
5753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005754 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005757 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005758 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005760 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005761 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005762 break;
5763
Bram Moolenaar835dc632016-02-07 14:27:38 +01005764 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005765 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005766#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005767 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005768 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5769 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005770 if (composite_val)
5771 {
5772 *tofree = string_quote(r, FALSE);
5773 r = *tofree;
5774 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005775#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005777
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005778 case VAR_INSTR:
5779 *tofree = NULL;
5780 r = (char_u *)"instructions";
5781 break;
5782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005783 case VAR_FLOAT:
5784 *tofree = NULL;
5785 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5786 r = numbuf;
5787 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005788
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005789 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005790 case VAR_SPECIAL:
5791 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005792 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005793 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005795
Bram Moolenaar8502c702014-06-17 12:51:16 +02005796 if (--recurse == 0)
5797 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005798 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799}
5800
5801/*
5802 * Return a string with the string representation of a variable.
5803 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5804 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005805 * Does not put quotes around strings, as ":echo" displays values.
5806 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5807 * May return NULL.
5808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810echo_string(
5811 typval_T *tv,
5812 char_u **tofree,
5813 char_u *numbuf,
5814 int copyID)
5815{
5816 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5817}
5818
5819/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005820 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5821 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005822 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005823 */
5824 int
5825buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5826{
5827 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005828 char_u *t;
5829 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005830
5831 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5832 return -1;
5833
5834 if (lnum > buf->b_ml.ml_line_count)
5835 lnum = buf->b_ml.ml_line_count;
5836
5837 str = ml_get_buf(buf, lnum, FALSE);
5838 if (str == NULL)
5839 return -1;
5840
5841 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005842 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005843
Bram Moolenaar91458462021-01-13 20:08:38 +01005844 // count the number of characters
5845 t = str;
5846 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5847 t += mb_ptr2len(t);
5848
5849 // In insert mode, when the cursor is at the end of a non-empty line,
5850 // byteidx points to the NUL character immediately past the end of the
5851 // string. In this case, add one to the character count.
5852 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5853 count++;
5854
5855 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005856}
5857
5858/*
5859 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005860 * byte index. Works only for loaded buffers. Returns -1 on failure.
5861 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005862 */
5863 int
5864buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5865{
5866 char_u *str;
5867 char_u *t;
5868
5869 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5870 return -1;
5871
5872 if (lnum > buf->b_ml.ml_line_count)
5873 lnum = buf->b_ml.ml_line_count;
5874
5875 str = ml_get_buf(buf, lnum, FALSE);
5876 if (str == NULL)
5877 return -1;
5878
5879 // Convert the character offset to a byte offset
5880 t = str;
5881 while (*t != NUL && --charidx > 0)
5882 t += mb_ptr2len(t);
5883
Bram Moolenaar91458462021-01-13 20:08:38 +01005884 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005885}
5886
5887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005889 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892var2fpos(
5893 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005894 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005895 int *fnum, // set to fnum for '0, 'A, etc.
5896 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005898 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005900 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005902 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 if (varp->v_type == VAR_LIST)
5904 {
5905 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005906 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005907 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005908 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005909
5910 l = varp->vval.v_list;
5911 if (l == NULL)
5912 return NULL;
5913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005915 pos.lnum = list_find_nr(l, 0L, &error);
5916 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005918 if (charcol)
5919 len = (long)mb_charlen(ml_get(pos.lnum));
5920 else
5921 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005922
Bram Moolenaarec65d772020-08-20 22:29:12 +02005923 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005924 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005925 li = list_find(l, 1L);
5926 if (li != NULL && li->li_tv.v_type == VAR_STRING
5927 && li->li_tv.vval.v_string != NULL
5928 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005929 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005930 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005931 }
5932 else
5933 {
5934 pos.col = list_find_nr(l, 1L, &error);
5935 if (error)
5936 return NULL;
5937 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005939 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005940 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005941 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005942 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005945 pos.coladd = list_find_nr(l, 2L, &error);
5946 if (error)
5947 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005948
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005949 return &pos;
5950 }
5951
Bram Moolenaarc5809432021-03-27 21:23:30 +01005952 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5953 return NULL;
5954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005955 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956 if (name == NULL)
5957 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005958
5959 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005960 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005961 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005962 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005963 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005964 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005965 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005966 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005967 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005968 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005969 pos = VIsual;
5970 else
5971 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005972 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005973 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005974 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005976 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005977 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5979 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005980 pos = *pp;
5981 }
5982 if (pos.lnum != 0)
5983 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005984 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005985 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5986 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005988
Bram Moolenaara5525202006-03-02 22:52:09 +00005989 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005990
Bram Moolenaar477933c2007-07-17 14:32:23 +00005991 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005992 {
5993 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005995 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005996 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005997 // In silent Ex mode topline is zero, but that's not a valid line
5998 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005999 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006000 return &pos;
6001 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006002 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006003 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006004 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006005 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006006 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006007 return &pos;
6008 }
6009 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006010 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006012 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 pos.lnum = curbuf->b_ml.ml_line_count;
6015 pos.col = 0;
6016 }
6017 else
6018 {
6019 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006020 if (charcol)
6021 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6022 else
6023 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 }
6025 return &pos;
6026 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006027 if (in_vim9script())
6028 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 return NULL;
6030}
6031
6032/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006033 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006034 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006035 * Note that the column is passed on as-is, the caller may want to decrement
6036 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006037 * If "charcol" is TRUE use the column as the character index instead of the
6038 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006039 * Return FAIL when conversion is not possible, doesn't check the position for
6040 * validity.
6041 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043list2fpos(
6044 typval_T *arg,
6045 pos_T *posp,
6046 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006047 colnr_T *curswantp,
6048 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006049{
6050 list_T *l = arg->vval.v_list;
6051 long i = 0;
6052 long n;
6053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006054 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6055 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006056 if (arg->v_type != VAR_LIST
6057 || l == NULL
6058 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006059 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006060 return FAIL;
6061
6062 if (fnump != NULL)
6063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006065 if (n < 0)
6066 return FAIL;
6067 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006069 *fnump = n;
6070 }
6071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006073 if (n < 0)
6074 return FAIL;
6075 posp->lnum = n;
6076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006077 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006078 if (n < 0)
6079 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006080 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006081 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006082 if (charcol)
6083 {
6084 buf_T *buf;
6085
6086 // Get the text for the specified line in a loaded buffer
6087 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6088 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6089 return FAIL;
6090
Bram Moolenaar79f23442022-10-10 12:42:57 +01006091 n = buf_charidx_to_byteidx(buf,
6092 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006093 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006094 posp->col = n;
6095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006097 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006098 posp->coladd = 0;
6099 else
6100 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006101
Bram Moolenaar493c1782014-05-28 14:34:46 +02006102 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006104
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006105 return OK;
6106}
6107
6108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 * Get the length of an environment variable name.
6110 * Advance "arg" to the first character after the name.
6111 * Return 0 for error.
6112 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006113 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006114get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 char_u *p;
6117 int len;
6118
6119 for (p = *arg; vim_isIDc(*p); ++p)
6120 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006121 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 return 0;
6123
6124 len = (int)(p - *arg);
6125 *arg = p;
6126 return len;
6127}
6128
6129/*
6130 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006131 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 * Return 0 if something is wrong.
6133 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 char_u *p;
6138 int len;
6139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006140 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006142 {
6143 if (*p == ':')
6144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006145 // "s:" is start of "s:var", but "n:" is not and can be used in
6146 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006147 len = (int)(p - *arg);
6148 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6149 || len > 1)
6150 break;
6151 }
6152 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006153 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 return 0;
6155
6156 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006157 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 return len;
6160}
6161
6162/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006163 * Get the length of the name of a variable or function.
6164 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006166 * Return -1 if curly braces expansion failed.
6167 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * If the name contains 'magic' {}'s, expand them and return the
6169 * expanded name in an allocated string via 'alias' - caller must free.
6170 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172get_name_len(
6173 char_u **arg,
6174 char_u **alias,
6175 int evaluate,
6176 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 char_u *p;
6180 char_u *expr_start;
6181 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6186 && (*arg)[2] == (int)KE_SNR)
6187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006188 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 *arg += 3;
6190 return get_id_len(arg) + 3;
6191 }
6192 len = eval_fname_script(*arg);
6193 if (len > 0)
6194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 *arg += len;
6197 }
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006200 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006202 p = find_name_end(*arg, &expr_start, &expr_end,
6203 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (expr_start != NULL)
6205 {
6206 char_u *temp_string;
6207
6208 if (!evaluate)
6209 {
6210 len += (int)(p - *arg);
6211 *arg = skipwhite(p);
6212 return len;
6213 }
6214
6215 /*
6216 * Include any <SID> etc in the expanded string:
6217 * Thus the -len here.
6218 */
6219 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6220 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006221 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 *alias = temp_string;
6223 *arg = skipwhite(p);
6224 return (int)STRLEN(temp_string);
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006228 // Only give an error when there is something, otherwise it will be
6229 // reported at a higher level.
6230 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006231 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 return len;
6234}
6235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006236/*
6237 * Find the end of a variable or function name, taking care of magic braces.
6238 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6239 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006240 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006241 * Return a pointer to just after the name. Equal to "arg" if there is no
6242 * valid name.
6243 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006244 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006245find_name_end(
6246 char_u *arg,
6247 char_u **expr_start,
6248 char_u **expr_end,
6249 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251 int mb_nest = 0;
6252 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006254 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006255 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006257 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006259 *expr_start = NULL;
6260 *expr_end = NULL;
6261 }
6262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006264 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6265 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006266 return arg;
6267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006268 for (p = arg; *p != NUL
6269 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006270 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006271 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006272 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006273 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006274 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006275 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006276 if (*p == '\'')
6277 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006278 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006279 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006280 ;
6281 if (*p == NUL)
6282 break;
6283 }
6284 else if (*p == '"')
6285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006286 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006287 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006288 if (*p == '\\' && p[1] != NUL)
6289 ++p;
6290 if (*p == NUL)
6291 break;
6292 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006293 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006295 // "s:" is start of "s:var", but "n:" is not and can be used in
6296 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006297 len = (int)(p - arg);
6298 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006299 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006300 break;
6301 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006303 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006305 if (*p == '[')
6306 ++br_nest;
6307 else if (*p == ']')
6308 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006310
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006311 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313 if (*p == '{')
6314 {
6315 mb_nest++;
6316 if (expr_start != NULL && *expr_start == NULL)
6317 *expr_start = p;
6318 }
6319 else if (*p == '}')
6320 {
6321 mb_nest--;
6322 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6323 *expr_end = p;
6324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327
6328 return p;
6329}
6330
6331/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006332 * Expands out the 'magic' {}'s in a variable/function name.
6333 * Note that this can call itself recursively, to deal with
6334 * constructs like foo{bar}{baz}{bam}
6335 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6336 * "in_start" ^
6337 * "expr_start" ^
6338 * "expr_end" ^
6339 * "in_end" ^
6340 *
6341 * Returns a new allocated string, which the caller must free.
6342 * Returns NULL for failure.
6343 */
6344 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345make_expanded_name(
6346 char_u *in_start,
6347 char_u *expr_start,
6348 char_u *expr_end,
6349 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006350{
6351 char_u c1;
6352 char_u *retval = NULL;
6353 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006354
6355 if (expr_end == NULL || in_end == NULL)
6356 return NULL;
6357 *expr_start = NUL;
6358 *expr_end = NUL;
6359 c1 = *in_end;
6360 *in_end = NUL;
6361
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006362 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006363 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006364 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006365 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6366 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006367 if (retval != NULL)
6368 {
6369 STRCPY(retval, in_start);
6370 STRCAT(retval, temp_result);
6371 STRCAT(retval, expr_end + 1);
6372 }
6373 }
6374 vim_free(temp_result);
6375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006376 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006377 *expr_start = '{';
6378 *expr_end = '}';
6379
6380 if (retval != NULL)
6381 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006382 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006383 if (expr_start != NULL)
6384 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006385 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006386 temp_result = make_expanded_name(retval, expr_start,
6387 expr_end, temp_result);
6388 vim_free(retval);
6389 retval = temp_result;
6390 }
6391 }
6392
6393 return retval;
6394}
6395
6396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006403 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006404}
6405
6406/*
6407 * Return TRUE if character "c" can be used as the first character in a
6408 * variable or function name (excluding '{' and '}').
6409 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006412{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006413 return ASCII_ISALPHA(c) || c == '_';
6414}
6415
6416/*
6417 * Return TRUE if character "c" can be used as the first character of a
6418 * dictionary key.
6419 */
6420 int
6421eval_isdictc(int c)
6422{
6423 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424}
6425
6426/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006427 * Handle:
6428 * - expr[expr], expr[expr:expr] subscript
6429 * - ".name" lookup
6430 * - function call with Funcref variable: func(expr)
6431 * - method call: var->method()
6432 *
6433 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006434 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006435 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437handle_subscript(
6438 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006439 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006441 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006442 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006443{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006444 int evaluate = evalarg != NULL
6445 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006446 int ret = OK;
6447 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006448 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006449 int getnext;
6450 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006452 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006453 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006454 // When at the end of the line and ".name" or "->{" or "->X" follows in
6455 // the next line then consume the line break.
6456 p = eval_next_non_blank(*arg, evalarg, &getnext);
6457 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006458 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006459 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6460 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6461 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006462 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006463 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006464 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006465 check_white = FALSE;
6466 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006467
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006468 if (rettv->v_type == VAR_ANY)
6469 {
6470 char_u *exp_name;
6471 int cc;
6472 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006473 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006474 type_T *type;
6475
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006476 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006477 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006478 if (**arg != '.')
6479 {
6480 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006481 semsg(_(e_expected_dot_after_name_str),
6482 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006483 ret = FAIL;
6484 break;
6485 }
6486 ++*arg;
6487 if (IS_WHITE_OR_NUL(**arg))
6488 {
6489 if (verbose)
6490 emsg(_(e_no_white_space_allowed_after_dot));
6491 ret = FAIL;
6492 break;
6493 }
6494
6495 // isolate the name
6496 exp_name = *arg;
6497 while (eval_isnamec(**arg))
6498 ++*arg;
6499 cc = **arg;
6500 **arg = NUL;
6501
6502 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006503 evalarg == NULL ? NULL : evalarg->eval_cctx,
6504 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006505 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006506
6507 if (idx < 0 && ufunc == NULL)
6508 {
6509 ret = FAIL;
6510 break;
6511 }
6512 if (idx >= 0)
6513 {
6514 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6515 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6516
6517 copy_tv(sv->sv_tv, rettv);
6518 }
6519 else
6520 {
6521 rettv->v_type = VAR_FUNC;
6522 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6523 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006524 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006525 }
6526
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006527 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6528 || rettv->v_type == VAR_PARTIAL))
6529 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006530 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006531 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6532 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006533
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006534 // Stop the expression evaluation when immediately aborting on
6535 // error, or when an interrupt occurred or an exception was thrown
6536 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006537 if (aborting())
6538 {
6539 if (ret == OK)
6540 clear_tv(rettv);
6541 ret = FAIL;
6542 }
6543 dict_unref(selfdict);
6544 selfdict = NULL;
6545 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006546 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006547 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006548 if (in_vim9script())
6549 *arg = skipwhite(p + 2);
6550 else
6551 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006552 if (ret == OK)
6553 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006554 if (VIM_ISWHITE(**arg))
6555 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006556 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006557 ret = FAIL;
6558 }
6559 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006560 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006561 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006562 else
6563 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006564 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006565 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006566 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006567 // "." is ".name" lookup when we found a dict or when evaluating and
6568 // scriptversion is at least 2, where string concatenation is "..".
6569 else if (**arg == '['
6570 || (**arg == '.' && (rettv->v_type == VAR_DICT
6571 || (!evaluate
6572 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006573 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006574 {
6575 dict_unref(selfdict);
6576 if (rettv->v_type == VAR_DICT)
6577 {
6578 selfdict = rettv->vval.v_dict;
6579 if (selfdict != NULL)
6580 ++selfdict->dv_refcount;
6581 }
6582 else
6583 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006584 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006585 {
6586 clear_tv(rettv);
6587 ret = FAIL;
6588 }
6589 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006590 else
6591 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006592 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6595 // Don't do this when "Func" is already a partial that was bound
6596 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006597 if (selfdict != NULL
6598 && (rettv->v_type == VAR_FUNC
6599 || (rettv->v_type == VAR_PARTIAL
6600 && (rettv->vval.v_partial->pt_auto
6601 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006602 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006604 dict_unref(selfdict);
6605 return ret;
6606}
6607
6608/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 * Make a copy of an item.
6610 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006611 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006612 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6613 * reference to an already copied list/dict can be used.
6614 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617item_copy(
6618 typval_T *from,
6619 typval_T *to,
6620 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006621 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623{
6624 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626
Bram Moolenaar33570922005-01-25 22:26:29 +00006627 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006629 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 }
6632 ++recurse;
6633
6634 switch (from->v_type)
6635 {
6636 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006637 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 case VAR_STRING:
6639 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006640 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006641 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006642 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006643 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006644 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006645 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006646 copy_tv(from, to);
6647 break;
6648 case VAR_LIST:
6649 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006650 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006651 if (from->vval.v_list == NULL)
6652 to->vval.v_list = NULL;
6653 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 to->vval.v_list = from->vval.v_list->lv_copylist;
6657 ++to->vval.v_list->lv_refcount;
6658 }
6659 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006660 to->vval.v_list = list_copy(from->vval.v_list,
6661 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 if (to->vval.v_list == NULL)
6663 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006665 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006667 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 case VAR_DICT:
6669 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006670 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006671 if (from->vval.v_dict == NULL)
6672 to->vval.v_dict = NULL;
6673 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006675 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006676 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6677 ++to->vval.v_dict->dv_refcount;
6678 }
6679 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006680 to->vval.v_dict = dict_copy(from->vval.v_dict,
6681 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 if (to->vval.v_dict == NULL)
6683 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006685 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006686 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006688 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 }
6691 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006692 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006693}
6694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 void
6696echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6697{
6698 char_u *tofree;
6699 char_u numbuf[NUMBUFLEN];
6700 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6701
6702 if (*atstart)
6703 {
6704 *atstart = FALSE;
6705 // Call msg_start() after eval1(), evaluating the expression
6706 // may cause a message to appear.
6707 if (with_space)
6708 {
6709 // Mark the saved text as finishing the line, so that what
6710 // follows is displayed on a new line when scrolling back
6711 // at the more prompt.
6712 msg_sb_eol();
6713 msg_start();
6714 }
6715 }
6716 else if (with_space)
6717 msg_puts_attr(" ", echo_attr);
6718
6719 if (p != NULL)
6720 for ( ; *p != NUL && !got_int; ++p)
6721 {
6722 if (*p == '\n' || *p == '\r' || *p == TAB)
6723 {
6724 if (*p != TAB && *needclr)
6725 {
6726 // remove any text still there from the command
6727 msg_clr_eos();
6728 *needclr = FALSE;
6729 }
6730 msg_putchar_attr(*p, echo_attr);
6731 }
6732 else
6733 {
6734 if (has_mbyte)
6735 {
6736 int i = (*mb_ptr2len)(p);
6737
6738 (void)msg_outtrans_len_attr(p, i, echo_attr);
6739 p += i - 1;
6740 }
6741 else
6742 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6743 }
6744 }
6745 vim_free(tofree);
6746}
6747
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 * ":echo expr1 ..." print each argument separated with a space, add a
6750 * newline at the end.
6751 * ":echon expr1 ..." print each argument plain.
6752 */
6753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006754ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006758 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 int needclr = TRUE;
6760 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006761 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006762 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006763 evalarg_T evalarg;
6764
Bram Moolenaare707c882020-07-01 13:07:37 +02006765 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 if (eap->skip)
6768 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006769 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006771 // If eval1() causes an error message the text from the command may
6772 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006773 need_clr_eos = needclr;
6774
Bram Moolenaar68db9962021-05-09 23:19:22 +02006775 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006776 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 /*
6779 * Report the invalid expression unless the expression evaluation
6780 * has been cancelled due to an aborting error, an interrupt, or an
6781 * exception.
6782 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006783 if (!aborting() && did_emsg == did_emsg_before
6784 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006785 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006786 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 break;
6788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006789 need_clr_eos = FALSE;
6790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006792 {
6793 if (rettv.v_type == VAR_VOID)
6794 {
6795 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6796 break;
6797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006799 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 arg = skipwhite(arg);
6803 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006804 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006805 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 if (eap->skip)
6808 --emsg_skip;
6809 else
6810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006811 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (needclr)
6813 msg_clr_eos();
6814 if (eap->cmdidx == CMD_echo)
6815 msg_end();
6816 }
6817}
6818
6819/*
6820 * ":echohl {name}".
6821 */
6822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006823ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006825 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826}
6827
6828/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006829 * Returns the :echo attribute
6830 */
6831 int
6832get_echo_attr(void)
6833{
6834 return echo_attr;
6835}
6836
6837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 * ":execute expr1 ..." execute the result of an expression.
6839 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006840 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006842 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 * Each gets spaces around each argument and a newline at the end for
6844 * echo commands
6845 */
6846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006850 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 int ret = OK;
6852 char_u *p;
6853 garray_T ga;
6854 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006855 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 ga_init2(&ga, 1, 80);
6858
6859 if (eap->skip)
6860 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006861 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006863 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006864 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 if (!eap->skip)
6868 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006869 char_u buf[NUMBUFLEN];
6870
6871 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006872 {
6873 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6874 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006875 semsg(_(e_using_invalid_value_as_string_str),
6876 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006877 p = NULL;
6878 }
6879 else
6880 p = tv_get_string_buf(&rettv, buf);
6881 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006882 else
6883 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006884 if (p == NULL)
6885 {
6886 clear_tv(&rettv);
6887 ret = FAIL;
6888 break;
6889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 len = (int)STRLEN(p);
6891 if (ga_grow(&ga, len + 2) == FAIL)
6892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 ret = FAIL;
6895 break;
6896 }
6897 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 ga.ga_len += len;
6901 }
6902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 arg = skipwhite(arg);
6905 }
6906
6907 if (ret != FAIL && ga.ga_data != NULL)
6908 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006909 // use the first line of continuation lines for messages
6910 SOURCING_LNUM = start_lnum;
6911
Bram Moolenaar37fef162022-08-29 18:16:32 +01006912 if (eap->cmdidx == CMD_echomsg
6913 || eap->cmdidx == CMD_echowindow
6914 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006916 // Mark the already saved text as finishing the line, so that what
6917 // follows is displayed on a new line when scrolling back at the
6918 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006919 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006920 }
6921
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006922 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006923 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006924 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006925 out_flush();
6926 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006927 else if (eap->cmdidx == CMD_echowindow)
6928 {
6929#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006930 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006931#endif
6932 msg_attr(ga.ga_data, echo_attr);
6933#ifdef HAS_MESSAGE_WINDOW
6934 end_echowindow();
6935#endif
6936 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006937 else if (eap->cmdidx == CMD_echoconsole)
6938 {
6939 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6940 ui_write((char_u *)"\r\n", 2, TRUE);
6941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 else if (eap->cmdidx == CMD_echoerr)
6943 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006944 int save_did_emsg = did_emsg;
6945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006947 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (!force_abort)
6949 did_emsg = save_did_emsg;
6950 }
6951 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006952 {
6953 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6954
6955 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6956 sticky_cmdmod_flags = cmdmod.cmod_flags
6957 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 do_cmdline((char_u *)ga.ga_data,
6959 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006960 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
6963
6964 ga_clear(&ga);
6965
6966 if (eap->skip)
6967 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006968 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970
6971/*
6972 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6973 * "arg" points to the "&" or '+' when called, to "option" when returning.
6974 * Returns NULL when no option name found. Otherwise pointer to the char
6975 * after the option name.
6976 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006977 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006978find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 char_u *p = *arg;
6981
6982 ++p;
6983 if (*p == 'g' && p[1] == ':')
6984 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006985 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 p += 2;
6987 }
6988 else if (*p == 'l' && p[1] == ':')
6989 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006990 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 p += 2;
6992 }
6993 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006994 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 if (!ASCII_ISALPHA(*p))
6997 return NULL;
6998 *arg = p;
6999
7000 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007001 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 else
7003 while (ASCII_ISALPHA(*p))
7004 ++p;
7005 return p;
7006}
7007
7008/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007009 * Display script name where an item was last set.
7010 * Should only be invoked when 'verbose' is non-zero.
7011 */
7012 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007013last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007014{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007015 char_u *p;
7016
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007017 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007018 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007019 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007020 if (p != NULL)
7021 {
7022 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007023 msg_puts(_("\n\tLast set from "));
7024 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007025 if (script_ctx.sc_lnum > 0)
7026 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007027 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007028 msg_outnum((long)script_ctx.sc_lnum);
7029 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007030 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007031 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007032 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007033 }
7034}
7035
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007036#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038/*
7039 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007040 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * "flags" can be "g" to do a global substitute.
7042 * Returns an allocated string, NULL for error.
7043 */
7044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045do_string_sub(
7046 char_u *str,
7047 char_u *pat,
7048 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007049 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007050 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int sublen;
7053 regmatch_T regmatch;
7054 int i;
7055 int do_all;
7056 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007057 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 garray_T ga;
7059 char_u *ret;
7060 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007061 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007063 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007065 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
7067 ga_init2(&ga, 1, 200);
7068
7069 do_all = (flags[0] == 'g');
7070
7071 regmatch.rm_ic = p_ic;
7072 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7073 if (regmatch.regprog != NULL)
7074 {
7075 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007076 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007080 if (regmatch.startp[0] == regmatch.endp[0])
7081 {
7082 if (zero_width == regmatch.startp[0])
7083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007084 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007085 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007086 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7087 (size_t)i);
7088 ga.ga_len += i;
7089 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007090 continue;
7091 }
7092 zero_width = regmatch.startp[0];
7093 }
7094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 /*
7096 * Get some space for a temporary buffer to do the substitution
7097 * into. It will contain:
7098 * - The text up to where the match is.
7099 * - The substituted text.
7100 * - The text after the match.
7101 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007102 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007103 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7105 {
7106 ga_clear(&ga);
7107 break;
7108 }
7109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007110 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 i = (int)(regmatch.startp[0] - tail);
7112 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007113 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007114 (void)vim_regsub(&regmatch, sub, expr,
7115 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7116 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007118 tail = regmatch.endp[0];
7119 if (*tail == NUL)
7120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (!do_all)
7122 break;
7123 }
7124
7125 if (ga.ga_data != NULL)
7126 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7127
Bram Moolenaar473de612013-06-08 18:19:48 +02007128 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130
7131 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7132 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007133 if (p_cpo == empty_option)
7134 p_cpo = save_cpo;
7135 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007137 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007138 // If it's still empty it was changed and restored, need to restore in
7139 // the complicated way.
7140 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007141 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007142 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 return ret;
7146}