blob: 062fab0ac9493d712b76827150ee966a72da9135 [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
1379 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001380 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001381 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001382 if (len != -1)
1383 key[len] = prevval;
1384 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001385 {
1386 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001387 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001388 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001389 }
1390
Bram Moolenaar10c65862020-10-08 21:16:42 +02001391 if (lp->ll_valtype != NULL)
1392 // use the type of the member
1393 lp->ll_valtype = lp->ll_valtype->tt_member;
1394
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001396 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001397 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001398 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001399 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001400 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001401 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001402 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001403 return NULL;
1404 }
1405
Bram Moolenaar31b81602019-02-10 22:14:27 +01001406 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001408 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001410 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001411 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001413 }
1414 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001418 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001420 p = NULL;
1421 break;
1422 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001423 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001424 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001425 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1426 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001427 {
1428 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001429 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001430 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001431
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001432 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001434 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001435 else if (lp->ll_tv->v_type == VAR_BLOB)
1436 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001437 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1438
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001439 /*
1440 * Get the number and item for the only or first index of the List.
1441 */
1442 if (empty1)
1443 lp->ll_n1 = 0;
1444 else
1445 // is number or string
1446 lp->ll_n1 = (long)tv_get_number(&var1);
1447 clear_tv(&var1);
1448
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001449 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001450 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001451 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001452 return NULL;
1453 }
1454 if (lp->ll_range && !lp->ll_empty2)
1455 {
1456 lp->ll_n2 = (long)tv_get_number(&var2);
1457 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001458 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1459 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001460 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 }
1462 lp->ll_blob = lp->ll_tv->vval.v_blob;
1463 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001464 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001466 else
1467 {
1468 /*
1469 * Get the number and item for the only or first index of the List.
1470 */
1471 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001474 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001475 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001476 clear_tv(&var1);
1477
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001480 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1481 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001482 if (lp->ll_li == NULL)
1483 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001484 clear_tv(&var2);
1485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001486 }
1487
Bram Moolenaar10c65862020-10-08 21:16:42 +02001488 if (lp->ll_valtype != NULL)
1489 // use the type of the member
1490 lp->ll_valtype = lp->ll_valtype->tt_member;
1491
Bram Moolenaar8c711452005-01-14 21:53:12 +00001492 /*
1493 * May need to find the item or absolute index for the second
1494 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 * When no index given: "lp->ll_empty2" is TRUE.
1496 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001497 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001500 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001501 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001502 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001503 if (check_range_index_two(lp->ll_list,
1504 &lp->ll_n1, lp->ll_li,
1505 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001507 }
1508
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 }
1512
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001513 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 return p;
1516}
1517
1518/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001519 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001520 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523{
1524 vim_free(lp->ll_exp_name);
1525 vim_free(lp->ll_newkey);
1526}
1527
1528/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001529 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001530 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001531 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1532 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535set_var_lval(
1536 lval_T *lp,
1537 char_u *endp,
1538 typval_T *rettv,
1539 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001540 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1541 char_u *op,
1542 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001543{
1544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001545 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001546
1547 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001549 cc = *endp;
1550 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001551 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1552 return;
1553
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001554 if (lp->ll_blob != NULL)
1555 {
1556 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001557
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001558 if (op != NULL && *op != '=')
1559 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001560 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001561 return;
1562 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001563 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001564 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001565
1566 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1567 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001568 if (lp->ll_empty2)
1569 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1570
Bram Moolenaar68452172021-04-12 21:21:02 +02001571 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1572 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001573 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 }
1575 else
1576 {
1577 val = (int)tv_get_number_chk(rettv, &error);
1578 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001579 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001580 }
1581 }
1582 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001584 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001586 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1587 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001588 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001589 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001590 *endp = cc;
1591 return;
1592 }
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001595 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001596 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001597 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001598 {
1599 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001600 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1601 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001602 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001603 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001604 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001605 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001608 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001609 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001610 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1611 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001612 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001613 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001614 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001615 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001616 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001617 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001618 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001619 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001620 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001621 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 else if (lp->ll_range)
1623 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001624 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1625 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001626 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001627 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001628 return;
1629 }
1630
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001631 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1632 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001633 }
1634 else
1635 {
1636 /*
1637 * Assign to a List or Dictionary item.
1638 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001639 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1640 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001641 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001642 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001643 return;
1644 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001645
1646 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001647 && check_typval_arg_type(lp->ll_valtype, rettv,
1648 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001649 return;
1650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001651 if (lp->ll_newkey != NULL)
1652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 if (op != NULL && *op != '=')
1654 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001655 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001656 return;
1657 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001658 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1659 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001660 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001662 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001663 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001664 if (di == NULL)
1665 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001666 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1667 {
1668 vim_free(di);
1669 return;
1670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001671 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 else if (op != NULL && *op != '=')
1674 {
1675 tv_op(lp->ll_tv, rettv, op);
1676 return;
1677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001679 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001680
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001681 /*
1682 * Assign the value to the variable or list item.
1683 */
1684 if (copy)
1685 copy_tv(rettv, lp->ll_tv);
1686 else
1687 {
1688 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001689 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001690 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 }
1692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693}
1694
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001696 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1697 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 * Returns OK or FAIL.
1699 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001703 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 char_u numbuf[NUMBUFLEN];
1705 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001706 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001708 // Can't do anything with a Funcref or Dict on the right.
1709 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001710 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001711 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1712 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 {
1714 switch (tv1->v_type)
1715 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001716 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001717 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 case VAR_DICT:
1720 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001721 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001722 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001723 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001724 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001725 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001726 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 break;
1728
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 case VAR_BLOB:
1730 if (*op != '+' || tv2->v_type != VAR_BLOB)
1731 break;
1732 // BLOB += BLOB
1733 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1734 {
1735 blob_T *b1 = tv1->vval.v_blob;
1736 blob_T *b2 = tv2->vval.v_blob;
1737 int i, len = blob_len(b2);
1738 for (i = 0; i < len; i++)
1739 ga_append(&b1->bv_ga, blob_get(b2, i));
1740 }
1741 return OK;
1742
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 case VAR_LIST:
1744 if (*op != '+' || tv2->v_type != VAR_LIST)
1745 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001746 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001747 if (tv2->vval.v_list != NULL)
1748 {
1749 if (tv1->vval.v_list == NULL)
1750 {
1751 tv1->vval.v_list = tv2->vval.v_list;
1752 ++tv1->vval.v_list->lv_refcount;
1753 }
1754 else
1755 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1756 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 return OK;
1758
1759 case VAR_NUMBER:
1760 case VAR_STRING:
1761 if (tv2->v_type == VAR_LIST)
1762 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001763 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001765 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001766 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001767 if (tv2->v_type == VAR_FLOAT)
1768 {
1769 float_T f = n;
1770
Bram Moolenaarff697e62019-02-12 22:28:33 +01001771 if (*op == '%')
1772 break;
1773 switch (*op)
1774 {
1775 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001780 clear_tv(tv1);
1781 tv1->v_type = VAR_FLOAT;
1782 tv1->vval.v_float = f;
1783 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001784 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001785 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001786 switch (*op)
1787 {
1788 case '+': n += tv_get_number(tv2); break;
1789 case '-': n -= tv_get_number(tv2); break;
1790 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001791 case '/': n = num_divide(n, tv_get_number(tv2),
1792 &failed); break;
1793 case '%': n = num_modulus(n, tv_get_number(tv2),
1794 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001795 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001796 clear_tv(tv1);
1797 tv1->v_type = VAR_NUMBER;
1798 tv1->vval.v_number = n;
1799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 }
1801 else
1802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001803 if (tv2->v_type == VAR_FLOAT)
1804 break;
1805
Bram Moolenaarff697e62019-02-12 22:28:33 +01001806 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001807 s = tv_get_string(tv1);
1808 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001809 clear_tv(tv1);
1810 tv1->v_type = VAR_STRING;
1811 tv1->vval.v_string = s;
1812 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001813 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001814
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001815 case VAR_FLOAT:
1816 {
1817 float_T f;
1818
Bram Moolenaarff697e62019-02-12 22:28:33 +01001819 if (*op == '%' || *op == '.'
1820 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001821 && tv2->v_type != VAR_NUMBER
1822 && tv2->v_type != VAR_STRING))
1823 break;
1824 if (tv2->v_type == VAR_FLOAT)
1825 f = tv2->vval.v_float;
1826 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001827 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001828 switch (*op)
1829 {
1830 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001835 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001836 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 }
1838 }
1839
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001840 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 return FAIL;
1842}
1843
1844/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 * Evaluate the expression used in a ":for var in expr" command.
1846 * "arg" points to "var".
1847 * Set "*errp" to TRUE for an error, FALSE otherwise;
1848 * Return a pointer that holds the info. Null when there is an error.
1849 */
1850 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851eval_for_line(
1852 char_u *arg,
1853 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001854 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001855 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856{
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001858 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001860 typval_T tv;
1861 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001862 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001864 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001866 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 if (fi == NULL)
1868 return NULL;
1869
Bram Moolenaar404557e2021-07-05 21:41:48 +02001870 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1871 &fi->fi_semicolon, FALSE);
1872 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 return fi;
1874
Bram Moolenaar404557e2021-07-05 21:41:48 +02001875 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001876 if (expr[0] != 'i' || expr[1] != 'n'
1877 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001879 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1880 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1881 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001882 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 return fi;
1884 }
1885
1886 if (skip)
1887 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001888 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1889 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 {
1891 *errp = FALSE;
1892 if (!skip)
1893 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001894 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001895 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001896 l = tv.vval.v_list;
1897 if (l == NULL)
1898 {
1899 // a null list is like an empty list: do nothing
1900 clear_tv(&tv);
1901 }
1902 else
1903 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001905 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001907 // No need to increment the refcount, it's already set for
1908 // the list being used in "tv".
1909 fi->fi_list = l;
1910 list_add_watch(l, &fi->fi_lw);
1911 fi->fi_lw.lw_item = l->lv_first;
1912 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001913 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001914 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001915 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001916 fi->fi_bi = 0;
1917 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001918 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001919 typval_T btv;
1920
1921 // Make a copy, so that the iteration still works when the
1922 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001924 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001925 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001926 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001927 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001928 else if (tv.v_type == VAR_STRING)
1929 {
1930 fi->fi_byte_idx = 0;
1931 fi->fi_string = tv.vval.v_string;
1932 tv.vval.v_string = NULL;
1933 if (fi->fi_string == NULL)
1934 fi->fi_string = vim_strsave((char_u *)"");
1935 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 else
1937 {
Sean Dewar80d73952021-08-04 19:25:54 +02001938 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001939 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 }
1941 }
1942 }
1943 if (skip)
1944 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001945 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946
1947 return fi;
1948}
1949
1950/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001951 * Used when looping over a :for line, skip the "in expr" part.
1952 */
1953 void
1954skip_for_lines(void *fi_void, evalarg_T *evalarg)
1955{
1956 forinfo_T *fi = (forinfo_T *)fi_void;
1957 int i;
1958
1959 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001960 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001961}
1962
1963/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 * Use the first item in a ":for" list. Advance to the next.
1965 * Assign the values to the variable (list). "arg" points to the first one.
1966 * Return TRUE when a valid item was found, FALSE when at end of list or
1967 * something wrong.
1968 */
1969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001972 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001974 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001975 ? (ASSIGN_FINAL
1976 // first round: error if variable exists
1977 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001978 | ASSIGN_NO_MEMBER_TYPE
1979 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001980 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001982 int skip_assign = in_vim9script() && arg[0] == '_'
1983 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001985 if (fi->fi_blob != NULL)
1986 {
1987 typval_T tv;
1988
1989 if (fi->fi_bi >= blob_len(fi->fi_blob))
1990 return FALSE;
1991 tv.v_type = VAR_NUMBER;
1992 tv.v_lock = VAR_FIXED;
1993 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1994 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001995 if (skip_assign)
1996 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001997 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001998 fi->fi_varcount, flag, NULL) == OK;
1999 }
2000
2001 if (fi->fi_string != NULL)
2002 {
2003 typval_T tv;
2004 int len;
2005
2006 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2007 if (len == 0)
2008 return FALSE;
2009 tv.v_type = VAR_STRING;
2010 tv.v_lock = VAR_FIXED;
2011 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2012 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002013 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002014 if (skip_assign)
2015 result = TRUE;
2016 else
2017 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002018 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002019 vim_free(tv.vval.v_string);
2020 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002021 }
2022
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 item = fi->fi_lw.lw_item;
2024 if (item == NULL)
2025 result = FALSE;
2026 else
2027 {
2028 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002029 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002030 if (skip_assign)
2031 result = TRUE;
2032 else
2033 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002034 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 }
2036 return result;
2037}
2038
2039/*
2040 * Free the structure used to store info used by ":for".
2041 */
2042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002043free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044{
Bram Moolenaar33570922005-01-25 22:26:29 +00002045 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002047 if (fi == NULL)
2048 return;
2049 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002050 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002052 list_unref(fi->fi_list);
2053 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002054 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002055 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002056 else
2057 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 vim_free(fi);
2059}
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002062set_context_for_expression(
2063 expand_T *xp,
2064 char_u *arg,
2065 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002067 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002071 if (cmdidx == CMD_let || cmdidx == CMD_var
2072 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 {
2074 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002077 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 {
2080 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002081 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002082 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083 break;
2084 }
2085 return;
2086 }
2087 }
2088 else
2089 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2090 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 while ((xp->xp_pattern = vim_strpbrk(arg,
2092 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2093 {
2094 c = *xp->xp_pattern;
2095 if (c == '&')
2096 {
2097 c = xp->xp_pattern[1];
2098 if (c == '&')
2099 {
2100 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002101 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002106 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2107 xp->xp_pattern += 2;
2108
2109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 else if (c == '$')
2112 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002113 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 xp->xp_context = EXPAND_ENV_VARS;
2115 }
2116 else if (c == '=')
2117 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002118 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 xp->xp_context = EXPAND_EXPRESSION;
2120 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002121 else if (c == '#'
2122 && xp->xp_context == EXPAND_EXPRESSION)
2123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002124 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002125 break;
2126 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002127 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 && xp->xp_context == EXPAND_FUNCTIONS
2129 && vim_strchr(xp->xp_pattern, '(') == NULL)
2130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002131 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 break;
2133 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002134 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002136 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2139 if (c == '\\' && xp->xp_pattern[1] != NUL)
2140 ++xp->xp_pattern;
2141 xp->xp_context = EXPAND_NOTHING;
2142 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002143 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002145 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2147 /* skip */ ;
2148 xp->xp_context = EXPAND_NOTHING;
2149 }
2150 else if (c == '|')
2151 {
2152 if (xp->xp_pattern[1] == '|')
2153 {
2154 ++xp->xp_pattern;
2155 xp->xp_context = EXPAND_EXPRESSION;
2156 }
2157 else
2158 xp->xp_context = EXPAND_COMMANDS;
2159 }
2160 else
2161 xp->xp_context = EXPAND_EXPRESSION;
2162 }
2163 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002164 // Doesn't look like something valid, expand as an expression
2165 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 arg = xp->xp_pattern;
2168 if (*arg != NUL)
2169 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2170 /* skip */ ;
2171 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002172
2173 // ":exe one two" completes "two"
2174 if ((cmdidx == CMD_execute
2175 || cmdidx == CMD_echo
2176 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002177 || cmdidx == CMD_echomsg
2178 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002179 && xp->xp_context == EXPAND_EXPRESSION)
2180 {
2181 for (;;)
2182 {
2183 char_u *n = skiptowhite(arg);
2184
2185 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2186 break;
2187 arg = skipwhite(n);
2188 }
2189 }
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 xp->xp_pattern = arg;
2192}
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002195 * Return TRUE if "pat" matches "text".
2196 * Does not use 'cpo' and always uses 'magic'.
2197 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002198 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002199pattern_match(char_u *pat, char_u *text, int ic)
2200{
2201 int matches = FALSE;
2202 char_u *save_cpo;
2203 regmatch_T regmatch;
2204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002205 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002206 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002207 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002208 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2209 if (regmatch.regprog != NULL)
2210 {
2211 regmatch.rm_ic = ic;
2212 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2213 vim_regfree(regmatch.regprog);
2214 }
2215 p_cpo = save_cpo;
2216 return matches;
2217}
2218
2219/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002220 * Handle a name followed by "(". Both for just "name(arg)" and for
2221 * "expr->name(arg)".
2222 * Returns OK or FAIL.
2223 */
2224 static int
2225eval_func(
2226 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002227 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002228 char_u *name,
2229 int name_len,
2230 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002231 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002232 typval_T *basetv) // "expr" for "expr->name(arg)"
2233{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002235 char_u *s = name;
2236 int len = name_len;
2237 partial_T *partial;
2238 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002239 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002240 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002241
2242 if (!evaluate)
2243 check_vars(s, len);
2244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 // If "s" is the name of a variable of type VAR_FUNC
2246 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002247 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002248 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002250 // Need to make a copy, in case evaluating the arguments makes
2251 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002252 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002253 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002254 ret = FAIL;
2255 else
2256 {
2257 funcexe_T funcexe;
2258
2259 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002260 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002261 funcexe.fe_firstline = curwin->w_cursor.lnum;
2262 funcexe.fe_lastline = curwin->w_cursor.lnum;
2263 funcexe.fe_evaluate = evaluate;
2264 funcexe.fe_partial = partial;
2265 funcexe.fe_basetv = basetv;
2266 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002267 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002268 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002269 }
2270 vim_free(s);
2271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002272 // If evaluate is FALSE rettv->v_type was not set in
2273 // get_func_tv, but it's needed in handle_subscript() to parse
2274 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002275 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2276 {
2277 rettv->vval.v_string = NULL;
2278 rettv->v_type = VAR_FUNC;
2279 }
2280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002281 // Stop the expression evaluation when immediately
2282 // aborting on error, or when an interrupt occurred or
2283 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002284 if (evaluate && aborting())
2285 {
2286 if (ret == OK)
2287 clear_tv(rettv);
2288 ret = FAIL;
2289 }
2290 return ret;
2291}
2292
2293/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002294 * After a NL, skip over empty lines and comment-only lines.
2295 */
2296 static char_u *
2297newline_skip_comments(char_u *arg)
2298{
2299 char_u *p = arg + 1;
2300
2301 for (;;)
2302 {
2303 p = skipwhite(p);
2304
2305 if (*p == NUL)
2306 break;
2307 if (vim9_comment_start(p))
2308 {
2309 char_u *nl = vim_strchr(p, NL);
2310
2311 if (nl == NULL)
2312 break;
2313 p = nl;
2314 }
2315 if (*p != NL)
2316 break;
2317 ++p; // skip another NL
2318 }
2319 return p;
2320}
2321
2322/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002323 * Get the next line source line without advancing. But do skip over comment
2324 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002325 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002326 */
2327 static char_u *
2328getline_peek_skip_comments(evalarg_T *evalarg)
2329{
2330 for (;;)
2331 {
2332 char_u *next = getline_peek(evalarg->eval_getline,
2333 evalarg->eval_cookie);
2334 char_u *p;
2335
2336 if (next == NULL)
2337 break;
2338 p = skipwhite(next);
2339 if (*p != NUL && !vim9_comment_start(p))
2340 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002341 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002342 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002343 }
2344 return NULL;
2345}
2346
2347/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002348 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2349 * comment) and there is a next line, return the next line (skipping blanks)
2350 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002351 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2352 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353 * "arg" must point somewhere inside a line, not at the start.
2354 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002355 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2357{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002358 char_u *p = skipwhite(arg);
2359
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002361 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002362 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002363 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2364 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002365 && (*p == NUL || *p == NL
2366 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002367 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002368 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002369
Bram Moolenaare442d592022-05-05 12:20:28 +01002370 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002371 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002372 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002373 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002374 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002375 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376
Bram Moolenaar9d489562020-07-30 20:08:50 +02002377 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002378 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002379 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002380 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 }
2382 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002383 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384}
2385
2386/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002387 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002388 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002389 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002390 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002391eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002392{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002393 garray_T *gap = &evalarg->eval_ga;
2394 char_u *line;
2395
Bram Moolenaar39be4982022-05-06 21:51:50 +01002396 if (arg != NULL)
2397 {
2398 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002399 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002400 // Truncate before a trailing comment, so that concatenating the lines
2401 // won't turn the rest into a comment.
2402 if (*skipwhite(arg) == '#')
2403 *arg = NUL;
2404 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002405
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002406 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002407 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2408 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002409 else
2410 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002411 if (line == NULL)
2412 return NULL;
2413
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002414 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002415 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2416 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002417 char_u *p = skipwhite(line);
2418
2419 // Going to concatenate the lines after parsing. For an empty or
2420 // comment line use an empty string.
2421 if (*p == NUL || vim9_comment_start(p))
2422 {
2423 vim_free(line);
2424 line = vim_strsave((char_u *)"");
2425 }
2426
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002427 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2428 ++gap->ga_len;
2429 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002430 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002431 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002432 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002433 evalarg->eval_tofree = line;
2434 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002435
2436 // Advanced to the next line, "arg" no longer points into the previous
2437 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002438 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002439 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002440}
2441
Bram Moolenaare6b53242020-07-01 17:28:33 +02002442/*
2443 * Call eval_next_non_blank() and get the next line if needed.
2444 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002445 char_u *
2446skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2447{
2448 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002449 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002450
Bram Moolenaar962d7212020-07-04 14:15:00 +02002451 if (evalarg == NULL)
2452 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002453 eval_next_non_blank(p, evalarg, &getnext);
2454 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002455 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002456 return p;
2457}
2458
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002459/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002460 * The "eval" functions have an "evalarg" argument: When NULL or
2461 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2462 * parsed but not executed. The functions may return OK, but the rettv will be
2463 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 */
2465
2466/*
2467 * Handle zero level expression.
2468 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002470 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 * Return OK or FAIL.
2473 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002475eval0(
2476 char_u *arg,
2477 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002478 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002481 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2482}
2483
2484/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002485 * If "arg" is a simple function call without arguments then call it and return
2486 * the result. Otherwise return NOTDONE.
2487 */
2488 int
2489may_call_simple_func(
2490 char_u *arg,
2491 typval_T *rettv)
2492{
2493 char_u *parens = (char_u *)strstr((char *)arg, "()");
2494 int r = NOTDONE;
2495
2496 // If the expression is "FuncName()" then we can skip a lot of overhead.
2497 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2498 {
2499 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2500
2501 if (to_name_end(p, TRUE) == parens)
2502 r = call_simple_func(arg, (int)(parens - arg), rettv);
2503 }
2504 return r;
2505}
2506
2507/*
2508 * Handle zero level expression with optimization for a simple function call.
2509 * Same arguments and return value as eval0().
2510 */
2511 int
2512eval0_simple_funccal(
2513 char_u *arg,
2514 typval_T *rettv,
2515 exarg_T *eap,
2516 evalarg_T *evalarg)
2517{
2518 int r = may_call_simple_func(arg, rettv);
2519
2520 if (r == NOTDONE)
2521 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2522 return r;
2523}
2524
2525/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002526 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2527 * expression and don't check what comes after the expression.
2528 */
2529 int
2530eval0_retarg(
2531 char_u *arg,
2532 typval_T *rettv,
2533 exarg_T *eap,
2534 evalarg_T *evalarg,
2535 char_u **retarg)
2536{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 int ret;
2538 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002539 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002540 int did_emsg_before = did_emsg;
2541 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002543 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002544 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002547 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002548
Bram Moolenaar79481362022-06-27 20:15:10 +01002549 if (ret != FAIL)
2550 {
2551 expr_end = p;
2552 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002553
Bram Moolenaar79481362022-06-27 20:15:10 +01002554 // In Vim9 script a command block is not split at NL characters for
2555 // commands using an expression argument. Skip over a '#' comment to
2556 // check for a following NL. Require white space before the '#'.
2557 if (in_vim9script() && p > expr_end && retarg == NULL)
2558 while (*p == '#')
2559 {
2560 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002561
Bram Moolenaar79481362022-06-27 20:15:10 +01002562 if (nl == NULL)
2563 break;
2564 p = skipwhite(nl + 1);
2565 if (eap != NULL && *p != NUL)
2566 eap->nextcmd = p;
2567 check_for_end = FALSE;
2568 }
2569
2570 if (check_for_end)
2571 end_error = !ends_excmd2(arg, p);
2572 }
2573
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002574 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /*
2579 * Report the invalid expression unless the expression evaluation has
2580 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002581 * exception, or we already gave a more specific error.
2582 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002584 if (!aborting()
2585 && did_emsg == did_emsg_before
2586 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002587 && (flags & EVAL_CONSTANT) == 0
2588 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002589 {
2590 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002591 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002592 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002593 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002594 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002595
2596 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002597 // a next command to avoid more errors, unless "|" is following, which
2598 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002599 if (eap != NULL && p != NULL
2600 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002601 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002604
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002605 if (retarg != NULL)
2606 *retarg = p;
2607 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002608 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 return ret;
2611}
2612
2613/*
2614 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002615 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002616 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 *
2618 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002619 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002621 * Note: "rettv.v_lock" is not set.
2622 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * Return OK or FAIL.
2624 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002625 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002628 char_u *p;
2629 int getnext;
2630
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002631 CLEAR_POINTER(rettv);
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * Get the first variable.
2635 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return FAIL;
2638
Bram Moolenaar793648f2020-06-26 21:28:25 +02002639 p = eval_next_non_blank(*arg, evalarg, &getnext);
2640 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002642 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 int result;
2644 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 evalarg_T *evalarg_used = evalarg;
2646 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002648 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002649 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650
2651 if (evalarg == NULL)
2652 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002653 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 orig_flags = evalarg_used->eval_flags;
2657 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002660 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002661 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002662 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002663 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002664 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002665 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002666 clear_tv(rettv);
2667 return FAIL;
2668 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002669 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002670 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675 int error = FALSE;
2676
Bram Moolenaar13106602020-10-04 16:06:05 +02002677 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002678 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002679 else if (vim9script)
2680 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002681 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002683 if (error || !op_falsy || !result)
2684 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 if (error)
2686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
2689 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002690 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002692 if (op_falsy)
2693 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002694 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002695 {
mityu4ac198c2021-05-28 17:52:40 +02002696 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002697 clear_tv(rettv);
2698 return FAIL;
2699 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002701 evalarg_used->eval_flags = (op_falsy ? !result : result)
2702 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2703 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002704 {
2705 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002707 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002708 if (!op_falsy || !result)
2709 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002711 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002713 /*
2714 * Check for the ":".
2715 */
2716 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2717 if (*p != ':')
2718 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002719 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002720 if (evaluate && result)
2721 clear_tv(rettv);
2722 evalarg_used->eval_flags = orig_flags;
2723 return FAIL;
2724 }
2725 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002726 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002727 else
2728 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002729 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002730 {
2731 error_white_both(p, 1);
2732 clear_tv(rettv);
2733 evalarg_used->eval_flags = orig_flags;
2734 return FAIL;
2735 }
2736 *arg = p;
2737 }
2738
2739 /*
2740 * Get the third variable. Recursive!
2741 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002742 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002743 {
mityu4ac198c2021-05-28 17:52:40 +02002744 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002745 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002746 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002747 return FAIL;
2748 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002749 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2750 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002752 if (eval1(arg, &var2, evalarg_used) == FAIL)
2753 {
2754 if (evaluate && result)
2755 clear_tv(rettv);
2756 evalarg_used->eval_flags = orig_flags;
2757 return FAIL;
2758 }
2759 if (evaluate && !result)
2760 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762
2763 if (evalarg == NULL)
2764 clear_evalarg(&local_evalarg, NULL);
2765 else
2766 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768
2769 return OK;
2770}
2771
2772/*
2773 * Handle first level expression:
2774 * expr2 || expr2 || expr2 logical OR
2775 *
2776 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002777 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 *
2779 * Return OK or FAIL.
2780 */
2781 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002784 char_u *p;
2785 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002788 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002790 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return FAIL;
2792
2793 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002794 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002796 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799 evalarg_T *evalarg_used = evalarg;
2800 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002801 int evaluate;
2802 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002803 long result = FALSE;
2804 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002805 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002806 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002807
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002808 if (evalarg == NULL)
2809 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002810 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002811 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 orig_flags = evalarg_used->eval_flags;
2814 evaluate = orig_flags & EVAL_EVALUATE;
2815 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002817 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002818 result = tv_get_bool_chk(rettv, &error);
2819 else if (tv_get_number_chk(rettv, &error) != 0)
2820 result = TRUE;
2821 clear_tv(rettv);
2822 if (error)
2823 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825
2826 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002827 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002829 while (p[0] == '|' && p[1] == '|')
2830 {
2831 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002832 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002833 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002834 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002835 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002836 {
2837 error_white_both(p, 2);
2838 clear_tv(rettv);
2839 return FAIL;
2840 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002841 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002842 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002843
2844 /*
2845 * Get the second variable.
2846 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002847 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002848 {
mityu4ac198c2021-05-28 17:52:40 +02002849 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002850 clear_tv(rettv);
2851 return FAIL;
2852 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002853 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2854 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002856 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858
2859 /*
2860 * Compute the result.
2861 */
2862 if (evaluate && !result)
2863 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002864 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002865 result = tv_get_bool_chk(&var2, &error);
2866 else if (tv_get_number_chk(&var2, &error) != 0)
2867 result = TRUE;
2868 clear_tv(&var2);
2869 if (error)
2870 return FAIL;
2871 }
2872 if (evaluate)
2873 {
2874 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002875 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002876 rettv->v_type = VAR_BOOL;
2877 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002878 }
2879 else
2880 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002881 rettv->v_type = VAR_NUMBER;
2882 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002884 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002885
2886 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002888
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002889 if (evalarg == NULL)
2890 clear_evalarg(&local_evalarg, NULL);
2891 else
2892 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894
2895 return OK;
2896}
2897
2898/*
2899 * Handle second level expression:
2900 * expr3 && expr3 && expr3 logical AND
2901 *
2902 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002903 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 *
2905 * Return OK or FAIL.
2906 */
2907 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002910 char_u *p;
2911 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002914 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 return FAIL;
2918
2919 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002920 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002922 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002923 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002925 evalarg_T *evalarg_used = evalarg;
2926 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 int orig_flags;
2928 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002929 long result = TRUE;
2930 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002931 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002932 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002933
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002934 if (evalarg == NULL)
2935 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002936 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002937 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002938 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002939 orig_flags = evalarg_used->eval_flags;
2940 evaluate = orig_flags & EVAL_EVALUATE;
2941 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002942 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002943 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002944 result = tv_get_bool_chk(rettv, &error);
2945 else if (tv_get_number_chk(rettv, &error) == 0)
2946 result = FALSE;
2947 clear_tv(rettv);
2948 if (error)
2949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951
2952 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002953 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002955 while (p[0] == '&' && p[1] == '&')
2956 {
2957 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002958 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002959 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002960 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002961 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002962 {
2963 error_white_both(p, 2);
2964 clear_tv(rettv);
2965 return FAIL;
2966 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002967 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002968 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002969
2970 /*
2971 * Get the second variable.
2972 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002973 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002974 {
mityu4ac198c2021-05-28 17:52:40 +02002975 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002976 clear_tv(rettv);
2977 return FAIL;
2978 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002979 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2980 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002982 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002983 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002985
2986 /*
2987 * Compute the result.
2988 */
2989 if (evaluate && result)
2990 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002991 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002992 result = tv_get_bool_chk(&var2, &error);
2993 else if (tv_get_number_chk(&var2, &error) == 0)
2994 result = FALSE;
2995 clear_tv(&var2);
2996 if (error)
2997 return FAIL;
2998 }
2999 if (evaluate)
3000 {
3001 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003002 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003003 rettv->v_type = VAR_BOOL;
3004 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003005 }
3006 else
3007 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003008 rettv->v_type = VAR_NUMBER;
3009 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003010 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003011 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003012
3013 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003015
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003016 if (evalarg == NULL)
3017 clear_evalarg(&local_evalarg, NULL);
3018 else
3019 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021
3022 return OK;
3023}
3024
3025/*
3026 * Handle third level expression:
3027 * var1 == var2
3028 * var1 =~ var2
3029 * var1 != var2
3030 * var1 !~ var2
3031 * var1 > var2
3032 * var1 >= var2
3033 * var1 < var2
3034 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003035 * var1 is var2
3036 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 *
3038 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003039 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 *
3041 * Return OK or FAIL.
3042 */
3043 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003044eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003047 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003048 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003050 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
3052 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003053 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 return FAIL;
3057
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003058 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003059
Bram Moolenaar696ba232020-07-29 21:20:41 +02003060 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003065 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003067 typval_T var2;
3068 int ic;
3069 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003070 int evaluate = evalarg == NULL
3071 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003072 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003073
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003074 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003075 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003076 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003077 p = *arg;
3078 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003079 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3080 {
mityu4ac198c2021-05-28 17:52:40 +02003081 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003082 clear_tv(rettv);
3083 return FAIL;
3084 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003085
Bram Moolenaar696ba232020-07-29 21:20:41 +02003086 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3087 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003088 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003089 clear_tv(rettv);
3090 return FAIL;
3091 }
3092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (p[len] == '?')
3095 {
3096 ic = TRUE;
3097 ++len;
3098 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else if (p[len] == '#')
3101 {
3102 ic = FALSE;
3103 ++len;
3104 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003105 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003107 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 /*
3110 * Get the second variable.
3111 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003112 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3113 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003114 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003115 clear_tv(rettv);
3116 return FAIL;
3117 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003118 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003119 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 return FAIL;
3123 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003124 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003125 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003126 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003127
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003128 // use the line of the comparison for messages
3129 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003130 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003131 {
3132 ret = FAIL;
3133 clear_tv(rettv);
3134 }
3135 else
3136 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003137 clear_tv(&var2);
3138 return ret;
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
3141
3142 return OK;
3143}
3144
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003145/*
3146 * Make a copy of blob "tv1" and append blob "tv2".
3147 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 void
3149eval_addblob(typval_T *tv1, typval_T *tv2)
3150{
3151 blob_T *b1 = tv1->vval.v_blob;
3152 blob_T *b2 = tv2->vval.v_blob;
3153 blob_T *b = blob_alloc();
3154 int i;
3155
3156 if (b != NULL)
3157 {
3158 for (i = 0; i < blob_len(b1); i++)
3159 ga_append(&b->bv_ga, blob_get(b1, i));
3160 for (i = 0; i < blob_len(b2); i++)
3161 ga_append(&b->bv_ga, blob_get(b2, i));
3162
3163 clear_tv(tv1);
3164 rettv_blob_set(tv1, b);
3165 }
3166}
3167
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003168/*
3169 * Make a copy of list "tv1" and append list "tv2".
3170 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 int
3172eval_addlist(typval_T *tv1, typval_T *tv2)
3173{
3174 typval_T var3;
3175
3176 // concatenate Lists
3177 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3178 {
3179 clear_tv(tv1);
3180 clear_tv(tv2);
3181 return FAIL;
3182 }
3183 clear_tv(tv1);
3184 *tv1 = var3;
3185 return OK;
3186}
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003189 * Handle the bitwise left/right shift operator expression:
3190 * var1 << var2
3191 * var1 >> var2
3192 *
3193 * "arg" must point to the first non-white of the expression.
3194 * "arg" is advanced to just after the recognized expression.
3195 *
3196 * Return OK or FAIL.
3197 */
3198 static int
3199eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3200{
3201 /*
3202 * Get the first expression.
3203 */
3204 if (eval6(arg, rettv, evalarg) == FAIL)
3205 return FAIL;
3206
3207 /*
3208 * Repeat computing, until no '<<' or '>>' is following.
3209 */
3210 for (;;)
3211 {
3212 char_u *p;
3213 int getnext;
3214 exprtype_T type;
3215 int evaluate;
3216 typval_T var2;
3217 int vim9script;
3218
3219 p = eval_next_non_blank(*arg, evalarg, &getnext);
3220 if (p[0] == '<' && p[1] == '<')
3221 type = EXPR_LSHIFT;
3222 else if (p[0] == '>' && p[1] == '>')
3223 type = EXPR_RSHIFT;
3224 else
3225 return OK;
3226
3227 // Handle a bitwise left or right shift operator
3228 if (rettv->v_type != VAR_NUMBER)
3229 {
3230 // left operand should be a number
3231 emsg(_(e_bitshift_ops_must_be_number));
3232 clear_tv(rettv);
3233 return FAIL;
3234 }
3235
3236 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3237 vim9script = in_vim9script();
3238 if (getnext)
3239 {
3240 *arg = eval_next_line(*arg, evalarg);
3241 p = *arg;
3242 }
3243 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3244 {
3245 error_white_both(*arg, 2);
3246 clear_tv(rettv);
3247 return FAIL;
3248 }
3249
3250 /*
3251 * Get the second variable.
3252 */
3253 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3254 {
3255 error_white_both(p, 2);
3256 clear_tv(rettv);
3257 return FAIL;
3258 }
3259 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3260 if (eval6(arg, &var2, evalarg) == FAIL)
3261 {
3262 clear_tv(rettv);
3263 return FAIL;
3264 }
3265
3266 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3267 {
3268 // right operand should be a positive number
3269 if (var2.v_type != VAR_NUMBER)
3270 emsg(_(e_bitshift_ops_must_be_number));
3271 else
3272 emsg(_(e_bitshift_ops_must_be_postive));
3273 clear_tv(rettv);
3274 clear_tv(&var2);
3275 return FAIL;
3276 }
3277
3278 if (evaluate)
3279 {
3280 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3281 // shifting more bits than we have always results in zero
3282 rettv->vval.v_number = 0;
3283 else if (type == EXPR_LSHIFT)
3284 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003285 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003286 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003287 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003288 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003289 }
3290
3291 clear_tv(&var2);
3292 }
3293
3294 return OK;
3295}
3296
3297/*
3298 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003299 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003301 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003302 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 *
3304 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003305 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 *
3307 * Return OK or FAIL.
3308 */
3309 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003310eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003313 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003315 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 return FAIL;
3317
3318 /*
3319 * Repeat computing, until no '+', '-' or '.' is following.
3320 */
3321 for (;;)
3322 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003323 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003324 int getnext;
3325 char_u *p;
3326 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003327 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328 int concat;
3329 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003330 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003331
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003332 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003333 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003334 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003335 p = eval_next_non_blank(*arg, evalarg, &getnext);
3336 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003337 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003338 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3339 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003341 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3342 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003343
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003344 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003345 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003346 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003347 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003348 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003349 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003350 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003351 {
mityu4ac198c2021-05-28 17:52:40 +02003352 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003353 clear_tv(rettv);
3354 return FAIL;
3355 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003356 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003357 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003358 if ((op != '+' || (rettv->v_type != VAR_LIST
3359 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003361 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003363 int error = FALSE;
3364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // For "list + ...", an illegal use of the first operand as
3366 // a number cannot be determined before evaluating the 2nd
3367 // operand: if this is also a list, all is ok.
3368 // For "something . ...", "something - ..." or "non-list + ...",
3369 // we know that the first operand needs to be a string or number
3370 // without evaluating the 2nd operand. So check before to avoid
3371 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003372 if (op != '.')
3373 tv_get_number_chk(rettv, &error);
3374 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003375 {
3376 clear_tv(rettv);
3377 return FAIL;
3378 }
3379 }
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 /*
3382 * Get the second variable.
3383 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003384 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003385 {
mityu89dcb4d2021-05-28 13:50:17 +02003386 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003387 clear_tv(rettv);
3388 return FAIL;
3389 }
3390 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003391 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return FAIL;
3395 }
3396
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003397 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 /*
3400 * Compute the result.
3401 */
3402 if (op == '.')
3403 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003404 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3405 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003406 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003407
Bram Moolenaar2e086612020-08-25 22:37:48 +02003408 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003409 || var2.v_type == VAR_CHANNEL
3410 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003411 semsg(_(e_using_invalid_value_as_string_str),
3412 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003413 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003414 {
3415 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3416 var2.vval.v_float);
3417 s2 = buf2;
3418 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003419 else
3420 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003421 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 {
3423 clear_tv(rettv);
3424 clear_tv(&var2);
3425 return FAIL;
3426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003427 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 clear_tv(rettv);
3429 rettv->v_type = VAR_STRING;
3430 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003432 else if (op == '+' && rettv->v_type == VAR_BLOB
3433 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003435 else if (op == '+' && rettv->v_type == VAR_LIST
3436 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003437 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003439 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 else
3442 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003443 int error = FALSE;
3444 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003445 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003447 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003448 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003449 f1 = rettv->vval.v_float;
3450 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003452 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003454 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003455 if (error)
3456 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003457 // This can only happen for "list + non-list" or
3458 // "blob + non-blob". For "non-list + ..." or
3459 // "something - ...", we returned before evaluating the
3460 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003461 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003462 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003463 return FAIL;
3464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003465 if (var2.v_type == VAR_FLOAT)
3466 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003467 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003468 if (var2.v_type == VAR_FLOAT)
3469 {
3470 f2 = var2.vval.v_float;
3471 n2 = 0;
3472 }
3473 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003475 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003476 if (error)
3477 {
3478 clear_tv(rettv);
3479 clear_tv(&var2);
3480 return FAIL;
3481 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003482 if (rettv->v_type == VAR_FLOAT)
3483 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003488 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3489 {
3490 if (op == '+')
3491 f1 = f1 + f2;
3492 else
3493 f1 = f1 - f2;
3494 rettv->v_type = VAR_FLOAT;
3495 rettv->vval.v_float = f1;
3496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003498 {
3499 if (op == '+')
3500 n1 = n1 + n2;
3501 else
3502 n1 = n1 - n2;
3503 rettv->v_type = VAR_NUMBER;
3504 rettv->vval.v_number = n1;
3505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 }
3510 return OK;
3511}
3512
3513/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003514 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 * * number multiplication
3516 * / number division
3517 * % number modulo
3518 *
3519 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003520 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 *
3522 * Return OK or FAIL.
3523 */
3524 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003525eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003526 char_u **arg,
3527 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003528 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003529 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003531 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003534 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003536 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 return FAIL;
3538
3539 /*
3540 * Repeat computing, until no '*', '/' or '%' is following.
3541 */
3542 for (;;)
3543 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003544 int evaluate;
3545 int getnext;
3546 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003547 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003548 int op;
3549 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003550 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003551 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003552
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003553 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003554 p = eval_next_non_blank(*arg, evalarg, &getnext);
3555 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003556 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003558
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003559 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003560 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003561 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003562 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003563 {
3564 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3565 {
mityu4ac198c2021-05-28 17:52:40 +02003566 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003567 clear_tv(rettv);
3568 return FAIL;
3569 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003570 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003573 f1 = 0;
3574 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003575 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003576 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578 if (rettv->v_type == VAR_FLOAT)
3579 {
3580 f1 = rettv->vval.v_float;
3581 use_float = TRUE;
3582 n1 = 0;
3583 }
3584 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003587 if (error)
3588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 else
3591 n1 = 0;
3592
3593 /*
3594 * Get the second variable.
3595 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003596 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3597 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003598 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003599 clear_tv(rettv);
3600 return FAIL;
3601 }
3602 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003603 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 return FAIL;
3605
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003606 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003608 if (var2.v_type == VAR_FLOAT)
3609 {
3610 if (!use_float)
3611 {
3612 f1 = n1;
3613 use_float = TRUE;
3614 }
3615 f2 = var2.vval.v_float;
3616 n2 = 0;
3617 }
3618 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003620 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003621 clear_tv(&var2);
3622 if (error)
3623 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003624 if (use_float)
3625 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /*
3629 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003630 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003632 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003634 if (op == '*')
3635 f1 = f1 * f2;
3636 else if (op == '/')
3637 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003638#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003640 if (f2 == 0.0)
3641 {
3642 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003644 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003645 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003646 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003647 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003648 }
3649 else
3650 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003651#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 // We rely on the floating point library to handle divide
3653 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003654 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003655#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003658 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003659 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003660 return FAIL;
3661 }
3662 rettv->v_type = VAR_FLOAT;
3663 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 else
3666 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003667 int failed = FALSE;
3668
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003669 if (op == '*')
3670 n1 = n1 * n2;
3671 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003672 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003674 n1 = num_modulus(n1, n2, &failed);
3675 if (failed)
3676 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003678 rettv->v_type = VAR_NUMBER;
3679 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 }
3683
3684 return OK;
3685}
3686
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003687/*
3688 * Handle a type cast before a base level expression.
3689 * "arg" must point to the first non-white of the expression.
3690 * "arg" is advanced to just after the recognized expression.
3691 * Return OK or FAIL.
3692 */
3693 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003694eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003695 char_u **arg,
3696 typval_T *rettv,
3697 evalarg_T *evalarg,
3698 int want_string) // after "." operator
3699{
3700 type_T *want_type = NULL;
3701 garray_T type_list; // list of pointers to allocated types
3702 int res;
3703 int evaluate = evalarg == NULL ? 0
3704 : (evalarg->eval_flags & EVAL_EVALUATE);
3705
3706 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003707 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3708 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003709 {
3710 ++*arg;
3711 ga_init2(&type_list, sizeof(type_T *), 10);
3712 want_type = parse_type(arg, &type_list, TRUE);
3713 if (want_type == NULL && (evaluate || **arg != '>'))
3714 {
3715 clear_type_list(&type_list);
3716 return FAIL;
3717 }
3718
3719 if (**arg != '>')
3720 {
3721 if (*skipwhite(*arg) == '>')
3722 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3723 else
3724 emsg(_(e_missing_gt));
3725 clear_type_list(&type_list);
3726 return FAIL;
3727 }
3728 ++*arg;
3729 *arg = skipwhite_and_linebreak(*arg, evalarg);
3730 }
3731
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003732 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003733
3734 if (want_type != NULL && evaluate)
3735 {
3736 if (res == OK)
3737 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003738 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3739 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003740
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003741 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003742 {
3743 if (want_type == &t_bool && actual != &t_bool
3744 && (actual->tt_flags & TTFLAG_BOOL_OK))
3745 {
3746 int n = tv2bool(rettv);
3747
3748 // can use "0" and "1" for boolean in some places
3749 clear_tv(rettv);
3750 rettv->v_type = VAR_BOOL;
3751 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3752 }
3753 else
3754 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003755 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003756
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003757 where.wt_variable = TRUE;
3758 res = check_type(want_type, actual, TRUE, where);
3759 }
3760 }
3761 }
3762 clear_type_list(&type_list);
3763 }
3764
3765 return res;
3766}
3767
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003768 int
3769eval_leader(char_u **arg, int vim9)
3770{
3771 char_u *s = *arg;
3772 char_u *p = *arg;
3773
3774 while (*p == '!' || *p == '-' || *p == '+')
3775 {
3776 char_u *n = skipwhite(p + 1);
3777
3778 // ++, --, -+ and +- are not accepted in Vim9 script
3779 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3780 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003781 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003782 return FAIL;
3783 }
3784 p = n;
3785 }
3786 *arg = p;
3787 return OK;
3788}
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003791 * Check for a predefined value "true", "false" and "null.*".
3792 * Return OK when recognized.
3793 */
3794 int
3795handle_predefined(char_u *s, int len, typval_T *rettv)
3796{
3797 switch (len)
3798 {
3799 case 4: if (STRNCMP(s, "true", 4) == 0)
3800 {
3801 rettv->v_type = VAR_BOOL;
3802 rettv->vval.v_number = VVAL_TRUE;
3803 return OK;
3804 }
3805 if (STRNCMP(s, "null", 4) == 0)
3806 {
3807 rettv->v_type = VAR_SPECIAL;
3808 rettv->vval.v_number = VVAL_NULL;
3809 return OK;
3810 }
3811 break;
3812 case 5: if (STRNCMP(s, "false", 5) == 0)
3813 {
3814 rettv->v_type = VAR_BOOL;
3815 rettv->vval.v_number = VVAL_FALSE;
3816 return OK;
3817 }
3818 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003819 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3820 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003821#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003822 rettv->v_type = VAR_JOB;
3823 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003824#else
3825 rettv->v_type = VAR_SPECIAL;
3826 rettv->vval.v_number = VVAL_NULL;
3827#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003828 return OK;
3829 }
3830 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003831 case 9:
3832 if (STRNCMP(s, "null_", 5) != 0)
3833 break;
3834 if (STRNCMP(s + 5, "list", 4) == 0)
3835 {
3836 rettv->v_type = VAR_LIST;
3837 rettv->vval.v_list = NULL;
3838 return OK;
3839 }
3840 if (STRNCMP(s + 5, "dict", 4) == 0)
3841 {
3842 rettv->v_type = VAR_DICT;
3843 rettv->vval.v_dict = NULL;
3844 return OK;
3845 }
3846 if (STRNCMP(s + 5, "blob", 4) == 0)
3847 {
3848 rettv->v_type = VAR_BLOB;
3849 rettv->vval.v_blob = NULL;
3850 return OK;
3851 }
3852 break;
3853 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3854 {
3855 rettv->v_type = VAR_STRING;
3856 rettv->vval.v_string = NULL;
3857 return OK;
3858 }
3859 break;
3860 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003861 if (STRNCMP(s, "null_channel", 12) == 0)
3862 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003864 rettv->v_type = VAR_CHANNEL;
3865 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003866#else
3867 rettv->v_type = VAR_SPECIAL;
3868 rettv->vval.v_number = VVAL_NULL;
3869#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003870 return OK;
3871 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003872 if (STRNCMP(s, "null_partial", 12) == 0)
3873 {
3874 rettv->v_type = VAR_PARTIAL;
3875 rettv->vval.v_partial = NULL;
3876 return OK;
3877 }
3878 break;
3879 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3880 {
3881 rettv->v_type = VAR_FUNC;
3882 rettv->vval.v_string = NULL;
3883 return OK;
3884 }
3885 break;
3886 }
3887 return FAIL;
3888}
3889
3890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * Handle sixth level expression:
3892 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003893 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003894 * "string" string constant
3895 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 * &option-name option value
3897 * @r register contents
3898 * identifier variable value
3899 * function() function call
3900 * $VAR environment variable
3901 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003902 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003904 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003905 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 *
3907 * Also handle:
3908 * ! in front logical NOT
3909 * - in front unary minus
3910 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003911 * trailing [] subscript in String or List
3912 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003913 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 *
3915 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003916 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 *
3918 * Return OK or FAIL.
3919 */
3920 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003921eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003922 char_u **arg,
3923 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003924 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003927 int evaluate = evalarg != NULL
3928 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 int len;
3930 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003931 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 char_u *start_leader, *end_leader;
3933 int ret = OK;
3934 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003935 static int recurse = 0;
3936 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003939 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003945 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
3947 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003948 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 end_leader = *arg;
3951
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003952 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003953 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003954 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003955 ++*arg;
3956 return FAIL;
3957 }
3958
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003959 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003960 // and crash. With MSVC the stack is smaller.
3961 if (recurse ==
3962#ifdef _MSC_VER
3963 300
3964#else
3965 1000
3966#endif
3967 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003968 {
3969 semsg(_(e_expression_too_recursive_str), *arg);
3970 return FAIL;
3971 }
3972 ++recurse;
3973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 switch (**arg)
3975 {
3976 /*
3977 * Number constant.
3978 */
3979 case '0':
3980 case '1':
3981 case '2':
3982 case '3':
3983 case '4':
3984 case '5':
3985 case '6':
3986 case '7':
3987 case '8':
3988 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003989 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003990
3991 // Apply prefixed "-" and "+" now. Matters especially when
3992 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003993 if (ret == OK && evaluate && end_leader > start_leader
3994 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003995 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 break;
3997
3998 /*
3999 * String constant: "string".
4000 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004001 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 break;
4003
4004 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004005 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004007 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 break;
4009
4010 /*
4011 * List: [expr, expr]
4012 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004013 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015
4016 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004017 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004018 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004019 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004020 {
4021 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4022 }
4023 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004024 {
4025 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004026 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004027 }
4028 else
4029 ret = NOTDONE;
4030 break;
4031
4032 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004033 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004034 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004035 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004036 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004037 ret = NOTDONE;
4038 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004039 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004040 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004041 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004042 break;
4043
4044 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004045 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004047 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 break;
4049
4050 /*
4051 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004052 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
LemonBoy2eaef102022-05-06 13:14:50 +01004054 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4055 ret = eval_interp_string(arg, rettv, evaluate);
4056 else
4057 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060 /*
4061 * Register contents: @r.
4062 */
4063 case '@': ++*arg;
4064 if (evaluate)
4065 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004066 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004067 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004068 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004069 emsg_invreg(**arg);
4070 else
4071 {
4072 rettv->v_type = VAR_STRING;
4073 rettv->vval.v_string = get_reg_contents(**arg,
4074 GREG_EXPR_SRC);
4075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 if (**arg != NUL)
4078 ++*arg;
4079 break;
4080
4081 /*
4082 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004083 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004085 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004086 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004087 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004088 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004089 if (ret == OK && evaluate)
4090 {
4091 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4092
Bram Moolenaara9931532021-06-12 15:58:16 +02004093 // Compile it here to get the return type. The return
4094 // type is optional, when it's missing use t_unknown.
4095 // This is recognized in compile_return().
4096 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4097 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004098 if (compile_def_function(ufunc, FALSE,
4099 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004100 {
4101 clear_tv(rettv);
4102 ret = FAIL;
4103 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004104 }
4105 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004106 if (ret == NOTDONE)
4107 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004108 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004109 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004110
Bram Moolenaar9215f012020-06-27 21:18:00 +02004111 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004112 if (**arg == ')')
4113 ++*arg;
4114 else if (ret == OK)
4115 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004116 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004117 clear_tv(rettv);
4118 ret = FAIL;
4119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121 break;
4122
Bram Moolenaar8c711452005-01-14 21:53:12 +00004123 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 break;
4125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004126
4127 if (ret == NOTDONE)
4128 {
4129 /*
4130 * Must be a variable or function name.
4131 * Can also be a curly-braces kind of name: {expr}.
4132 */
4133 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004134 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004135 if (alias != NULL)
4136 s = alias;
4137
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004138 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004139 ret = FAIL;
4140 else
4141 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004142 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4143
Bram Moolenaar4525a572022-02-13 11:57:33 +00004144 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004145 {
4146 emsg(_(e_cannot_use_underscore_here));
4147 ret = FAIL;
4148 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004149 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004150 && s[0] == 's' && s[1] == ':')
4151 {
4152 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4153 ret = FAIL;
4154 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004155 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004156 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004157 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004158 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004159 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004160 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004161 else if (flags & EVAL_CONSTANT)
4162 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004163 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004164 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004165 // get the value of "true", "false", etc. or a variable
4166 ret = FAIL;
4167 if (vim9script)
4168 ret = handle_predefined(s, len, rettv);
4169 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004170 {
4171 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004172 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004173 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004174 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004176 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004177 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004178 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004179 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004180 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004181 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004182 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004183 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004184 }
4185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004186 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4187 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004188 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004189 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 /*
4192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4193 */
4194 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004195 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004196
4197 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004198 return ret;
4199}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004201/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004202 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004203 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004204 * Adjusts "end_leaderp" until it is at "start_leader".
4205 */
4206 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004207eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004208 typval_T *rettv,
4209 int numeric_only,
4210 char_u *start_leader,
4211 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004212{
4213 char_u *end_leader = *end_leaderp;
4214 int ret = OK;
4215 int error = FALSE;
4216 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004217 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004218 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004219 float_T f = 0.0;
4220
4221 if (rettv->v_type == VAR_FLOAT)
4222 f = rettv->vval.v_float;
4223 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004224 {
4225 while (VIM_ISWHITE(end_leader[-1]))
4226 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004227 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004228 val = tv2bool(rettv);
4229 else
4230 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004231 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004232 if (error)
4233 {
4234 clear_tv(rettv);
4235 ret = FAIL;
4236 }
4237 else
4238 {
4239 while (end_leader > start_leader)
4240 {
4241 --end_leader;
4242 if (*end_leader == '!')
4243 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004244 if (numeric_only)
4245 {
4246 ++end_leader;
4247 break;
4248 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004249 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004250 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004251 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004252 {
4253 rettv->v_type = VAR_BOOL;
4254 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4255 }
4256 else
4257 f = !f;
4258 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004259 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004260 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004261 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004262 type = VAR_BOOL;
4263 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004264 }
4265 else if (*end_leader == '-')
4266 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004267 if (rettv->v_type == VAR_FLOAT)
4268 f = -f;
4269 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004270 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004271 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004272 type = VAR_NUMBER;
4273 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004274 }
4275 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004276 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004279 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 else
4282 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004283 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004284 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004285 rettv->v_type = type;
4286 else
4287 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004288 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004291 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return ret;
4293}
4294
4295/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 * Call the function referred to in "rettv".
4297 */
4298 static int
4299call_func_rettv(
4300 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004301 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004302 typval_T *rettv,
4303 int evaluate,
4304 dict_T *selfdict,
4305 typval_T *basetv)
4306{
4307 partial_T *pt = NULL;
4308 funcexe_T funcexe;
4309 typval_T functv;
4310 char_u *s;
4311 int ret;
4312
4313 // need to copy the funcref so that we can clear rettv
4314 if (evaluate)
4315 {
4316 functv = *rettv;
4317 rettv->v_type = VAR_UNKNOWN;
4318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004319 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004320 if (functv.v_type == VAR_PARTIAL)
4321 {
4322 pt = functv.vval.v_partial;
4323 s = partial_name(pt);
4324 }
4325 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004326 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004327 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004328 if (s == NULL || *s == NUL)
4329 {
4330 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004331 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004332 goto theend;
4333 }
4334 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004335 }
4336 else
4337 s = (char_u *)"";
4338
Bram Moolenaara80faa82020-04-12 19:37:17 +02004339 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004340 funcexe.fe_firstline = curwin->w_cursor.lnum;
4341 funcexe.fe_lastline = curwin->w_cursor.lnum;
4342 funcexe.fe_evaluate = evaluate;
4343 funcexe.fe_partial = pt;
4344 funcexe.fe_selfdict = selfdict;
4345 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004346 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004347
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004348theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004349 // Clear the funcref afterwards, so that deleting it while
4350 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004351 if (evaluate)
4352 clear_tv(&functv);
4353
4354 return ret;
4355}
4356
4357/*
4358 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004359 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004360 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4361 */
4362 static int
4363eval_lambda(
4364 char_u **arg,
4365 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004366 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004368{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004369 int evaluate = evalarg != NULL
4370 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004371 typval_T base = *rettv;
4372 int ret;
4373
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004374 rettv->v_type = VAR_UNKNOWN;
4375
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004376 if (**arg == '{')
4377 {
4378 // ->{lambda}()
4379 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4380 }
4381 else
4382 {
4383 // ->(lambda)()
4384 ++*arg;
4385 ret = eval1(arg, rettv, evalarg);
4386 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004387 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004388 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004389 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004390 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004391 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004392 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4393 && rettv->v_type != VAR_PARTIAL)
4394 {
4395 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4396 return FAIL;
4397 }
4398 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004399 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004400 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004401 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004402
4403 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004404 {
4405 if (verbose)
4406 {
4407 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004408 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004409 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004410 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004411 }
4412 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004413 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004414 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004415 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004416 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004417
4418 // Clear the funcref afterwards, so that deleting it while
4419 // evaluating the arguments is possible (see test55).
4420 if (evaluate)
4421 clear_tv(&base);
4422
4423 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004424}
4425
4426/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004427 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004428 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004429 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4430 */
4431 static int
4432eval_method(
4433 char_u **arg,
4434 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004435 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004437{
4438 char_u *name;
4439 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004440 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004441 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004442 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004443 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004444 int evaluate = evalarg != NULL
4445 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004446
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004447 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004448
Bram Moolenaarac92e252019-08-03 21:58:38 +02004449 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004450 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004451 if (alias != NULL)
4452 name = alias;
4453
4454 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004455 {
4456 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004457 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004458 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004459 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004460 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004461 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004462 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004463
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004464 // If there is no "(" immediately following, but there is further on,
4465 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4466 // Does not handle anything where "(" is part of the expression.
4467 *arg = skipwhite(*arg);
4468
4469 if (**arg != '(' && alias == NULL
4470 && (paren = vim_strchr(*arg, '(')) != NULL)
4471 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004472 char_u *deref;
4473
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004474 *arg = name;
4475 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004476 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4477 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004478 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004479 *arg = name + len;
4480 ret = FAIL;
4481 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004482 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004483 {
4484 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004485 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004486 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004487 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004488 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004489
4490 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004491 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004492 *arg = skipwhite(*arg);
4493
4494 if (**arg != '(')
4495 {
4496 if (verbose)
4497 semsg(_(e_missing_parenthesis_str), name);
4498 ret = FAIL;
4499 }
4500 else if (VIM_ISWHITE((*arg)[-1]))
4501 {
4502 if (verbose)
4503 emsg(_(e_no_white_space_allowed_before_parenthesis));
4504 ret = FAIL;
4505 }
4506 else
4507 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004508 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004509 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004510 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004511
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004512 // Clear the funcref afterwards, so that deleting it while
4513 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004514 if (evaluate)
4515 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004516 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004517
Bram Moolenaarac92e252019-08-03 21:58:38 +02004518 return ret;
4519}
4520
4521/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004522 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4523 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4525 */
4526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004527eval_index(
4528 char_u **arg,
4529 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004530 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004533 int evaluate = evalarg != NULL
4534 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004539 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004540 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004542 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4543 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004545 init_tv(&var1);
4546 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 /*
4550 * dict.name
4551 */
4552 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004553 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004557 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 }
4559 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 /*
4562 * something[idx]
4563 *
4564 * Get the (first) variable from inside the [].
4565 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004566 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 if (**arg == ':')
4568 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004569 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004571 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004572 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004573 semsg(_(e_white_space_required_before_and_after_str_at_str),
4574 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004575 clear_tv(&var1);
4576 return FAIL;
4577 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004578 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004580 int error = FALSE;
4581
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004582 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004583 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004584 && var1.v_type == VAR_FLOAT)
4585 {
4586 var1.vval.v_string = typval_tostring(&var1, TRUE);
4587 var1.v_type = VAR_STRING;
4588 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004589
Bram Moolenaar4525a572022-02-13 11:57:33 +00004590 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004591 tv_get_number_chk(&var1, &error);
4592 else
4593 error = tv_get_string_chk(&var1) == NULL;
4594 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004595 {
4596 // not a number or string
4597 clear_tv(&var1);
4598 return FAIL;
4599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601
4602 /*
4603 * Get the second variable from inside the [:].
4604 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004605 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 if (**arg == ':')
4607 {
4608 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004609 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004610 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004611 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004612 semsg(_(e_white_space_required_before_and_after_str_at_str),
4613 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004614 if (!empty1)
4615 clear_tv(&var1);
4616 return FAIL;
4617 }
4618 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 if (**arg == ']')
4620 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004621 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 if (!empty1)
4624 clear_tv(&var1);
4625 return FAIL;
4626 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004627 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 if (!empty1)
4631 clear_tv(&var1);
4632 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 return FAIL;
4634 }
4635 }
4636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004637 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004638 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 if (**arg != ']')
4640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004641 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004642 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 clear_tv(&var1);
4644 if (range)
4645 clear_tv(&var2);
4646 return FAIL;
4647 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004648 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 }
4650
4651 if (evaluate)
4652 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004654 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004655 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004657 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004660 clear_tv(&var2);
4661 return res;
4662 }
4663 return OK;
4664}
4665
4666/*
4667 * Check if "rettv" can have an [index] or [sli:ce]
4668 */
4669 int
4670check_can_index(typval_T *rettv, int evaluate, int verbose)
4671{
4672 switch (rettv->v_type)
4673 {
4674 case VAR_FUNC:
4675 case VAR_PARTIAL:
4676 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004677 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678 return FAIL;
4679 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004681 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004682 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004683 case VAR_BOOL:
4684 case VAR_SPECIAL:
4685 case VAR_JOB:
4686 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004687 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004688 if (verbose)
4689 emsg(_(e_cannot_index_special_variable));
4690 return FAIL;
4691 case VAR_UNKNOWN:
4692 case VAR_ANY:
4693 case VAR_VOID:
4694 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004696 emsg(_(e_cannot_index_special_variable));
4697 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004699 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004701 case VAR_STRING:
4702 case VAR_LIST:
4703 case VAR_DICT:
4704 case VAR_BLOB:
4705 break;
4706 case VAR_NUMBER:
4707 if (in_vim9script())
4708 emsg(_(e_cannot_index_number));
4709 break;
4710 }
4711 return OK;
4712}
4713
4714/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004715 * slice() function
4716 */
4717 void
4718f_slice(typval_T *argvars, typval_T *rettv)
4719{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004720 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004721 && ((argvars[0].v_type != VAR_STRING
4722 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004723 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004724 && check_for_list_arg(argvars, 0) == FAIL)
4725 || check_for_number_arg(argvars, 1) == FAIL
4726 || check_for_opt_number_arg(argvars, 2) == FAIL))
4727 return;
4728
Bram Moolenaar6601b622021-01-13 21:47:15 +01004729 if (check_can_index(argvars, TRUE, FALSE) == OK)
4730 {
4731 copy_tv(argvars, rettv);
4732 eval_index_inner(rettv, TRUE, argvars + 1,
4733 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4734 TRUE, NULL, 0, FALSE);
4735 }
4736}
4737
4738/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004739 * Apply index or range to "rettv".
4740 * "var1" is the first index, NULL for [:expr].
4741 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004742 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4743 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004744 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4745 */
4746 int
4747eval_index_inner(
4748 typval_T *rettv,
4749 int is_range,
4750 typval_T *var1,
4751 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004752 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004753 char_u *key,
4754 int keylen,
4755 int verbose)
4756{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004757 varnumber_T n1, n2 = 0;
4758 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004759
4760 n1 = 0;
4761 if (var1 != NULL && rettv->v_type != VAR_DICT)
4762 n1 = tv_get_number(var1);
4763
4764 if (is_range)
4765 {
4766 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004768 if (verbose)
4769 emsg(_(e_cannot_slice_dictionary));
4770 return FAIL;
4771 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004772 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004774 else
4775 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004778 switch (rettv->v_type)
4779 {
4780 case VAR_UNKNOWN:
4781 case VAR_ANY:
4782 case VAR_VOID:
4783 case VAR_FUNC:
4784 case VAR_PARTIAL:
4785 case VAR_FLOAT:
4786 case VAR_BOOL:
4787 case VAR_SPECIAL:
4788 case VAR_JOB:
4789 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004790 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 break; // not evaluating, skipping over subscript
4792
4793 case VAR_NUMBER:
4794 case VAR_STRING:
4795 {
4796 char_u *s = tv_get_string(rettv);
4797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004799 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004800 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004802 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004803 else
4804 s = char_from_string(s, n1);
4805 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004806 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // The resulting variable is a substring. If the indexes
4809 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 if (n1 < 0)
4811 {
4812 n1 = len + n1;
4813 if (n1 < 0)
4814 n1 = 0;
4815 }
4816 if (n2 < 0)
4817 n2 = len + n2;
4818 else if (n2 >= len)
4819 n2 = len;
4820 if (n1 >= len || n2 < 0 || n1 > n2)
4821 s = NULL;
4822 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004823 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 else
4826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 // The resulting variable is a string of a single
4828 // character. If the index is too big or negative the
4829 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 if (n1 >= len || n1 < 0)
4831 s = NULL;
4832 else
4833 s = vim_strnsave(s + n1, 1);
4834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 clear_tv(rettv);
4836 rettv->v_type = VAR_STRING;
4837 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004838 }
4839 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004841 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004842 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4843 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004844 break;
4845
4846 case VAR_LIST:
4847 if (var1 == NULL)
4848 n1 = 0;
4849 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004850 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004851 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004852 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004853 return FAIL;
4854 break;
4855
4856 case VAR_DICT:
4857 {
4858 dictitem_T *item;
4859 typval_T tmp;
4860
4861 if (key == NULL)
4862 {
4863 key = tv_get_string_chk(var1);
4864 if (key == NULL)
4865 return FAIL;
4866 }
4867
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004868 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004869
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004870 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004871 {
4872 if (verbose)
4873 {
4874 if (keylen > 0)
4875 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004876 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004877 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004879 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004880
4881 copy_tv(&item->di_tv, &tmp);
4882 clear_tv(rettv);
4883 *rettv = tmp;
4884 }
4885 break;
4886 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 return OK;
4888}
4889
4890/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004891 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004892 */
4893 char_u *
4894partial_name(partial_T *pt)
4895{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004896 if (pt != NULL)
4897 {
4898 if (pt->pt_name != NULL)
4899 return pt->pt_name;
4900 if (pt->pt_func != NULL)
4901 return pt->pt_func->uf_name;
4902 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004903 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004904}
4905
Bram Moolenaarddecc252016-04-06 22:59:37 +02004906 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004907partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004908{
4909 int i;
4910
4911 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004912 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004913 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004914 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004915 if (pt->pt_name != NULL)
4916 {
4917 func_unref(pt->pt_name);
4918 vim_free(pt->pt_name);
4919 }
4920 else
4921 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004922
Bram Moolenaar54656012021-06-09 20:50:46 +02004923 // "out_up" is no longer used, decrement refcount on partial that owns it.
4924 partial_unref(pt->pt_outer.out_up_partial);
4925
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004926 // Using pt_outer from another partial.
4927 partial_unref(pt->pt_outer_partial);
4928
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004929 // Decrease the reference count for the context of a closure. If down
4930 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004931 if (pt->pt_funcstack != NULL)
4932 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004933 --pt->pt_funcstack->fs_refcount;
4934 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004935 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004936 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004937 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4938 if (pt->pt_loopvars[i] != NULL)
4939 {
4940 --pt->pt_loopvars[i]->lvs_refcount;
4941 loopvars_check_refcount(pt->pt_loopvars[i]);
4942 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004943
Bram Moolenaarddecc252016-04-06 22:59:37 +02004944 vim_free(pt);
4945}
4946
4947/*
4948 * Unreference a closure: decrement the reference count and free it when it
4949 * becomes zero.
4950 */
4951 void
4952partial_unref(partial_T *pt)
4953{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004954 if (pt != NULL)
4955 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004956 int done = FALSE;
4957
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004958 if (--pt->pt_refcount <= 0)
4959 partial_free(pt);
4960
4961 // If the reference count goes down to one, the funcstack may be the
4962 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004963 else if (pt->pt_refcount == 1)
4964 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004965 // careful: if the funcstack is freed it may contain this partial
4966 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004967 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004968 done = funcstack_check_refcount(pt->pt_funcstack);
4969
Bram Moolenaarcc341812022-09-19 15:54:34 +01004970 if (!done)
4971 {
4972 int depth;
4973
4974 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4975 if (pt->pt_loopvars[depth] != NULL
4976 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4977 break;
4978 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004979 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004980 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004981}
4982
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004983/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004984 * Return the next (unique) copy ID.
4985 * Used for serializing nested structures.
4986 */
4987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004988get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004989{
4990 current_copyID += COPYID_INC;
4991 return current_copyID;
4992}
4993
4994/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004995 * Garbage collection for lists and dictionaries.
4996 *
4997 * We use reference counts to be able to free most items right away when they
4998 * are no longer used. But for composite items it's possible that it becomes
4999 * unused while the reference count is > 0: When there is a recursive
5000 * reference. Example:
5001 * :let l = [1, 2, 3]
5002 * :let d = {9: l}
5003 * :let l[1] = d
5004 *
5005 * Since this is quite unusual we handle this with garbage collection: every
5006 * once in a while find out which lists and dicts are not referenced from any
5007 * variable.
5008 *
5009 * Here is a good reference text about garbage collection (refers to Python
5010 * but it applies to all reference-counting mechanisms):
5011 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005012 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005013
5014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005015 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005016 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005017 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005018 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005019 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005020garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005021{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005022 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005024 buf_T *buf;
5025 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005026 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005027 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005028
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005029 if (!testing)
5030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005032 want_garbage_collect = FALSE;
5033 may_garbage_collect = FALSE;
5034 garbage_collect_at_exit = FALSE;
5035 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005036
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005037 // The execution stack can grow big, limit the size.
5038 if (exestack.ga_maxlen - exestack.ga_len > 500)
5039 {
5040 size_t new_len;
5041 char_u *pp;
5042 int n;
5043
5044 // Keep 150% of the current size, with a minimum of the growth size.
5045 n = exestack.ga_len / 2;
5046 if (n < exestack.ga_growsize)
5047 n = exestack.ga_growsize;
5048
5049 // Don't make it bigger though.
5050 if (exestack.ga_len + n < exestack.ga_maxlen)
5051 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005052 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005053 pp = vim_realloc(exestack.ga_data, new_len);
5054 if (pp == NULL)
5055 return FAIL;
5056 exestack.ga_maxlen = exestack.ga_len + n;
5057 exestack.ga_data = pp;
5058 }
5059 }
5060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // We advance by two because we add one for items referenced through
5062 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005063 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005064
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005065 /*
5066 * 1. Go through all accessible variables and mark all lists and dicts
5067 * with copyID.
5068 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 // Don't free variables in the previous_funccal list unless they are only
5071 // referenced through previous_funccal. This must be first, because if
5072 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005076 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005079 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005080 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5081 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005084 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005085 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5086 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005087 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005088 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5089 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005090#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005091 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005092 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5093 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005094 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005095 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005096 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5097 NULL, NULL);
5098#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005101 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005102 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5103 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005105 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005108 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005111 abort = abort || set_ref_in_functions(copyID);
5112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005115
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005116 // funcstacks keep variables for closures
5117 abort = abort || set_ref_in_funcstacks(copyID);
5118
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005119 // loopvars keep variables for loop blocks
5120 abort = abort || set_ref_in_loopvars(copyID);
5121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005123 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005124
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005125 // callbacks in buffers
5126 abort = abort || set_ref_in_buffers(copyID);
5127
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005128 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5129 abort = abort || set_ref_in_insexpand_funcs(copyID);
5130
5131 // 'operatorfunc' callback
5132 abort = abort || set_ref_in_opfunc(copyID);
5133
5134 // 'tagfunc' callback
5135 abort = abort || set_ref_in_tagfunc(copyID);
5136
5137 // 'imactivatefunc' and 'imstatusfunc' callbacks
5138 abort = abort || set_ref_in_im_funcs(copyID);
5139
Bram Moolenaar1dced572012-04-05 16:54:08 +02005140#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005141 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005142#endif
5143
Bram Moolenaardb913952012-06-29 12:54:53 +02005144#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005145 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005146#endif
5147
5148#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005149 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005150#endif
5151
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005152#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005153 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005154 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005155#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005156#ifdef FEAT_NETBEANS_INTG
5157 abort = abort || set_ref_in_nb_channel(copyID);
5158#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005159
Bram Moolenaare3188e22016-05-31 21:13:04 +02005160#ifdef FEAT_TIMERS
5161 abort = abort || set_ref_in_timer(copyID);
5162#endif
5163
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005164#ifdef FEAT_QUICKFIX
5165 abort = abort || set_ref_in_quickfix(copyID);
5166#endif
5167
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005168#ifdef FEAT_TERMINAL
5169 abort = abort || set_ref_in_term(copyID);
5170#endif
5171
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005172#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005173 abort = abort || set_ref_in_popups(copyID);
5174#endif
5175
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005177 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005178 /*
5179 * 2. Free lists and dictionaries that are not referenced.
5180 */
5181 did_free = free_unref_items(copyID);
5182
5183 /*
5184 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005185 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005186 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005188 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005189 else if (p_verbose > 0)
5190 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005191 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005192 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005193
5194 return did_free;
5195}
5196
5197/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005198 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005199 */
5200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005201free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005202{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005203 int did_free = FALSE;
5204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // Let all "free" functions know that we are here. This means no
5206 // dictionaries, lists, channels or jobs are to be freed, because we will
5207 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 in_free_unref_items = TRUE;
5209
5210 /*
5211 * PASS 1: free the contents of the items. We don't free the items
5212 * themselves yet, so that it is possible to decrement refcount counters
5213 */
5214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005216 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005219 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005220
5221#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005222 // Go through the list of jobs and free items without the copyID. This
5223 // must happen before doing channels, because jobs refer to channels, but
5224 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005225 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005228 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5229#endif
5230
5231 /*
5232 * PASS 2: free the items themselves.
5233 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005234 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005235 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005236
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005237#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // Go through the list of jobs and free items without the copyID. This
5239 // must happen before doing channels, because jobs refer to channels, but
5240 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005241 free_unused_jobs(copyID, COPYID_MASK);
5242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005243 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005244 free_unused_channels(copyID, COPYID_MASK);
5245#endif
5246
5247 in_free_unref_items = FALSE;
5248
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005249 return did_free;
5250}
5251
5252/*
5253 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005254 * "list_stack" is used to add lists to be marked. Can be NULL.
5255 *
5256 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005259set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260{
5261 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264 hashtab_T *cur_ht;
5265 ht_stack_T *ht_stack = NULL;
5266 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005267
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 cur_ht = ht;
5269 for (;;)
5270 {
5271 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 // Mark each item in the hashtab. If the item contains a hashtab
5274 // it is added to ht_stack, if it contains a list it is added to
5275 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 todo = (int)cur_ht->ht_used;
5277 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5278 if (!HASHITEM_EMPTY(hi))
5279 {
5280 --todo;
5281 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5282 &ht_stack, list_stack);
5283 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285
5286 if (ht_stack == NULL)
5287 break;
5288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 cur_ht = ht_stack->ht;
5291 tempitem = ht_stack;
5292 ht_stack = ht_stack->prev;
5293 free(tempitem);
5294 }
5295
5296 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005297}
5298
Dominique Pelle748b3082022-01-08 12:41:16 +00005299#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5300 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005302 * Mark a dict and its items with "copyID".
5303 * Returns TRUE if setting references failed somehow.
5304 */
5305 int
5306set_ref_in_dict(dict_T *d, int copyID)
5307{
5308 if (d != NULL && d->dv_copyID != copyID)
5309 {
5310 d->dv_copyID = copyID;
5311 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5312 }
5313 return FALSE;
5314}
Dominique Pelle748b3082022-01-08 12:41:16 +00005315#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005316
5317/*
5318 * Mark a list and its items with "copyID".
5319 * Returns TRUE if setting references failed somehow.
5320 */
5321 int
5322set_ref_in_list(list_T *ll, int copyID)
5323{
5324 if (ll != NULL && ll->lv_copyID != copyID)
5325 {
5326 ll->lv_copyID = copyID;
5327 return set_ref_in_list_items(ll, copyID, NULL);
5328 }
5329 return FALSE;
5330}
5331
5332/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005333 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5335 *
5336 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005339set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005340{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005341 listitem_T *li;
5342 int abort = FALSE;
5343 list_T *cur_l;
5344 list_stack_T *list_stack = NULL;
5345 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005346
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005347 cur_l = l;
5348 for (;;)
5349 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005350 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // Mark each item in the list. If the item contains a hashtab
5352 // it is added to ht_stack, if it contains a list it is added to
5353 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5355 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5356 ht_stack, &list_stack);
5357 if (list_stack == NULL)
5358 break;
5359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 cur_l = list_stack->list;
5362 tempitem = list_stack;
5363 list_stack = list_stack->prev;
5364 free(tempitem);
5365 }
5366
5367 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005368}
5369
5370/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005371 * Mark the partial in callback 'cb' with "copyID".
5372 */
5373 int
5374set_ref_in_callback(callback_T *cb, int copyID)
5375{
5376 typval_T tv;
5377
5378 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5379 return FALSE;
5380
5381 tv.v_type = VAR_PARTIAL;
5382 tv.vval.v_partial = cb->cb_partial;
5383 return set_ref_in_item(&tv, copyID, NULL, NULL);
5384}
5385
5386/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005387 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 * "list_stack" is used to add lists to be marked. Can be NULL.
5389 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5390 *
5391 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005394set_ref_in_item(
5395 typval_T *tv,
5396 int copyID,
5397 ht_stack_T **ht_stack,
5398 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005401
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005402 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005403 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005404 dict_T *dd = tv->vval.v_dict;
5405
Bram Moolenaara03f2332016-02-06 18:09:59 +01005406 if (dd != NULL && dd->dv_copyID != copyID)
5407 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005409 dd->dv_copyID = copyID;
5410 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005411 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5413 }
5414 else
5415 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005416 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5417
Bram Moolenaara03f2332016-02-06 18:09:59 +01005418 if (newitem == NULL)
5419 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005420 else
5421 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005422 newitem->ht = &dd->dv_hashtab;
5423 newitem->prev = *ht_stack;
5424 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005426 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005427 }
5428 }
5429 else if (tv->v_type == VAR_LIST)
5430 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005431 list_T *ll = tv->vval.v_list;
5432
Bram Moolenaara03f2332016-02-06 18:09:59 +01005433 if (ll != NULL && ll->lv_copyID != copyID)
5434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005436 ll->lv_copyID = copyID;
5437 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005439 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005440 }
5441 else
5442 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005443 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5444
Bram Moolenaara03f2332016-02-06 18:09:59 +01005445 if (newitem == NULL)
5446 abort = TRUE;
5447 else
5448 {
5449 newitem->list = ll;
5450 newitem->prev = *list_stack;
5451 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005452 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005453 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005454 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005455 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005456 else if (tv->v_type == VAR_FUNC)
5457 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005458 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005459 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005460 else if (tv->v_type == VAR_PARTIAL)
5461 {
5462 partial_T *pt = tv->vval.v_partial;
5463 int i;
5464
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005465 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005466 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005467 // Didn't see this partial yet.
5468 pt->pt_copyID = copyID;
5469
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005470 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005471
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005472 if (pt->pt_dict != NULL)
5473 {
5474 typval_T dtv;
5475
5476 dtv.v_type = VAR_DICT;
5477 dtv.vval.v_dict = pt->pt_dict;
5478 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5479 }
5480
5481 for (i = 0; i < pt->pt_argc; ++i)
5482 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5483 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005484 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005485 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005486 }
5487 }
5488#ifdef FEAT_JOB_CHANNEL
5489 else if (tv->v_type == VAR_JOB)
5490 {
5491 job_T *job = tv->vval.v_job;
5492 typval_T dtv;
5493
5494 if (job != NULL && job->jv_copyID != copyID)
5495 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005496 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005497 if (job->jv_channel != NULL)
5498 {
5499 dtv.v_type = VAR_CHANNEL;
5500 dtv.vval.v_channel = job->jv_channel;
5501 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5502 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005503 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005504 {
5505 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005506 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005507 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5508 }
5509 }
5510 }
5511 else if (tv->v_type == VAR_CHANNEL)
5512 {
5513 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005514 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005515 typval_T dtv;
5516 jsonq_T *jq;
5517 cbq_T *cq;
5518
5519 if (ch != NULL && ch->ch_copyID != copyID)
5520 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005521 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005522 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005523 {
5524 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5525 jq = jq->jq_next)
5526 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5527 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5528 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005529 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005530 {
5531 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005532 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005533 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5534 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005535 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005536 {
5537 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005538 dtv.vval.v_partial =
5539 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005540 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5541 }
5542 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005543 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005544 {
5545 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005546 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005547 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5548 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005549 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005550 {
5551 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005552 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005553 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5554 }
5555 }
5556 }
5557#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005558 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005559}
5560
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 * Return a string with the string representation of a variable.
5563 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005564 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005565 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005566 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005567 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005568 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005569 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5570 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005571 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005573 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005574echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005575 typval_T *tv,
5576 char_u **tofree,
5577 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005578 int copyID,
5579 int echo_style,
5580 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005581 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005583 static int recurse = 0;
5584 char_u *r = NULL;
5585
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005587 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005588 if (!did_echo_string_emsg)
5589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // Only give this message once for a recursive call to avoid
5591 // flooding the user with errors. And stop iterating over lists
5592 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005593 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005594 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005596 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005597 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005598 }
5599 ++recurse;
5600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 switch (tv->v_type)
5602 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005603 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005604 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005605 {
5606 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005607 r = tv->vval.v_string;
5608 if (r == NULL)
5609 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005610 }
5611 else
5612 {
5613 *tofree = string_quote(tv->vval.v_string, FALSE);
5614 r = *tofree;
5615 }
5616 break;
5617
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005619 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005620 char_u buf[MAX_FUNC_NAME_LEN];
5621
5622 if (echo_style)
5623 {
LemonBoya5d35902022-04-29 21:15:02 +01005624 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5625 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005626 buf, MAX_FUNC_NAME_LEN);
5627 if (r == buf)
5628 {
5629 r = vim_strsave(buf);
5630 *tofree = r;
5631 }
5632 else
5633 *tofree = NULL;
5634 }
5635 else
5636 {
5637 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5638 : make_ufunc_name_readable(
5639 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5640 TRUE);
5641 r = *tofree;
5642 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005645
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005646 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005647 {
5648 partial_T *pt = tv->vval.v_partial;
5649 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005650 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005651 garray_T ga;
5652 int i;
5653 char_u *tf;
5654
5655 ga_init2(&ga, 1, 100);
5656 ga_concat(&ga, (char_u *)"function(");
5657 if (fname != NULL)
5658 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005659 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005660 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005661 && vim_isupper(fname[1]))
5662 {
5663 ga_concat(&ga, (char_u *)"'g:");
5664 ga_concat(&ga, fname + 1);
5665 }
5666 else
5667 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005668 vim_free(fname);
5669 }
5670 if (pt != NULL && pt->pt_argc > 0)
5671 {
5672 ga_concat(&ga, (char_u *)", [");
5673 for (i = 0; i < pt->pt_argc; ++i)
5674 {
5675 if (i > 0)
5676 ga_concat(&ga, (char_u *)", ");
5677 ga_concat(&ga,
5678 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5679 vim_free(tf);
5680 }
5681 ga_concat(&ga, (char_u *)"]");
5682 }
5683 if (pt != NULL && pt->pt_dict != NULL)
5684 {
5685 typval_T dtv;
5686
5687 ga_concat(&ga, (char_u *)", ");
5688 dtv.v_type = VAR_DICT;
5689 dtv.vval.v_dict = pt->pt_dict;
5690 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5691 vim_free(tf);
5692 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005693 // terminate with ')' and a NUL
5694 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005695
5696 *tofree = ga.ga_data;
5697 r = *tofree;
5698 break;
5699 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005700
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005701 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005702 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005703 break;
5704
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005706 if (tv->vval.v_list == NULL)
5707 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005708 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005709 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005710 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005711 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005712 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5713 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005714 {
5715 *tofree = NULL;
5716 r = (char_u *)"[...]";
5717 }
5718 else
5719 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005720 int old_copyID = tv->vval.v_list->lv_copyID;
5721
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005722 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005723 *tofree = list2string(tv, copyID, restore_copyID);
5724 if (restore_copyID)
5725 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005726 r = *tofree;
5727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005728 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005731 if (tv->vval.v_dict == NULL)
5732 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005733 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005734 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005735 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005736 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005737 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5738 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005739 {
5740 *tofree = NULL;
5741 r = (char_u *)"{...}";
5742 }
5743 else
5744 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005745 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005746
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005747 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005748 *tofree = dict2string(tv, copyID, restore_copyID);
5749 if (restore_copyID)
5750 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005751 r = *tofree;
5752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005753 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005756 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005757 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005759 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005761 break;
5762
Bram Moolenaar835dc632016-02-07 14:27:38 +01005763 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005764 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005765#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005766 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005767 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5768 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005769 if (composite_val)
5770 {
5771 *tofree = string_quote(r, FALSE);
5772 r = *tofree;
5773 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005774#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005776
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005777 case VAR_INSTR:
5778 *tofree = NULL;
5779 r = (char_u *)"instructions";
5780 break;
5781
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005782 case VAR_FLOAT:
5783 *tofree = NULL;
5784 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5785 r = numbuf;
5786 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005787
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005788 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005789 case VAR_SPECIAL:
5790 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005791 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005792 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005794
Bram Moolenaar8502c702014-06-17 12:51:16 +02005795 if (--recurse == 0)
5796 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005797 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798}
5799
5800/*
5801 * Return a string with the string representation of a variable.
5802 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5803 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005804 * Does not put quotes around strings, as ":echo" displays values.
5805 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5806 * May return NULL.
5807 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005808 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005809echo_string(
5810 typval_T *tv,
5811 char_u **tofree,
5812 char_u *numbuf,
5813 int copyID)
5814{
5815 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5816}
5817
5818/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005819 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5820 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005821 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005822 */
5823 int
5824buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5825{
5826 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005827 char_u *t;
5828 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005829
5830 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5831 return -1;
5832
5833 if (lnum > buf->b_ml.ml_line_count)
5834 lnum = buf->b_ml.ml_line_count;
5835
5836 str = ml_get_buf(buf, lnum, FALSE);
5837 if (str == NULL)
5838 return -1;
5839
5840 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005841 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005842
Bram Moolenaar91458462021-01-13 20:08:38 +01005843 // count the number of characters
5844 t = str;
5845 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5846 t += mb_ptr2len(t);
5847
5848 // In insert mode, when the cursor is at the end of a non-empty line,
5849 // byteidx points to the NUL character immediately past the end of the
5850 // string. In this case, add one to the character count.
5851 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5852 count++;
5853
5854 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005855}
5856
5857/*
5858 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005859 * byte index. Works only for loaded buffers. Returns -1 on failure.
5860 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005861 */
5862 int
5863buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5864{
5865 char_u *str;
5866 char_u *t;
5867
5868 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5869 return -1;
5870
5871 if (lnum > buf->b_ml.ml_line_count)
5872 lnum = buf->b_ml.ml_line_count;
5873
5874 str = ml_get_buf(buf, lnum, FALSE);
5875 if (str == NULL)
5876 return -1;
5877
5878 // Convert the character offset to a byte offset
5879 t = str;
5880 while (*t != NUL && --charidx > 0)
5881 t += mb_ptr2len(t);
5882
Bram Moolenaar91458462021-01-13 20:08:38 +01005883 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005884}
5885
5886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005888 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005891var2fpos(
5892 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005894 int *fnum, // set to fnum for '0, 'A, etc.
5895 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005897 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005899 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005901 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005902 if (varp->v_type == VAR_LIST)
5903 {
5904 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005905 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005906 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005907 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005908
5909 l = varp->vval.v_list;
5910 if (l == NULL)
5911 return NULL;
5912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005913 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005914 pos.lnum = list_find_nr(l, 0L, &error);
5915 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005916 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005917 if (charcol)
5918 len = (long)mb_charlen(ml_get(pos.lnum));
5919 else
5920 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005921
Bram Moolenaarec65d772020-08-20 22:29:12 +02005922 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005924 li = list_find(l, 1L);
5925 if (li != NULL && li->li_tv.v_type == VAR_STRING
5926 && li->li_tv.vval.v_string != NULL
5927 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005928 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005929 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005930 }
5931 else
5932 {
5933 pos.col = list_find_nr(l, 1L, &error);
5934 if (error)
5935 return NULL;
5936 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005939 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005941 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005944 pos.coladd = list_find_nr(l, 2L, &error);
5945 if (error)
5946 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005947
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005948 return &pos;
5949 }
5950
Bram Moolenaarc5809432021-03-27 21:23:30 +01005951 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5952 return NULL;
5953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005954 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005955 if (name == NULL)
5956 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005957
5958 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005959 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005960 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005961 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005962 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005963 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005964 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005965 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005966 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005967 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005968 pos = VIsual;
5969 else
5970 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005971 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005972 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005973 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005975 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005976 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5978 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005979 pos = *pp;
5980 }
5981 if (pos.lnum != 0)
5982 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005983 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005984 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5985 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005987
Bram Moolenaara5525202006-03-02 22:52:09 +00005988 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005989
Bram Moolenaar477933c2007-07-17 14:32:23 +00005990 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005991 {
5992 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005994 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005995 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005996 // In silent Ex mode topline is zero, but that's not a valid line
5997 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005998 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005999 return &pos;
6000 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006002 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006003 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006005 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006006 return &pos;
6007 }
6008 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006009 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006011 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
6013 pos.lnum = curbuf->b_ml.ml_line_count;
6014 pos.col = 0;
6015 }
6016 else
6017 {
6018 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006019 if (charcol)
6020 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6021 else
6022 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024 return &pos;
6025 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006026 if (in_vim9script())
6027 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return NULL;
6029}
6030
6031/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006032 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006033 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006034 * Note that the column is passed on as-is, the caller may want to decrement
6035 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006036 * If "charcol" is TRUE use the column as the character index instead of the
6037 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006038 * Return FAIL when conversion is not possible, doesn't check the position for
6039 * validity.
6040 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042list2fpos(
6043 typval_T *arg,
6044 pos_T *posp,
6045 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006046 colnr_T *curswantp,
6047 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006048{
6049 list_T *l = arg->vval.v_list;
6050 long i = 0;
6051 long n;
6052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006053 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6054 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006055 if (arg->v_type != VAR_LIST
6056 || l == NULL
6057 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006058 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006059 return FAIL;
6060
6061 if (fnump != NULL)
6062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006064 if (n < 0)
6065 return FAIL;
6066 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006068 *fnump = n;
6069 }
6070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006071 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006072 if (n < 0)
6073 return FAIL;
6074 posp->lnum = n;
6075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006076 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006077 if (n < 0)
6078 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006079 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006080 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006081 if (charcol)
6082 {
6083 buf_T *buf;
6084
6085 // Get the text for the specified line in a loaded buffer
6086 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6087 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6088 return FAIL;
6089
Bram Moolenaar79f23442022-10-10 12:42:57 +01006090 n = buf_charidx_to_byteidx(buf,
6091 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006092 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006093 posp->col = n;
6094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006095 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006096 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006097 posp->coladd = 0;
6098 else
6099 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006100
Bram Moolenaar493c1782014-05-28 14:34:46 +02006101 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006102 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006103
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006104 return OK;
6105}
6106
6107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 * Get the length of an environment variable name.
6109 * Advance "arg" to the first character after the name.
6110 * Return 0 for error.
6111 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006112 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115 char_u *p;
6116 int len;
6117
6118 for (p = *arg; vim_isIDc(*p); ++p)
6119 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006120 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 return 0;
6122
6123 len = (int)(p - *arg);
6124 *arg = p;
6125 return len;
6126}
6127
6128/*
6129 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006130 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * Return 0 if something is wrong.
6132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006133 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 char_u *p;
6137 int len;
6138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006139 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006141 {
6142 if (*p == ':')
6143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006144 // "s:" is start of "s:var", but "n:" is not and can be used in
6145 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006146 len = (int)(p - *arg);
6147 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6148 || len > 1)
6149 break;
6150 }
6151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 return 0;
6154
6155 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006156 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 return len;
6159}
6160
6161/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006162 * Get the length of the name of a variable or function.
6163 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006165 * Return -1 if curly braces expansion failed.
6166 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * If the name contains 'magic' {}'s, expand them and return the
6168 * expanded name in an allocated string via 'alias' - caller must free.
6169 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171get_name_len(
6172 char_u **arg,
6173 char_u **alias,
6174 int evaluate,
6175 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 char_u *p;
6179 char_u *expr_start;
6180 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006182 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6185 && (*arg)[2] == (int)KE_SNR)
6186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 *arg += 3;
6189 return get_id_len(arg) + 3;
6190 }
6191 len = eval_fname_script(*arg);
6192 if (len > 0)
6193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 *arg += len;
6196 }
6197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006199 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006201 p = find_name_end(*arg, &expr_start, &expr_end,
6202 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (expr_start != NULL)
6204 {
6205 char_u *temp_string;
6206
6207 if (!evaluate)
6208 {
6209 len += (int)(p - *arg);
6210 *arg = skipwhite(p);
6211 return len;
6212 }
6213
6214 /*
6215 * Include any <SID> etc in the expanded string:
6216 * Thus the -len here.
6217 */
6218 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6219 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006220 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 *alias = temp_string;
6222 *arg = skipwhite(p);
6223 return (int)STRLEN(temp_string);
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006227 // Only give an error when there is something, otherwise it will be
6228 // reported at a higher level.
6229 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006230 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 return len;
6233}
6234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006235/*
6236 * Find the end of a variable or function name, taking care of magic braces.
6237 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6238 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006239 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006240 * Return a pointer to just after the name. Equal to "arg" if there is no
6241 * valid name.
6242 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006244find_name_end(
6245 char_u *arg,
6246 char_u **expr_start,
6247 char_u **expr_end,
6248 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006250 int mb_nest = 0;
6251 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006253 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006254 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006256 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006258 *expr_start = NULL;
6259 *expr_end = NULL;
6260 }
6261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006263 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6264 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006265 return arg;
6266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 for (p = arg; *p != NUL
6268 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006269 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006270 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006271 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006273 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006274 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006275 if (*p == '\'')
6276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006277 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006278 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006279 ;
6280 if (*p == NUL)
6281 break;
6282 }
6283 else if (*p == '"')
6284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006286 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006287 if (*p == '\\' && p[1] != NUL)
6288 ++p;
6289 if (*p == NUL)
6290 break;
6291 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006292 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // "s:" is start of "s:var", but "n:" is not and can be used in
6295 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006296 len = (int)(p - arg);
6297 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006298 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006299 break;
6300 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006301
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006302 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006304 if (*p == '[')
6305 ++br_nest;
6306 else if (*p == ']')
6307 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006309
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006310 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006312 if (*p == '{')
6313 {
6314 mb_nest++;
6315 if (expr_start != NULL && *expr_start == NULL)
6316 *expr_start = p;
6317 }
6318 else if (*p == '}')
6319 {
6320 mb_nest--;
6321 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6322 *expr_end = p;
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
6326
6327 return p;
6328}
6329
6330/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006331 * Expands out the 'magic' {}'s in a variable/function name.
6332 * Note that this can call itself recursively, to deal with
6333 * constructs like foo{bar}{baz}{bam}
6334 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6335 * "in_start" ^
6336 * "expr_start" ^
6337 * "expr_end" ^
6338 * "in_end" ^
6339 *
6340 * Returns a new allocated string, which the caller must free.
6341 * Returns NULL for failure.
6342 */
6343 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006344make_expanded_name(
6345 char_u *in_start,
6346 char_u *expr_start,
6347 char_u *expr_end,
6348 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006349{
6350 char_u c1;
6351 char_u *retval = NULL;
6352 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006353
6354 if (expr_end == NULL || in_end == NULL)
6355 return NULL;
6356 *expr_start = NUL;
6357 *expr_end = NUL;
6358 c1 = *in_end;
6359 *in_end = NUL;
6360
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006361 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006362 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006363 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006364 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6365 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006366 if (retval != NULL)
6367 {
6368 STRCPY(retval, in_start);
6369 STRCAT(retval, temp_result);
6370 STRCAT(retval, expr_end + 1);
6371 }
6372 }
6373 vim_free(temp_result);
6374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006375 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006376 *expr_start = '{';
6377 *expr_end = '}';
6378
6379 if (retval != NULL)
6380 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006381 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006382 if (expr_start != NULL)
6383 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006384 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006385 temp_result = make_expanded_name(retval, expr_start,
6386 expr_end, temp_result);
6387 vim_free(retval);
6388 retval = temp_result;
6389 }
6390 }
6391
6392 return retval;
6393}
6394
6395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006400eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006402 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006403}
6404
6405/*
6406 * Return TRUE if character "c" can be used as the first character in a
6407 * variable or function name (excluding '{' and '}').
6408 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006411{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006412 return ASCII_ISALPHA(c) || c == '_';
6413}
6414
6415/*
6416 * Return TRUE if character "c" can be used as the first character of a
6417 * dictionary key.
6418 */
6419 int
6420eval_isdictc(int c)
6421{
6422 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006426 * Handle:
6427 * - expr[expr], expr[expr:expr] subscript
6428 * - ".name" lookup
6429 * - function call with Funcref variable: func(expr)
6430 * - method call: var->method()
6431 *
6432 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006433 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006434 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436handle_subscript(
6437 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006438 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006440 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006441 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006442{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006443 int evaluate = evalarg != NULL
6444 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006445 int ret = OK;
6446 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006447 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006448 int getnext;
6449 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006450
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006451 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006453 // When at the end of the line and ".name" or "->{" or "->X" follows in
6454 // the next line then consume the line break.
6455 p = eval_next_non_blank(*arg, evalarg, &getnext);
6456 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006457 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006458 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6459 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6460 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006461 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006462 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006463 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006464 check_white = FALSE;
6465 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006466
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006467 if (rettv->v_type == VAR_ANY)
6468 {
6469 char_u *exp_name;
6470 int cc;
6471 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006472 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006473 type_T *type;
6474
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006475 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006476 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006477 if (**arg != '.')
6478 {
6479 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006480 semsg(_(e_expected_dot_after_name_str),
6481 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006482 ret = FAIL;
6483 break;
6484 }
6485 ++*arg;
6486 if (IS_WHITE_OR_NUL(**arg))
6487 {
6488 if (verbose)
6489 emsg(_(e_no_white_space_allowed_after_dot));
6490 ret = FAIL;
6491 break;
6492 }
6493
6494 // isolate the name
6495 exp_name = *arg;
6496 while (eval_isnamec(**arg))
6497 ++*arg;
6498 cc = **arg;
6499 **arg = NUL;
6500
6501 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006502 evalarg == NULL ? NULL : evalarg->eval_cctx,
6503 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006504 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006505
6506 if (idx < 0 && ufunc == NULL)
6507 {
6508 ret = FAIL;
6509 break;
6510 }
6511 if (idx >= 0)
6512 {
6513 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6514 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6515
6516 copy_tv(sv->sv_tv, rettv);
6517 }
6518 else
6519 {
6520 rettv->v_type = VAR_FUNC;
6521 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6522 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006523 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006524 }
6525
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006526 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6527 || rettv->v_type == VAR_PARTIAL))
6528 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006529 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006530 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6531 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006532
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006533 // Stop the expression evaluation when immediately aborting on
6534 // error, or when an interrupt occurred or an exception was thrown
6535 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006536 if (aborting())
6537 {
6538 if (ret == OK)
6539 clear_tv(rettv);
6540 ret = FAIL;
6541 }
6542 dict_unref(selfdict);
6543 selfdict = NULL;
6544 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006545 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006546 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006547 if (in_vim9script())
6548 *arg = skipwhite(p + 2);
6549 else
6550 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006551 if (ret == OK)
6552 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006553 if (VIM_ISWHITE(**arg))
6554 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006555 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006556 ret = FAIL;
6557 }
6558 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006559 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006560 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006561 else
6562 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006563 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006564 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006565 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006566 // "." is ".name" lookup when we found a dict or when evaluating and
6567 // scriptversion is at least 2, where string concatenation is "..".
6568 else if (**arg == '['
6569 || (**arg == '.' && (rettv->v_type == VAR_DICT
6570 || (!evaluate
6571 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006572 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006573 {
6574 dict_unref(selfdict);
6575 if (rettv->v_type == VAR_DICT)
6576 {
6577 selfdict = rettv->vval.v_dict;
6578 if (selfdict != NULL)
6579 ++selfdict->dv_refcount;
6580 }
6581 else
6582 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006583 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006584 {
6585 clear_tv(rettv);
6586 ret = FAIL;
6587 }
6588 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006589 else
6590 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006591 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6594 // Don't do this when "Func" is already a partial that was bound
6595 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006596 if (selfdict != NULL
6597 && (rettv->v_type == VAR_FUNC
6598 || (rettv->v_type == VAR_PARTIAL
6599 && (rettv->vval.v_partial->pt_auto
6600 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006601 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006602
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006603 dict_unref(selfdict);
6604 return ret;
6605}
6606
6607/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 * Make a copy of an item.
6609 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006610 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6612 * reference to an already copied list/dict can be used.
6613 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006614 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616item_copy(
6617 typval_T *from,
6618 typval_T *to,
6619 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006620 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622{
6623 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006625
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006628 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006629 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006630 }
6631 ++recurse;
6632
6633 switch (from->v_type)
6634 {
6635 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006636 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006637 case VAR_STRING:
6638 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006639 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006640 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006641 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006642 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006643 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006644 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006645 copy_tv(from, to);
6646 break;
6647 case VAR_LIST:
6648 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006649 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 if (from->vval.v_list == NULL)
6651 to->vval.v_list = NULL;
6652 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 to->vval.v_list = from->vval.v_list->lv_copylist;
6656 ++to->vval.v_list->lv_refcount;
6657 }
6658 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006659 to->vval.v_list = list_copy(from->vval.v_list,
6660 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006661 if (to->vval.v_list == NULL)
6662 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006664 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006666 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 case VAR_DICT:
6668 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006669 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 if (from->vval.v_dict == NULL)
6671 to->vval.v_dict = NULL;
6672 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6676 ++to->vval.v_dict->dv_refcount;
6677 }
6678 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006679 to->vval.v_dict = dict_copy(from->vval.v_dict,
6680 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (to->vval.v_dict == NULL)
6682 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006683 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006684 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006685 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006687 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006689 }
6690 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006692}
6693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 void
6695echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6696{
6697 char_u *tofree;
6698 char_u numbuf[NUMBUFLEN];
6699 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6700
6701 if (*atstart)
6702 {
6703 *atstart = FALSE;
6704 // Call msg_start() after eval1(), evaluating the expression
6705 // may cause a message to appear.
6706 if (with_space)
6707 {
6708 // Mark the saved text as finishing the line, so that what
6709 // follows is displayed on a new line when scrolling back
6710 // at the more prompt.
6711 msg_sb_eol();
6712 msg_start();
6713 }
6714 }
6715 else if (with_space)
6716 msg_puts_attr(" ", echo_attr);
6717
6718 if (p != NULL)
6719 for ( ; *p != NUL && !got_int; ++p)
6720 {
6721 if (*p == '\n' || *p == '\r' || *p == TAB)
6722 {
6723 if (*p != TAB && *needclr)
6724 {
6725 // remove any text still there from the command
6726 msg_clr_eos();
6727 *needclr = FALSE;
6728 }
6729 msg_putchar_attr(*p, echo_attr);
6730 }
6731 else
6732 {
6733 if (has_mbyte)
6734 {
6735 int i = (*mb_ptr2len)(p);
6736
6737 (void)msg_outtrans_len_attr(p, i, echo_attr);
6738 p += i - 1;
6739 }
6740 else
6741 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6742 }
6743 }
6744 vim_free(tofree);
6745}
6746
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 * ":echo expr1 ..." print each argument separated with a space, add a
6749 * newline at the end.
6750 * ":echon expr1 ..." print each argument plain.
6751 */
6752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006757 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 int needclr = TRUE;
6759 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006760 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006761 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006762 evalarg_T evalarg;
6763
Bram Moolenaare707c882020-07-01 13:07:37 +02006764 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 if (eap->skip)
6767 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006768 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006770 // If eval1() causes an error message the text from the command may
6771 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006772 need_clr_eos = needclr;
6773
Bram Moolenaar68db9962021-05-09 23:19:22 +02006774 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006775 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 /*
6778 * Report the invalid expression unless the expression evaluation
6779 * has been cancelled due to an aborting error, an interrupt, or an
6780 * exception.
6781 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006782 if (!aborting() && did_emsg == did_emsg_before
6783 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006784 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006785 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 break;
6787 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006788 need_clr_eos = FALSE;
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006791 {
6792 if (rettv.v_type == VAR_VOID)
6793 {
6794 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6795 break;
6796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006798 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006799
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006800 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 arg = skipwhite(arg);
6802 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006803 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006804 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 if (eap->skip)
6807 --emsg_skip;
6808 else
6809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006810 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (needclr)
6812 msg_clr_eos();
6813 if (eap->cmdidx == CMD_echo)
6814 msg_end();
6815 }
6816}
6817
6818/*
6819 * ":echohl {name}".
6820 */
6821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006824 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825}
6826
6827/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006828 * Returns the :echo attribute
6829 */
6830 int
6831get_echo_attr(void)
6832{
6833 return echo_attr;
6834}
6835
6836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 * ":execute expr1 ..." execute the result of an expression.
6838 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006839 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006841 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 * Each gets spaces around each argument and a newline at the end for
6843 * echo commands
6844 */
6845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006846ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006849 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 int ret = OK;
6851 char_u *p;
6852 garray_T ga;
6853 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006854 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 ga_init2(&ga, 1, 80);
6857
6858 if (eap->skip)
6859 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006860 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006862 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006863 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866 if (!eap->skip)
6867 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006868 char_u buf[NUMBUFLEN];
6869
6870 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006871 {
6872 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6873 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006874 semsg(_(e_using_invalid_value_as_string_str),
6875 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006876 p = NULL;
6877 }
6878 else
6879 p = tv_get_string_buf(&rettv, buf);
6880 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006881 else
6882 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006883 if (p == NULL)
6884 {
6885 clear_tv(&rettv);
6886 ret = FAIL;
6887 break;
6888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 len = (int)STRLEN(p);
6890 if (ga_grow(&ga, len + 2) == FAIL)
6891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 ret = FAIL;
6894 break;
6895 }
6896 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 ga.ga_len += len;
6900 }
6901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006902 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 arg = skipwhite(arg);
6904 }
6905
6906 if (ret != FAIL && ga.ga_data != NULL)
6907 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006908 // use the first line of continuation lines for messages
6909 SOURCING_LNUM = start_lnum;
6910
Bram Moolenaar37fef162022-08-29 18:16:32 +01006911 if (eap->cmdidx == CMD_echomsg
6912 || eap->cmdidx == CMD_echowindow
6913 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 // Mark the already saved text as finishing the line, so that what
6916 // follows is displayed on a new line when scrolling back at the
6917 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006918 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006919 }
6920
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006921 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006922 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006923 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006924 out_flush();
6925 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006926 else if (eap->cmdidx == CMD_echowindow)
6927 {
6928#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006929 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006930#endif
6931 msg_attr(ga.ga_data, echo_attr);
6932#ifdef HAS_MESSAGE_WINDOW
6933 end_echowindow();
6934#endif
6935 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006936 else if (eap->cmdidx == CMD_echoconsole)
6937 {
6938 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6939 ui_write((char_u *)"\r\n", 2, TRUE);
6940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 else if (eap->cmdidx == CMD_echoerr)
6942 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006943 int save_did_emsg = did_emsg;
6944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006946 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (!force_abort)
6948 did_emsg = save_did_emsg;
6949 }
6950 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006951 {
6952 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6953
6954 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6955 sticky_cmdmod_flags = cmdmod.cmod_flags
6956 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 do_cmdline((char_u *)ga.ga_data,
6958 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006959 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962
6963 ga_clear(&ga);
6964
6965 if (eap->skip)
6966 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006967 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969
6970/*
6971 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6972 * "arg" points to the "&" or '+' when called, to "option" when returning.
6973 * Returns NULL when no option name found. Otherwise pointer to the char
6974 * after the option name.
6975 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006976 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006977find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 char_u *p = *arg;
6980
6981 ++p;
6982 if (*p == 'g' && p[1] == ':')
6983 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006984 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 p += 2;
6986 }
6987 else if (*p == 'l' && p[1] == ':')
6988 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006989 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 p += 2;
6991 }
6992 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006993 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 if (!ASCII_ISALPHA(*p))
6996 return NULL;
6997 *arg = p;
6998
6999 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007000 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else
7002 while (ASCII_ISALPHA(*p))
7003 ++p;
7004 return p;
7005}
7006
7007/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007008 * Display script name where an item was last set.
7009 * Should only be invoked when 'verbose' is non-zero.
7010 */
7011 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007012last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007013{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007014 char_u *p;
7015
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007016 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007017 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007018 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007019 if (p != NULL)
7020 {
7021 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007022 msg_puts(_("\n\tLast set from "));
7023 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007024 if (script_ctx.sc_lnum > 0)
7025 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007026 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007027 msg_outnum((long)script_ctx.sc_lnum);
7028 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007029 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007030 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007031 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007032 }
7033}
7034
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007035#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037/*
7038 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007039 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * "flags" can be "g" to do a global substitute.
7041 * Returns an allocated string, NULL for error.
7042 */
7043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007044do_string_sub(
7045 char_u *str,
7046 char_u *pat,
7047 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007048 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007049 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 int sublen;
7052 regmatch_T regmatch;
7053 int i;
7054 int do_all;
7055 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007056 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 garray_T ga;
7058 char_u *ret;
7059 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007060 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007062 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007064 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
7066 ga_init2(&ga, 1, 200);
7067
7068 do_all = (flags[0] == 'g');
7069
7070 regmatch.rm_ic = p_ic;
7071 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7072 if (regmatch.regprog != NULL)
7073 {
7074 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007075 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007078 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007079 if (regmatch.startp[0] == regmatch.endp[0])
7080 {
7081 if (zero_width == regmatch.startp[0])
7082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007083 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007084 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007085 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7086 (size_t)i);
7087 ga.ga_len += i;
7088 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007089 continue;
7090 }
7091 zero_width = regmatch.startp[0];
7092 }
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 /*
7095 * Get some space for a temporary buffer to do the substitution
7096 * into. It will contain:
7097 * - The text up to where the match is.
7098 * - The substituted text.
7099 * - The text after the match.
7100 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007101 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007102 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7104 {
7105 ga_clear(&ga);
7106 break;
7107 }
7108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 i = (int)(regmatch.startp[0] - tail);
7111 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007112 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007113 (void)vim_regsub(&regmatch, sub, expr,
7114 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7115 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007117 tail = regmatch.endp[0];
7118 if (*tail == NUL)
7119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (!do_all)
7121 break;
7122 }
7123
7124 if (ga.ga_data != NULL)
7125 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7126
Bram Moolenaar473de612013-06-08 18:19:48 +02007127 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129
7130 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7131 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007132 if (p_cpo == empty_option)
7133 p_cpo = save_cpo;
7134 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007136 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007137 // If it's still empty it was changed and restored, need to restore in
7138 // the complicated way.
7139 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007140 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007141 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
7144 return ret;
7145}