blob: c0e9f76ade22c6605ce19f635ca13a97da97c682 [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,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000298 DEF_USE_PT_ARGV, partial, NULL, 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 Moolenaar7ce7daf2022-12-10 18:42:12 +00001196 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT
1197 && lp->ll_tv->v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001198 {
1199 if (!quiet)
1200 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1201 return NULL;
1202 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001203 if (lp->ll_tv->v_type != VAR_LIST
1204 && lp->ll_tv->v_type != VAR_DICT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001205 && lp->ll_tv->v_type != VAR_BLOB
1206 && lp->ll_tv->v_type != VAR_CLASS)
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 Moolenaar7ce7daf2022-12-10 18:42:12 +00001214 int r = OK;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001215 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001216 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001217 else if (lp->ll_tv->v_type == VAR_BLOB
1218 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001219 r = rettv_blob_alloc(lp->ll_tv);
1220 if (r == FAIL)
1221 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001222
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001223 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001226 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001229
Bram Moolenaar4525a572022-02-13 11:57:33 +00001230 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001231 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001232 && lp->ll_tv == &v->di_tv
1233 && ht != NULL && ht == get_script_local_ht())
1234 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001235 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001236
1237 // Vim9 script local variable: get the type
1238 if (sv != NULL)
1239 lp->ll_valtype = sv->sv_type;
1240 }
1241
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 len = -1;
1243 if (*p == '.')
1244 {
1245 key = p + 1;
1246 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1247 ;
1248 if (len == 0)
1249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001251 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 }
1254 p = key + len;
1255 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001256 else
1257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001258 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 if (*p == ':')
1261 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 else
1263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001264 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001265 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001267 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001269 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 clear_tv(&var1);
1271 return NULL;
1272 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001273 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001274 }
1275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001276 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 if (*p == ':')
1278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001282 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001283 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001285 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 if (rettv != NULL
1287 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001288 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001290 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001291 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001293 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001294 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001296 }
1297 p = skipwhite(p + 1);
1298 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 else
1301 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001303 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001304 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001305 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001306 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001309 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001311 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001313 clear_tv(&var2);
1314 return NULL;
1315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001319 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001321
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001325 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001326 clear_tv(&var1);
1327 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 }
1330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001331 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 ++p;
1333 }
1334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 {
1337 if (len == -1)
1338 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001339 // "[key]": get key from "var1"
1340 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001341 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001343 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 }
1346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001348
1349 // a NULL dict is equivalent with an empty dict
1350 if (lp->ll_tv->vval.v_dict == NULL)
1351 {
1352 lp->ll_tv->vval.v_dict = dict_alloc();
1353 if (lp->ll_tv->vval.v_dict == NULL)
1354 {
1355 clear_tv(&var1);
1356 return NULL;
1357 }
1358 ++lp->ll_tv->vval.v_dict->dv_refcount;
1359 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001361
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001362 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001364 // When assigning to a scope dictionary check that a function and
1365 // variable name is valid (only variable name unless it is l: or
1366 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001367 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001368 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001369 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001370
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 Moolenaar88456cd2022-11-18 22:14:09 +00001378 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001379 && (rettv->v_type == VAR_FUNC
1380 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001381 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001382 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001383 if (len != -1)
1384 key[len] = prevval;
1385 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001386 {
1387 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001388 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001389 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001390 }
1391
Bram Moolenaar10c65862020-10-08 21:16:42 +02001392 if (lp->ll_valtype != NULL)
1393 // use the type of the member
1394 lp->ll_valtype = lp->ll_valtype->tt_member;
1395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001398 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001399 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001400 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001401 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001402 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001403 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001404 return NULL;
1405 }
1406
Bram Moolenaar31b81602019-02-10 22:14:27 +01001407 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001408 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001411 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001412 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 }
1415 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001417 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001419 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 p = NULL;
1422 break;
1423 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001424 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001425 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001426 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1427 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001428 {
1429 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001430 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001431 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001432
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001433 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001435 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001436 else if (lp->ll_tv->v_type == VAR_BLOB)
1437 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001438 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1439
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001440 /*
1441 * Get the number and item for the only or first index of the List.
1442 */
1443 if (empty1)
1444 lp->ll_n1 = 0;
1445 else
1446 // is number or string
1447 lp->ll_n1 = (long)tv_get_number(&var1);
1448 clear_tv(&var1);
1449
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001450 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001451 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001452 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 return NULL;
1454 }
1455 if (lp->ll_range && !lp->ll_empty2)
1456 {
1457 lp->ll_n2 = (long)tv_get_number(&var2);
1458 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001459 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1460 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001461 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001462 }
1463 lp->ll_blob = lp->ll_tv->vval.v_blob;
1464 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001465 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001467 else if (lp->ll_tv->v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001468 {
1469 /*
1470 * Get the number and item for the only or first index of the List.
1471 */
1472 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001474 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001475 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001476 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001477 clear_tv(&var1);
1478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001481 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1482 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001483 if (lp->ll_li == NULL)
1484 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001485 clear_tv(&var2);
1486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001487 }
1488
Bram Moolenaar10c65862020-10-08 21:16:42 +02001489 if (lp->ll_valtype != NULL)
1490 // use the type of the member
1491 lp->ll_valtype = lp->ll_valtype->tt_member;
1492
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 /*
1494 * May need to find the item or absolute index for the second
1495 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 * When no index given: "lp->ll_empty2" is TRUE.
1497 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001501 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001502 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001504 if (check_range_index_two(lp->ll_list,
1505 &lp->ll_n1, lp->ll_li,
1506 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001508 }
1509
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001510 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001511 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001512 else // v_type == VAR_CLASS
1513 {
1514 // TODO: check object members and methods if
1515 // "key" points name start, "p" to the end
1516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 }
1518
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001519 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 return p;
1522}
1523
1524/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001525 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001526 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001528clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001529{
1530 vim_free(lp->ll_exp_name);
1531 vim_free(lp->ll_newkey);
1532}
1533
1534/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001535 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001536 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1538 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001539 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001541set_var_lval(
1542 lval_T *lp,
1543 char_u *endp,
1544 typval_T *rettv,
1545 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001546 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1547 char_u *op,
1548 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001549{
1550 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001551 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001552
1553 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001555 cc = *endp;
1556 *endp = NUL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001557 if (in_vim9script() && check_reserved_name(lp->ll_name, NULL) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001558 return;
1559
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 if (lp->ll_blob != NULL)
1561 {
1562 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001563
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 if (op != NULL && *op != '=')
1565 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001566 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001567 return;
1568 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001569 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001570 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571
1572 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1573 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001574 if (lp->ll_empty2)
1575 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1576
Bram Moolenaar68452172021-04-12 21:21:02 +02001577 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1578 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001579 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001580 }
1581 else
1582 {
1583 val = (int)tv_get_number_chk(rettv, &error);
1584 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001585 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001586 }
1587 }
1588 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001590 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001592 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1593 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001594 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001595 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001596 *endp = cc;
1597 return;
1598 }
1599
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001601 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001602 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001603 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001604 {
1605 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001606 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1607 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001608 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001609 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001610 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001611 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001614 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001615 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001616 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1617 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001618 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001619 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001620 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001621 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001622 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001623 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001624 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001625 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001626 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001627 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001628 else if (lp->ll_range)
1629 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001630 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1631 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001632 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001633 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001634 return;
1635 }
1636
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001637 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1638 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001639 }
1640 else
1641 {
1642 /*
1643 * Assign to a List or Dictionary item.
1644 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001645 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1646 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001647 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001648 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001649 return;
1650 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001651
1652 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001653 && check_typval_arg_type(lp->ll_valtype, rettv,
1654 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001655 return;
1656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001657 if (lp->ll_newkey != NULL)
1658 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 if (op != NULL && *op != '=')
1660 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001661 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 return;
1663 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001664 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1665 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001666 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001668 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001669 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001670 if (di == NULL)
1671 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001672 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1673 {
1674 vim_free(di);
1675 return;
1676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001677 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001678 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 else if (op != NULL && *op != '=')
1680 {
1681 tv_op(lp->ll_tv, rettv, op);
1682 return;
1683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001685 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001686
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001687 /*
1688 * Assign the value to the variable or list item.
1689 */
1690 if (copy)
1691 copy_tv(rettv, lp->ll_tv);
1692 else
1693 {
1694 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001695 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001696 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 }
1698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699}
1700
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001702 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1703 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 * Returns OK or FAIL.
1705 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001707tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001709 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 char_u numbuf[NUMBUFLEN];
1711 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001712 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001714 // Can't do anything with a Funcref or Dict on the right.
1715 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001716 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001717 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1718 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 switch (tv1->v_type)
1721 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001722 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001723 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 case VAR_DICT:
1726 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001727 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001728 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001729 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001730 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001731 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001732 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001733 case VAR_CLASS:
1734 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 break;
1736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 case VAR_BLOB:
1738 if (*op != '+' || tv2->v_type != VAR_BLOB)
1739 break;
1740 // BLOB += BLOB
1741 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1742 {
1743 blob_T *b1 = tv1->vval.v_blob;
1744 blob_T *b2 = tv2->vval.v_blob;
1745 int i, len = blob_len(b2);
1746 for (i = 0; i < len; i++)
1747 ga_append(&b1->bv_ga, blob_get(b2, i));
1748 }
1749 return OK;
1750
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 case VAR_LIST:
1752 if (*op != '+' || tv2->v_type != VAR_LIST)
1753 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001754 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001755 if (tv2->vval.v_list != NULL)
1756 {
1757 if (tv1->vval.v_list == NULL)
1758 {
1759 tv1->vval.v_list = tv2->vval.v_list;
1760 ++tv1->vval.v_list->lv_refcount;
1761 }
1762 else
1763 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 return OK;
1766
1767 case VAR_NUMBER:
1768 case VAR_STRING:
1769 if (tv2->v_type == VAR_LIST)
1770 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001771 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001773 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001774 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001775 if (tv2->v_type == VAR_FLOAT)
1776 {
1777 float_T f = n;
1778
Bram Moolenaarff697e62019-02-12 22:28:33 +01001779 if (*op == '%')
1780 break;
1781 switch (*op)
1782 {
1783 case '+': f += tv2->vval.v_float; break;
1784 case '-': f -= tv2->vval.v_float; break;
1785 case '*': f *= tv2->vval.v_float; break;
1786 case '/': f /= tv2->vval.v_float; break;
1787 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001788 clear_tv(tv1);
1789 tv1->v_type = VAR_FLOAT;
1790 tv1->vval.v_float = f;
1791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001793 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001794 switch (*op)
1795 {
1796 case '+': n += tv_get_number(tv2); break;
1797 case '-': n -= tv_get_number(tv2); break;
1798 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001799 case '/': n = num_divide(n, tv_get_number(tv2),
1800 &failed); break;
1801 case '%': n = num_modulus(n, tv_get_number(tv2),
1802 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001803 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001804 clear_tv(tv1);
1805 tv1->v_type = VAR_NUMBER;
1806 tv1->vval.v_number = n;
1807 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 }
1809 else
1810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001811 if (tv2->v_type == VAR_FLOAT)
1812 break;
1813
Bram Moolenaarff697e62019-02-12 22:28:33 +01001814 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001815 s = tv_get_string(tv1);
1816 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 clear_tv(tv1);
1818 tv1->v_type = VAR_STRING;
1819 tv1->vval.v_string = s;
1820 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001821 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001823 case VAR_FLOAT:
1824 {
1825 float_T f;
1826
Bram Moolenaarff697e62019-02-12 22:28:33 +01001827 if (*op == '%' || *op == '.'
1828 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001829 && tv2->v_type != VAR_NUMBER
1830 && tv2->v_type != VAR_STRING))
1831 break;
1832 if (tv2->v_type == VAR_FLOAT)
1833 f = tv2->vval.v_float;
1834 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001835 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001836 switch (*op)
1837 {
1838 case '+': tv1->vval.v_float += f; break;
1839 case '-': tv1->vval.v_float -= f; break;
1840 case '*': tv1->vval.v_float *= f; break;
1841 case '/': tv1->vval.v_float /= f; break;
1842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001843 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001844 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 }
1846 }
1847
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001848 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 return FAIL;
1850}
1851
1852/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 * Evaluate the expression used in a ":for var in expr" command.
1854 * "arg" points to "var".
1855 * Set "*errp" to TRUE for an error, FALSE otherwise;
1856 * Return a pointer that holds the info. Null when there is an error.
1857 */
1858 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001859eval_for_line(
1860 char_u *arg,
1861 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001862 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001863 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864{
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001866 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 typval_T tv;
1869 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001870 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001874 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 if (fi == NULL)
1876 return NULL;
1877
Bram Moolenaar404557e2021-07-05 21:41:48 +02001878 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1879 &fi->fi_semicolon, FALSE);
1880 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 return fi;
1882
Bram Moolenaar404557e2021-07-05 21:41:48 +02001883 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001884 if (expr[0] != 'i' || expr[1] != 'n'
1885 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001887 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1888 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1889 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001890 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 return fi;
1892 }
1893
1894 if (skip)
1895 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001896 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1897 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 {
1899 *errp = FALSE;
1900 if (!skip)
1901 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001902 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001903 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001904 l = tv.vval.v_list;
1905 if (l == NULL)
1906 {
1907 // a null list is like an empty list: do nothing
1908 clear_tv(&tv);
1909 }
1910 else
1911 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001913 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001915 // No need to increment the refcount, it's already set for
1916 // the list being used in "tv".
1917 fi->fi_list = l;
1918 list_add_watch(l, &fi->fi_lw);
1919 fi->fi_lw.lw_item = l->lv_first;
1920 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001921 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001922 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001923 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001924 fi->fi_bi = 0;
1925 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001926 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001927 typval_T btv;
1928
1929 // Make a copy, so that the iteration still works when the
1930 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001932 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001933 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001934 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001935 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001936 else if (tv.v_type == VAR_STRING)
1937 {
1938 fi->fi_byte_idx = 0;
1939 fi->fi_string = tv.vval.v_string;
1940 tv.vval.v_string = NULL;
1941 if (fi->fi_string == NULL)
1942 fi->fi_string = vim_strsave((char_u *)"");
1943 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 else
1945 {
Sean Dewar80d73952021-08-04 19:25:54 +02001946 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001947 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 }
1949 }
1950 }
1951 if (skip)
1952 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001953 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954
1955 return fi;
1956}
1957
1958/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001959 * Used when looping over a :for line, skip the "in expr" part.
1960 */
1961 void
1962skip_for_lines(void *fi_void, evalarg_T *evalarg)
1963{
1964 forinfo_T *fi = (forinfo_T *)fi_void;
1965 int i;
1966
1967 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001968 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001969}
1970
1971/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 * Use the first item in a ":for" list. Advance to the next.
1973 * Assign the values to the variable (list). "arg" points to the first one.
1974 * Return TRUE when a valid item was found, FALSE when at end of list or
1975 * something wrong.
1976 */
1977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001978next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001980 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001982 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001983 ? (ASSIGN_FINAL
1984 // first round: error if variable exists
1985 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001986 | ASSIGN_NO_MEMBER_TYPE
1987 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001988 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001990 int skip_assign = in_vim9script() && arg[0] == '_'
1991 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001993 if (fi->fi_blob != NULL)
1994 {
1995 typval_T tv;
1996
1997 if (fi->fi_bi >= blob_len(fi->fi_blob))
1998 return FALSE;
1999 tv.v_type = VAR_NUMBER;
2000 tv.v_lock = VAR_FIXED;
2001 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2002 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002003 if (skip_assign)
2004 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002005 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002006 fi->fi_varcount, flag, NULL) == OK;
2007 }
2008
2009 if (fi->fi_string != NULL)
2010 {
2011 typval_T tv;
2012 int len;
2013
2014 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2015 if (len == 0)
2016 return FALSE;
2017 tv.v_type = VAR_STRING;
2018 tv.v_lock = VAR_FIXED;
2019 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2020 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002021 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002022 if (skip_assign)
2023 result = TRUE;
2024 else
2025 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002026 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002027 vim_free(tv.vval.v_string);
2028 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002029 }
2030
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 item = fi->fi_lw.lw_item;
2032 if (item == NULL)
2033 result = FALSE;
2034 else
2035 {
2036 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002037 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002038 if (skip_assign)
2039 result = TRUE;
2040 else
2041 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002042 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 }
2044 return result;
2045}
2046
2047/*
2048 * Free the structure used to store info used by ":for".
2049 */
2050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002051free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052{
Bram Moolenaar33570922005-01-25 22:26:29 +00002053 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002055 if (fi == NULL)
2056 return;
2057 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002058 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002060 list_unref(fi->fi_list);
2061 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002062 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002063 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002064 else
2065 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 vim_free(fi);
2067}
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002070set_context_for_expression(
2071 expand_T *xp,
2072 char_u *arg,
2073 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002075 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002079 if (cmdidx == CMD_let || cmdidx == CMD_var
2080 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 {
2082 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 {
2088 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002089 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002090 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 break;
2092 }
2093 return;
2094 }
2095 }
2096 else
2097 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2098 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 while ((xp->xp_pattern = vim_strpbrk(arg,
2100 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2101 {
2102 c = *xp->xp_pattern;
2103 if (c == '&')
2104 {
2105 c = xp->xp_pattern[1];
2106 if (c == '&')
2107 {
2108 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002109 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002114 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2115 xp->xp_pattern += 2;
2116
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119 else if (c == '$')
2120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002121 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 xp->xp_context = EXPAND_ENV_VARS;
2123 }
2124 else if (c == '=')
2125 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002126 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 xp->xp_context = EXPAND_EXPRESSION;
2128 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002129 else if (c == '#'
2130 && xp->xp_context == EXPAND_EXPRESSION)
2131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002133 break;
2134 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002135 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 && xp->xp_context == EXPAND_FUNCTIONS
2137 && vim_strchr(xp->xp_pattern, '(') == NULL)
2138 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 break;
2141 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002142 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2147 if (c == '\\' && xp->xp_pattern[1] != NUL)
2148 ++xp->xp_pattern;
2149 xp->xp_context = EXPAND_NOTHING;
2150 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002151 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002153 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2155 /* skip */ ;
2156 xp->xp_context = EXPAND_NOTHING;
2157 }
2158 else if (c == '|')
2159 {
2160 if (xp->xp_pattern[1] == '|')
2161 {
2162 ++xp->xp_pattern;
2163 xp->xp_context = EXPAND_EXPRESSION;
2164 }
2165 else
2166 xp->xp_context = EXPAND_COMMANDS;
2167 }
2168 else
2169 xp->xp_context = EXPAND_EXPRESSION;
2170 }
2171 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002172 // Doesn't look like something valid, expand as an expression
2173 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 arg = xp->xp_pattern;
2176 if (*arg != NUL)
2177 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2178 /* skip */ ;
2179 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002180
2181 // ":exe one two" completes "two"
2182 if ((cmdidx == CMD_execute
2183 || cmdidx == CMD_echo
2184 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002185 || cmdidx == CMD_echomsg
2186 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002187 && xp->xp_context == EXPAND_EXPRESSION)
2188 {
2189 for (;;)
2190 {
2191 char_u *n = skiptowhite(arg);
2192
2193 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2194 break;
2195 arg = skipwhite(n);
2196 }
2197 }
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 xp->xp_pattern = arg;
2200}
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002203 * Return TRUE if "pat" matches "text".
2204 * Does not use 'cpo' and always uses 'magic'.
2205 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002206 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002207pattern_match(char_u *pat, char_u *text, int ic)
2208{
2209 int matches = FALSE;
2210 char_u *save_cpo;
2211 regmatch_T regmatch;
2212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002214 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002215 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002216 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2217 if (regmatch.regprog != NULL)
2218 {
2219 regmatch.rm_ic = ic;
2220 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2221 vim_regfree(regmatch.regprog);
2222 }
2223 p_cpo = save_cpo;
2224 return matches;
2225}
2226
2227/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002228 * Handle a name followed by "(". Both for just "name(arg)" and for
2229 * "expr->name(arg)".
2230 * Returns OK or FAIL.
2231 */
2232 static int
2233eval_func(
2234 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002235 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002236 char_u *name,
2237 int name_len,
2238 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002239 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002240 typval_T *basetv) // "expr" for "expr->name(arg)"
2241{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002242 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002243 char_u *s = name;
2244 int len = name_len;
2245 partial_T *partial;
2246 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002247 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002248 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002249
2250 if (!evaluate)
2251 check_vars(s, len);
2252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002253 // If "s" is the name of a variable of type VAR_FUNC
2254 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002255 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002256 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002258 // Need to make a copy, in case evaluating the arguments makes
2259 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002260 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002261 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002262 ret = FAIL;
2263 else
2264 {
2265 funcexe_T funcexe;
2266
2267 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002269 funcexe.fe_firstline = curwin->w_cursor.lnum;
2270 funcexe.fe_lastline = curwin->w_cursor.lnum;
2271 funcexe.fe_evaluate = evaluate;
2272 funcexe.fe_partial = partial;
2273 funcexe.fe_basetv = basetv;
2274 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002275 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002276 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002277 }
2278 vim_free(s);
2279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002280 // If evaluate is FALSE rettv->v_type was not set in
2281 // get_func_tv, but it's needed in handle_subscript() to parse
2282 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002283 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2284 {
2285 rettv->vval.v_string = NULL;
2286 rettv->v_type = VAR_FUNC;
2287 }
2288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002289 // Stop the expression evaluation when immediately
2290 // aborting on error, or when an interrupt occurred or
2291 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002292 if (evaluate && aborting())
2293 {
2294 if (ret == OK)
2295 clear_tv(rettv);
2296 ret = FAIL;
2297 }
2298 return ret;
2299}
2300
2301/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002302 * After a NL, skip over empty lines and comment-only lines.
2303 */
2304 static char_u *
2305newline_skip_comments(char_u *arg)
2306{
2307 char_u *p = arg + 1;
2308
2309 for (;;)
2310 {
2311 p = skipwhite(p);
2312
2313 if (*p == NUL)
2314 break;
2315 if (vim9_comment_start(p))
2316 {
2317 char_u *nl = vim_strchr(p, NL);
2318
2319 if (nl == NULL)
2320 break;
2321 p = nl;
2322 }
2323 if (*p != NL)
2324 break;
2325 ++p; // skip another NL
2326 }
2327 return p;
2328}
2329
2330/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002331 * Get the next line source line without advancing. But do skip over comment
2332 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002333 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002334 */
2335 static char_u *
2336getline_peek_skip_comments(evalarg_T *evalarg)
2337{
2338 for (;;)
2339 {
2340 char_u *next = getline_peek(evalarg->eval_getline,
2341 evalarg->eval_cookie);
2342 char_u *p;
2343
2344 if (next == NULL)
2345 break;
2346 p = skipwhite(next);
2347 if (*p != NUL && !vim9_comment_start(p))
2348 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002349 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002350 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002351 }
2352 return NULL;
2353}
2354
2355/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002356 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2357 * comment) and there is a next line, return the next line (skipping blanks)
2358 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002359 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2360 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002361 * "arg" must point somewhere inside a line, not at the start.
2362 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002363 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2365{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002366 char_u *p = skipwhite(arg);
2367
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002368 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002369 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002370 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002371 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2372 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002373 && (*p == NUL || *p == NL
2374 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002375 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002376 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002377
Bram Moolenaare442d592022-05-05 12:20:28 +01002378 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002379 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002380 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002381 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002382 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002383 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384
Bram Moolenaar9d489562020-07-30 20:08:50 +02002385 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002386 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002387 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002388 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002389 }
2390 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002391 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392}
2393
2394/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002395 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002396 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002397 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002398 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002399eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002400{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002401 garray_T *gap = &evalarg->eval_ga;
2402 char_u *line;
2403
Bram Moolenaar39be4982022-05-06 21:51:50 +01002404 if (arg != NULL)
2405 {
2406 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002407 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002408 // Truncate before a trailing comment, so that concatenating the lines
2409 // won't turn the rest into a comment.
2410 if (*skipwhite(arg) == '#')
2411 *arg = NUL;
2412 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002413
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002414 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002415 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2416 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002417 else
2418 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002419 if (line == NULL)
2420 return NULL;
2421
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002422 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002423 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2424 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002425 char_u *p = skipwhite(line);
2426
2427 // Going to concatenate the lines after parsing. For an empty or
2428 // comment line use an empty string.
2429 if (*p == NUL || vim9_comment_start(p))
2430 {
2431 vim_free(line);
2432 line = vim_strsave((char_u *)"");
2433 }
2434
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002435 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2436 ++gap->ga_len;
2437 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002438 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002439 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002440 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002441 evalarg->eval_tofree = line;
2442 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002443
2444 // Advanced to the next line, "arg" no longer points into the previous
2445 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002446 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002447 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002448}
2449
Bram Moolenaare6b53242020-07-01 17:28:33 +02002450/*
2451 * Call eval_next_non_blank() and get the next line if needed.
2452 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002453 char_u *
2454skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2455{
2456 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002457 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002458
Bram Moolenaar962d7212020-07-04 14:15:00 +02002459 if (evalarg == NULL)
2460 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002461 eval_next_non_blank(p, evalarg, &getnext);
2462 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002463 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002464 return p;
2465}
2466
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002467/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002468 * The "eval" functions have an "evalarg" argument: When NULL or
2469 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2470 * parsed but not executed. The functions may return OK, but the rettv will be
2471 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 */
2473
2474/*
2475 * Handle zero level expression.
2476 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002478 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002479 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 * Return OK or FAIL.
2481 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002483eval0(
2484 char_u *arg,
2485 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002486 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002489 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2490}
2491
2492/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002493 * If "arg" is a simple function call without arguments then call it and return
2494 * the result. Otherwise return NOTDONE.
2495 */
2496 int
2497may_call_simple_func(
2498 char_u *arg,
2499 typval_T *rettv)
2500{
2501 char_u *parens = (char_u *)strstr((char *)arg, "()");
2502 int r = NOTDONE;
2503
2504 // If the expression is "FuncName()" then we can skip a lot of overhead.
2505 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2506 {
2507 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2508
2509 if (to_name_end(p, TRUE) == parens)
2510 r = call_simple_func(arg, (int)(parens - arg), rettv);
2511 }
2512 return r;
2513}
2514
2515/*
2516 * Handle zero level expression with optimization for a simple function call.
2517 * Same arguments and return value as eval0().
2518 */
2519 int
2520eval0_simple_funccal(
2521 char_u *arg,
2522 typval_T *rettv,
2523 exarg_T *eap,
2524 evalarg_T *evalarg)
2525{
2526 int r = may_call_simple_func(arg, rettv);
2527
2528 if (r == NOTDONE)
2529 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2530 return r;
2531}
2532
2533/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002534 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2535 * expression and don't check what comes after the expression.
2536 */
2537 int
2538eval0_retarg(
2539 char_u *arg,
2540 typval_T *rettv,
2541 exarg_T *eap,
2542 evalarg_T *evalarg,
2543 char_u **retarg)
2544{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 int ret;
2546 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002547 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002548 int did_emsg_before = did_emsg;
2549 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002551 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002552 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002556
Bram Moolenaar79481362022-06-27 20:15:10 +01002557 if (ret != FAIL)
2558 {
2559 expr_end = p;
2560 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002561
Bram Moolenaar79481362022-06-27 20:15:10 +01002562 // In Vim9 script a command block is not split at NL characters for
2563 // commands using an expression argument. Skip over a '#' comment to
2564 // check for a following NL. Require white space before the '#'.
2565 if (in_vim9script() && p > expr_end && retarg == NULL)
2566 while (*p == '#')
2567 {
2568 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002569
Bram Moolenaar79481362022-06-27 20:15:10 +01002570 if (nl == NULL)
2571 break;
2572 p = skipwhite(nl + 1);
2573 if (eap != NULL && *p != NUL)
2574 eap->nextcmd = p;
2575 check_for_end = FALSE;
2576 }
2577
2578 if (check_for_end)
2579 end_error = !ends_excmd2(arg, p);
2580 }
2581
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002582 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /*
2587 * Report the invalid expression unless the expression evaluation has
2588 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002589 * exception, or we already gave a more specific error.
2590 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002592 if (!aborting()
2593 && did_emsg == did_emsg_before
2594 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002595 && (flags & EVAL_CONSTANT) == 0
2596 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002597 {
2598 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002599 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002600 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002601 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002602 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002603
2604 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002605 // a next command to avoid more errors, unless "|" is following, which
2606 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002607 if (eap != NULL && p != NULL
2608 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002609 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002610 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002612
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002613 if (retarg != NULL)
2614 *retarg = p;
2615 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002616 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 return ret;
2619}
2620
2621/*
2622 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002623 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002624 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 *
2626 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002627 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002629 * Note: "rettv.v_lock" is not set.
2630 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * Return OK or FAIL.
2632 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002633 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002634eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002636 char_u *p;
2637 int getnext;
2638
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002639 CLEAR_POINTER(rettv);
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 /*
2642 * Get the first variable.
2643 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return FAIL;
2646
Bram Moolenaar793648f2020-06-26 21:28:25 +02002647 p = eval_next_non_blank(*arg, evalarg, &getnext);
2648 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002650 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 int result;
2652 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 evalarg_T *evalarg_used = evalarg;
2654 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002655 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002656 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002657 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658
2659 if (evalarg == NULL)
2660 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002661 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002662 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 orig_flags = evalarg_used->eval_flags;
2665 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002668 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002669 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002670 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002671 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002672 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002673 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002674 clear_tv(rettv);
2675 return FAIL;
2676 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002677 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002678 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 int error = FALSE;
2684
Bram Moolenaar13106602020-10-04 16:06:05 +02002685 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002686 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002687 else if (vim9script)
2688 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002689 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002691 if (error || !op_falsy || !result)
2692 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 if (error)
2694 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696
2697 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002698 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002700 if (op_falsy)
2701 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002702 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002703 {
mityu4ac198c2021-05-28 17:52:40 +02002704 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002705 clear_tv(rettv);
2706 return FAIL;
2707 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002709 evalarg_used->eval_flags = (op_falsy ? !result : result)
2710 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2711 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002712 {
2713 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002715 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002716 if (!op_falsy || !result)
2717 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002719 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002721 /*
2722 * Check for the ":".
2723 */
2724 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2725 if (*p != ':')
2726 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002727 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002728 if (evaluate && result)
2729 clear_tv(rettv);
2730 evalarg_used->eval_flags = orig_flags;
2731 return FAIL;
2732 }
2733 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002734 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002735 else
2736 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002737 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002738 {
2739 error_white_both(p, 1);
2740 clear_tv(rettv);
2741 evalarg_used->eval_flags = orig_flags;
2742 return FAIL;
2743 }
2744 *arg = p;
2745 }
2746
2747 /*
2748 * Get the third variable. Recursive!
2749 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002750 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002751 {
mityu4ac198c2021-05-28 17:52:40 +02002752 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002753 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002754 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002755 return FAIL;
2756 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002757 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2758 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002759 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002760 if (eval1(arg, &var2, evalarg_used) == FAIL)
2761 {
2762 if (evaluate && result)
2763 clear_tv(rettv);
2764 evalarg_used->eval_flags = orig_flags;
2765 return FAIL;
2766 }
2767 if (evaluate && !result)
2768 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002770
2771 if (evalarg == NULL)
2772 clear_evalarg(&local_evalarg, NULL);
2773 else
2774 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776
2777 return OK;
2778}
2779
2780/*
2781 * Handle first level expression:
2782 * expr2 || expr2 || expr2 logical OR
2783 *
2784 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002785 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 *
2787 * Return OK or FAIL.
2788 */
2789 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002790eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002792 char_u *p;
2793 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002796 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002798 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return FAIL;
2800
2801 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002802 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002804 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002807 evalarg_T *evalarg_used = evalarg;
2808 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002809 int evaluate;
2810 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002811 long result = FALSE;
2812 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002813 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002814 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002815
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 if (evalarg == NULL)
2817 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002818 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002819 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002820 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 orig_flags = evalarg_used->eval_flags;
2822 evaluate = orig_flags & EVAL_EVALUATE;
2823 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002824 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002825 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002826 result = tv_get_bool_chk(rettv, &error);
2827 else if (tv_get_number_chk(rettv, &error) != 0)
2828 result = TRUE;
2829 clear_tv(rettv);
2830 if (error)
2831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833
2834 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002835 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002837 while (p[0] == '|' && p[1] == '|')
2838 {
2839 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002840 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002841 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002842 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002843 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002844 {
2845 error_white_both(p, 2);
2846 clear_tv(rettv);
2847 return FAIL;
2848 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002849 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002850 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002851
2852 /*
2853 * Get the second variable.
2854 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002855 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002856 {
mityu4ac198c2021-05-28 17:52:40 +02002857 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002858 clear_tv(rettv);
2859 return FAIL;
2860 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002861 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2862 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002864 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002865 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002866
2867 /*
2868 * Compute the result.
2869 */
2870 if (evaluate && !result)
2871 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002873 result = tv_get_bool_chk(&var2, &error);
2874 else if (tv_get_number_chk(&var2, &error) != 0)
2875 result = TRUE;
2876 clear_tv(&var2);
2877 if (error)
2878 return FAIL;
2879 }
2880 if (evaluate)
2881 {
2882 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 rettv->v_type = VAR_BOOL;
2885 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002886 }
2887 else
2888 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002889 rettv->v_type = VAR_NUMBER;
2890 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002891 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002892 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893
2894 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002896
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002897 if (evalarg == NULL)
2898 clear_evalarg(&local_evalarg, NULL);
2899 else
2900 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902
2903 return OK;
2904}
2905
2906/*
2907 * Handle second level expression:
2908 * expr3 && expr3 && expr3 logical AND
2909 *
2910 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002911 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 *
2913 * Return OK or FAIL.
2914 */
2915 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002918 char_u *p;
2919 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002922 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 return FAIL;
2926
2927 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002928 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002930 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002931 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002933 evalarg_T *evalarg_used = evalarg;
2934 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935 int orig_flags;
2936 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002937 long result = TRUE;
2938 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002939 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002940 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002941
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002942 if (evalarg == NULL)
2943 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002944 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002945 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002946 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002947 orig_flags = evalarg_used->eval_flags;
2948 evaluate = orig_flags & EVAL_EVALUATE;
2949 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002951 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002952 result = tv_get_bool_chk(rettv, &error);
2953 else if (tv_get_number_chk(rettv, &error) == 0)
2954 result = FALSE;
2955 clear_tv(rettv);
2956 if (error)
2957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959
2960 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002961 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002963 while (p[0] == '&' && p[1] == '&')
2964 {
2965 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002966 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002967 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002968 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002969 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002970 {
2971 error_white_both(p, 2);
2972 clear_tv(rettv);
2973 return FAIL;
2974 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002975 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002976 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002977
2978 /*
2979 * Get the second variable.
2980 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002981 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002982 {
mityu4ac198c2021-05-28 17:52:40 +02002983 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002984 clear_tv(rettv);
2985 return FAIL;
2986 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002987 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2988 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002990 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002991 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002992 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002993
2994 /*
2995 * Compute the result.
2996 */
2997 if (evaluate && result)
2998 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002999 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003000 result = tv_get_bool_chk(&var2, &error);
3001 else if (tv_get_number_chk(&var2, &error) == 0)
3002 result = FALSE;
3003 clear_tv(&var2);
3004 if (error)
3005 return FAIL;
3006 }
3007 if (evaluate)
3008 {
3009 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003010 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003011 rettv->v_type = VAR_BOOL;
3012 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003013 }
3014 else
3015 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003016 rettv->v_type = VAR_NUMBER;
3017 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003018 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003019 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003020
3021 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003023
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003024 if (evalarg == NULL)
3025 clear_evalarg(&local_evalarg, NULL);
3026 else
3027 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
3029
3030 return OK;
3031}
3032
3033/*
3034 * Handle third level expression:
3035 * var1 == var2
3036 * var1 =~ var2
3037 * var1 != var2
3038 * var1 !~ var2
3039 * var1 > var2
3040 * var1 >= var2
3041 * var1 < var2
3042 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003043 * var1 is var2
3044 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 *
3046 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003047 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 *
3049 * Return OK or FAIL.
3050 */
3051 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003052eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003055 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003056 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003058 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003061 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003063 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return FAIL;
3065
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003066 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003067
Bram Moolenaar696ba232020-07-29 21:20:41 +02003068 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003073 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003075 typval_T var2;
3076 int ic;
3077 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003078 int evaluate = evalarg == NULL
3079 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003080 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003081
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003082 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003083 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003084 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003085 p = *arg;
3086 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003087 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3088 {
mityu4ac198c2021-05-28 17:52:40 +02003089 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003090 clear_tv(rettv);
3091 return FAIL;
3092 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003093
Bram Moolenaar696ba232020-07-29 21:20:41 +02003094 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3095 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003096 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003097 clear_tv(rettv);
3098 return FAIL;
3099 }
3100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (p[len] == '?')
3103 {
3104 ic = TRUE;
3105 ++len;
3106 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else if (p[len] == '#')
3109 {
3110 ic = FALSE;
3111 ++len;
3112 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003113 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003115 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 /*
3118 * Get the second variable.
3119 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003120 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3121 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003122 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003123 clear_tv(rettv);
3124 return FAIL;
3125 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003126 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 return FAIL;
3131 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003132 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003133 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003134 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003135
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003136 // use the line of the comparison for messages
3137 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003138 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003139 {
3140 ret = FAIL;
3141 clear_tv(rettv);
3142 }
3143 else
3144 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003145 clear_tv(&var2);
3146 return ret;
3147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149
3150 return OK;
3151}
3152
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003153/*
3154 * Make a copy of blob "tv1" and append blob "tv2".
3155 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 void
3157eval_addblob(typval_T *tv1, typval_T *tv2)
3158{
3159 blob_T *b1 = tv1->vval.v_blob;
3160 blob_T *b2 = tv2->vval.v_blob;
3161 blob_T *b = blob_alloc();
3162 int i;
3163
3164 if (b != NULL)
3165 {
3166 for (i = 0; i < blob_len(b1); i++)
3167 ga_append(&b->bv_ga, blob_get(b1, i));
3168 for (i = 0; i < blob_len(b2); i++)
3169 ga_append(&b->bv_ga, blob_get(b2, i));
3170
3171 clear_tv(tv1);
3172 rettv_blob_set(tv1, b);
3173 }
3174}
3175
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003176/*
3177 * Make a copy of list "tv1" and append list "tv2".
3178 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 int
3180eval_addlist(typval_T *tv1, typval_T *tv2)
3181{
3182 typval_T var3;
3183
3184 // concatenate Lists
3185 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3186 {
3187 clear_tv(tv1);
3188 clear_tv(tv2);
3189 return FAIL;
3190 }
3191 clear_tv(tv1);
3192 *tv1 = var3;
3193 return OK;
3194}
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003197 * Handle the bitwise left/right shift operator expression:
3198 * var1 << var2
3199 * var1 >> var2
3200 *
3201 * "arg" must point to the first non-white of the expression.
3202 * "arg" is advanced to just after the recognized expression.
3203 *
3204 * Return OK or FAIL.
3205 */
3206 static int
3207eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3208{
3209 /*
3210 * Get the first expression.
3211 */
3212 if (eval6(arg, rettv, evalarg) == FAIL)
3213 return FAIL;
3214
3215 /*
3216 * Repeat computing, until no '<<' or '>>' is following.
3217 */
3218 for (;;)
3219 {
3220 char_u *p;
3221 int getnext;
3222 exprtype_T type;
3223 int evaluate;
3224 typval_T var2;
3225 int vim9script;
3226
3227 p = eval_next_non_blank(*arg, evalarg, &getnext);
3228 if (p[0] == '<' && p[1] == '<')
3229 type = EXPR_LSHIFT;
3230 else if (p[0] == '>' && p[1] == '>')
3231 type = EXPR_RSHIFT;
3232 else
3233 return OK;
3234
3235 // Handle a bitwise left or right shift operator
3236 if (rettv->v_type != VAR_NUMBER)
3237 {
3238 // left operand should be a number
3239 emsg(_(e_bitshift_ops_must_be_number));
3240 clear_tv(rettv);
3241 return FAIL;
3242 }
3243
3244 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3245 vim9script = in_vim9script();
3246 if (getnext)
3247 {
3248 *arg = eval_next_line(*arg, evalarg);
3249 p = *arg;
3250 }
3251 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3252 {
3253 error_white_both(*arg, 2);
3254 clear_tv(rettv);
3255 return FAIL;
3256 }
3257
3258 /*
3259 * Get the second variable.
3260 */
3261 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3262 {
3263 error_white_both(p, 2);
3264 clear_tv(rettv);
3265 return FAIL;
3266 }
3267 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3268 if (eval6(arg, &var2, evalarg) == FAIL)
3269 {
3270 clear_tv(rettv);
3271 return FAIL;
3272 }
3273
3274 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3275 {
3276 // right operand should be a positive number
3277 if (var2.v_type != VAR_NUMBER)
3278 emsg(_(e_bitshift_ops_must_be_number));
3279 else
dundargocc57b5bc2022-11-02 13:30:51 +00003280 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003281 clear_tv(rettv);
3282 clear_tv(&var2);
3283 return FAIL;
3284 }
3285
3286 if (evaluate)
3287 {
3288 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3289 // shifting more bits than we have always results in zero
3290 rettv->vval.v_number = 0;
3291 else if (type == EXPR_LSHIFT)
3292 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003293 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003294 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003295 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003296 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003297 }
3298
3299 clear_tv(&var2);
3300 }
3301
3302 return OK;
3303}
3304
3305/*
3306 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003307 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003309 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003310 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 *
3312 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003313 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 *
3315 * Return OK or FAIL.
3316 */
3317 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003318eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003321 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003323 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 return FAIL;
3325
3326 /*
3327 * Repeat computing, until no '+', '-' or '.' is following.
3328 */
3329 for (;;)
3330 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003331 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003332 int getnext;
3333 char_u *p;
3334 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003335 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336 int concat;
3337 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003338 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003339
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003340 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003341 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003342 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003343 p = eval_next_non_blank(*arg, evalarg, &getnext);
3344 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003345 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003346 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3347 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003349 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3350 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003351
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003352 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003353 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003354 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003355 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003356 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003357 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003358 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003359 {
mityu4ac198c2021-05-28 17:52:40 +02003360 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003361 clear_tv(rettv);
3362 return FAIL;
3363 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003364 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003365 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003366 if ((op != '+' || (rettv->v_type != VAR_LIST
3367 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003368 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003369 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003370 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003371 int error = FALSE;
3372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003373 // For "list + ...", an illegal use of the first operand as
3374 // a number cannot be determined before evaluating the 2nd
3375 // operand: if this is also a list, all is ok.
3376 // For "something . ...", "something - ..." or "non-list + ...",
3377 // we know that the first operand needs to be a string or number
3378 // without evaluating the 2nd operand. So check before to avoid
3379 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003380 if (op != '.')
3381 tv_get_number_chk(rettv, &error);
3382 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 {
3384 clear_tv(rettv);
3385 return FAIL;
3386 }
3387 }
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 /*
3390 * Get the second variable.
3391 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003392 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003393 {
mityu89dcb4d2021-05-28 13:50:17 +02003394 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003395 clear_tv(rettv);
3396 return FAIL;
3397 }
3398 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003399 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return FAIL;
3403 }
3404
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003405 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
3407 /*
3408 * Compute the result.
3409 */
3410 if (op == '.')
3411 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3413 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003414 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003415
Bram Moolenaar2e086612020-08-25 22:37:48 +02003416 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003417 || var2.v_type == VAR_CHANNEL
3418 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003419 semsg(_(e_using_invalid_value_as_string_str),
3420 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003421 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003422 {
3423 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3424 var2.vval.v_float);
3425 s2 = buf2;
3426 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003427 else
3428 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003429 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 {
3431 clear_tv(rettv);
3432 clear_tv(&var2);
3433 return FAIL;
3434 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 clear_tv(rettv);
3437 rettv->v_type = VAR_STRING;
3438 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003440 else if (op == '+' && rettv->v_type == VAR_BLOB
3441 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003443 else if (op == '+' && rettv->v_type == VAR_LIST
3444 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003445 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003447 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 else
3450 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003451 int error = FALSE;
3452 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003453 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003454
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003455 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003456 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003457 f1 = rettv->vval.v_float;
3458 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003460 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003461 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003462 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003463 if (error)
3464 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003465 // This can only happen for "list + non-list" or
3466 // "blob + non-blob". For "non-list + ..." or
3467 // "something - ...", we returned before evaluating the
3468 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003469 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003470 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003471 return FAIL;
3472 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003473 if (var2.v_type == VAR_FLOAT)
3474 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003476 if (var2.v_type == VAR_FLOAT)
3477 {
3478 f2 = var2.vval.v_float;
3479 n2 = 0;
3480 }
3481 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003483 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003484 if (error)
3485 {
3486 clear_tv(rettv);
3487 clear_tv(&var2);
3488 return FAIL;
3489 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003490 if (rettv->v_type == VAR_FLOAT)
3491 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003495 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003496 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3497 {
3498 if (op == '+')
3499 f1 = f1 + f2;
3500 else
3501 f1 = f1 - f2;
3502 rettv->v_type = VAR_FLOAT;
3503 rettv->vval.v_float = f1;
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003506 {
3507 if (op == '+')
3508 n1 = n1 + n2;
3509 else
3510 n1 = n1 - n2;
3511 rettv->v_type = VAR_NUMBER;
3512 rettv->vval.v_number = n1;
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
3518 return OK;
3519}
3520
3521/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003522 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 * * number multiplication
3524 * / number division
3525 * % number modulo
3526 *
3527 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003528 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 *
3530 * Return OK or FAIL.
3531 */
3532 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003533eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534 char_u **arg,
3535 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003536 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003537 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003539 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003542 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003544 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return FAIL;
3546
3547 /*
3548 * Repeat computing, until no '*', '/' or '%' is following.
3549 */
3550 for (;;)
3551 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003552 int evaluate;
3553 int getnext;
3554 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003555 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003556 int op;
3557 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003558 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003559 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003560
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003561 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003562 p = eval_next_non_blank(*arg, evalarg, &getnext);
3563 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003564 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003566
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003567 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003568 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003569 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003570 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003571 {
3572 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3573 {
mityu4ac198c2021-05-28 17:52:40 +02003574 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003575 clear_tv(rettv);
3576 return FAIL;
3577 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003578 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003581 f1 = 0;
3582 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003583 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003584 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003586 if (rettv->v_type == VAR_FLOAT)
3587 {
3588 f1 = rettv->vval.v_float;
3589 use_float = TRUE;
3590 n1 = 0;
3591 }
3592 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003593 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003595 if (error)
3596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 else
3599 n1 = 0;
3600
3601 /*
3602 * Get the second variable.
3603 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003604 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3605 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003606 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003607 clear_tv(rettv);
3608 return FAIL;
3609 }
3610 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003611 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 return FAIL;
3613
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003614 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003616 if (var2.v_type == VAR_FLOAT)
3617 {
3618 if (!use_float)
3619 {
3620 f1 = n1;
3621 use_float = TRUE;
3622 }
3623 f2 = var2.vval.v_float;
3624 n2 = 0;
3625 }
3626 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003628 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003629 clear_tv(&var2);
3630 if (error)
3631 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003632 if (use_float)
3633 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 /*
3637 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003638 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003640 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003642 if (op == '*')
3643 f1 = f1 * f2;
3644 else if (op == '/')
3645 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003646#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003648 if (f2 == 0.0)
3649 {
3650 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003652 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003653 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003654 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003655 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003656 }
3657 else
3658 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003659#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003660 // We rely on the floating point library to handle divide
3661 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003662 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003663#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003666 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003667 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003668 return FAIL;
3669 }
3670 rettv->v_type = VAR_FLOAT;
3671 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 else
3674 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003675 int failed = FALSE;
3676
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003677 if (op == '*')
3678 n1 = n1 * n2;
3679 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003680 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003682 n1 = num_modulus(n1, n2, &failed);
3683 if (failed)
3684 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003686 rettv->v_type = VAR_NUMBER;
3687 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
3691
3692 return OK;
3693}
3694
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003695/*
3696 * Handle a type cast before a base level expression.
3697 * "arg" must point to the first non-white of the expression.
3698 * "arg" is advanced to just after the recognized expression.
3699 * Return OK or FAIL.
3700 */
3701 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003702eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003703 char_u **arg,
3704 typval_T *rettv,
3705 evalarg_T *evalarg,
3706 int want_string) // after "." operator
3707{
3708 type_T *want_type = NULL;
3709 garray_T type_list; // list of pointers to allocated types
3710 int res;
3711 int evaluate = evalarg == NULL ? 0
3712 : (evalarg->eval_flags & EVAL_EVALUATE);
3713
3714 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003715 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3716 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003717 {
3718 ++*arg;
3719 ga_init2(&type_list, sizeof(type_T *), 10);
3720 want_type = parse_type(arg, &type_list, TRUE);
3721 if (want_type == NULL && (evaluate || **arg != '>'))
3722 {
3723 clear_type_list(&type_list);
3724 return FAIL;
3725 }
3726
3727 if (**arg != '>')
3728 {
3729 if (*skipwhite(*arg) == '>')
3730 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3731 else
3732 emsg(_(e_missing_gt));
3733 clear_type_list(&type_list);
3734 return FAIL;
3735 }
3736 ++*arg;
3737 *arg = skipwhite_and_linebreak(*arg, evalarg);
3738 }
3739
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003740 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003741
3742 if (want_type != NULL && evaluate)
3743 {
3744 if (res == OK)
3745 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003746 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3747 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003748
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003749 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003750 {
3751 if (want_type == &t_bool && actual != &t_bool
3752 && (actual->tt_flags & TTFLAG_BOOL_OK))
3753 {
3754 int n = tv2bool(rettv);
3755
3756 // can use "0" and "1" for boolean in some places
3757 clear_tv(rettv);
3758 rettv->v_type = VAR_BOOL;
3759 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3760 }
3761 else
3762 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003763 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003764
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003765 where.wt_variable = TRUE;
3766 res = check_type(want_type, actual, TRUE, where);
3767 }
3768 }
3769 }
3770 clear_type_list(&type_list);
3771 }
3772
3773 return res;
3774}
3775
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003776 int
3777eval_leader(char_u **arg, int vim9)
3778{
3779 char_u *s = *arg;
3780 char_u *p = *arg;
3781
3782 while (*p == '!' || *p == '-' || *p == '+')
3783 {
3784 char_u *n = skipwhite(p + 1);
3785
3786 // ++, --, -+ and +- are not accepted in Vim9 script
3787 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3788 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003789 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003790 return FAIL;
3791 }
3792 p = n;
3793 }
3794 *arg = p;
3795 return OK;
3796}
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003799 * Check for a predefined value "true", "false" and "null.*".
3800 * Return OK when recognized.
3801 */
3802 int
3803handle_predefined(char_u *s, int len, typval_T *rettv)
3804{
3805 switch (len)
3806 {
3807 case 4: if (STRNCMP(s, "true", 4) == 0)
3808 {
3809 rettv->v_type = VAR_BOOL;
3810 rettv->vval.v_number = VVAL_TRUE;
3811 return OK;
3812 }
3813 if (STRNCMP(s, "null", 4) == 0)
3814 {
3815 rettv->v_type = VAR_SPECIAL;
3816 rettv->vval.v_number = VVAL_NULL;
3817 return OK;
3818 }
3819 break;
3820 case 5: if (STRNCMP(s, "false", 5) == 0)
3821 {
3822 rettv->v_type = VAR_BOOL;
3823 rettv->vval.v_number = VVAL_FALSE;
3824 return OK;
3825 }
3826 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003827 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3828 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003830 rettv->v_type = VAR_JOB;
3831 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003832#else
3833 rettv->v_type = VAR_SPECIAL;
3834 rettv->vval.v_number = VVAL_NULL;
3835#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003836 return OK;
3837 }
3838 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003839 case 9:
3840 if (STRNCMP(s, "null_", 5) != 0)
3841 break;
3842 if (STRNCMP(s + 5, "list", 4) == 0)
3843 {
3844 rettv->v_type = VAR_LIST;
3845 rettv->vval.v_list = NULL;
3846 return OK;
3847 }
3848 if (STRNCMP(s + 5, "dict", 4) == 0)
3849 {
3850 rettv->v_type = VAR_DICT;
3851 rettv->vval.v_dict = NULL;
3852 return OK;
3853 }
3854 if (STRNCMP(s + 5, "blob", 4) == 0)
3855 {
3856 rettv->v_type = VAR_BLOB;
3857 rettv->vval.v_blob = NULL;
3858 return OK;
3859 }
3860 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003861 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3862 {
3863 rettv->v_type = VAR_CLASS;
3864 rettv->vval.v_class = NULL;
3865 return OK;
3866 }
3867 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003868 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3869 {
3870 rettv->v_type = VAR_STRING;
3871 rettv->vval.v_string = NULL;
3872 return OK;
3873 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003874 if (STRNCMP(s, "null_object", 11) == 0)
3875 {
3876 rettv->v_type = VAR_OBJECT;
3877 rettv->vval.v_object = NULL;
3878 return OK;
3879 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003880 break;
3881 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003882 if (STRNCMP(s, "null_channel", 12) == 0)
3883 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003884#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003885 rettv->v_type = VAR_CHANNEL;
3886 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003887#else
3888 rettv->v_type = VAR_SPECIAL;
3889 rettv->vval.v_number = VVAL_NULL;
3890#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003891 return OK;
3892 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003893 if (STRNCMP(s, "null_partial", 12) == 0)
3894 {
3895 rettv->v_type = VAR_PARTIAL;
3896 rettv->vval.v_partial = NULL;
3897 return OK;
3898 }
3899 break;
3900 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3901 {
3902 rettv->v_type = VAR_FUNC;
3903 rettv->vval.v_string = NULL;
3904 return OK;
3905 }
3906 break;
3907 }
3908 return FAIL;
3909}
3910
3911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * Handle sixth level expression:
3913 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003914 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003915 * "string" string constant
3916 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 * &option-name option value
3918 * @r register contents
3919 * identifier variable value
3920 * function() function call
3921 * $VAR environment variable
3922 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003923 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003925 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003926 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 *
3928 * Also handle:
3929 * ! in front logical NOT
3930 * - in front unary minus
3931 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003932 * trailing [] subscript in String or List
3933 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 *
3936 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003937 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 *
3939 * Return OK or FAIL.
3940 */
3941 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003942eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003943 char_u **arg,
3944 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003945 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003948 int evaluate = evalarg != NULL
3949 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int len;
3951 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003952 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 char_u *start_leader, *end_leader;
3954 int ret = OK;
3955 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003956 static int recurse = 0;
3957 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003966 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003969 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003970 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 end_leader = *arg;
3972
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003973 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003974 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003975 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003976 ++*arg;
3977 return FAIL;
3978 }
3979
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003980 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003981 // and crash. With MSVC the stack is smaller.
3982 if (recurse ==
3983#ifdef _MSC_VER
3984 300
3985#else
3986 1000
3987#endif
3988 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003989 {
3990 semsg(_(e_expression_too_recursive_str), *arg);
3991 return FAIL;
3992 }
3993 ++recurse;
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 switch (**arg)
3996 {
3997 /*
3998 * Number constant.
3999 */
4000 case '0':
4001 case '1':
4002 case '2':
4003 case '3':
4004 case '4':
4005 case '5':
4006 case '6':
4007 case '7':
4008 case '8':
4009 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004010 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004011
4012 // Apply prefixed "-" and "+" now. Matters especially when
4013 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004014 if (ret == OK && evaluate && end_leader > start_leader
4015 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004016 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 break;
4018
4019 /*
4020 * String constant: "string".
4021 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004022 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 break;
4024
4025 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004028 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 break;
4030
4031 /*
4032 * List: [expr, expr]
4033 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004034 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036
4037 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004038 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004039 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004040 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004041 {
4042 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4043 }
4044 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004045 {
4046 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004047 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004048 }
4049 else
4050 ret = NOTDONE;
4051 break;
4052
4053 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004054 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004055 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004056 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004058 ret = NOTDONE;
4059 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004060 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004061 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004062 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 break;
4064
4065 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004066 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004068 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070
4071 /*
4072 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004073 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 */
LemonBoy2eaef102022-05-06 13:14:50 +01004075 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4076 ret = eval_interp_string(arg, rettv, evaluate);
4077 else
4078 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080
4081 /*
4082 * Register contents: @r.
4083 */
4084 case '@': ++*arg;
4085 if (evaluate)
4086 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004087 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004088 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004089 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004090 emsg_invreg(**arg);
4091 else
4092 {
4093 rettv->v_type = VAR_STRING;
4094 rettv->vval.v_string = get_reg_contents(**arg,
4095 GREG_EXPR_SRC);
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 if (**arg != NUL)
4099 ++*arg;
4100 break;
4101
4102 /*
4103 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004104 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004106 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004107 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004108 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004109 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004110 if (ret == OK && evaluate)
4111 {
4112 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4113
Bram Moolenaara9931532021-06-12 15:58:16 +02004114 // Compile it here to get the return type. The return
4115 // type is optional, when it's missing use t_unknown.
4116 // This is recognized in compile_return().
4117 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4118 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004119 if (compile_def_function(ufunc, FALSE,
4120 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004121 {
4122 clear_tv(rettv);
4123 ret = FAIL;
4124 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004125 }
4126 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004127 if (ret == NOTDONE)
4128 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004129 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004130 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004131
Bram Moolenaar9215f012020-06-27 21:18:00 +02004132 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004133 if (**arg == ')')
4134 ++*arg;
4135 else if (ret == OK)
4136 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004137 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004138 clear_tv(rettv);
4139 ret = FAIL;
4140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 break;
4143
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 break;
4146 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004147
4148 if (ret == NOTDONE)
4149 {
4150 /*
4151 * Must be a variable or function name.
4152 * Can also be a curly-braces kind of name: {expr}.
4153 */
4154 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004155 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 if (alias != NULL)
4157 s = alias;
4158
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004159 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 ret = FAIL;
4161 else
4162 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004163 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4164
Bram Moolenaar4525a572022-02-13 11:57:33 +00004165 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004166 {
4167 emsg(_(e_cannot_use_underscore_here));
4168 ret = FAIL;
4169 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004170 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004171 && s[0] == 's' && s[1] == ':')
4172 {
4173 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4174 ret = FAIL;
4175 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004176 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004177 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004178 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004179 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004180 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004181 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004182 else if (flags & EVAL_CONSTANT)
4183 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004184 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004185 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004186 // get the value of "true", "false", etc. or a variable
4187 ret = FAIL;
4188 if (vim9script)
4189 ret = handle_predefined(s, len, rettv);
4190 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004191 {
4192 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004193 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004194 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004195 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004196 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004197 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004198 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004199 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004200 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004201 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004203 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004204 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004205 }
4206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004207 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4208 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004209 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004210 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 /*
4213 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4214 */
4215 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004216 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004217
4218 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004219 return ret;
4220}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004222/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004223 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004224 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004225 * Adjusts "end_leaderp" until it is at "start_leader".
4226 */
4227 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004228eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004229 typval_T *rettv,
4230 int numeric_only,
4231 char_u *start_leader,
4232 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004233{
4234 char_u *end_leader = *end_leaderp;
4235 int ret = OK;
4236 int error = FALSE;
4237 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004238 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004239 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004240 float_T f = 0.0;
4241
4242 if (rettv->v_type == VAR_FLOAT)
4243 f = rettv->vval.v_float;
4244 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004245 {
4246 while (VIM_ISWHITE(end_leader[-1]))
4247 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004248 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004249 val = tv2bool(rettv);
4250 else
4251 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004252 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004253 if (error)
4254 {
4255 clear_tv(rettv);
4256 ret = FAIL;
4257 }
4258 else
4259 {
4260 while (end_leader > start_leader)
4261 {
4262 --end_leader;
4263 if (*end_leader == '!')
4264 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004265 if (numeric_only)
4266 {
4267 ++end_leader;
4268 break;
4269 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004270 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004271 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004272 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004273 {
4274 rettv->v_type = VAR_BOOL;
4275 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4276 }
4277 else
4278 f = !f;
4279 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004280 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004281 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004282 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004283 type = VAR_BOOL;
4284 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004285 }
4286 else if (*end_leader == '-')
4287 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004288 if (rettv->v_type == VAR_FLOAT)
4289 f = -f;
4290 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004291 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004292 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004293 type = VAR_NUMBER;
4294 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004295 }
4296 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004297 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004300 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 else
4303 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004304 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004305 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004306 rettv->v_type = type;
4307 else
4308 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004309 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004312 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return ret;
4314}
4315
4316/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004317 * Call the function referred to in "rettv".
4318 */
4319 static int
4320call_func_rettv(
4321 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004322 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004323 typval_T *rettv,
4324 int evaluate,
4325 dict_T *selfdict,
4326 typval_T *basetv)
4327{
4328 partial_T *pt = NULL;
4329 funcexe_T funcexe;
4330 typval_T functv;
4331 char_u *s;
4332 int ret;
4333
4334 // need to copy the funcref so that we can clear rettv
4335 if (evaluate)
4336 {
4337 functv = *rettv;
4338 rettv->v_type = VAR_UNKNOWN;
4339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004341 if (functv.v_type == VAR_PARTIAL)
4342 {
4343 pt = functv.vval.v_partial;
4344 s = partial_name(pt);
4345 }
4346 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004347 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004348 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004349 if (s == NULL || *s == NUL)
4350 {
4351 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004352 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004353 goto theend;
4354 }
4355 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004356 }
4357 else
4358 s = (char_u *)"";
4359
Bram Moolenaara80faa82020-04-12 19:37:17 +02004360 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004361 funcexe.fe_firstline = curwin->w_cursor.lnum;
4362 funcexe.fe_lastline = curwin->w_cursor.lnum;
4363 funcexe.fe_evaluate = evaluate;
4364 funcexe.fe_partial = pt;
4365 funcexe.fe_selfdict = selfdict;
4366 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004367 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004368
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004369theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004370 // Clear the funcref afterwards, so that deleting it while
4371 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004372 if (evaluate)
4373 clear_tv(&functv);
4374
4375 return ret;
4376}
4377
4378/*
4379 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004380 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004381 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4382 */
4383 static int
4384eval_lambda(
4385 char_u **arg,
4386 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004387 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004388 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004389{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004390 int evaluate = evalarg != NULL
4391 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004392 typval_T base = *rettv;
4393 int ret;
4394
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004395 rettv->v_type = VAR_UNKNOWN;
4396
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004397 if (**arg == '{')
4398 {
4399 // ->{lambda}()
4400 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4401 }
4402 else
4403 {
4404 // ->(lambda)()
4405 ++*arg;
4406 ret = eval1(arg, rettv, evalarg);
4407 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004408 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004409 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004410 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004411 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004412 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004413 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4414 && rettv->v_type != VAR_PARTIAL)
4415 {
4416 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4417 return FAIL;
4418 }
4419 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004420 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004421 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004422 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004423
4424 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004425 {
4426 if (verbose)
4427 {
4428 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004429 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004430 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004431 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004432 }
4433 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004434 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004435 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004436 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004437 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004438
4439 // Clear the funcref afterwards, so that deleting it while
4440 // evaluating the arguments is possible (see test55).
4441 if (evaluate)
4442 clear_tv(&base);
4443
4444 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004445}
4446
4447/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004448 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004449 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004450 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4451 */
4452 static int
4453eval_method(
4454 char_u **arg,
4455 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004456 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004458{
4459 char_u *name;
4460 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004461 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004462 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004463 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004464 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004465 int evaluate = evalarg != NULL
4466 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004467
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004468 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004469
Bram Moolenaarac92e252019-08-03 21:58:38 +02004470 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004471 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004472 if (alias != NULL)
4473 name = alias;
4474
4475 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004476 {
4477 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004478 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004479 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004480 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004481 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004482 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004483 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004484
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004485 // If there is no "(" immediately following, but there is further on,
4486 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4487 // Does not handle anything where "(" is part of the expression.
4488 *arg = skipwhite(*arg);
4489
4490 if (**arg != '(' && alias == NULL
4491 && (paren = vim_strchr(*arg, '(')) != NULL)
4492 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004493 char_u *deref;
4494
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004495 *arg = name;
4496 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004497 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4498 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004499 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004500 *arg = name + len;
4501 ret = FAIL;
4502 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004503 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004504 {
4505 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004506 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004507 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004508 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004509 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004510
4511 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004512 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004513 *arg = skipwhite(*arg);
4514
4515 if (**arg != '(')
4516 {
4517 if (verbose)
4518 semsg(_(e_missing_parenthesis_str), name);
4519 ret = FAIL;
4520 }
4521 else if (VIM_ISWHITE((*arg)[-1]))
4522 {
4523 if (verbose)
4524 emsg(_(e_no_white_space_allowed_before_parenthesis));
4525 ret = FAIL;
4526 }
4527 else
4528 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004529 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004530 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004531 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004532
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004533 // Clear the funcref afterwards, so that deleting it while
4534 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004535 if (evaluate)
4536 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004537 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004538
Bram Moolenaarac92e252019-08-03 21:58:38 +02004539 return ret;
4540}
4541
4542/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004543 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4544 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4546 */
4547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004548eval_index(
4549 char_u **arg,
4550 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004551 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004554 int evaluate = evalarg != NULL
4555 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004557 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004560 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004561 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004563 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4564 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004566 init_tv(&var1);
4567 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 /*
4571 * dict.name
4572 */
4573 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004576 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004577 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004578 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 /*
4583 * something[idx]
4584 *
4585 * Get the (first) variable from inside the [].
4586 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004587 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 if (**arg == ':')
4589 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004590 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004592 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004593 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004594 semsg(_(e_white_space_required_before_and_after_str_at_str),
4595 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004596 clear_tv(&var1);
4597 return FAIL;
4598 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004599 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004601 int error = FALSE;
4602
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004603 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004604 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004605 && var1.v_type == VAR_FLOAT)
4606 {
4607 var1.vval.v_string = typval_tostring(&var1, TRUE);
4608 var1.v_type = VAR_STRING;
4609 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004610
Bram Moolenaar4525a572022-02-13 11:57:33 +00004611 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004612 tv_get_number_chk(&var1, &error);
4613 else
4614 error = tv_get_string_chk(&var1) == NULL;
4615 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004616 {
4617 // not a number or string
4618 clear_tv(&var1);
4619 return FAIL;
4620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622
4623 /*
4624 * Get the second variable from inside the [:].
4625 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004626 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 if (**arg == ':')
4628 {
4629 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004630 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004631 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004632 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004633 semsg(_(e_white_space_required_before_and_after_str_at_str),
4634 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004635 if (!empty1)
4636 clear_tv(&var1);
4637 return FAIL;
4638 }
4639 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 if (**arg == ']')
4641 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004642 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 if (!empty1)
4645 clear_tv(&var1);
4646 return FAIL;
4647 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004648 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 if (!empty1)
4652 clear_tv(&var1);
4653 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 return FAIL;
4655 }
4656 }
4657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004659 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 if (**arg != ']')
4661 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004662 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004663 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 clear_tv(&var1);
4665 if (range)
4666 clear_tv(&var2);
4667 return FAIL;
4668 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004669 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 }
4671
4672 if (evaluate)
4673 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004675 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004677
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 clear_tv(&var2);
4682 return res;
4683 }
4684 return OK;
4685}
4686
4687/*
4688 * Check if "rettv" can have an [index] or [sli:ce]
4689 */
4690 int
4691check_can_index(typval_T *rettv, int evaluate, int verbose)
4692{
4693 switch (rettv->v_type)
4694 {
4695 case VAR_FUNC:
4696 case VAR_PARTIAL:
4697 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004698 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004699 return FAIL;
4700 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004701 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004702 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004703 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004704 case VAR_BOOL:
4705 case VAR_SPECIAL:
4706 case VAR_JOB:
4707 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004708 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004709 case VAR_CLASS:
4710 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004711 if (verbose)
4712 emsg(_(e_cannot_index_special_variable));
4713 return FAIL;
4714 case VAR_UNKNOWN:
4715 case VAR_ANY:
4716 case VAR_VOID:
4717 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004719 emsg(_(e_cannot_index_special_variable));
4720 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004722 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004724 case VAR_STRING:
4725 case VAR_LIST:
4726 case VAR_DICT:
4727 case VAR_BLOB:
4728 break;
4729 case VAR_NUMBER:
4730 if (in_vim9script())
4731 emsg(_(e_cannot_index_number));
4732 break;
4733 }
4734 return OK;
4735}
4736
4737/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004738 * slice() function
4739 */
4740 void
4741f_slice(typval_T *argvars, typval_T *rettv)
4742{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004743 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004744 && ((argvars[0].v_type != VAR_STRING
4745 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004746 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004747 && check_for_list_arg(argvars, 0) == FAIL)
4748 || check_for_number_arg(argvars, 1) == FAIL
4749 || check_for_opt_number_arg(argvars, 2) == FAIL))
4750 return;
4751
Bram Moolenaar6601b622021-01-13 21:47:15 +01004752 if (check_can_index(argvars, TRUE, FALSE) == OK)
4753 {
4754 copy_tv(argvars, rettv);
4755 eval_index_inner(rettv, TRUE, argvars + 1,
4756 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4757 TRUE, NULL, 0, FALSE);
4758 }
4759}
4760
4761/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004762 * Apply index or range to "rettv".
4763 * "var1" is the first index, NULL for [:expr].
4764 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004765 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4766 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004767 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4768 */
4769 int
4770eval_index_inner(
4771 typval_T *rettv,
4772 int is_range,
4773 typval_T *var1,
4774 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004775 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 char_u *key,
4777 int keylen,
4778 int verbose)
4779{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004780 varnumber_T n1, n2 = 0;
4781 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004782
4783 n1 = 0;
4784 if (var1 != NULL && rettv->v_type != VAR_DICT)
4785 n1 = tv_get_number(var1);
4786
4787 if (is_range)
4788 {
4789 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 if (verbose)
4792 emsg(_(e_cannot_slice_dictionary));
4793 return FAIL;
4794 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004795 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004796 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004797 else
4798 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004800
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801 switch (rettv->v_type)
4802 {
4803 case VAR_UNKNOWN:
4804 case VAR_ANY:
4805 case VAR_VOID:
4806 case VAR_FUNC:
4807 case VAR_PARTIAL:
4808 case VAR_FLOAT:
4809 case VAR_BOOL:
4810 case VAR_SPECIAL:
4811 case VAR_JOB:
4812 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004813 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004814 case VAR_CLASS:
4815 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004816 break; // not evaluating, skipping over subscript
4817
4818 case VAR_NUMBER:
4819 case VAR_STRING:
4820 {
4821 char_u *s = tv_get_string(rettv);
4822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004824 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004825 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004826 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004827 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004828 else
4829 s = char_from_string(s, n1);
4830 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004831 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // The resulting variable is a substring. If the indexes
4834 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 if (n1 < 0)
4836 {
4837 n1 = len + n1;
4838 if (n1 < 0)
4839 n1 = 0;
4840 }
4841 if (n2 < 0)
4842 n2 = len + n2;
4843 else if (n2 >= len)
4844 n2 = len;
4845 if (n1 >= len || n2 < 0 || n1 > n2)
4846 s = NULL;
4847 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004848 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 }
4850 else
4851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // The resulting variable is a string of a single
4853 // character. If the index is too big or negative the
4854 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 if (n1 >= len || n1 < 0)
4856 s = NULL;
4857 else
4858 s = vim_strnsave(s + n1, 1);
4859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(rettv);
4861 rettv->v_type = VAR_STRING;
4862 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004863 }
4864 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004866 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004867 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4868 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004869 break;
4870
4871 case VAR_LIST:
4872 if (var1 == NULL)
4873 n1 = 0;
4874 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004875 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004876 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004877 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878 return FAIL;
4879 break;
4880
4881 case VAR_DICT:
4882 {
4883 dictitem_T *item;
4884 typval_T tmp;
4885
4886 if (key == NULL)
4887 {
4888 key = tv_get_string_chk(var1);
4889 if (key == NULL)
4890 return FAIL;
4891 }
4892
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004893 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004894
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004895 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004896 {
4897 if (verbose)
4898 {
4899 if (keylen > 0)
4900 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004901 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004902 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004903 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004904 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004905
4906 copy_tv(&item->di_tv, &tmp);
4907 clear_tv(rettv);
4908 *rettv = tmp;
4909 }
4910 break;
4911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 return OK;
4913}
4914
4915/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004916 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004917 */
4918 char_u *
4919partial_name(partial_T *pt)
4920{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004921 if (pt != NULL)
4922 {
4923 if (pt->pt_name != NULL)
4924 return pt->pt_name;
4925 if (pt->pt_func != NULL)
4926 return pt->pt_func->uf_name;
4927 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004928 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004929}
4930
Bram Moolenaarddecc252016-04-06 22:59:37 +02004931 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004932partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004933{
4934 int i;
4935
4936 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004937 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004938 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004939 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004940 if (pt->pt_name != NULL)
4941 {
4942 func_unref(pt->pt_name);
4943 vim_free(pt->pt_name);
4944 }
4945 else
4946 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004947
Bram Moolenaar54656012021-06-09 20:50:46 +02004948 // "out_up" is no longer used, decrement refcount on partial that owns it.
4949 partial_unref(pt->pt_outer.out_up_partial);
4950
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004951 // Using pt_outer from another partial.
4952 partial_unref(pt->pt_outer_partial);
4953
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004954 // Decrease the reference count for the context of a closure. If down
4955 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004956 if (pt->pt_funcstack != NULL)
4957 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004958 --pt->pt_funcstack->fs_refcount;
4959 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004960 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004961 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004962 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4963 if (pt->pt_loopvars[i] != NULL)
4964 {
4965 --pt->pt_loopvars[i]->lvs_refcount;
4966 loopvars_check_refcount(pt->pt_loopvars[i]);
4967 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004968
Bram Moolenaarddecc252016-04-06 22:59:37 +02004969 vim_free(pt);
4970}
4971
4972/*
4973 * Unreference a closure: decrement the reference count and free it when it
4974 * becomes zero.
4975 */
4976 void
4977partial_unref(partial_T *pt)
4978{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004979 if (pt != NULL)
4980 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004981 int done = FALSE;
4982
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004983 if (--pt->pt_refcount <= 0)
4984 partial_free(pt);
4985
4986 // If the reference count goes down to one, the funcstack may be the
4987 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004988 else if (pt->pt_refcount == 1)
4989 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004990 // careful: if the funcstack is freed it may contain this partial
4991 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004992 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004993 done = funcstack_check_refcount(pt->pt_funcstack);
4994
Bram Moolenaarcc341812022-09-19 15:54:34 +01004995 if (!done)
4996 {
4997 int depth;
4998
4999 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5000 if (pt->pt_loopvars[depth] != NULL
5001 && loopvars_check_refcount(pt->pt_loopvars[depth]))
5002 break;
5003 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005004 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005005 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005006}
5007
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005008/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005009 * Return the next (unique) copy ID.
5010 * Used for serializing nested structures.
5011 */
5012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005013get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005014{
5015 current_copyID += COPYID_INC;
5016 return current_copyID;
5017}
5018
5019/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005020 * Garbage collection for lists and dictionaries.
5021 *
5022 * We use reference counts to be able to free most items right away when they
5023 * are no longer used. But for composite items it's possible that it becomes
5024 * unused while the reference count is > 0: When there is a recursive
5025 * reference. Example:
5026 * :let l = [1, 2, 3]
5027 * :let d = {9: l}
5028 * :let l[1] = d
5029 *
5030 * Since this is quite unusual we handle this with garbage collection: every
5031 * once in a while find out which lists and dicts are not referenced from any
5032 * variable.
5033 *
5034 * Here is a good reference text about garbage collection (refers to Python
5035 * but it applies to all reference-counting mechanisms):
5036 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005037 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005038
5039/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005040 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005041 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005042 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005043 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005044 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005045garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005046{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005047 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005048 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005049 buf_T *buf;
5050 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005051 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005053
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005054 if (!testing)
5055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005057 want_garbage_collect = FALSE;
5058 may_garbage_collect = FALSE;
5059 garbage_collect_at_exit = FALSE;
5060 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005061
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005062 // The execution stack can grow big, limit the size.
5063 if (exestack.ga_maxlen - exestack.ga_len > 500)
5064 {
5065 size_t new_len;
5066 char_u *pp;
5067 int n;
5068
5069 // Keep 150% of the current size, with a minimum of the growth size.
5070 n = exestack.ga_len / 2;
5071 if (n < exestack.ga_growsize)
5072 n = exestack.ga_growsize;
5073
5074 // Don't make it bigger though.
5075 if (exestack.ga_len + n < exestack.ga_maxlen)
5076 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005077 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005078 pp = vim_realloc(exestack.ga_data, new_len);
5079 if (pp == NULL)
5080 return FAIL;
5081 exestack.ga_maxlen = exestack.ga_len + n;
5082 exestack.ga_data = pp;
5083 }
5084 }
5085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005086 // We advance by two because we add one for items referenced through
5087 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005088 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005089
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090 /*
5091 * 1. Go through all accessible variables and mark all lists and dicts
5092 * with copyID.
5093 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // Don't free variables in the previous_funccal list unless they are only
5096 // referenced through previous_funccal. This must be first, because if
5097 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005101 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005104 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5106 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005109 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005110 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5111 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005112 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005113 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005114 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005115 abort = abort || set_ref_in_item(
5116 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005117#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005118 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005119 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5120 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005121 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005122 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005123 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5124 NULL, NULL);
5125#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005128 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005129 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5130 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005132 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005138 abort = abort || set_ref_in_functions(copyID);
5139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005140 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005142
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005143 // funcstacks keep variables for closures
5144 abort = abort || set_ref_in_funcstacks(copyID);
5145
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005146 // loopvars keep variables for loop blocks
5147 abort = abort || set_ref_in_loopvars(copyID);
5148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005150 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005151
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005152 // callbacks in buffers
5153 abort = abort || set_ref_in_buffers(copyID);
5154
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005155 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5156 abort = abort || set_ref_in_insexpand_funcs(copyID);
5157
5158 // 'operatorfunc' callback
5159 abort = abort || set_ref_in_opfunc(copyID);
5160
5161 // 'tagfunc' callback
5162 abort = abort || set_ref_in_tagfunc(copyID);
5163
5164 // 'imactivatefunc' and 'imstatusfunc' callbacks
5165 abort = abort || set_ref_in_im_funcs(copyID);
5166
Bram Moolenaar1dced572012-04-05 16:54:08 +02005167#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005169#endif
5170
Bram Moolenaardb913952012-06-29 12:54:53 +02005171#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005172 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005173#endif
5174
5175#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005177#endif
5178
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005179#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005180 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005181 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005182#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005183#ifdef FEAT_NETBEANS_INTG
5184 abort = abort || set_ref_in_nb_channel(copyID);
5185#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005186
Bram Moolenaare3188e22016-05-31 21:13:04 +02005187#ifdef FEAT_TIMERS
5188 abort = abort || set_ref_in_timer(copyID);
5189#endif
5190
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005191#ifdef FEAT_QUICKFIX
5192 abort = abort || set_ref_in_quickfix(copyID);
5193#endif
5194
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005195#ifdef FEAT_TERMINAL
5196 abort = abort || set_ref_in_term(copyID);
5197#endif
5198
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005199#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005200 abort = abort || set_ref_in_popups(copyID);
5201#endif
5202
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005203 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005204 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005205 /*
5206 * 2. Free lists and dictionaries that are not referenced.
5207 */
5208 did_free = free_unref_items(copyID);
5209
5210 /*
5211 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005212 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005213 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005215 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005216 else if (p_verbose > 0)
5217 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005218 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005220
5221 return did_free;
5222}
5223
5224/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005225 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005226 */
5227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005228free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005229{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005230 int did_free = FALSE;
5231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Let all "free" functions know that we are here. This means no
5233 // dictionaries, lists, channels or jobs are to be freed, because we will
5234 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005235 in_free_unref_items = TRUE;
5236
5237 /*
5238 * PASS 1: free the contents of the items. We don't free the items
5239 * themselves yet, so that it is possible to decrement refcount counters
5240 */
5241
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005242 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005243 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005245 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005246 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005247
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005248 // Go through the list of objects and free items without this copyID.
5249 did_free |= object_free_nonref(copyID);
5250
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // Go through the list of jobs and free items without the copyID. This
5253 // must happen before doing channels, because jobs refer to channels, but
5254 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005255 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5259#endif
5260
5261 /*
5262 * PASS 2: free the items themselves.
5263 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005264 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005265 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005266
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005267#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // Go through the list of jobs and free items without the copyID. This
5269 // must happen before doing channels, because jobs refer to channels, but
5270 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005271 free_unused_jobs(copyID, COPYID_MASK);
5272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005274 free_unused_channels(copyID, COPYID_MASK);
5275#endif
5276
5277 in_free_unref_items = FALSE;
5278
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005279 return did_free;
5280}
5281
5282/*
5283 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 * "list_stack" is used to add lists to be marked. Can be NULL.
5285 *
5286 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005289set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005290{
5291 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005292 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 hashtab_T *cur_ht;
5295 ht_stack_T *ht_stack = NULL;
5296 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005297
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 cur_ht = ht;
5299 for (;;)
5300 {
5301 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005302 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 // Mark each item in the hashtab. If the item contains a hashtab
5304 // it is added to ht_stack, if it contains a list it is added to
5305 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005306 todo = (int)cur_ht->ht_used;
5307 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5308 if (!HASHITEM_EMPTY(hi))
5309 {
5310 --todo;
5311 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5312 &ht_stack, list_stack);
5313 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005315
5316 if (ht_stack == NULL)
5317 break;
5318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005320 cur_ht = ht_stack->ht;
5321 tempitem = ht_stack;
5322 ht_stack = ht_stack->prev;
5323 free(tempitem);
5324 }
5325
5326 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327}
5328
Dominique Pelle748b3082022-01-08 12:41:16 +00005329#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5330 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005331/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005332 * Mark a dict and its items with "copyID".
5333 * Returns TRUE if setting references failed somehow.
5334 */
5335 int
5336set_ref_in_dict(dict_T *d, int copyID)
5337{
5338 if (d != NULL && d->dv_copyID != copyID)
5339 {
5340 d->dv_copyID = copyID;
5341 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5342 }
5343 return FALSE;
5344}
Dominique Pelle748b3082022-01-08 12:41:16 +00005345#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005346
5347/*
5348 * Mark a list and its items with "copyID".
5349 * Returns TRUE if setting references failed somehow.
5350 */
5351 int
5352set_ref_in_list(list_T *ll, int copyID)
5353{
5354 if (ll != NULL && ll->lv_copyID != copyID)
5355 {
5356 ll->lv_copyID = copyID;
5357 return set_ref_in_list_items(ll, copyID, NULL);
5358 }
5359 return FALSE;
5360}
5361
5362/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005364 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5365 *
5366 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005367 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005368 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005369set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005370{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005371 listitem_T *li;
5372 int abort = FALSE;
5373 list_T *cur_l;
5374 list_stack_T *list_stack = NULL;
5375 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005377 cur_l = l;
5378 for (;;)
5379 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005380 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 // Mark each item in the list. If the item contains a hashtab
5382 // it is added to ht_stack, if it contains a list it is added to
5383 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005384 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5385 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5386 ht_stack, &list_stack);
5387 if (list_stack == NULL)
5388 break;
5389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 cur_l = list_stack->list;
5392 tempitem = list_stack;
5393 list_stack = list_stack->prev;
5394 free(tempitem);
5395 }
5396
5397 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398}
5399
5400/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005401 * Mark the partial in callback 'cb' with "copyID".
5402 */
5403 int
5404set_ref_in_callback(callback_T *cb, int copyID)
5405{
5406 typval_T tv;
5407
5408 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5409 return FALSE;
5410
5411 tv.v_type = VAR_PARTIAL;
5412 tv.vval.v_partial = cb->cb_partial;
5413 return set_ref_in_item(&tv, copyID, NULL, NULL);
5414}
5415
5416/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005417 * Mark all lists, dicts and other container types referenced through typval
5418 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 * "list_stack" is used to add lists to be marked. Can be NULL.
5420 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5421 *
5422 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005425set_ref_in_item(
5426 typval_T *tv,
5427 int copyID,
5428 ht_stack_T **ht_stack,
5429 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005431 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005432
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005433 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005434 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005435 case VAR_DICT:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005436 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005437 dict_T *dd = tv->vval.v_dict;
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005438
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005439 if (dd != NULL && dd->dv_copyID != copyID)
5440 {
5441 // Didn't see this dict yet.
5442 dd->dv_copyID = copyID;
5443 if (ht_stack == NULL)
5444 {
5445 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5446 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005447 else
5448 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005449 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005450
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005451 if (newitem == NULL)
5452 abort = TRUE;
5453 else
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005454 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005455 newitem->ht = &dd->dv_hashtab;
5456 newitem->prev = *ht_stack;
5457 *ht_stack = newitem;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458 }
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005459 }
5460 }
5461 break;
5462 }
5463
5464 case VAR_LIST:
5465 {
5466 list_T *ll = tv->vval.v_list;
5467
5468 if (ll != NULL && ll->lv_copyID != copyID)
5469 {
5470 // Didn't see this list yet.
5471 ll->lv_copyID = copyID;
5472 if (list_stack == NULL)
5473 {
5474 abort = set_ref_in_list_items(ll, copyID, ht_stack);
5475 }
5476 else
5477 {
5478 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5479
5480 if (newitem == NULL)
5481 abort = TRUE;
5482 else
5483 {
5484 newitem->list = ll;
5485 newitem->prev = *list_stack;
5486 *list_stack = newitem;
5487 }
5488 }
5489 }
5490 break;
5491 }
5492
5493 case VAR_FUNC:
5494 {
5495 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5496 break;
5497 }
5498
5499 case VAR_PARTIAL:
5500 {
5501 partial_T *pt = tv->vval.v_partial;
5502 int i;
5503
5504 if (pt != NULL && pt->pt_copyID != copyID)
5505 {
5506 // Didn't see this partial yet.
5507 pt->pt_copyID = copyID;
5508
5509 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5510
5511 if (pt->pt_dict != NULL)
5512 {
5513 typval_T dtv;
5514
5515 dtv.v_type = VAR_DICT;
5516 dtv.vval.v_dict = pt->pt_dict;
5517 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5518 }
5519
5520 for (i = 0; i < pt->pt_argc; ++i)
5521 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5522 ht_stack, list_stack);
5523 // pt_funcstack is handled in set_ref_in_funcstacks()
5524 // pt_loopvars is handled in set_ref_in_loopvars()
5525 }
5526 break;
5527 }
5528
5529 case VAR_JOB:
5530 {
5531#ifdef FEAT_JOB_CHANNEL
5532 job_T *job = tv->vval.v_job;
5533 typval_T dtv;
5534
5535 if (job != NULL && job->jv_copyID != copyID)
5536 {
5537 job->jv_copyID = copyID;
5538 if (job->jv_channel != NULL)
5539 {
5540 dtv.v_type = VAR_CHANNEL;
5541 dtv.vval.v_channel = job->jv_channel;
5542 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5543 }
5544 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005545 {
5546 dtv.v_type = VAR_PARTIAL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005547 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005548 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5549 }
5550 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005551#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005552 break;
5553 }
5554
5555 case VAR_CHANNEL:
5556 {
5557#ifdef FEAT_JOB_CHANNEL
5558 channel_T *ch = tv->vval.v_channel;
5559 ch_part_T part;
5560 typval_T dtv;
5561 jsonq_T *jq;
5562 cbq_T *cq;
5563
5564 if (ch != NULL && ch->ch_copyID != copyID)
5565 {
5566 ch->ch_copyID = copyID;
5567 for (part = PART_SOCK; part < PART_COUNT; ++part)
5568 {
5569 for (jq = ch->ch_part[part].ch_json_head.jq_next;
5570 jq != NULL; jq = jq->jq_next)
5571 set_ref_in_item(jq->jq_value, copyID,
5572 ht_stack, list_stack);
5573 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5574 cq = cq->cq_next)
5575 if (cq->cq_callback.cb_partial != NULL)
5576 {
5577 dtv.v_type = VAR_PARTIAL;
5578 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5579 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5580 }
5581 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5582 {
5583 dtv.v_type = VAR_PARTIAL;
5584 dtv.vval.v_partial =
5585 ch->ch_part[part].ch_callback.cb_partial;
5586 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5587 }
5588 }
5589 if (ch->ch_callback.cb_partial != NULL)
5590 {
5591 dtv.v_type = VAR_PARTIAL;
5592 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5593 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5594 }
5595 if (ch->ch_close_cb.cb_partial != NULL)
5596 {
5597 dtv.v_type = VAR_PARTIAL;
5598 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5599 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5600 }
5601 }
5602#endif
5603 break;
5604 }
5605
5606 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005607 // TODO: Mark methods in class_obj_methods ?
5608 // Mark initializer expressions?
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005609 break;
5610
5611 case VAR_OBJECT:
5612 {
5613 object_T *obj = tv->vval.v_object;
5614 if (obj != NULL && obj->obj_copyID != copyID)
5615 {
5616 obj->obj_copyID = copyID;
5617
5618 // The typval_T array is right after the object_T.
5619 typval_T *mtv = (typval_T *)(obj + 1);
5620 for (int i = 0; !abort
5621 && i < obj->obj_class->class_obj_member_count; ++i)
5622 abort = abort || set_ref_in_item(mtv + i, copyID,
5623 ht_stack, list_stack);
5624 }
5625 break;
5626 }
5627
5628 case VAR_UNKNOWN:
5629 case VAR_ANY:
5630 case VAR_VOID:
5631 case VAR_BOOL:
5632 case VAR_SPECIAL:
5633 case VAR_NUMBER:
5634 case VAR_FLOAT:
5635 case VAR_STRING:
5636 case VAR_BLOB:
5637 case VAR_INSTR:
5638 // Types that do not contain any other item
5639 break;
5640 }
5641
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005643}
5644
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 * Return a string with the string representation of a variable.
5647 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005648 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005649 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005650 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005651 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005652 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005653 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5654 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005655 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005657 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005658echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005659 typval_T *tv,
5660 char_u **tofree,
5661 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005662 int copyID,
5663 int echo_style,
5664 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005665 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005667 static int recurse = 0;
5668 char_u *r = NULL;
5669
Bram Moolenaar33570922005-01-25 22:26:29 +00005670 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005671 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005672 if (!did_echo_string_emsg)
5673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // Only give this message once for a recursive call to avoid
5675 // flooding the user with errors. And stop iterating over lists
5676 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005677 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005678 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005680 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005681 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005682 }
5683 ++recurse;
5684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 switch (tv->v_type)
5686 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005688 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005689 {
5690 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005691 r = tv->vval.v_string;
5692 if (r == NULL)
5693 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005694 }
5695 else
5696 {
5697 *tofree = string_quote(tv->vval.v_string, FALSE);
5698 r = *tofree;
5699 }
5700 break;
5701
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005703 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005704 char_u buf[MAX_FUNC_NAME_LEN];
5705
5706 if (echo_style)
5707 {
LemonBoya5d35902022-04-29 21:15:02 +01005708 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5709 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005710 buf, MAX_FUNC_NAME_LEN);
5711 if (r == buf)
5712 {
5713 r = vim_strsave(buf);
5714 *tofree = r;
5715 }
5716 else
5717 *tofree = NULL;
5718 }
5719 else
5720 {
5721 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5722 : make_ufunc_name_readable(
5723 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5724 TRUE);
5725 r = *tofree;
5726 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005728 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005729
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005730 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005731 {
5732 partial_T *pt = tv->vval.v_partial;
5733 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005734 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005735 garray_T ga;
5736 int i;
5737 char_u *tf;
5738
5739 ga_init2(&ga, 1, 100);
5740 ga_concat(&ga, (char_u *)"function(");
5741 if (fname != NULL)
5742 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005743 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005744 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005745 && vim_isupper(fname[1]))
5746 {
5747 ga_concat(&ga, (char_u *)"'g:");
5748 ga_concat(&ga, fname + 1);
5749 }
5750 else
5751 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005752 vim_free(fname);
5753 }
5754 if (pt != NULL && pt->pt_argc > 0)
5755 {
5756 ga_concat(&ga, (char_u *)", [");
5757 for (i = 0; i < pt->pt_argc; ++i)
5758 {
5759 if (i > 0)
5760 ga_concat(&ga, (char_u *)", ");
5761 ga_concat(&ga,
5762 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5763 vim_free(tf);
5764 }
5765 ga_concat(&ga, (char_u *)"]");
5766 }
5767 if (pt != NULL && pt->pt_dict != NULL)
5768 {
5769 typval_T dtv;
5770
5771 ga_concat(&ga, (char_u *)", ");
5772 dtv.v_type = VAR_DICT;
5773 dtv.vval.v_dict = pt->pt_dict;
5774 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5775 vim_free(tf);
5776 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005777 // terminate with ')' and a NUL
5778 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005779
5780 *tofree = ga.ga_data;
5781 r = *tofree;
5782 break;
5783 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005784
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005785 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005786 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005787 break;
5788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005790 if (tv->vval.v_list == NULL)
5791 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005792 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005793 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005794 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005795 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005796 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5797 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798 {
5799 *tofree = NULL;
5800 r = (char_u *)"[...]";
5801 }
5802 else
5803 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005804 int old_copyID = tv->vval.v_list->lv_copyID;
5805
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005806 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005807 *tofree = list2string(tv, copyID, restore_copyID);
5808 if (restore_copyID)
5809 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005810 r = *tofree;
5811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005815 if (tv->vval.v_dict == NULL)
5816 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005817 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005818 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005819 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005820 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005821 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5822 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005823 {
5824 *tofree = NULL;
5825 r = (char_u *)"{...}";
5826 }
5827 else
5828 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005829 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005830
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005831 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005832 *tofree = dict2string(tv, copyID, restore_copyID);
5833 if (restore_copyID)
5834 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005835 r = *tofree;
5836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005839 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005840 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005841 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005843 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005845 break;
5846
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005848 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005849#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005850 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005851 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5852 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005853 if (composite_val)
5854 {
5855 *tofree = string_quote(r, FALSE);
5856 r = *tofree;
5857 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005858#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005860
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005861 case VAR_INSTR:
5862 *tofree = NULL;
5863 r = (char_u *)"instructions";
5864 break;
5865
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005866 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005867 {
5868 class_T *cl = tv->vval.v_class;
5869 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5870 r = *tofree = alloc(len);
5871 vim_snprintf((char *)r, len, "class %s",
5872 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5873 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005874 break;
5875
5876 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005877 garray_T ga;
5878 ga_init2(&ga, 1, 50);
5879 ga_concat(&ga, (char_u *)"object of ");
5880 object_T *obj = tv->vval.v_object;
5881 class_T *cl = obj == NULL ? NULL : obj->obj_class;
5882 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]" : cl->class_name);
5883 if (cl != NULL)
5884 {
5885 ga_concat(&ga, (char_u *)" {");
5886 for (int i = 0; i < cl->class_obj_member_count; ++i)
5887 {
5888 if (i > 0)
5889 ga_concat(&ga, (char_u *)", ");
5890 objmember_T *m = &cl->class_obj_members[i];
5891 ga_concat(&ga, m->om_name);
5892 ga_concat(&ga, (char_u *)": ");
5893 char_u *tf = NULL;
5894 ga_concat(&ga, echo_string_core((typval_T *)(obj + 1) + i,
5895 &tf, numbuf, copyID, echo_style,
5896 restore_copyID, composite_val));
5897 vim_free(tf);
5898 }
5899 ga_concat(&ga, (char_u *)"}");
5900 }
5901
5902 *tofree = r = ga.ga_data;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005903 break;
5904
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005905 case VAR_FLOAT:
5906 *tofree = NULL;
5907 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5908 r = numbuf;
5909 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005910
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005911 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005912 case VAR_SPECIAL:
5913 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005914 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005915 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917
Bram Moolenaar8502c702014-06-17 12:51:16 +02005918 if (--recurse == 0)
5919 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005920 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921}
5922
5923/*
5924 * Return a string with the string representation of a variable.
5925 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5926 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005927 * Does not put quotes around strings, as ":echo" displays values.
5928 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5929 * May return NULL.
5930 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005932echo_string(
5933 typval_T *tv,
5934 char_u **tofree,
5935 char_u *numbuf,
5936 int copyID)
5937{
5938 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5939}
5940
5941/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005942 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5943 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005944 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005945 */
5946 int
5947buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5948{
5949 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005950 char_u *t;
5951 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005952
5953 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5954 return -1;
5955
5956 if (lnum > buf->b_ml.ml_line_count)
5957 lnum = buf->b_ml.ml_line_count;
5958
5959 str = ml_get_buf(buf, lnum, FALSE);
5960 if (str == NULL)
5961 return -1;
5962
5963 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005964 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005965
Bram Moolenaar91458462021-01-13 20:08:38 +01005966 // count the number of characters
5967 t = str;
5968 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5969 t += mb_ptr2len(t);
5970
5971 // In insert mode, when the cursor is at the end of a non-empty line,
5972 // byteidx points to the NUL character immediately past the end of the
5973 // string. In this case, add one to the character count.
5974 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5975 count++;
5976
5977 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005978}
5979
5980/*
5981 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005982 * byte index. Works only for loaded buffers. Returns -1 on failure.
5983 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005984 */
5985 int
5986buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5987{
5988 char_u *str;
5989 char_u *t;
5990
5991 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5992 return -1;
5993
5994 if (lnum > buf->b_ml.ml_line_count)
5995 lnum = buf->b_ml.ml_line_count;
5996
5997 str = ml_get_buf(buf, lnum, FALSE);
5998 if (str == NULL)
5999 return -1;
6000
6001 // Convert the character offset to a byte offset
6002 t = str;
6003 while (*t != NUL && --charidx > 0)
6004 t += mb_ptr2len(t);
6005
Bram Moolenaar91458462021-01-13 20:08:38 +01006006 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006007}
6008
6009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006011 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014var2fpos(
6015 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006016 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006017 int *fnum, // set to fnum for '0, 'A, etc.
6018 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006020 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006022 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006024 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006025 if (varp->v_type == VAR_LIST)
6026 {
6027 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006028 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006029 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006030 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006031
6032 l = varp->vval.v_list;
6033 if (l == NULL)
6034 return NULL;
6035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006036 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006037 pos.lnum = list_find_nr(l, 0L, &error);
6038 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006039 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006040 if (charcol)
6041 len = (long)mb_charlen(ml_get(pos.lnum));
6042 else
6043 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006044
Bram Moolenaarec65d772020-08-20 22:29:12 +02006045 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006047 li = list_find(l, 1L);
6048 if (li != NULL && li->li_tv.v_type == VAR_STRING
6049 && li->li_tv.vval.v_string != NULL
6050 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006051 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006052 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006053 }
6054 else
6055 {
6056 pos.col = list_find_nr(l, 1L, &error);
6057 if (error)
6058 return NULL;
6059 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006061 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006062 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006064 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006066 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006067 pos.coladd = list_find_nr(l, 2L, &error);
6068 if (error)
6069 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006070
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006071 return &pos;
6072 }
6073
Bram Moolenaarc5809432021-03-27 21:23:30 +01006074 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6075 return NULL;
6076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006077 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006078 if (name == NULL)
6079 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006080
6081 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006082 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006083 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006084 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006085 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006086 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006087 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006088 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006089 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006090 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006091 pos = VIsual;
6092 else
6093 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006094 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006095 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006096 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006098 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006099 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6101 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006102 pos = *pp;
6103 }
6104 if (pos.lnum != 0)
6105 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006106 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006107 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6108 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006110
Bram Moolenaara5525202006-03-02 22:52:09 +00006111 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006112
Bram Moolenaar477933c2007-07-17 14:32:23 +00006113 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006114 {
6115 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006116 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006117 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006118 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // In silent Ex mode topline is zero, but that's not a valid line
6120 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006121 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006122 return &pos;
6123 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006124 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006125 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006126 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006127 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006128 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006129 return &pos;
6130 }
6131 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006132 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006134 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 pos.lnum = curbuf->b_ml.ml_line_count;
6137 pos.col = 0;
6138 }
6139 else
6140 {
6141 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006142 if (charcol)
6143 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6144 else
6145 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
6147 return &pos;
6148 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006149 if (in_vim9script())
6150 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 return NULL;
6152}
6153
6154/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006155 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006156 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006157 * Note that the column is passed on as-is, the caller may want to decrement
6158 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006159 * If "charcol" is TRUE use the column as the character index instead of the
6160 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006161 * Return FAIL when conversion is not possible, doesn't check the position for
6162 * validity.
6163 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006165list2fpos(
6166 typval_T *arg,
6167 pos_T *posp,
6168 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006169 colnr_T *curswantp,
6170 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006171{
6172 list_T *l = arg->vval.v_list;
6173 long i = 0;
6174 long n;
6175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6177 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006178 if (arg->v_type != VAR_LIST
6179 || l == NULL
6180 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006181 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006182 return FAIL;
6183
6184 if (fnump != NULL)
6185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006186 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006187 if (n < 0)
6188 return FAIL;
6189 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006190 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006191 *fnump = n;
6192 }
6193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006195 if (n < 0)
6196 return FAIL;
6197 posp->lnum = n;
6198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006200 if (n < 0)
6201 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006202 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006203 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006204 if (charcol)
6205 {
6206 buf_T *buf;
6207
6208 // Get the text for the specified line in a loaded buffer
6209 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6210 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6211 return FAIL;
6212
Bram Moolenaar79f23442022-10-10 12:42:57 +01006213 n = buf_charidx_to_byteidx(buf,
6214 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006215 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006216 posp->col = n;
6217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006218 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006219 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006220 posp->coladd = 0;
6221 else
6222 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006223
Bram Moolenaar493c1782014-05-28 14:34:46 +02006224 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006226
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006227 return OK;
6228}
6229
6230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 * Get the length of an environment variable name.
6232 * Advance "arg" to the first character after the name.
6233 * Return 0 for error.
6234 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 char_u *p;
6239 int len;
6240
6241 for (p = *arg; vim_isIDc(*p); ++p)
6242 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006243 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 return 0;
6245
6246 len = (int)(p - *arg);
6247 *arg = p;
6248 return len;
6249}
6250
6251/*
6252 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006253 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 * Return 0 if something is wrong.
6255 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 char_u *p;
6260 int len;
6261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006264 {
6265 if (*p == ':')
6266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006267 // "s:" is start of "s:var", but "n:" is not and can be used in
6268 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006269 len = (int)(p - *arg);
6270 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6271 || len > 1)
6272 break;
6273 }
6274 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 return 0;
6277
6278 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006279 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281 return len;
6282}
6283
6284/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006285 * Get the length of the name of a variable or function.
6286 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006288 * Return -1 if curly braces expansion failed.
6289 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 * If the name contains 'magic' {}'s, expand them and return the
6291 * expanded name in an allocated string via 'alias' - caller must free.
6292 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294get_name_len(
6295 char_u **arg,
6296 char_u **alias,
6297 int evaluate,
6298 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 char_u *p;
6302 char_u *expr_start;
6303 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6308 && (*arg)[2] == (int)KE_SNR)
6309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 *arg += 3;
6312 return get_id_len(arg) + 3;
6313 }
6314 len = eval_fname_script(*arg);
6315 if (len > 0)
6316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006317 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 *arg += len;
6319 }
6320
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006322 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006324 p = find_name_end(*arg, &expr_start, &expr_end,
6325 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (expr_start != NULL)
6327 {
6328 char_u *temp_string;
6329
6330 if (!evaluate)
6331 {
6332 len += (int)(p - *arg);
6333 *arg = skipwhite(p);
6334 return len;
6335 }
6336
6337 /*
6338 * Include any <SID> etc in the expanded string:
6339 * Thus the -len here.
6340 */
6341 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6342 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006343 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 *alias = temp_string;
6345 *arg = skipwhite(p);
6346 return (int)STRLEN(temp_string);
6347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006350 // Only give an error when there is something, otherwise it will be
6351 // reported at a higher level.
6352 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006353 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 return len;
6356}
6357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006358/*
6359 * Find the end of a variable or function name, taking care of magic braces.
6360 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6361 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006362 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006363 * Return a pointer to just after the name. Equal to "arg" if there is no
6364 * valid name.
6365 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006367find_name_end(
6368 char_u *arg,
6369 char_u **expr_start,
6370 char_u **expr_end,
6371 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006373 int mb_nest = 0;
6374 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006376 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006377 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006379 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 *expr_start = NULL;
6382 *expr_end = NULL;
6383 }
6384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006385 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006386 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6387 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006388 return arg;
6389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006390 for (p = arg; *p != NUL
6391 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006392 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006393 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006394 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006395 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006396 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006397 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006398 if (*p == '\'')
6399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006401 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006402 ;
6403 if (*p == NUL)
6404 break;
6405 }
6406 else if (*p == '"')
6407 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006409 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006410 if (*p == '\\' && p[1] != NUL)
6411 ++p;
6412 if (*p == NUL)
6413 break;
6414 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006415 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6416 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006417 // "s:" is start of "s:var", but "n:" is not and can be used in
6418 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006419 len = (int)(p - arg);
6420 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006421 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006422 break;
6423 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 if (*p == '[')
6428 ++br_nest;
6429 else if (*p == ']')
6430 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006432
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006433 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 if (*p == '{')
6436 {
6437 mb_nest++;
6438 if (expr_start != NULL && *expr_start == NULL)
6439 *expr_start = p;
6440 }
6441 else if (*p == '}')
6442 {
6443 mb_nest--;
6444 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6445 *expr_end = p;
6446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449
6450 return p;
6451}
6452
6453/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006454 * Expands out the 'magic' {}'s in a variable/function name.
6455 * Note that this can call itself recursively, to deal with
6456 * constructs like foo{bar}{baz}{bam}
6457 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6458 * "in_start" ^
6459 * "expr_start" ^
6460 * "expr_end" ^
6461 * "in_end" ^
6462 *
6463 * Returns a new allocated string, which the caller must free.
6464 * Returns NULL for failure.
6465 */
6466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467make_expanded_name(
6468 char_u *in_start,
6469 char_u *expr_start,
6470 char_u *expr_end,
6471 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006472{
6473 char_u c1;
6474 char_u *retval = NULL;
6475 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006476
6477 if (expr_end == NULL || in_end == NULL)
6478 return NULL;
6479 *expr_start = NUL;
6480 *expr_end = NUL;
6481 c1 = *in_end;
6482 *in_end = NUL;
6483
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006484 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006485 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006486 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006487 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6488 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006489 if (retval != NULL)
6490 {
6491 STRCPY(retval, in_start);
6492 STRCAT(retval, temp_result);
6493 STRCAT(retval, expr_end + 1);
6494 }
6495 }
6496 vim_free(temp_result);
6497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006499 *expr_start = '{';
6500 *expr_end = '}';
6501
6502 if (retval != NULL)
6503 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006504 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006505 if (expr_start != NULL)
6506 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006507 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006508 temp_result = make_expanded_name(retval, expr_start,
6509 expr_end, temp_result);
6510 vim_free(retval);
6511 retval = temp_result;
6512 }
6513 }
6514
6515 return retval;
6516}
6517
6518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006523eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006525 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006526}
6527
6528/*
6529 * Return TRUE if character "c" can be used as the first character in a
6530 * variable or function name (excluding '{' and '}').
6531 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006533eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006534{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006535 return ASCII_ISALPHA(c) || c == '_';
6536}
6537
6538/*
6539 * Return TRUE if character "c" can be used as the first character of a
6540 * dictionary key.
6541 */
6542 int
6543eval_isdictc(int c)
6544{
6545 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
6547
6548/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006549 * Handle:
6550 * - expr[expr], expr[expr:expr] subscript
6551 * - ".name" lookup
6552 * - function call with Funcref variable: func(expr)
6553 * - method call: var->method()
6554 *
6555 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006556 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006557 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006558 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559handle_subscript(
6560 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006561 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006562 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006563 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006564 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006565{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006566 int evaluate = evalarg != NULL
6567 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006568 int ret = OK;
6569 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006570 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006571 int getnext;
6572 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006573
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006574 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006575 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006576 // When at the end of the line and ".name" or "->{" or "->X" follows in
6577 // the next line then consume the line break.
6578 p = eval_next_non_blank(*arg, evalarg, &getnext);
6579 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006580 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006581 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6582 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6583 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006584 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006585 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006586 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006587 check_white = FALSE;
6588 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006589
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006590 if (rettv->v_type == VAR_ANY)
6591 {
6592 char_u *exp_name;
6593 int cc;
6594 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006595 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006596 type_T *type;
6597
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006598 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006599 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006600 if (**arg != '.')
6601 {
6602 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006603 semsg(_(e_expected_dot_after_name_str),
6604 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006605 ret = FAIL;
6606 break;
6607 }
6608 ++*arg;
6609 if (IS_WHITE_OR_NUL(**arg))
6610 {
6611 if (verbose)
6612 emsg(_(e_no_white_space_allowed_after_dot));
6613 ret = FAIL;
6614 break;
6615 }
6616
6617 // isolate the name
6618 exp_name = *arg;
6619 while (eval_isnamec(**arg))
6620 ++*arg;
6621 cc = **arg;
6622 **arg = NUL;
6623
6624 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006625 evalarg == NULL ? NULL : evalarg->eval_cctx,
6626 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006627 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006628
6629 if (idx < 0 && ufunc == NULL)
6630 {
6631 ret = FAIL;
6632 break;
6633 }
6634 if (idx >= 0)
6635 {
6636 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6637 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6638
6639 copy_tv(sv->sv_tv, rettv);
6640 }
6641 else
6642 {
6643 rettv->v_type = VAR_FUNC;
6644 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6645 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006646 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006647 }
6648
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006649 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6650 || rettv->v_type == VAR_PARTIAL))
6651 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006652 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006653 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6654 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006655
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006656 // Stop the expression evaluation when immediately aborting on
6657 // error, or when an interrupt occurred or an exception was thrown
6658 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006659 if (aborting())
6660 {
6661 if (ret == OK)
6662 clear_tv(rettv);
6663 ret = FAIL;
6664 }
6665 dict_unref(selfdict);
6666 selfdict = NULL;
6667 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006668 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006669 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006670 if (in_vim9script())
6671 *arg = skipwhite(p + 2);
6672 else
6673 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006674 if (ret == OK)
6675 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006676 if (VIM_ISWHITE(**arg))
6677 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006678 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006679 ret = FAIL;
6680 }
6681 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006682 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006683 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006684 else
6685 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006686 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006687 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006688 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006689 // "." is ".name" lookup when we found a dict or when evaluating and
6690 // scriptversion is at least 2, where string concatenation is "..".
6691 else if (**arg == '['
6692 || (**arg == '.' && (rettv->v_type == VAR_DICT
6693 || (!evaluate
6694 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006695 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006696 {
6697 dict_unref(selfdict);
6698 if (rettv->v_type == VAR_DICT)
6699 {
6700 selfdict = rettv->vval.v_dict;
6701 if (selfdict != NULL)
6702 ++selfdict->dv_refcount;
6703 }
6704 else
6705 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006706 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006707 {
6708 clear_tv(rettv);
6709 ret = FAIL;
6710 }
6711 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006712 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6713 || rettv->v_type == VAR_OBJECT))
6714 {
6715 // class member: SomeClass.varname
6716 // class method: SomeClass.SomeMethod()
6717 // class constructor: SomeClass.new()
6718 // object member: someObject.varname
6719 // object method: someObject.SomeMethod()
6720 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6721 {
6722 clear_tv(rettv);
6723 ret = FAIL;
6724 }
6725 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006726 else
6727 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006728 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006730 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6731 // Don't do this when "Func" is already a partial that was bound
6732 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006733 if (selfdict != NULL
6734 && (rettv->v_type == VAR_FUNC
6735 || (rettv->v_type == VAR_PARTIAL
6736 && (rettv->vval.v_partial->pt_auto
6737 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006739
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740 dict_unref(selfdict);
6741 return ret;
6742}
6743
6744/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 * Make a copy of an item.
6746 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006747 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006748 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6749 * reference to an already copied list/dict can be used.
6750 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753item_copy(
6754 typval_T *from,
6755 typval_T *to,
6756 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006757 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759{
6760 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006761 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006762
Bram Moolenaar33570922005-01-25 22:26:29 +00006763 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006765 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006767 }
6768 ++recurse;
6769
6770 switch (from->v_type)
6771 {
6772 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006773 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 case VAR_STRING:
6775 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006776 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006777 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006778 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006779 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006780 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006781 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006782 case VAR_CLASS:
6783 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006784 copy_tv(from, to);
6785 break;
6786 case VAR_LIST:
6787 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006788 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006789 if (from->vval.v_list == NULL)
6790 to->vval.v_list = NULL;
6791 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6792 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006793 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006794 to->vval.v_list = from->vval.v_list->lv_copylist;
6795 ++to->vval.v_list->lv_refcount;
6796 }
6797 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006798 to->vval.v_list = list_copy(from->vval.v_list,
6799 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800 if (to->vval.v_list == NULL)
6801 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006803 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006805 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 case VAR_DICT:
6807 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006808 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006809 if (from->vval.v_dict == NULL)
6810 to->vval.v_dict = NULL;
6811 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006813 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006814 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6815 ++to->vval.v_dict->dv_refcount;
6816 }
6817 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006818 to->vval.v_dict = dict_copy(from->vval.v_dict,
6819 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006820 if (to->vval.v_dict == NULL)
6821 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006823 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006824 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006826 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006827 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006828 }
6829 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006830 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831}
6832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 void
6834echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6835{
6836 char_u *tofree;
6837 char_u numbuf[NUMBUFLEN];
6838 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6839
6840 if (*atstart)
6841 {
6842 *atstart = FALSE;
6843 // Call msg_start() after eval1(), evaluating the expression
6844 // may cause a message to appear.
6845 if (with_space)
6846 {
6847 // Mark the saved text as finishing the line, so that what
6848 // follows is displayed on a new line when scrolling back
6849 // at the more prompt.
6850 msg_sb_eol();
6851 msg_start();
6852 }
6853 }
6854 else if (with_space)
6855 msg_puts_attr(" ", echo_attr);
6856
6857 if (p != NULL)
6858 for ( ; *p != NUL && !got_int; ++p)
6859 {
6860 if (*p == '\n' || *p == '\r' || *p == TAB)
6861 {
6862 if (*p != TAB && *needclr)
6863 {
6864 // remove any text still there from the command
6865 msg_clr_eos();
6866 *needclr = FALSE;
6867 }
6868 msg_putchar_attr(*p, echo_attr);
6869 }
6870 else
6871 {
6872 if (has_mbyte)
6873 {
6874 int i = (*mb_ptr2len)(p);
6875
6876 (void)msg_outtrans_len_attr(p, i, echo_attr);
6877 p += i - 1;
6878 }
6879 else
6880 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6881 }
6882 }
6883 vim_free(tofree);
6884}
6885
Bram Moolenaare9a41262005-01-15 22:18:47 +00006886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 * ":echo expr1 ..." print each argument separated with a space, add a
6888 * newline at the end.
6889 * ":echon expr1 ..." print each argument plain.
6890 */
6891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006896 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 int needclr = TRUE;
6898 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006899 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006900 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006901 evalarg_T evalarg;
6902
Bram Moolenaare707c882020-07-01 13:07:37 +02006903 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905 if (eap->skip)
6906 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006907 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006909 // If eval1() causes an error message the text from the command may
6910 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006911 need_clr_eos = needclr;
6912
Bram Moolenaar68db9962021-05-09 23:19:22 +02006913 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006914 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
6916 /*
6917 * Report the invalid expression unless the expression evaluation
6918 * has been cancelled due to an aborting error, an interrupt, or an
6919 * exception.
6920 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006921 if (!aborting() && did_emsg == did_emsg_before
6922 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006923 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006924 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 break;
6926 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006927 need_clr_eos = FALSE;
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006930 {
6931 if (rettv.v_type == VAR_VOID)
6932 {
6933 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6934 break;
6935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006937 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006938
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 arg = skipwhite(arg);
6941 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006942 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006943 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 if (eap->skip)
6946 --emsg_skip;
6947 else
6948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006949 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (needclr)
6951 msg_clr_eos();
6952 if (eap->cmdidx == CMD_echo)
6953 msg_end();
6954 }
6955}
6956
6957/*
6958 * ":echohl {name}".
6959 */
6960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006961ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006963 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
6966/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006967 * Returns the :echo attribute
6968 */
6969 int
6970get_echo_attr(void)
6971{
6972 return echo_attr;
6973}
6974
6975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 * ":execute expr1 ..." execute the result of an expression.
6977 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006978 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006980 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 * Each gets spaces around each argument and a newline at the end for
6982 * echo commands
6983 */
6984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006988 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 int ret = OK;
6990 char_u *p;
6991 garray_T ga;
6992 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006993 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 ga_init2(&ga, 1, 80);
6996
6997 if (eap->skip)
6998 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006999 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007001 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007002 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 if (!eap->skip)
7006 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007007 char_u buf[NUMBUFLEN];
7008
7009 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007010 {
7011 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7012 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007013 semsg(_(e_using_invalid_value_as_string_str),
7014 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007015 p = NULL;
7016 }
7017 else
7018 p = tv_get_string_buf(&rettv, buf);
7019 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007020 else
7021 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007022 if (p == NULL)
7023 {
7024 clear_tv(&rettv);
7025 ret = FAIL;
7026 break;
7027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 len = (int)STRLEN(p);
7029 if (ga_grow(&ga, len + 2) == FAIL)
7030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007031 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 ret = FAIL;
7033 break;
7034 }
7035 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 ga.ga_len += len;
7039 }
7040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007041 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 arg = skipwhite(arg);
7043 }
7044
7045 if (ret != FAIL && ga.ga_data != NULL)
7046 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007047 // use the first line of continuation lines for messages
7048 SOURCING_LNUM = start_lnum;
7049
Bram Moolenaar37fef162022-08-29 18:16:32 +01007050 if (eap->cmdidx == CMD_echomsg
7051 || eap->cmdidx == CMD_echowindow
7052 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007054 // Mark the already saved text as finishing the line, so that what
7055 // follows is displayed on a new line when scrolling back at the
7056 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007057 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007058 }
7059
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007060 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007061 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007062 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007063 out_flush();
7064 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007065 else if (eap->cmdidx == CMD_echowindow)
7066 {
7067#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007068 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007069#endif
7070 msg_attr(ga.ga_data, echo_attr);
7071#ifdef HAS_MESSAGE_WINDOW
7072 end_echowindow();
7073#endif
7074 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007075 else if (eap->cmdidx == CMD_echoconsole)
7076 {
7077 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7078 ui_write((char_u *)"\r\n", 2, TRUE);
7079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 else if (eap->cmdidx == CMD_echoerr)
7081 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007082 int save_did_emsg = did_emsg;
7083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007084 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007085 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (!force_abort)
7087 did_emsg = save_did_emsg;
7088 }
7089 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007090 {
7091 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7092
7093 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7094 sticky_cmdmod_flags = cmdmod.cmod_flags
7095 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 do_cmdline((char_u *)ga.ga_data,
7097 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007098 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 }
7101
7102 ga_clear(&ga);
7103
7104 if (eap->skip)
7105 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007106 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107}
7108
7109/*
7110 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7111 * "arg" points to the "&" or '+' when called, to "option" when returning.
7112 * Returns NULL when no option name found. Otherwise pointer to the char
7113 * after the option name.
7114 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007115 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007116find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 char_u *p = *arg;
7119
7120 ++p;
7121 if (*p == 'g' && p[1] == ':')
7122 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007123 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 p += 2;
7125 }
7126 else if (*p == 'l' && p[1] == ':')
7127 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007128 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 p += 2;
7130 }
7131 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007132 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133
7134 if (!ASCII_ISALPHA(*p))
7135 return NULL;
7136 *arg = p;
7137
7138 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007139 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 else
7141 while (ASCII_ISALPHA(*p))
7142 ++p;
7143 return p;
7144}
7145
7146/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007147 * Display script name where an item was last set.
7148 * Should only be invoked when 'verbose' is non-zero.
7149 */
7150 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007151last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007152{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007153 char_u *p;
7154
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007155 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007156 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007157 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007158 if (p != NULL)
7159 {
7160 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007161 msg_puts(_("\n\tLast set from "));
7162 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007163 if (script_ctx.sc_lnum > 0)
7164 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007165 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007166 msg_outnum((long)script_ctx.sc_lnum);
7167 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007168 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007169 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007170 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007171 }
7172}
7173
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007174#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
7176/*
7177 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007178 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * "flags" can be "g" to do a global substitute.
7180 * Returns an allocated string, NULL for error.
7181 */
7182 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007183do_string_sub(
7184 char_u *str,
7185 char_u *pat,
7186 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007187 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 int sublen;
7191 regmatch_T regmatch;
7192 int i;
7193 int do_all;
7194 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007195 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 garray_T ga;
7197 char_u *ret;
7198 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007199 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007201 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007203 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
7205 ga_init2(&ga, 1, 200);
7206
7207 do_all = (flags[0] == 'g');
7208
7209 regmatch.rm_ic = p_ic;
7210 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7211 if (regmatch.regprog != NULL)
7212 {
7213 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007214 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007217 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007218 if (regmatch.startp[0] == regmatch.endp[0])
7219 {
7220 if (zero_width == regmatch.startp[0])
7221 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007222 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007223 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007224 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7225 (size_t)i);
7226 ga.ga_len += i;
7227 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007228 continue;
7229 }
7230 zero_width = regmatch.startp[0];
7231 }
7232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 /*
7234 * Get some space for a temporary buffer to do the substitution
7235 * into. It will contain:
7236 * - The text up to where the match is.
7237 * - The substituted text.
7238 * - The text after the match.
7239 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007240 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007241 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7243 {
7244 ga_clear(&ga);
7245 break;
7246 }
7247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007248 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 i = (int)(regmatch.startp[0] - tail);
7250 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007251 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007252 (void)vim_regsub(&regmatch, sub, expr,
7253 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7254 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007256 tail = regmatch.endp[0];
7257 if (*tail == NUL)
7258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (!do_all)
7260 break;
7261 }
7262
7263 if (ga.ga_data != NULL)
7264 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7265
Bram Moolenaar473de612013-06-08 18:19:48 +02007266 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268
7269 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7270 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007271 if (p_cpo == empty_option)
7272 p_cpo = save_cpo;
7273 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007275 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007276 // If it's still empty it was changed and restored, need to restore in
7277 // the complicated way.
7278 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007279 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007280 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 return ret;
7284}