blob: bdf03896a450190053473d250e53a55313fbf460 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
69 else
70 result = n1 / n2;
71
72 return result;
73}
74
75/*
76 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010077 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020079 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010080num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010081{
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010084 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085 if (failed != NULL)
86 *failed = TRUE;
87 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010088 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaar33570922005-01-25 22:26:29 +000091/*
92 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000093 */
94 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010095eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020097 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020098 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000099}
100
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000101#if defined(EXITFREE) || defined(PROTO)
102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100103eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000104{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200105 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100106 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200107 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000108
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200109 // autoloaded script names
110 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000111
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200112 // unreferenced lists and dicts
113 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200114
115 // functions not garbage collected
116 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117}
118#endif
119
Bram Moolenaare6b53242020-07-01 17:28:33 +0200120 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200121fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
122{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100123 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200124 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200125 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200126 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200127 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100128 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200129 {
130 evalarg->eval_getline = eap->getline;
131 evalarg->eval_cookie = eap->cookie;
132 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200133 }
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Top level evaluation function, returning a boolean.
138 * Sets "error" to TRUE if there was an error.
139 * Return TRUE or FALSE.
140 */
141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_to_bool(
143 char_u *arg,
144 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200145 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100146 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
151
Bram Moolenaar37c83712020-06-30 21:18:36 +0200152 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 if (skip)
155 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200156 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159 {
160 *error = FALSE;
161 if (!skip)
162 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200163 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200164 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200165 else
166 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000167 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
169 }
170 if (skip)
171 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200174 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175}
176
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100177/*
178 * Call eval1() and give an error message if not done at a lower level.
179 */
180 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200181eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100183 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100184 int ret;
185 int did_emsg_before = did_emsg;
186 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200187 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100188
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200189 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
190
191 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192 if (ret == FAIL)
193 {
194 // Report the invalid expression unless the expression evaluation has
195 // been cancelled due to an aborting error, an interrupt, or an
196 // exception, or we already gave a more specific error.
197 // Also check called_emsg for when using assert_fails().
198 if (!aborting() && did_emsg == did_emsg_before
199 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200200 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 return ret;
204}
205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200207 * Return whether a typval is a valid expression to pass to eval_expr_typval()
208 * or eval_expr_to_bool(). An empty string returns FALSE;
209 */
210 int
211eval_expr_valid_arg(typval_T *tv)
212{
213 return tv->v_type != VAR_UNKNOWN
214 && (tv->v_type != VAR_STRING
215 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
216}
217
218/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000236 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar822ba242020-05-24 23:00:18 +0200247 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200248 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 {
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100250 funccall_T *fc = create_funccal(partial->pt_func, rettv);
251 int r;
252
253 if (fc == NULL)
254 return FAIL;
255
Bram Moolenaar69082912022-09-22 21:35:19 +0100256 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100257 r = call_def_function(partial->pt_func, argc, argv,
258 DEF_USE_PT_ARGV, partial, fc, rettv);
259 remove_funccal();
260 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 return FAIL;
262 }
263 else
264 {
265 s = partial_name(partial);
266 if (s == NULL || *s == NUL)
267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000269 funcexe.fe_evaluate = TRUE;
270 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
272 return FAIL;
273 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100274 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200275 else if (expr->v_type == VAR_INSTR)
276 {
277 return exe_typval_instr(expr, rettv);
278 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100279 else
280 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000281 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100282 if (s == NULL)
283 return FAIL;
284 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200285 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200287 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200289 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200290 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100291 return FAIL;
292 }
293 }
294 return OK;
295}
296
297/*
298 * Like eval_to_bool() but using a typval_T instead of a string.
299 * Works for string, funcref and partial.
300 */
301 int
302eval_expr_to_bool(typval_T *expr, int *error)
303{
304 typval_T rettv;
305 int res;
306
307 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
308 {
309 *error = TRUE;
310 return FALSE;
311 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200312 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100313 clear_tv(&rettv);
314 return res;
315}
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/*
318 * Top level evaluation function, returning a string. If "skip" is TRUE,
319 * only parsing to "nextcmd" is done, without reporting errors. Return
320 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
321 */
322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323eval_to_string_skip(
324 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200325 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100326 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
Bram Moolenaar33570922005-01-25 22:26:29 +0000328 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200330 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
Bram Moolenaar37c83712020-06-30 21:18:36 +0200332 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (skip)
334 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200335 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 retval = NULL;
337 else
338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100339 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 if (skip)
343 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346 return retval;
347}
348
349/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100350 * Initialize "evalarg" for use.
351 */
352 void
353init_evalarg(evalarg_T *evalarg)
354{
355 CLEAR_POINTER(evalarg);
356 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
357}
358
359/*
360 * If "evalarg->eval_tofree" is not NULL free it later.
361 * Caller is expected to overwrite "evalarg->eval_tofree" next.
362 */
363 static void
364free_eval_tofree_later(evalarg_T *evalarg)
365{
366 if (evalarg->eval_tofree != NULL)
367 {
368 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
369 ((char_u **)evalarg->eval_tofree_ga.ga_data)
370 [evalarg->eval_tofree_ga.ga_len++]
371 = evalarg->eval_tofree;
372 else
373 vim_free(evalarg->eval_tofree);
374 }
375}
376
377/*
378 * After using "evalarg" filled from "eap": free the memory.
379 */
380 void
381clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
382{
383 if (evalarg != NULL)
384 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100385 garray_T *etga = &evalarg->eval_tofree_ga;
386
387 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100388 {
389 if (eap != NULL)
390 {
391 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100392 // ":let" it has the variable names. But we may also need
393 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100394 vim_free(eap->cmdline_tofree);
395 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100396
397 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
398 {
399 // "nextcmd" points into the last line in eval_tofree_ga,
400 // need to keep it around.
401 --etga->ga_len;
402 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
403 }
404 else
405 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100406 }
407 else
408 vim_free(evalarg->eval_tofree);
409 evalarg->eval_tofree = NULL;
410 }
411
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100412 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100413 VIM_CLEAR(evalarg->eval_tofree_lambda);
414 }
415}
416
417/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000418 * Skip over an expression at "*pp".
419 * Return FAIL for an error, OK otherwise.
420 */
421 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200422skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000423{
Bram Moolenaar33570922005-01-25 22:26:29 +0000424 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000425
426 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200427 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000428}
429
430/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200431 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 * If in Vim9 script and line breaks are encountered, the lines are
433 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 * "arg" is advanced to just after the expression.
435 * "start" is set to the start of the expression, "end" to just after the end.
436 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 * Return FAIL for an error, OK otherwise.
438 */
439 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200440skip_expr_concatenate(
441 char_u **arg,
442 char_u **start,
443 char_u **end,
444 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200445{
446 typval_T rettv;
447 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200448 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200449 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200450 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200451 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200452 int evaluate = evalarg == NULL
453 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200454
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200455 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200456 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457 {
458 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200460 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200462 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200463 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200464 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200465
466 // Don't evaluate the expression.
467 if (evalarg != NULL)
468 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200469 *arg = skipwhite(*arg);
470 res = eval1(arg, &rettv, evalarg);
471 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200472 if (evalarg != NULL)
473 evalarg->eval_flags = save_flags;
474
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200475 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200476 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200478 if (evalarg->eval_ga.ga_len == 1)
479 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200480 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200481 ga_clear(gap);
482 gap->ga_itemsize = 0;
483 }
484 else
485 {
486 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200488
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200489 // Line breaks encountered, concatenate all the lines.
490 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200491 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200492
493 // free the lines only when using getsourceline()
494 if (evalarg->eval_cookie != NULL)
495 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200496 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200497 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200498 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100499 // later. Also free "eval_tofree" later if needed.
500 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200501 evalarg->eval_tofree =
502 ((char_u **)gap->ga_data)[gap->ga_len - 1];
503 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200504 ga_clear_strings(gap);
505 }
506 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200507 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200508 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509
510 // free lines that were explicitly marked for freeing
511 ga_clear_strings(freegap);
512 }
513
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200514 gap->ga_itemsize = 0;
515 if (p == NULL)
516 return FAIL;
517 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200518 vim_free(evalarg->eval_tofree_lambda);
519 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200520 // Compute "end" relative to the end.
521 *end = *start + STRLEN(*start) - endoff;
522 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200523 }
524
525 return res;
526}
527
528/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100529 * Convert "tv" to a string.
530 * When "convert" is TRUE convert a List into a sequence of lines and convert
531 * a Float to a String.
532 * Returns an allocated string (NULL when out of memory).
533 */
534 char_u *
535typval2string(typval_T *tv, int convert)
536{
537 garray_T ga;
538 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100539 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100540
541 if (convert && tv->v_type == VAR_LIST)
542 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000543 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100544 if (tv->vval.v_list != NULL)
545 {
546 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
547 if (tv->vval.v_list->lv_len > 0)
548 ga_append(&ga, NL);
549 }
550 ga_append(&ga, NUL);
551 retval = (char_u *)ga.ga_data;
552 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100553 else if (convert && tv->v_type == VAR_FLOAT)
554 {
555 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
556 retval = vim_strsave(numbuf);
557 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100558 else
559 retval = vim_strsave(tv_get_string(tv));
560 return retval;
561}
562
563/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200564 * Top level evaluation function, returning a string. Does not handle line
565 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000566 * When "convert" is TRUE convert a List into a sequence of lines and convert
567 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 * Return pointer to allocated memory, or NULL for failure.
569 */
570 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100571eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573 int convert,
574 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaar33570922005-01-25 22:26:29 +0000576 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100578 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
581 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = NULL;
583 else
584 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100588 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 return retval;
591}
592
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100593 char_u *
594eval_to_string(
595 char_u *arg,
596 int convert)
597{
598 return eval_to_string_eap(arg, convert, NULL);
599}
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100603 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200604 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607eval_to_string_safe(
608 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000609 int use_sandbox,
610 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200613 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200614 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200615 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar9530b582022-01-22 13:39:08 +0000617 if (!keep_script_version)
618 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200619 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000620 if (use_sandbox)
621 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100622 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200623 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200624 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000625 if (use_sandbox)
626 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100627 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200628 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200629 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200630 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return retval;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
635 * Top level evaluation function, returning a number.
636 * Evaluates "expr" silently.
637 * Returns -1 for an error.
638 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200639 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200643 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000644 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ++emsg_off;
647
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200648 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 retval = -1;
650 else
651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 --emsg_off;
656
657 return retval;
658}
659
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 * Top level evaluation function.
662 * Returns an allocated typval_T with the result.
663 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000664 */
665 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200666eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667{
668 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200669 evalarg_T evalarg;
670
671 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000672
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200673 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200674 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100675 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676
Bram Moolenaar37c83712020-06-30 21:18:36 +0200677 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000678 return tv;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000682 * "*arg" points to what can be a function name in the form of "import.Name" or
683 * "Funcref". Return the name of the function. Set "tofree" to something that
684 * was allocated.
685 * If "verbose" is FALSE no errors are given.
686 * Return NULL for any failure.
687 */
688 static char_u *
689deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000690 char_u **arg,
691 char_u **tofree,
692 evalarg_T *evalarg,
693 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000694{
695 typval_T ref;
696 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100697 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000698
699 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100700 if (evalarg != NULL)
701 {
702 // need to evaluate this to get an import, like in "a.Func"
703 save_flags = evalarg->eval_flags;
704 evalarg->eval_flags |= EVAL_EVALUATE;
705 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100706 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100707 {
708 dictitem_T *v;
709
710 // If <SID>VarName was used it would not be found, try another way.
711 v = find_var_also_in_script(name, NULL, FALSE);
712 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100713 {
714 name = NULL;
715 goto theend;
716 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100717 copy_tv(&v->di_tv, &ref);
718 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000719 if (*skipwhite(*arg) != NUL)
720 {
721 if (verbose)
722 semsg(_(e_trailing_characters_str), *arg);
723 name = NULL;
724 }
725 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
726 {
727 name = ref.vval.v_string;
728 ref.vval.v_string = NULL;
729 *tofree = name;
730 }
731 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
732 {
733 if (ref.vval.v_partial->pt_argc > 0
734 || ref.vval.v_partial->pt_dict != NULL)
735 {
736 if (verbose)
737 emsg(_(e_cannot_use_partial_here));
738 name = NULL;
739 }
740 else
741 {
742 name = vim_strsave(partial_name(ref.vval.v_partial));
743 *tofree = name;
744 }
745 }
746 else
747 {
748 if (verbose)
749 semsg(_(e_not_callable_type_str), name);
750 name = NULL;
751 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100752
753theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000754 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100755 if (evalarg != NULL)
756 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000757 return name;
758}
759
760/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100761 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200762 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
763 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767call_vim_function(
768 char_u *func,
769 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200770 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200771 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000773 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200774 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000775 char_u *arg;
776 char_u *name;
777 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000778 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200781 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000782 funcexe.fe_firstline = curwin->w_cursor.lnum;
783 funcexe.fe_lastline = curwin->w_cursor.lnum;
784 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000785
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000786 // The name might be "import.Func" or "Funcref". We don't know, we need to
787 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000788 // autoload script has errors. Guess that when there is a dot in the name
789 // showing errors is the right choice.
790 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000791 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000792 if (ignore_errors)
793 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000794 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000795 if (ignore_errors)
796 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000797 if (name == NULL)
798 name = func;
799
800 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
801
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000802 if (ret == FAIL)
803 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000804 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000805
806 return ret;
807}
808
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100809/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100810 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000811 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
812 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000813 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 */
815 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816call_func_retstr(
817 char_u *func,
818 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200819 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000820{
821 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000822 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000823
Bram Moolenaarded27a12018-08-01 19:06:03 +0200824 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000825 return NULL;
826
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100827 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return retval;
830}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000831
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000832/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100833 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000834 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000835 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100836 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000837 */
838 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839call_func_retlist(
840 char_u *func,
841 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200842 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000843{
844 typval_T rettv;
845
Bram Moolenaarded27a12018-08-01 19:06:03 +0200846 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000847 return NULL;
848
849 if (rettv.v_type != VAR_LIST)
850 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100851 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
852 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000853 clear_tv(&rettv);
854 return NULL;
855 }
856
857 return rettv.vval.v_list;
858}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaare70dd112022-01-21 16:31:11 +0000860#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100862 * Evaluate "arg", which is 'foldexpr'.
863 * Note: caller must set "curwin" to match "arg".
864 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
865 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 */
867 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000868eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000870 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200872 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000874 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000875 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
876 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
Bram Moolenaare70dd112022-01-21 16:31:11 +0000878 arg = wp->w_p_fde;
879 current_sctx = wp->w_p_script_ctx[WV_FDE];
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000882 if (use_sandbox)
883 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100884 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200886 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 retval = 0;
888 else
889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100890 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (tv.v_type == VAR_NUMBER)
892 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000893 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 retval = 0;
895 else
896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // If the result is a string, check if there is a non-digit before
898 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (!VIM_ISDIGIT(*s) && *s != '-')
901 *cp = *s++;
902 retval = atol((char *)s);
903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000904 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000907 if (use_sandbox)
908 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100909 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200910 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000911 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200913 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914}
915#endif
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 * Get an lval: variable, Dict item or List item that can be assigned a value
919 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
920 * "name.key", "name.key[expr]" etc.
921 * Indexing only works if "name" is an existing List or Dictionary.
922 * "name" points to the start of the name.
923 * If "rettv" is not NULL it points to the value to be assigned.
924 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
925 * wrong; must end in space or cmd separator.
926 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100927 * flags:
928 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100929 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100930 * GLV_NO_AUTOLOAD: do not use script autoloading
931 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000932 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000933 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000935 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200936 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937get_lval(
938 char_u *name,
939 typval_T *rettv,
940 lval_T *lp,
941 int unlet,
942 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 int flags, // GLV_ values
944 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000946 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 char_u *expr_start, *expr_end;
948 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 dictitem_T *v;
950 typval_T var1;
951 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100955 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100956 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100957 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000958 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100960 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200961 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100963 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100965 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200967 lp->ll_name_end = find_name_end(name, NULL, NULL,
968 FNE_INCL_BR | fne_flags);
969 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 }
971
Bram Moolenaara749a422022-02-12 19:52:25 +0000972 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000973 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000974 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
975 {
976 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
977 return NULL;
978 }
979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100980 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000981 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 if (expr_start != NULL)
984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100985 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100986 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 && *p != '[' && *p != '.')
988 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000989 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
991 }
992
993 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
994 if (lp->ll_exp_name == NULL)
995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // Report an invalid expression in braces, unless the
997 // expression evaluation has been cancelled due to an
998 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 if (!aborting() && !quiet)
1000 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001001 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001002 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 return NULL;
1004 }
1005 }
1006 lp->ll_name = lp->ll_exp_name;
1007 }
1008 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_name = name;
1011
Bram Moolenaar4525a572022-02-13 11:57:33 +00001012 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001014 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001015 // However, "g:[key]" is indexing a dictionary.
1016 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001017 {
1018 --p;
1019 lp->ll_name_end = p;
1020 }
1021 if (*p == ':')
1022 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001023 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001024
Bram Moolenaar9510d222022-09-11 15:14:05 +01001025 if (is_scoped_variable(name))
1026 {
1027 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1028 return NULL;
1029 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001030 if (tp == p + 1 && !quiet)
1031 {
1032 semsg(_(e_white_space_required_after_str_str), ":", p);
1033 return NULL;
1034 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001035 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001036 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001037 semsg(_(e_using_type_not_in_script_context_str), p);
1038 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001039 }
1040
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001041 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001042 lp->ll_type = parse_type(&tp,
1043 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1044 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001045 if (lp->ll_type == NULL && !quiet)
1046 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001047 lp->ll_name_end = tp;
1048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 }
1050 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001051 if (lp->ll_name == NULL)
1052 return p;
1053
Bram Moolenaar62aec932022-01-29 21:45:34 +00001054 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001055 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001056 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001057
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001058 if (import != NULL)
1059 {
1060 ufunc_T *ufunc;
1061 type_T *type;
1062
Bram Moolenaar753885b2022-08-24 16:30:36 +01001063 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001064 lp->ll_sid = import->imp_sid;
1065 lp->ll_name = skipwhite(p + 1);
1066 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1067 lp->ll_name_end = p;
1068
1069 // check the item is exported
1070 cc = *p;
1071 *p = NUL;
1072 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001073 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001074 {
1075 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001076 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001077 }
1078 *p = cc;
1079 }
1080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001082 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001083 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return p;
1085
Bram Moolenaar4525a572022-02-13 11:57:33 +00001086 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001087 {
1088 // using local variable
1089 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001090 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001091 }
1092 else
1093 {
1094 cc = *p;
1095 *p = NUL;
1096 // When we would write to the variable pass &ht and prevent autoload.
1097 writing = !(flags & GLV_READ_ONLY);
1098 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001099 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001100 if (v == NULL && !quiet)
1101 semsg(_(e_undefined_variable_str), lp->ll_name);
1102 *p = cc;
1103 if (v == NULL)
1104 return NULL;
1105 lp->ll_tv = &v->di_tv;
1106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107
Bram Moolenaar4525a572022-02-13 11:57:33 +00001108 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001109 {
1110 if (!quiet)
1111 semsg(_(e_variable_already_declared), lp->ll_name);
1112 return NULL;
1113 }
1114
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 /*
1116 * Loop until no more [idx] or .key is following.
1117 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 var1.v_type = VAR_UNKNOWN;
1119 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001120 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001122 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001123
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001124 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1125 {
1126 if (!quiet)
1127 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1128 return NULL;
1129 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001130 if (lp->ll_tv->v_type != VAR_LIST
1131 && lp->ll_tv->v_type != VAR_DICT
1132 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001135 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001138
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001139 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001140 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001141 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001142 else if (lp->ll_tv->v_type == VAR_BLOB
1143 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001144 r = rettv_blob_alloc(lp->ll_tv);
1145 if (r == FAIL)
1146 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001147
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001151 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154
Bram Moolenaar4525a572022-02-13 11:57:33 +00001155 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001156 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001157 && lp->ll_tv == &v->di_tv
1158 && ht != NULL && ht == get_script_local_ht())
1159 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001160 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001161
1162 // Vim9 script local variable: get the type
1163 if (sv != NULL)
1164 lp->ll_valtype = sv->sv_type;
1165 }
1166
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 len = -1;
1168 if (*p == '.')
1169 {
1170 key = p + 1;
1171 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1172 ;
1173 if (len == 0)
1174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001176 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 }
1179 p = key + len;
1180 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001181 else
1182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001183 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001184 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 if (*p == ':')
1186 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001187 else
1188 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001190 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001192 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001194 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001195 clear_tv(&var1);
1196 return NULL;
1197 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001198 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001199 }
1200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001201 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 if (*p == ':')
1203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001207 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001208 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001210 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 if (rettv != NULL
1212 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001213 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001215 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001218 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001219 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 }
1222 p = skipwhite(p + 1);
1223 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 else
1226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001228 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001229 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001231 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001233 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001234 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001236 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001237 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238 clear_tv(&var2);
1239 return NULL;
1240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001243 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001246
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001250 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001251 clear_tv(&var1);
1252 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001254 }
1255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001256 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 ++p;
1258 }
1259
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 {
1262 if (len == -1)
1263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 // "[key]": get key from "var1"
1265 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001266 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001267 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 }
1271 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001273
1274 // a NULL dict is equivalent with an empty dict
1275 if (lp->ll_tv->vval.v_dict == NULL)
1276 {
1277 lp->ll_tv->vval.v_dict = dict_alloc();
1278 if (lp->ll_tv->vval.v_dict == NULL)
1279 {
1280 clear_tv(&var1);
1281 return NULL;
1282 }
1283 ++lp->ll_tv->vval.v_dict->dv_refcount;
1284 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001286
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001287 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001289 // When assigning to a scope dictionary check that a function and
1290 // variable name is valid (only variable name unless it is l: or
1291 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001292 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001293 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001294 int prevval;
1295 int wrong;
1296
1297 if (len != -1)
1298 {
1299 prevval = key[len];
1300 key[len] = NUL;
1301 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001302 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001303 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001304 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1305 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001306 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001307 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001308 if (len != -1)
1309 key[len] = prevval;
1310 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001311 {
1312 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001313 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001314 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001315 }
1316
Bram Moolenaar10c65862020-10-08 21:16:42 +02001317 if (lp->ll_valtype != NULL)
1318 // use the type of the member
1319 lp->ll_valtype = lp->ll_valtype->tt_member;
1320
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001323 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001324 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001325 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001326 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001327 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001328 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001329 return NULL;
1330 }
1331
Bram Moolenaar31b81602019-02-10 22:14:27 +01001332 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001336 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001337 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 }
1340 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001344 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001345 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001346 p = NULL;
1347 break;
1348 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001349 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001350 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001351 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1352 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001353 {
1354 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001355 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001356 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001357
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001358 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001359 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001360 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001361 else if (lp->ll_tv->v_type == VAR_BLOB)
1362 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001363 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1364
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 /*
1366 * Get the number and item for the only or first index of the List.
1367 */
1368 if (empty1)
1369 lp->ll_n1 = 0;
1370 else
1371 // is number or string
1372 lp->ll_n1 = (long)tv_get_number(&var1);
1373 clear_tv(&var1);
1374
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001375 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001376 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001377 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001378 return NULL;
1379 }
1380 if (lp->ll_range && !lp->ll_empty2)
1381 {
1382 lp->ll_n2 = (long)tv_get_number(&var2);
1383 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001384 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1385 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001386 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001387 }
1388 lp->ll_blob = lp->ll_tv->vval.v_blob;
1389 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001390 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001391 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001392 else
1393 {
1394 /*
1395 * Get the number and item for the only or first index of the List.
1396 */
1397 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001400 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001401 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001402 clear_tv(&var1);
1403
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001406 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1407 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001408 if (lp->ll_li == NULL)
1409 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001410 clear_tv(&var2);
1411 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001412 }
1413
Bram Moolenaar10c65862020-10-08 21:16:42 +02001414 if (lp->ll_valtype != NULL)
1415 // use the type of the member
1416 lp->ll_valtype = lp->ll_valtype->tt_member;
1417
Bram Moolenaar8c711452005-01-14 21:53:12 +00001418 /*
1419 * May need to find the item or absolute index for the second
1420 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 * When no index given: "lp->ll_empty2" is TRUE.
1422 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001426 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001427 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001428 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001429 if (check_range_index_two(lp->ll_list,
1430 &lp->ll_n1, lp->ll_li,
1431 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001432 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001433 }
1434
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 }
1438
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001439 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001441 return p;
1442}
1443
1444/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001448clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449{
1450 vim_free(lp->ll_exp_name);
1451 vim_free(lp->ll_newkey);
1452}
1453
1454/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001455 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001457 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1458 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461set_var_lval(
1462 lval_T *lp,
1463 char_u *endp,
1464 typval_T *rettv,
1465 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001466 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1467 char_u *op,
1468 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469{
1470 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001471 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472
1473 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001475 cc = *endp;
1476 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001477 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1478 return;
1479
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 if (lp->ll_blob != NULL)
1481 {
1482 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001484 if (op != NULL && *op != '=')
1485 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001486 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001487 return;
1488 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001489 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001490 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001491
1492 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1493 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001494 if (lp->ll_empty2)
1495 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1496
Bram Moolenaar68452172021-04-12 21:21:02 +02001497 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1498 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001499 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001500 }
1501 else
1502 {
1503 val = (int)tv_get_number_chk(rettv, &error);
1504 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001505 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001506 }
1507 }
1508 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001510 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001512 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1513 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001514 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001515 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001516 *endp = cc;
1517 return;
1518 }
1519
Bram Moolenaarff697e62019-02-12 22:28:33 +01001520 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001521 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001522 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001523 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001524 {
1525 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001526 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1527 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001528 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001529 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001530 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001531 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001534 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001535 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001536 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1537 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001538 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001539 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001540 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001541 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001542 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001543 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001544 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001545 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001546 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001547 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001548 else if (lp->ll_range)
1549 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001550 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1551 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001552 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001553 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001554 return;
1555 }
1556
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001557 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1558 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001559 }
1560 else
1561 {
1562 /*
1563 * Assign to a List or Dictionary item.
1564 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001565 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1566 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001567 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001568 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001569 return;
1570 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001571
1572 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001573 && check_typval_arg_type(lp->ll_valtype, rettv,
1574 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001575 return;
1576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001577 if (lp->ll_newkey != NULL)
1578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 if (op != NULL && *op != '=')
1580 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001581 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 return;
1583 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001584 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1585 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001586 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001588 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001589 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001590 if (di == NULL)
1591 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001592 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1593 {
1594 vim_free(di);
1595 return;
1596 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 else if (op != NULL && *op != '=')
1600 {
1601 tv_op(lp->ll_tv, rettv, op);
1602 return;
1603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001605 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607 /*
1608 * Assign the value to the variable or list item.
1609 */
1610 if (copy)
1611 copy_tv(rettv, lp->ll_tv);
1612 else
1613 {
1614 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001615 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 }
1618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619}
1620
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1623 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 * Returns OK or FAIL.
1625 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001629 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 char_u numbuf[NUMBUFLEN];
1631 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001632 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001634 // Can't do anything with a Funcref or Dict on the right.
1635 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001636 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001637 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1638 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 {
1640 switch (tv1->v_type)
1641 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001642 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001643 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 case VAR_DICT:
1646 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001647 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001648 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001649 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001650 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001651 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001652 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 break;
1654
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001655 case VAR_BLOB:
1656 if (*op != '+' || tv2->v_type != VAR_BLOB)
1657 break;
1658 // BLOB += BLOB
1659 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1660 {
1661 blob_T *b1 = tv1->vval.v_blob;
1662 blob_T *b2 = tv2->vval.v_blob;
1663 int i, len = blob_len(b2);
1664 for (i = 0; i < len; i++)
1665 ga_append(&b1->bv_ga, blob_get(b2, i));
1666 }
1667 return OK;
1668
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 case VAR_LIST:
1670 if (*op != '+' || tv2->v_type != VAR_LIST)
1671 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001672 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001673 if (tv2->vval.v_list != NULL)
1674 {
1675 if (tv1->vval.v_list == NULL)
1676 {
1677 tv1->vval.v_list = tv2->vval.v_list;
1678 ++tv1->vval.v_list->lv_refcount;
1679 }
1680 else
1681 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1682 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 return OK;
1684
1685 case VAR_NUMBER:
1686 case VAR_STRING:
1687 if (tv2->v_type == VAR_LIST)
1688 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001689 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001691 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001692 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001693 if (tv2->v_type == VAR_FLOAT)
1694 {
1695 float_T f = n;
1696
Bram Moolenaarff697e62019-02-12 22:28:33 +01001697 if (*op == '%')
1698 break;
1699 switch (*op)
1700 {
1701 case '+': f += tv2->vval.v_float; break;
1702 case '-': f -= tv2->vval.v_float; break;
1703 case '*': f *= tv2->vval.v_float; break;
1704 case '/': f /= tv2->vval.v_float; break;
1705 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001706 clear_tv(tv1);
1707 tv1->v_type = VAR_FLOAT;
1708 tv1->vval.v_float = f;
1709 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001711 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001712 switch (*op)
1713 {
1714 case '+': n += tv_get_number(tv2); break;
1715 case '-': n -= tv_get_number(tv2); break;
1716 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001717 case '/': n = num_divide(n, tv_get_number(tv2),
1718 &failed); break;
1719 case '%': n = num_modulus(n, tv_get_number(tv2),
1720 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001721 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001722 clear_tv(tv1);
1723 tv1->v_type = VAR_NUMBER;
1724 tv1->vval.v_number = n;
1725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 }
1727 else
1728 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001729 if (tv2->v_type == VAR_FLOAT)
1730 break;
1731
Bram Moolenaarff697e62019-02-12 22:28:33 +01001732 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001733 s = tv_get_string(tv1);
1734 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 clear_tv(tv1);
1736 tv1->v_type = VAR_STRING;
1737 tv1->vval.v_string = s;
1738 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001739 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001741 case VAR_FLOAT:
1742 {
1743 float_T f;
1744
Bram Moolenaarff697e62019-02-12 22:28:33 +01001745 if (*op == '%' || *op == '.'
1746 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001747 && tv2->v_type != VAR_NUMBER
1748 && tv2->v_type != VAR_STRING))
1749 break;
1750 if (tv2->v_type == VAR_FLOAT)
1751 f = tv2->vval.v_float;
1752 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001753 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001754 switch (*op)
1755 {
1756 case '+': tv1->vval.v_float += f; break;
1757 case '-': tv1->vval.v_float -= f; break;
1758 case '*': tv1->vval.v_float *= f; break;
1759 case '/': tv1->vval.v_float /= f; break;
1760 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001761 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001762 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 }
1764 }
1765
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001766 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 return FAIL;
1768}
1769
1770/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 * Evaluate the expression used in a ":for var in expr" command.
1772 * "arg" points to "var".
1773 * Set "*errp" to TRUE for an error, FALSE otherwise;
1774 * Return a pointer that holds the info. Null when there is an error.
1775 */
1776 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777eval_for_line(
1778 char_u *arg,
1779 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001780 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001784 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 typval_T tv;
1787 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001788 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001792 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 if (fi == NULL)
1794 return NULL;
1795
Bram Moolenaar404557e2021-07-05 21:41:48 +02001796 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1797 &fi->fi_semicolon, FALSE);
1798 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 return fi;
1800
Bram Moolenaar404557e2021-07-05 21:41:48 +02001801 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001802 if (expr[0] != 'i' || expr[1] != 'n'
1803 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001805 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1806 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1807 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001808 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 return fi;
1810 }
1811
1812 if (skip)
1813 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001814 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1815 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
1817 *errp = FALSE;
1818 if (!skip)
1819 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001820 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001821 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 l = tv.vval.v_list;
1823 if (l == NULL)
1824 {
1825 // a null list is like an empty list: do nothing
1826 clear_tv(&tv);
1827 }
1828 else
1829 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001831 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001833 // No need to increment the refcount, it's already set for
1834 // the list being used in "tv".
1835 fi->fi_list = l;
1836 list_add_watch(l, &fi->fi_lw);
1837 fi->fi_lw.lw_item = l->lv_first;
1838 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001839 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001840 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001841 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001842 fi->fi_bi = 0;
1843 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001844 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001845 typval_T btv;
1846
1847 // Make a copy, so that the iteration still works when the
1848 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001850 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001851 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001852 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001853 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001854 else if (tv.v_type == VAR_STRING)
1855 {
1856 fi->fi_byte_idx = 0;
1857 fi->fi_string = tv.vval.v_string;
1858 tv.vval.v_string = NULL;
1859 if (fi->fi_string == NULL)
1860 fi->fi_string = vim_strsave((char_u *)"");
1861 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 else
1863 {
Sean Dewar80d73952021-08-04 19:25:54 +02001864 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001865 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 }
1867 }
1868 }
1869 if (skip)
1870 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001871 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872
1873 return fi;
1874}
1875
1876/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001877 * Used when looping over a :for line, skip the "in expr" part.
1878 */
1879 void
1880skip_for_lines(void *fi_void, evalarg_T *evalarg)
1881{
1882 forinfo_T *fi = (forinfo_T *)fi_void;
1883 int i;
1884
1885 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001886 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001887}
1888
1889/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 * Use the first item in a ":for" list. Advance to the next.
1891 * Assign the values to the variable (list). "arg" points to the first one.
1892 * Return TRUE when a valid item was found, FALSE when at end of list or
1893 * something wrong.
1894 */
1895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001898 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001900 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001901 ? (ASSIGN_FINAL
1902 // first round: error if variable exists
1903 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001904 | ASSIGN_NO_MEMBER_TYPE
1905 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001906 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001908 int skip_assign = in_vim9script() && arg[0] == '_'
1909 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001911 if (fi->fi_blob != NULL)
1912 {
1913 typval_T tv;
1914
1915 if (fi->fi_bi >= blob_len(fi->fi_blob))
1916 return FALSE;
1917 tv.v_type = VAR_NUMBER;
1918 tv.v_lock = VAR_FIXED;
1919 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1920 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001921 if (skip_assign)
1922 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001923 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001924 fi->fi_varcount, flag, NULL) == OK;
1925 }
1926
1927 if (fi->fi_string != NULL)
1928 {
1929 typval_T tv;
1930 int len;
1931
1932 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1933 if (len == 0)
1934 return FALSE;
1935 tv.v_type = VAR_STRING;
1936 tv.v_lock = VAR_FIXED;
1937 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1938 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001939 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001940 if (skip_assign)
1941 result = TRUE;
1942 else
1943 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001944 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001945 vim_free(tv.vval.v_string);
1946 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001947 }
1948
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 item = fi->fi_lw.lw_item;
1950 if (item == NULL)
1951 result = FALSE;
1952 else
1953 {
1954 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001955 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001956 if (skip_assign)
1957 result = TRUE;
1958 else
1959 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001960 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 }
1962 return result;
1963}
1964
1965/*
1966 * Free the structure used to store info used by ":for".
1967 */
1968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001969free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970{
Bram Moolenaar33570922005-01-25 22:26:29 +00001971 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001973 if (fi == NULL)
1974 return;
1975 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001976 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001978 list_unref(fi->fi_list);
1979 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001980 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001981 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001982 else
1983 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 vim_free(fi);
1985}
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001988set_context_for_expression(
1989 expand_T *xp,
1990 char_u *arg,
1991 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001993 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001997 if (cmdidx == CMD_let || cmdidx == CMD_var
1998 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002004 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002007 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002008 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 break;
2010 }
2011 return;
2012 }
2013 }
2014 else
2015 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2016 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 while ((xp->xp_pattern = vim_strpbrk(arg,
2018 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2019 {
2020 c = *xp->xp_pattern;
2021 if (c == '&')
2022 {
2023 c = xp->xp_pattern[1];
2024 if (c == '&')
2025 {
2026 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002027 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002032 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2033 xp->xp_pattern += 2;
2034
2035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037 else if (c == '$')
2038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 xp->xp_context = EXPAND_ENV_VARS;
2041 }
2042 else if (c == '=')
2043 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002044 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 xp->xp_context = EXPAND_EXPRESSION;
2046 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002047 else if (c == '#'
2048 && xp->xp_context == EXPAND_EXPRESSION)
2049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002051 break;
2052 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002053 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && xp->xp_context == EXPAND_FUNCTIONS
2055 && vim_strchr(xp->xp_pattern, '(') == NULL)
2056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002057 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 break;
2059 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002060 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2065 if (c == '\\' && xp->xp_pattern[1] != NUL)
2066 ++xp->xp_pattern;
2067 xp->xp_context = EXPAND_NOTHING;
2068 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002069 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002071 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2073 /* skip */ ;
2074 xp->xp_context = EXPAND_NOTHING;
2075 }
2076 else if (c == '|')
2077 {
2078 if (xp->xp_pattern[1] == '|')
2079 {
2080 ++xp->xp_pattern;
2081 xp->xp_context = EXPAND_EXPRESSION;
2082 }
2083 else
2084 xp->xp_context = EXPAND_COMMANDS;
2085 }
2086 else
2087 xp->xp_context = EXPAND_EXPRESSION;
2088 }
2089 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002090 // Doesn't look like something valid, expand as an expression
2091 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 arg = xp->xp_pattern;
2094 if (*arg != NUL)
2095 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2096 /* skip */ ;
2097 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002098
2099 // ":exe one two" completes "two"
2100 if ((cmdidx == CMD_execute
2101 || cmdidx == CMD_echo
2102 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002103 || cmdidx == CMD_echomsg
2104 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002105 && xp->xp_context == EXPAND_EXPRESSION)
2106 {
2107 for (;;)
2108 {
2109 char_u *n = skiptowhite(arg);
2110
2111 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2112 break;
2113 arg = skipwhite(n);
2114 }
2115 }
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 xp->xp_pattern = arg;
2118}
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002121 * Return TRUE if "pat" matches "text".
2122 * Does not use 'cpo' and always uses 'magic'.
2123 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002124 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002125pattern_match(char_u *pat, char_u *text, int ic)
2126{
2127 int matches = FALSE;
2128 char_u *save_cpo;
2129 regmatch_T regmatch;
2130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002131 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002132 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002133 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002134 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2135 if (regmatch.regprog != NULL)
2136 {
2137 regmatch.rm_ic = ic;
2138 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2139 vim_regfree(regmatch.regprog);
2140 }
2141 p_cpo = save_cpo;
2142 return matches;
2143}
2144
2145/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002146 * Handle a name followed by "(". Both for just "name(arg)" and for
2147 * "expr->name(arg)".
2148 * Returns OK or FAIL.
2149 */
2150 static int
2151eval_func(
2152 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002153 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002154 char_u *name,
2155 int name_len,
2156 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002157 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002158 typval_T *basetv) // "expr" for "expr->name(arg)"
2159{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002160 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002161 char_u *s = name;
2162 int len = name_len;
2163 partial_T *partial;
2164 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002165 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002166 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002167
2168 if (!evaluate)
2169 check_vars(s, len);
2170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002171 // If "s" is the name of a variable of type VAR_FUNC
2172 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002173 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002174 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002176 // Need to make a copy, in case evaluating the arguments makes
2177 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002178 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002179 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002180 ret = FAIL;
2181 else
2182 {
2183 funcexe_T funcexe;
2184
2185 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002186 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002187 funcexe.fe_firstline = curwin->w_cursor.lnum;
2188 funcexe.fe_lastline = curwin->w_cursor.lnum;
2189 funcexe.fe_evaluate = evaluate;
2190 funcexe.fe_partial = partial;
2191 funcexe.fe_basetv = basetv;
2192 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002193 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002194 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002195 }
2196 vim_free(s);
2197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002198 // If evaluate is FALSE rettv->v_type was not set in
2199 // get_func_tv, but it's needed in handle_subscript() to parse
2200 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002201 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2202 {
2203 rettv->vval.v_string = NULL;
2204 rettv->v_type = VAR_FUNC;
2205 }
2206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002207 // Stop the expression evaluation when immediately
2208 // aborting on error, or when an interrupt occurred or
2209 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002210 if (evaluate && aborting())
2211 {
2212 if (ret == OK)
2213 clear_tv(rettv);
2214 ret = FAIL;
2215 }
2216 return ret;
2217}
2218
2219/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002220 * After a NL, skip over empty lines and comment-only lines.
2221 */
2222 static char_u *
2223newline_skip_comments(char_u *arg)
2224{
2225 char_u *p = arg + 1;
2226
2227 for (;;)
2228 {
2229 p = skipwhite(p);
2230
2231 if (*p == NUL)
2232 break;
2233 if (vim9_comment_start(p))
2234 {
2235 char_u *nl = vim_strchr(p, NL);
2236
2237 if (nl == NULL)
2238 break;
2239 p = nl;
2240 }
2241 if (*p != NL)
2242 break;
2243 ++p; // skip another NL
2244 }
2245 return p;
2246}
2247
2248/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002249 * Get the next line source line without advancing. But do skip over comment
2250 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002251 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002252 */
2253 static char_u *
2254getline_peek_skip_comments(evalarg_T *evalarg)
2255{
2256 for (;;)
2257 {
2258 char_u *next = getline_peek(evalarg->eval_getline,
2259 evalarg->eval_cookie);
2260 char_u *p;
2261
2262 if (next == NULL)
2263 break;
2264 p = skipwhite(next);
2265 if (*p != NUL && !vim9_comment_start(p))
2266 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002267 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002268 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002269 }
2270 return NULL;
2271}
2272
2273/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002274 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2275 * comment) and there is a next line, return the next line (skipping blanks)
2276 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002277 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2278 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 * "arg" must point somewhere inside a line, not at the start.
2280 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002281 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2283{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002284 char_u *p = skipwhite(arg);
2285
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002287 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002289 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2290 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002291 && (*p == NUL || *p == NL
2292 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002294 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002295
Bram Moolenaare442d592022-05-05 12:20:28 +01002296 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002297 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002298 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002299 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002300 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002301 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302
Bram Moolenaar9d489562020-07-30 20:08:50 +02002303 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002305 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 }
2308 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002309 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310}
2311
2312/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002313 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002314 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002315 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002316 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002317eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002318{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002319 garray_T *gap = &evalarg->eval_ga;
2320 char_u *line;
2321
Bram Moolenaar39be4982022-05-06 21:51:50 +01002322 if (arg != NULL)
2323 {
2324 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002325 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002326 // Truncate before a trailing comment, so that concatenating the lines
2327 // won't turn the rest into a comment.
2328 if (*skipwhite(arg) == '#')
2329 *arg = NUL;
2330 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002331
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002332 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002333 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2334 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002335 else
2336 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002337 if (line == NULL)
2338 return NULL;
2339
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002340 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002341 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2342 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002343 char_u *p = skipwhite(line);
2344
2345 // Going to concatenate the lines after parsing. For an empty or
2346 // comment line use an empty string.
2347 if (*p == NUL || vim9_comment_start(p))
2348 {
2349 vim_free(line);
2350 line = vim_strsave((char_u *)"");
2351 }
2352
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002353 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2354 ++gap->ga_len;
2355 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002356 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002357 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002358 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002359 evalarg->eval_tofree = line;
2360 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002361
2362 // Advanced to the next line, "arg" no longer points into the previous
2363 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002364 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002365 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002366}
2367
Bram Moolenaare6b53242020-07-01 17:28:33 +02002368/*
2369 * Call eval_next_non_blank() and get the next line if needed.
2370 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002371 char_u *
2372skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2373{
2374 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002375 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002376
Bram Moolenaar962d7212020-07-04 14:15:00 +02002377 if (evalarg == NULL)
2378 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002379 eval_next_non_blank(p, evalarg, &getnext);
2380 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002381 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002382 return p;
2383}
2384
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2389 */
2390
2391/*
2392 * Handle zero level expression.
2393 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002395 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 * Return OK or FAIL.
2398 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002400eval0(
2401 char_u *arg,
2402 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002403 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002406 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2407}
2408
2409/*
2410 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2411 * expression and don't check what comes after the expression.
2412 */
2413 int
2414eval0_retarg(
2415 char_u *arg,
2416 typval_T *rettv,
2417 exarg_T *eap,
2418 evalarg_T *evalarg,
2419 char_u **retarg)
2420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int ret;
2422 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002423 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002424 int did_emsg_before = did_emsg;
2425 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002427 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002428 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002432
Bram Moolenaar79481362022-06-27 20:15:10 +01002433 if (ret != FAIL)
2434 {
2435 expr_end = p;
2436 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002437
Bram Moolenaar79481362022-06-27 20:15:10 +01002438 // In Vim9 script a command block is not split at NL characters for
2439 // commands using an expression argument. Skip over a '#' comment to
2440 // check for a following NL. Require white space before the '#'.
2441 if (in_vim9script() && p > expr_end && retarg == NULL)
2442 while (*p == '#')
2443 {
2444 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002445
Bram Moolenaar79481362022-06-27 20:15:10 +01002446 if (nl == NULL)
2447 break;
2448 p = skipwhite(nl + 1);
2449 if (eap != NULL && *p != NUL)
2450 eap->nextcmd = p;
2451 check_for_end = FALSE;
2452 }
2453
2454 if (check_for_end)
2455 end_error = !ends_excmd2(arg, p);
2456 }
2457
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002458 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /*
2463 * Report the invalid expression unless the expression evaluation has
2464 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002465 * exception, or we already gave a more specific error.
2466 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002468 if (!aborting()
2469 && did_emsg == did_emsg_before
2470 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002471 && (flags & EVAL_CONSTANT) == 0
2472 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002473 {
2474 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002475 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002476 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002477 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002478 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002479
2480 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002481 // a next command to avoid more errors, unless "|" is following, which
2482 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002483 if (eap != NULL && p != NULL
2484 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002485 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002488
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002489 if (retarg != NULL)
2490 *retarg = p;
2491 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002492 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 return ret;
2495}
2496
2497/*
2498 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002499 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002500 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 *
2502 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002503 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002505 * Note: "rettv.v_lock" is not set.
2506 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * Return OK or FAIL.
2508 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002509 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002512 char_u *p;
2513 int getnext;
2514
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002515 CLEAR_POINTER(rettv);
2516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /*
2518 * Get the first variable.
2519 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 return FAIL;
2522
Bram Moolenaar793648f2020-06-26 21:28:25 +02002523 p = eval_next_non_blank(*arg, evalarg, &getnext);
2524 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002526 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002527 int result;
2528 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 evalarg_T *evalarg_used = evalarg;
2530 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002532 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002533 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534
2535 if (evalarg == NULL)
2536 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002537 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540 orig_flags = evalarg_used->eval_flags;
2541 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002544 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002545 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002547 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002549 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002550 clear_tv(rettv);
2551 return FAIL;
2552 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002553 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002554 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002559 int error = FALSE;
2560
Bram Moolenaar13106602020-10-04 16:06:05 +02002561 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002562 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002563 else if (vim9script)
2564 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002565 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002567 if (error || !op_falsy || !result)
2568 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002569 if (error)
2570 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572
2573 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002574 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002576 if (op_falsy)
2577 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002578 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002579 {
mityu4ac198c2021-05-28 17:52:40 +02002580 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002581 clear_tv(rettv);
2582 return FAIL;
2583 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002585 evalarg_used->eval_flags = (op_falsy ? !result : result)
2586 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2587 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002588 {
2589 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002591 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002592 if (!op_falsy || !result)
2593 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002595 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002597 /*
2598 * Check for the ":".
2599 */
2600 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2601 if (*p != ':')
2602 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002603 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002604 if (evaluate && result)
2605 clear_tv(rettv);
2606 evalarg_used->eval_flags = orig_flags;
2607 return FAIL;
2608 }
2609 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002610 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002611 else
2612 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002613 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002614 {
2615 error_white_both(p, 1);
2616 clear_tv(rettv);
2617 evalarg_used->eval_flags = orig_flags;
2618 return FAIL;
2619 }
2620 *arg = p;
2621 }
2622
2623 /*
2624 * Get the third variable. Recursive!
2625 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002626 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002627 {
mityu4ac198c2021-05-28 17:52:40 +02002628 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002629 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002630 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002631 return FAIL;
2632 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002633 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2634 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002636 if (eval1(arg, &var2, evalarg_used) == FAIL)
2637 {
2638 if (evaluate && result)
2639 clear_tv(rettv);
2640 evalarg_used->eval_flags = orig_flags;
2641 return FAIL;
2642 }
2643 if (evaluate && !result)
2644 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002646
2647 if (evalarg == NULL)
2648 clear_evalarg(&local_evalarg, NULL);
2649 else
2650 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652
2653 return OK;
2654}
2655
2656/*
2657 * Handle first level expression:
2658 * expr2 || expr2 || expr2 logical OR
2659 *
2660 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002661 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 *
2663 * Return OK or FAIL.
2664 */
2665 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002668 char_u *p;
2669 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002672 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 return FAIL;
2676
2677 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002678 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002680 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683 evalarg_T *evalarg_used = evalarg;
2684 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 int evaluate;
2686 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687 long result = FALSE;
2688 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002689 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002691
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002692 if (evalarg == NULL)
2693 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002694 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002695 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002697 orig_flags = evalarg_used->eval_flags;
2698 evaluate = orig_flags & EVAL_EVALUATE;
2699 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002701 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002702 result = tv_get_bool_chk(rettv, &error);
2703 else if (tv_get_number_chk(rettv, &error) != 0)
2704 result = TRUE;
2705 clear_tv(rettv);
2706 if (error)
2707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709
2710 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002713 while (p[0] == '|' && p[1] == '|')
2714 {
2715 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002716 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002717 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002718 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002719 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002720 {
2721 error_white_both(p, 2);
2722 clear_tv(rettv);
2723 return FAIL;
2724 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002725 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002726 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727
2728 /*
2729 * Get the second variable.
2730 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002731 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002732 {
mityu4ac198c2021-05-28 17:52:40 +02002733 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002734 clear_tv(rettv);
2735 return FAIL;
2736 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002737 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2738 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002740 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002741 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002742
2743 /*
2744 * Compute the result.
2745 */
2746 if (evaluate && !result)
2747 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002748 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002749 result = tv_get_bool_chk(&var2, &error);
2750 else if (tv_get_number_chk(&var2, &error) != 0)
2751 result = TRUE;
2752 clear_tv(&var2);
2753 if (error)
2754 return FAIL;
2755 }
2756 if (evaluate)
2757 {
2758 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002759 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002760 rettv->v_type = VAR_BOOL;
2761 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002762 }
2763 else
2764 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002765 rettv->v_type = VAR_NUMBER;
2766 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002767 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002768 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002769
2770 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002772
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002773 if (evalarg == NULL)
2774 clear_evalarg(&local_evalarg, NULL);
2775 else
2776 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778
2779 return OK;
2780}
2781
2782/*
2783 * Handle second level expression:
2784 * expr3 && expr3 && expr3 logical AND
2785 *
2786 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002787 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 *
2789 * Return OK or FAIL.
2790 */
2791 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002794 char_u *p;
2795 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002798 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002800 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 return FAIL;
2802
2803 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002804 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002806 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002807 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002809 evalarg_T *evalarg_used = evalarg;
2810 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002811 int orig_flags;
2812 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 long result = TRUE;
2814 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002815 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002816 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002817
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 if (evalarg == NULL)
2819 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002820 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002823 orig_flags = evalarg_used->eval_flags;
2824 evaluate = orig_flags & EVAL_EVALUATE;
2825 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002827 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002828 result = tv_get_bool_chk(rettv, &error);
2829 else if (tv_get_number_chk(rettv, &error) == 0)
2830 result = FALSE;
2831 clear_tv(rettv);
2832 if (error)
2833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835
2836 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002837 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002839 while (p[0] == '&' && p[1] == '&')
2840 {
2841 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002842 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002843 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002844 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002845 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002846 {
2847 error_white_both(p, 2);
2848 clear_tv(rettv);
2849 return FAIL;
2850 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002851 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002852 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002853
2854 /*
2855 * Get the second variable.
2856 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002857 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002858 {
mityu4ac198c2021-05-28 17:52:40 +02002859 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002860 clear_tv(rettv);
2861 return FAIL;
2862 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002863 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2864 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002866 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002867 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002868 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002869
2870 /*
2871 * Compute the result.
2872 */
2873 if (evaluate && result)
2874 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002875 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002876 result = tv_get_bool_chk(&var2, &error);
2877 else if (tv_get_number_chk(&var2, &error) == 0)
2878 result = FALSE;
2879 clear_tv(&var2);
2880 if (error)
2881 return FAIL;
2882 }
2883 if (evaluate)
2884 {
2885 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002886 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002887 rettv->v_type = VAR_BOOL;
2888 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002889 }
2890 else
2891 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002892 rettv->v_type = VAR_NUMBER;
2893 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002894 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002895 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002896
2897 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002899
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002900 if (evalarg == NULL)
2901 clear_evalarg(&local_evalarg, NULL);
2902 else
2903 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905
2906 return OK;
2907}
2908
2909/*
2910 * Handle third level expression:
2911 * var1 == var2
2912 * var1 =~ var2
2913 * var1 != var2
2914 * var1 !~ var2
2915 * var1 > var2
2916 * var1 >= var2
2917 * var1 < var2
2918 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002919 * var1 is var2
2920 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 *
2922 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002923 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 *
2925 * Return OK or FAIL.
2926 */
2927 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002928eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002931 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002932 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002934 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002937 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002939 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 return FAIL;
2941
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002942 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002943
Bram Moolenaar696ba232020-07-29 21:20:41 +02002944 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002947 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002949 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002951 typval_T var2;
2952 int ic;
2953 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002954 int evaluate = evalarg == NULL
2955 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002956 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002957
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002958 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002959 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002960 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002961 p = *arg;
2962 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002963 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2964 {
mityu4ac198c2021-05-28 17:52:40 +02002965 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002966 clear_tv(rettv);
2967 return FAIL;
2968 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002969
Bram Moolenaar696ba232020-07-29 21:20:41 +02002970 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2971 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002972 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002973 clear_tv(rettv);
2974 return FAIL;
2975 }
2976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002977 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (p[len] == '?')
2979 {
2980 ic = TRUE;
2981 ++len;
2982 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002983 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 else if (p[len] == '#')
2985 {
2986 ic = FALSE;
2987 ++len;
2988 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002989 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002991 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 /*
2994 * Get the second variable.
2995 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002996 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2997 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002998 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002999 clear_tv(rettv);
3000 return FAIL;
3001 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003002 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003003 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003005 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 return FAIL;
3007 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003008 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003009 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003010 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003011
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003012 // use the line of the comparison for messages
3013 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003014 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003015 {
3016 ret = FAIL;
3017 clear_tv(rettv);
3018 }
3019 else
3020 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003021 clear_tv(&var2);
3022 return ret;
3023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025
3026 return OK;
3027}
3028
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003029/*
3030 * Make a copy of blob "tv1" and append blob "tv2".
3031 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 void
3033eval_addblob(typval_T *tv1, typval_T *tv2)
3034{
3035 blob_T *b1 = tv1->vval.v_blob;
3036 blob_T *b2 = tv2->vval.v_blob;
3037 blob_T *b = blob_alloc();
3038 int i;
3039
3040 if (b != NULL)
3041 {
3042 for (i = 0; i < blob_len(b1); i++)
3043 ga_append(&b->bv_ga, blob_get(b1, i));
3044 for (i = 0; i < blob_len(b2); i++)
3045 ga_append(&b->bv_ga, blob_get(b2, i));
3046
3047 clear_tv(tv1);
3048 rettv_blob_set(tv1, b);
3049 }
3050}
3051
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003052/*
3053 * Make a copy of list "tv1" and append list "tv2".
3054 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 int
3056eval_addlist(typval_T *tv1, typval_T *tv2)
3057{
3058 typval_T var3;
3059
3060 // concatenate Lists
3061 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3062 {
3063 clear_tv(tv1);
3064 clear_tv(tv2);
3065 return FAIL;
3066 }
3067 clear_tv(tv1);
3068 *tv1 = var3;
3069 return OK;
3070}
3071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003073 * Handle the bitwise left/right shift operator expression:
3074 * var1 << var2
3075 * var1 >> var2
3076 *
3077 * "arg" must point to the first non-white of the expression.
3078 * "arg" is advanced to just after the recognized expression.
3079 *
3080 * Return OK or FAIL.
3081 */
3082 static int
3083eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3084{
3085 /*
3086 * Get the first expression.
3087 */
3088 if (eval6(arg, rettv, evalarg) == FAIL)
3089 return FAIL;
3090
3091 /*
3092 * Repeat computing, until no '<<' or '>>' is following.
3093 */
3094 for (;;)
3095 {
3096 char_u *p;
3097 int getnext;
3098 exprtype_T type;
3099 int evaluate;
3100 typval_T var2;
3101 int vim9script;
3102
3103 p = eval_next_non_blank(*arg, evalarg, &getnext);
3104 if (p[0] == '<' && p[1] == '<')
3105 type = EXPR_LSHIFT;
3106 else if (p[0] == '>' && p[1] == '>')
3107 type = EXPR_RSHIFT;
3108 else
3109 return OK;
3110
3111 // Handle a bitwise left or right shift operator
3112 if (rettv->v_type != VAR_NUMBER)
3113 {
3114 // left operand should be a number
3115 emsg(_(e_bitshift_ops_must_be_number));
3116 clear_tv(rettv);
3117 return FAIL;
3118 }
3119
3120 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3121 vim9script = in_vim9script();
3122 if (getnext)
3123 {
3124 *arg = eval_next_line(*arg, evalarg);
3125 p = *arg;
3126 }
3127 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3128 {
3129 error_white_both(*arg, 2);
3130 clear_tv(rettv);
3131 return FAIL;
3132 }
3133
3134 /*
3135 * Get the second variable.
3136 */
3137 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3138 {
3139 error_white_both(p, 2);
3140 clear_tv(rettv);
3141 return FAIL;
3142 }
3143 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3144 if (eval6(arg, &var2, evalarg) == FAIL)
3145 {
3146 clear_tv(rettv);
3147 return FAIL;
3148 }
3149
3150 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3151 {
3152 // right operand should be a positive number
3153 if (var2.v_type != VAR_NUMBER)
3154 emsg(_(e_bitshift_ops_must_be_number));
3155 else
3156 emsg(_(e_bitshift_ops_must_be_postive));
3157 clear_tv(rettv);
3158 clear_tv(&var2);
3159 return FAIL;
3160 }
3161
3162 if (evaluate)
3163 {
3164 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3165 // shifting more bits than we have always results in zero
3166 rettv->vval.v_number = 0;
3167 else if (type == EXPR_LSHIFT)
3168 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003169 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003170 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003171 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003172 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003173 }
3174
3175 clear_tv(&var2);
3176 }
3177
3178 return OK;
3179}
3180
3181/*
3182 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003183 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003185 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003186 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 *
3188 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003189 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 *
3191 * Return OK or FAIL.
3192 */
3193 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003197 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003199 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return FAIL;
3201
3202 /*
3203 * Repeat computing, until no '+', '-' or '.' is following.
3204 */
3205 for (;;)
3206 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003207 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 int getnext;
3209 char_u *p;
3210 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003211 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003212 int concat;
3213 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003214 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003216 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003217 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003218 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003219 p = eval_next_non_blank(*arg, evalarg, &getnext);
3220 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003221 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003222 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3223 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003225 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3226 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003227
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003228 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003229 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003230 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003231 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003232 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003233 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003234 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003235 {
mityu4ac198c2021-05-28 17:52:40 +02003236 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003237 clear_tv(rettv);
3238 return FAIL;
3239 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003240 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003241 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003242 if ((op != '+' || (rettv->v_type != VAR_LIST
3243 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003244 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003245 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003246 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003247 int error = FALSE;
3248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003249 // For "list + ...", an illegal use of the first operand as
3250 // a number cannot be determined before evaluating the 2nd
3251 // operand: if this is also a list, all is ok.
3252 // For "something . ...", "something - ..." or "non-list + ...",
3253 // we know that the first operand needs to be a string or number
3254 // without evaluating the 2nd operand. So check before to avoid
3255 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003256 if (op != '.')
3257 tv_get_number_chk(rettv, &error);
3258 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003259 {
3260 clear_tv(rettv);
3261 return FAIL;
3262 }
3263 }
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /*
3266 * Get the second variable.
3267 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003268 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003269 {
mityu89dcb4d2021-05-28 13:50:17 +02003270 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003271 clear_tv(rettv);
3272 return FAIL;
3273 }
3274 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003275 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003277 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 return FAIL;
3279 }
3280
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003281 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
3283 /*
3284 * Compute the result.
3285 */
3286 if (op == '.')
3287 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003288 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3289 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003290 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003291
Bram Moolenaar2e086612020-08-25 22:37:48 +02003292 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003293 || var2.v_type == VAR_CHANNEL
3294 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003295 semsg(_(e_using_invalid_value_as_string_str),
3296 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003297 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003298 {
3299 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3300 var2.vval.v_float);
3301 s2 = buf2;
3302 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003303 else
3304 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003305 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003306 {
3307 clear_tv(rettv);
3308 clear_tv(&var2);
3309 return FAIL;
3310 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003311 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003312 clear_tv(rettv);
3313 rettv->v_type = VAR_STRING;
3314 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003316 else if (op == '+' && rettv->v_type == VAR_BLOB
3317 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003319 else if (op == '+' && rettv->v_type == VAR_LIST
3320 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003321 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003323 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 else
3326 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003327 int error = FALSE;
3328 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003329 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003330
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003333 f1 = rettv->vval.v_float;
3334 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003336 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003338 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 if (error)
3340 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003341 // This can only happen for "list + non-list" or
3342 // "blob + non-blob". For "non-list + ..." or
3343 // "something - ...", we returned before evaluating the
3344 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003346 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 return FAIL;
3348 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 if (var2.v_type == VAR_FLOAT)
3350 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352 if (var2.v_type == VAR_FLOAT)
3353 {
3354 f2 = var2.vval.v_float;
3355 n2 = 0;
3356 }
3357 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003359 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360 if (error)
3361 {
3362 clear_tv(rettv);
3363 clear_tv(&var2);
3364 return FAIL;
3365 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003366 if (rettv->v_type == VAR_FLOAT)
3367 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003371 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003372 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3373 {
3374 if (op == '+')
3375 f1 = f1 + f2;
3376 else
3377 f1 = f1 - f2;
3378 rettv->v_type = VAR_FLOAT;
3379 rettv->vval.v_float = f1;
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003382 {
3383 if (op == '+')
3384 n1 = n1 + n2;
3385 else
3386 n1 = n1 - n2;
3387 rettv->v_type = VAR_NUMBER;
3388 rettv->vval.v_number = n1;
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393 }
3394 return OK;
3395}
3396
3397/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003398 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * * number multiplication
3400 * / number division
3401 * % number modulo
3402 *
3403 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003404 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 *
3406 * Return OK or FAIL.
3407 */
3408 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003409eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410 char_u **arg,
3411 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003413 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003415 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003418 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003420 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return FAIL;
3422
3423 /*
3424 * Repeat computing, until no '*', '/' or '%' is following.
3425 */
3426 for (;;)
3427 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003428 int evaluate;
3429 int getnext;
3430 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003431 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003432 int op;
3433 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003434 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003435 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003437 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003438 p = eval_next_non_blank(*arg, evalarg, &getnext);
3439 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003440 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003442
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003443 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003445 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003446 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003447 {
3448 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3449 {
mityu4ac198c2021-05-28 17:52:40 +02003450 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003451 clear_tv(rettv);
3452 return FAIL;
3453 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003454 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003457 f1 = 0;
3458 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003459 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003460 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003462 if (rettv->v_type == VAR_FLOAT)
3463 {
3464 f1 = rettv->vval.v_float;
3465 use_float = TRUE;
3466 n1 = 0;
3467 }
3468 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 if (error)
3472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 else
3475 n1 = 0;
3476
3477 /*
3478 * Get the second variable.
3479 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003480 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3481 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003482 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003483 clear_tv(rettv);
3484 return FAIL;
3485 }
3486 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003487 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return FAIL;
3489
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003490 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 if (var2.v_type == VAR_FLOAT)
3493 {
3494 if (!use_float)
3495 {
3496 f1 = n1;
3497 use_float = TRUE;
3498 }
3499 f2 = var2.vval.v_float;
3500 n2 = 0;
3501 }
3502 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003504 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003505 clear_tv(&var2);
3506 if (error)
3507 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003508 if (use_float)
3509 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 /*
3513 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003514 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003516 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518 if (op == '*')
3519 f1 = f1 * f2;
3520 else if (op == '/')
3521 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003522#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003523 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003524 if (f2 == 0.0)
3525 {
3526 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003527 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003528 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003529 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003530 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003531 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003532 }
3533 else
3534 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003535#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 // We rely on the floating point library to handle divide
3537 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003538 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003539#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003543 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003544 return FAIL;
3545 }
3546 rettv->v_type = VAR_FLOAT;
3547 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549 else
3550 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003551 int failed = FALSE;
3552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 if (op == '*')
3554 n1 = n1 * n2;
3555 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003556 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003558 n1 = num_modulus(n1, n2, &failed);
3559 if (failed)
3560 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003561
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003562 rettv->v_type = VAR_NUMBER;
3563 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 }
3567
3568 return OK;
3569}
3570
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003571/*
3572 * Handle a type cast before a base level expression.
3573 * "arg" must point to the first non-white of the expression.
3574 * "arg" is advanced to just after the recognized expression.
3575 * Return OK or FAIL.
3576 */
3577 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003578eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003579 char_u **arg,
3580 typval_T *rettv,
3581 evalarg_T *evalarg,
3582 int want_string) // after "." operator
3583{
3584 type_T *want_type = NULL;
3585 garray_T type_list; // list of pointers to allocated types
3586 int res;
3587 int evaluate = evalarg == NULL ? 0
3588 : (evalarg->eval_flags & EVAL_EVALUATE);
3589
3590 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003591 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3592 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003593 {
3594 ++*arg;
3595 ga_init2(&type_list, sizeof(type_T *), 10);
3596 want_type = parse_type(arg, &type_list, TRUE);
3597 if (want_type == NULL && (evaluate || **arg != '>'))
3598 {
3599 clear_type_list(&type_list);
3600 return FAIL;
3601 }
3602
3603 if (**arg != '>')
3604 {
3605 if (*skipwhite(*arg) == '>')
3606 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3607 else
3608 emsg(_(e_missing_gt));
3609 clear_type_list(&type_list);
3610 return FAIL;
3611 }
3612 ++*arg;
3613 *arg = skipwhite_and_linebreak(*arg, evalarg);
3614 }
3615
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003616 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003617
3618 if (want_type != NULL && evaluate)
3619 {
3620 if (res == OK)
3621 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003622 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3623 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003624
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003625 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003626 {
3627 if (want_type == &t_bool && actual != &t_bool
3628 && (actual->tt_flags & TTFLAG_BOOL_OK))
3629 {
3630 int n = tv2bool(rettv);
3631
3632 // can use "0" and "1" for boolean in some places
3633 clear_tv(rettv);
3634 rettv->v_type = VAR_BOOL;
3635 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3636 }
3637 else
3638 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003639 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003640
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003641 where.wt_variable = TRUE;
3642 res = check_type(want_type, actual, TRUE, where);
3643 }
3644 }
3645 }
3646 clear_type_list(&type_list);
3647 }
3648
3649 return res;
3650}
3651
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003652 int
3653eval_leader(char_u **arg, int vim9)
3654{
3655 char_u *s = *arg;
3656 char_u *p = *arg;
3657
3658 while (*p == '!' || *p == '-' || *p == '+')
3659 {
3660 char_u *n = skipwhite(p + 1);
3661
3662 // ++, --, -+ and +- are not accepted in Vim9 script
3663 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3664 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003665 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003666 return FAIL;
3667 }
3668 p = n;
3669 }
3670 *arg = p;
3671 return OK;
3672}
3673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003675 * Check for a predefined value "true", "false" and "null.*".
3676 * Return OK when recognized.
3677 */
3678 int
3679handle_predefined(char_u *s, int len, typval_T *rettv)
3680{
3681 switch (len)
3682 {
3683 case 4: if (STRNCMP(s, "true", 4) == 0)
3684 {
3685 rettv->v_type = VAR_BOOL;
3686 rettv->vval.v_number = VVAL_TRUE;
3687 return OK;
3688 }
3689 if (STRNCMP(s, "null", 4) == 0)
3690 {
3691 rettv->v_type = VAR_SPECIAL;
3692 rettv->vval.v_number = VVAL_NULL;
3693 return OK;
3694 }
3695 break;
3696 case 5: if (STRNCMP(s, "false", 5) == 0)
3697 {
3698 rettv->v_type = VAR_BOOL;
3699 rettv->vval.v_number = VVAL_FALSE;
3700 return OK;
3701 }
3702 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003703 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3704 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003705#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003706 rettv->v_type = VAR_JOB;
3707 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003708#else
3709 rettv->v_type = VAR_SPECIAL;
3710 rettv->vval.v_number = VVAL_NULL;
3711#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003712 return OK;
3713 }
3714 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003715 case 9:
3716 if (STRNCMP(s, "null_", 5) != 0)
3717 break;
3718 if (STRNCMP(s + 5, "list", 4) == 0)
3719 {
3720 rettv->v_type = VAR_LIST;
3721 rettv->vval.v_list = NULL;
3722 return OK;
3723 }
3724 if (STRNCMP(s + 5, "dict", 4) == 0)
3725 {
3726 rettv->v_type = VAR_DICT;
3727 rettv->vval.v_dict = NULL;
3728 return OK;
3729 }
3730 if (STRNCMP(s + 5, "blob", 4) == 0)
3731 {
3732 rettv->v_type = VAR_BLOB;
3733 rettv->vval.v_blob = NULL;
3734 return OK;
3735 }
3736 break;
3737 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3738 {
3739 rettv->v_type = VAR_STRING;
3740 rettv->vval.v_string = NULL;
3741 return OK;
3742 }
3743 break;
3744 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003745 if (STRNCMP(s, "null_channel", 12) == 0)
3746 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003747#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003748 rettv->v_type = VAR_CHANNEL;
3749 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003750#else
3751 rettv->v_type = VAR_SPECIAL;
3752 rettv->vval.v_number = VVAL_NULL;
3753#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003754 return OK;
3755 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003756 if (STRNCMP(s, "null_partial", 12) == 0)
3757 {
3758 rettv->v_type = VAR_PARTIAL;
3759 rettv->vval.v_partial = NULL;
3760 return OK;
3761 }
3762 break;
3763 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3764 {
3765 rettv->v_type = VAR_FUNC;
3766 rettv->vval.v_string = NULL;
3767 return OK;
3768 }
3769 break;
3770 }
3771 return FAIL;
3772}
3773
3774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 * Handle sixth level expression:
3776 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003777 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003778 * "string" string constant
3779 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * &option-name option value
3781 * @r register contents
3782 * identifier variable value
3783 * function() function call
3784 * $VAR environment variable
3785 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003788 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003789 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 *
3791 * Also handle:
3792 * ! in front logical NOT
3793 * - in front unary minus
3794 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003795 * trailing [] subscript in String or List
3796 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003797 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 *
3799 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003800 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *
3802 * Return OK or FAIL.
3803 */
3804 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003805eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806 char_u **arg,
3807 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003808 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003811 int evaluate = evalarg != NULL
3812 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 int len;
3814 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003815 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 char_u *start_leader, *end_leader;
3817 int ret = OK;
3818 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003819 static int recurse = 0;
3820 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003824 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003829 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 */
3831 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003832 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 end_leader = *arg;
3835
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003836 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003837 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003838 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003839 ++*arg;
3840 return FAIL;
3841 }
3842
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003843 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003844 // and crash. With MSVC the stack is smaller.
3845 if (recurse ==
3846#ifdef _MSC_VER
3847 300
3848#else
3849 1000
3850#endif
3851 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003852 {
3853 semsg(_(e_expression_too_recursive_str), *arg);
3854 return FAIL;
3855 }
3856 ++recurse;
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 switch (**arg)
3859 {
3860 /*
3861 * Number constant.
3862 */
3863 case '0':
3864 case '1':
3865 case '2':
3866 case '3':
3867 case '4':
3868 case '5':
3869 case '6':
3870 case '7':
3871 case '8':
3872 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003873 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003874
3875 // Apply prefixed "-" and "+" now. Matters especially when
3876 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003877 if (ret == OK && evaluate && end_leader > start_leader
3878 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003879 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 break;
3881
3882 /*
3883 * String constant: "string".
3884 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003885 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 break;
3887
3888 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003889 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003891 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003892 break;
3893
3894 /*
3895 * List: [expr, expr]
3896 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003897 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 break;
3899
3900 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003901 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003902 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003903 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003904 {
3905 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3906 }
3907 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003908 {
3909 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003910 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003911 }
3912 else
3913 ret = NOTDONE;
3914 break;
3915
3916 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003917 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003918 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003919 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003920 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003921 ret = NOTDONE;
3922 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003923 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003924 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003925 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003926 break;
3927
3928 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003929 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003931 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 break;
3933
3934 /*
3935 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003936 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 */
LemonBoy2eaef102022-05-06 13:14:50 +01003938 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3939 ret = eval_interp_string(arg, rettv, evaluate);
3940 else
3941 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 /*
3945 * Register contents: @r.
3946 */
3947 case '@': ++*arg;
3948 if (evaluate)
3949 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003950 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003951 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003952 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003953 emsg_invreg(**arg);
3954 else
3955 {
3956 rettv->v_type = VAR_STRING;
3957 rettv->vval.v_string = get_reg_contents(**arg,
3958 GREG_EXPR_SRC);
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 if (**arg != NUL)
3962 ++*arg;
3963 break;
3964
3965 /*
3966 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003967 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003969 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003970 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003971 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003972 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003973 if (ret == OK && evaluate)
3974 {
3975 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3976
Bram Moolenaara9931532021-06-12 15:58:16 +02003977 // Compile it here to get the return type. The return
3978 // type is optional, when it's missing use t_unknown.
3979 // This is recognized in compile_return().
3980 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3981 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003982 if (compile_def_function(ufunc, FALSE,
3983 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003984 {
3985 clear_tv(rettv);
3986 ret = FAIL;
3987 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003988 }
3989 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003990 if (ret == NOTDONE)
3991 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003992 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003993 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003994
Bram Moolenaar9215f012020-06-27 21:18:00 +02003995 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003996 if (**arg == ')')
3997 ++*arg;
3998 else if (ret == OK)
3999 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004000 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004001 clear_tv(rettv);
4002 ret = FAIL;
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 break;
4006
Bram Moolenaar8c711452005-01-14 21:53:12 +00004007 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 break;
4009 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004010
4011 if (ret == NOTDONE)
4012 {
4013 /*
4014 * Must be a variable or function name.
4015 * Can also be a curly-braces kind of name: {expr}.
4016 */
4017 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004018 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019 if (alias != NULL)
4020 s = alias;
4021
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004022 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 ret = FAIL;
4024 else
4025 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004026 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4027
Bram Moolenaar4525a572022-02-13 11:57:33 +00004028 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004029 {
4030 emsg(_(e_cannot_use_underscore_here));
4031 ret = FAIL;
4032 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004033 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004034 && s[0] == 's' && s[1] == ':')
4035 {
4036 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4037 ret = FAIL;
4038 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004039 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004040 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004041 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004042 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004043 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004044 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004045 else if (flags & EVAL_CONSTANT)
4046 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004048 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004049 // get the value of "true", "false", etc. or a variable
4050 ret = FAIL;
4051 if (vim9script)
4052 ret = handle_predefined(s, len, rettv);
4053 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004054 {
4055 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004056 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004057 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004058 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004060 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004061 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004062 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004063 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004064 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004066 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004067 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004068 }
4069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004070 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4071 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004072 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004073 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 /*
4076 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4077 */
4078 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004079 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004080
4081 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004082 return ret;
4083}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004085/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004086 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004087 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004088 * Adjusts "end_leaderp" until it is at "start_leader".
4089 */
4090 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004091eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004092 typval_T *rettv,
4093 int numeric_only,
4094 char_u *start_leader,
4095 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004096{
4097 char_u *end_leader = *end_leaderp;
4098 int ret = OK;
4099 int error = FALSE;
4100 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004101 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004102 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004103 float_T f = 0.0;
4104
4105 if (rettv->v_type == VAR_FLOAT)
4106 f = rettv->vval.v_float;
4107 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004108 {
4109 while (VIM_ISWHITE(end_leader[-1]))
4110 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004111 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004112 val = tv2bool(rettv);
4113 else
4114 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004115 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004116 if (error)
4117 {
4118 clear_tv(rettv);
4119 ret = FAIL;
4120 }
4121 else
4122 {
4123 while (end_leader > start_leader)
4124 {
4125 --end_leader;
4126 if (*end_leader == '!')
4127 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004128 if (numeric_only)
4129 {
4130 ++end_leader;
4131 break;
4132 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004133 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004134 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004135 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004136 {
4137 rettv->v_type = VAR_BOOL;
4138 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4139 }
4140 else
4141 f = !f;
4142 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004143 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004144 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004145 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004146 type = VAR_BOOL;
4147 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004148 }
4149 else if (*end_leader == '-')
4150 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004151 if (rettv->v_type == VAR_FLOAT)
4152 f = -f;
4153 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004154 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004155 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004156 type = VAR_NUMBER;
4157 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004158 }
4159 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004160 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004163 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 else
4166 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004167 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004168 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004169 rettv->v_type = type;
4170 else
4171 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004172 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004175 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return ret;
4177}
4178
4179/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004180 * Call the function referred to in "rettv".
4181 */
4182 static int
4183call_func_rettv(
4184 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004185 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004186 typval_T *rettv,
4187 int evaluate,
4188 dict_T *selfdict,
4189 typval_T *basetv)
4190{
4191 partial_T *pt = NULL;
4192 funcexe_T funcexe;
4193 typval_T functv;
4194 char_u *s;
4195 int ret;
4196
4197 // need to copy the funcref so that we can clear rettv
4198 if (evaluate)
4199 {
4200 functv = *rettv;
4201 rettv->v_type = VAR_UNKNOWN;
4202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004204 if (functv.v_type == VAR_PARTIAL)
4205 {
4206 pt = functv.vval.v_partial;
4207 s = partial_name(pt);
4208 }
4209 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004210 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004211 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004212 if (s == NULL || *s == NUL)
4213 {
4214 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004215 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004216 goto theend;
4217 }
4218 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004219 }
4220 else
4221 s = (char_u *)"";
4222
Bram Moolenaara80faa82020-04-12 19:37:17 +02004223 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004224 funcexe.fe_firstline = curwin->w_cursor.lnum;
4225 funcexe.fe_lastline = curwin->w_cursor.lnum;
4226 funcexe.fe_evaluate = evaluate;
4227 funcexe.fe_partial = pt;
4228 funcexe.fe_selfdict = selfdict;
4229 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004230 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004231
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004232theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004233 // Clear the funcref afterwards, so that deleting it while
4234 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004235 if (evaluate)
4236 clear_tv(&functv);
4237
4238 return ret;
4239}
4240
4241/*
4242 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004243 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004244 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4245 */
4246 static int
4247eval_lambda(
4248 char_u **arg,
4249 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004250 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004253 int evaluate = evalarg != NULL
4254 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004255 typval_T base = *rettv;
4256 int ret;
4257
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004258 rettv->v_type = VAR_UNKNOWN;
4259
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004260 if (**arg == '{')
4261 {
4262 // ->{lambda}()
4263 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4264 }
4265 else
4266 {
4267 // ->(lambda)()
4268 ++*arg;
4269 ret = eval1(arg, rettv, evalarg);
4270 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004271 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004272 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004273 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004274 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004275 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004276 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4277 && rettv->v_type != VAR_PARTIAL)
4278 {
4279 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4280 return FAIL;
4281 }
4282 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004283 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004284 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004285 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004286
4287 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004288 {
4289 if (verbose)
4290 {
4291 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004292 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004293 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004294 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004295 }
4296 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004297 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004298 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004299 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004300 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004301
4302 // Clear the funcref afterwards, so that deleting it while
4303 // evaluating the arguments is possible (see test55).
4304 if (evaluate)
4305 clear_tv(&base);
4306
4307 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004308}
4309
4310/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004311 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004312 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004313 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4314 */
4315 static int
4316eval_method(
4317 char_u **arg,
4318 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004319 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004321{
4322 char_u *name;
4323 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004324 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004325 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004326 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004327 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004328 int evaluate = evalarg != NULL
4329 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004330
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004331 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004332
Bram Moolenaarac92e252019-08-03 21:58:38 +02004333 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004334 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004335 if (alias != NULL)
4336 name = alias;
4337
4338 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004339 {
4340 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004341 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004342 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004343 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004344 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004345 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004346 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004347
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004348 // If there is no "(" immediately following, but there is further on,
4349 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4350 // Does not handle anything where "(" is part of the expression.
4351 *arg = skipwhite(*arg);
4352
4353 if (**arg != '(' && alias == NULL
4354 && (paren = vim_strchr(*arg, '(')) != NULL)
4355 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004356 char_u *deref;
4357
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004358 *arg = name;
4359 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004360 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4361 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004362 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004363 *arg = name + len;
4364 ret = FAIL;
4365 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004366 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004367 {
4368 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004369 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004370 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004371 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004372 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004373
4374 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004375 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004376 *arg = skipwhite(*arg);
4377
4378 if (**arg != '(')
4379 {
4380 if (verbose)
4381 semsg(_(e_missing_parenthesis_str), name);
4382 ret = FAIL;
4383 }
4384 else if (VIM_ISWHITE((*arg)[-1]))
4385 {
4386 if (verbose)
4387 emsg(_(e_no_white_space_allowed_before_parenthesis));
4388 ret = FAIL;
4389 }
4390 else
4391 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004392 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004393 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004394 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004395
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004396 // Clear the funcref afterwards, so that deleting it while
4397 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004398 if (evaluate)
4399 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004400 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004401
Bram Moolenaarac92e252019-08-03 21:58:38 +02004402 return ret;
4403}
4404
4405/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004406 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4407 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4409 */
4410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004411eval_index(
4412 char_u **arg,
4413 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004414 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004415 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004416{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004417 int evaluate = evalarg != NULL
4418 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004420 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004421 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004422 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004423 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004424 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004426 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4427 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004428
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004429 init_tv(&var1);
4430 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 /*
4434 * dict.name
4435 */
4436 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004437 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004439 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004441 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 }
4443 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 /*
4446 * something[idx]
4447 *
4448 * Get the (first) variable from inside the [].
4449 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004450 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451 if (**arg == ':')
4452 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004453 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004455 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004456 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004457 semsg(_(e_white_space_required_before_and_after_str_at_str),
4458 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004459 clear_tv(&var1);
4460 return FAIL;
4461 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004462 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004464 int error = FALSE;
4465
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004466 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004467 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004468 && var1.v_type == VAR_FLOAT)
4469 {
4470 var1.vval.v_string = typval_tostring(&var1, TRUE);
4471 var1.v_type = VAR_STRING;
4472 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004473
Bram Moolenaar4525a572022-02-13 11:57:33 +00004474 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004475 tv_get_number_chk(&var1, &error);
4476 else
4477 error = tv_get_string_chk(&var1) == NULL;
4478 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004479 {
4480 // not a number or string
4481 clear_tv(&var1);
4482 return FAIL;
4483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485
4486 /*
4487 * Get the second variable from inside the [:].
4488 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004489 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 if (**arg == ':')
4491 {
4492 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004493 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004494 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004495 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004496 semsg(_(e_white_space_required_before_and_after_str_at_str),
4497 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004498 if (!empty1)
4499 clear_tv(&var1);
4500 return FAIL;
4501 }
4502 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 if (**arg == ']')
4504 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004505 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004507 if (!empty1)
4508 clear_tv(&var1);
4509 return FAIL;
4510 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004511 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 if (!empty1)
4515 clear_tv(&var1);
4516 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517 return FAIL;
4518 }
4519 }
4520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004522 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 if (**arg != ']')
4524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004525 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004526 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004527 clear_tv(&var1);
4528 if (range)
4529 clear_tv(&var2);
4530 return FAIL;
4531 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004532 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 }
4534
4535 if (evaluate)
4536 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004537 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004538 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004539 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004540
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004541 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004544 clear_tv(&var2);
4545 return res;
4546 }
4547 return OK;
4548}
4549
4550/*
4551 * Check if "rettv" can have an [index] or [sli:ce]
4552 */
4553 int
4554check_can_index(typval_T *rettv, int evaluate, int verbose)
4555{
4556 switch (rettv->v_type)
4557 {
4558 case VAR_FUNC:
4559 case VAR_PARTIAL:
4560 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004561 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004562 return FAIL;
4563 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004564 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004565 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004566 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004567 case VAR_BOOL:
4568 case VAR_SPECIAL:
4569 case VAR_JOB:
4570 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004571 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572 if (verbose)
4573 emsg(_(e_cannot_index_special_variable));
4574 return FAIL;
4575 case VAR_UNKNOWN:
4576 case VAR_ANY:
4577 case VAR_VOID:
4578 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004580 emsg(_(e_cannot_index_special_variable));
4581 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004585 case VAR_STRING:
4586 case VAR_LIST:
4587 case VAR_DICT:
4588 case VAR_BLOB:
4589 break;
4590 case VAR_NUMBER:
4591 if (in_vim9script())
4592 emsg(_(e_cannot_index_number));
4593 break;
4594 }
4595 return OK;
4596}
4597
4598/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004599 * slice() function
4600 */
4601 void
4602f_slice(typval_T *argvars, typval_T *rettv)
4603{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004604 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004605 && ((argvars[0].v_type != VAR_STRING
4606 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004607 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004608 && check_for_list_arg(argvars, 0) == FAIL)
4609 || check_for_number_arg(argvars, 1) == FAIL
4610 || check_for_opt_number_arg(argvars, 2) == FAIL))
4611 return;
4612
Bram Moolenaar6601b622021-01-13 21:47:15 +01004613 if (check_can_index(argvars, TRUE, FALSE) == OK)
4614 {
4615 copy_tv(argvars, rettv);
4616 eval_index_inner(rettv, TRUE, argvars + 1,
4617 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4618 TRUE, NULL, 0, FALSE);
4619 }
4620}
4621
4622/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 * Apply index or range to "rettv".
4624 * "var1" is the first index, NULL for [:expr].
4625 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004626 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4627 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004628 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4629 */
4630 int
4631eval_index_inner(
4632 typval_T *rettv,
4633 int is_range,
4634 typval_T *var1,
4635 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004636 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004637 char_u *key,
4638 int keylen,
4639 int verbose)
4640{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004641 varnumber_T n1, n2 = 0;
4642 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004643
4644 n1 = 0;
4645 if (var1 != NULL && rettv->v_type != VAR_DICT)
4646 n1 = tv_get_number(var1);
4647
4648 if (is_range)
4649 {
4650 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004652 if (verbose)
4653 emsg(_(e_cannot_slice_dictionary));
4654 return FAIL;
4655 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004657 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004658 else
4659 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004660 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004661
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 switch (rettv->v_type)
4663 {
4664 case VAR_UNKNOWN:
4665 case VAR_ANY:
4666 case VAR_VOID:
4667 case VAR_FUNC:
4668 case VAR_PARTIAL:
4669 case VAR_FLOAT:
4670 case VAR_BOOL:
4671 case VAR_SPECIAL:
4672 case VAR_JOB:
4673 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004674 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004675 break; // not evaluating, skipping over subscript
4676
4677 case VAR_NUMBER:
4678 case VAR_STRING:
4679 {
4680 char_u *s = tv_get_string(rettv);
4681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004683 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004684 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004685 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004686 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004687 else
4688 s = char_from_string(s, n1);
4689 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004690 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // The resulting variable is a substring. If the indexes
4693 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 if (n1 < 0)
4695 {
4696 n1 = len + n1;
4697 if (n1 < 0)
4698 n1 = 0;
4699 }
4700 if (n2 < 0)
4701 n2 = len + n2;
4702 else if (n2 >= len)
4703 n2 = len;
4704 if (n1 >= len || n2 < 0 || n1 > n2)
4705 s = NULL;
4706 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004707 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 }
4709 else
4710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 // The resulting variable is a string of a single
4712 // character. If the index is too big or negative the
4713 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 if (n1 >= len || n1 < 0)
4715 s = NULL;
4716 else
4717 s = vim_strnsave(s + n1, 1);
4718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
4720 rettv->v_type = VAR_STRING;
4721 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004722 }
4723 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004725 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004726 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4727 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004728 break;
4729
4730 case VAR_LIST:
4731 if (var1 == NULL)
4732 n1 = 0;
4733 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004734 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004735 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004736 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004737 return FAIL;
4738 break;
4739
4740 case VAR_DICT:
4741 {
4742 dictitem_T *item;
4743 typval_T tmp;
4744
4745 if (key == NULL)
4746 {
4747 key = tv_get_string_chk(var1);
4748 if (key == NULL)
4749 return FAIL;
4750 }
4751
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004752 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004753
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004754 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004755 {
4756 if (verbose)
4757 {
4758 if (keylen > 0)
4759 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004760 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004761 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004762 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004763 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004764
4765 copy_tv(&item->di_tv, &tmp);
4766 clear_tv(rettv);
4767 *rettv = tmp;
4768 }
4769 break;
4770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 return OK;
4772}
4773
4774/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004775 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004776 */
4777 char_u *
4778partial_name(partial_T *pt)
4779{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004780 if (pt != NULL)
4781 {
4782 if (pt->pt_name != NULL)
4783 return pt->pt_name;
4784 if (pt->pt_func != NULL)
4785 return pt->pt_func->uf_name;
4786 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004787 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004788}
4789
Bram Moolenaarddecc252016-04-06 22:59:37 +02004790 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004792{
4793 int i;
4794
4795 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004796 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004797 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004799 if (pt->pt_name != NULL)
4800 {
4801 func_unref(pt->pt_name);
4802 vim_free(pt->pt_name);
4803 }
4804 else
4805 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004806
Bram Moolenaar54656012021-06-09 20:50:46 +02004807 // "out_up" is no longer used, decrement refcount on partial that owns it.
4808 partial_unref(pt->pt_outer.out_up_partial);
4809
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004810 // Using pt_outer from another partial.
4811 partial_unref(pt->pt_outer_partial);
4812
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004813 // Decrease the reference count for the context of a closure. If down
4814 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004815 if (pt->pt_funcstack != NULL)
4816 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004817 --pt->pt_funcstack->fs_refcount;
4818 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004819 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004820 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004821 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4822 if (pt->pt_loopvars[i] != NULL)
4823 {
4824 --pt->pt_loopvars[i]->lvs_refcount;
4825 loopvars_check_refcount(pt->pt_loopvars[i]);
4826 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004827
Bram Moolenaarddecc252016-04-06 22:59:37 +02004828 vim_free(pt);
4829}
4830
4831/*
4832 * Unreference a closure: decrement the reference count and free it when it
4833 * becomes zero.
4834 */
4835 void
4836partial_unref(partial_T *pt)
4837{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004838 if (pt != NULL)
4839 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004840 int done = FALSE;
4841
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004842 if (--pt->pt_refcount <= 0)
4843 partial_free(pt);
4844
4845 // If the reference count goes down to one, the funcstack may be the
4846 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004847 else if (pt->pt_refcount == 1)
4848 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004849 // careful: if the funcstack is freed it may contain this partial
4850 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004851 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004852 done = funcstack_check_refcount(pt->pt_funcstack);
4853
Bram Moolenaarcc341812022-09-19 15:54:34 +01004854 if (!done)
4855 {
4856 int depth;
4857
4858 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4859 if (pt->pt_loopvars[depth] != NULL
4860 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4861 break;
4862 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004863 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004864 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004865}
4866
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004867/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004868 * Return the next (unique) copy ID.
4869 * Used for serializing nested structures.
4870 */
4871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004873{
4874 current_copyID += COPYID_INC;
4875 return current_copyID;
4876}
4877
4878/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004879 * Garbage collection for lists and dictionaries.
4880 *
4881 * We use reference counts to be able to free most items right away when they
4882 * are no longer used. But for composite items it's possible that it becomes
4883 * unused while the reference count is > 0: When there is a recursive
4884 * reference. Example:
4885 * :let l = [1, 2, 3]
4886 * :let d = {9: l}
4887 * :let l[1] = d
4888 *
4889 * Since this is quite unusual we handle this with garbage collection: every
4890 * once in a while find out which lists and dicts are not referenced from any
4891 * variable.
4892 *
4893 * Here is a good reference text about garbage collection (refers to Python
4894 * but it applies to all reference-counting mechanisms):
4895 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004896 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004897
4898/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004899 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004900 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004901 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004902 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004903 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004904garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004905{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004906 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004907 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004908 buf_T *buf;
4909 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004910 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004911 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004912
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004913 if (!testing)
4914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004916 want_garbage_collect = FALSE;
4917 may_garbage_collect = FALSE;
4918 garbage_collect_at_exit = FALSE;
4919 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004920
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004921 // The execution stack can grow big, limit the size.
4922 if (exestack.ga_maxlen - exestack.ga_len > 500)
4923 {
4924 size_t new_len;
4925 char_u *pp;
4926 int n;
4927
4928 // Keep 150% of the current size, with a minimum of the growth size.
4929 n = exestack.ga_len / 2;
4930 if (n < exestack.ga_growsize)
4931 n = exestack.ga_growsize;
4932
4933 // Don't make it bigger though.
4934 if (exestack.ga_len + n < exestack.ga_maxlen)
4935 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004936 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004937 pp = vim_realloc(exestack.ga_data, new_len);
4938 if (pp == NULL)
4939 return FAIL;
4940 exestack.ga_maxlen = exestack.ga_len + n;
4941 exestack.ga_data = pp;
4942 }
4943 }
4944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 // We advance by two because we add one for items referenced through
4946 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004947 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004949 /*
4950 * 1. Go through all accessible variables and mark all lists and dicts
4951 * with copyID.
4952 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Don't free variables in the previous_funccal list unless they are only
4955 // referenced through previous_funccal. This must be first, because if
4956 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004957 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004960 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004963 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4965 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004968 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4970 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004971 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004972 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4973 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004974#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004975 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004976 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4977 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004978 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004979 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004980 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4981 NULL, NULL);
4982#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004985 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004986 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4987 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004989 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004995 abort = abort || set_ref_in_functions(copyID);
4996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004998 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004999
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005000 // funcstacks keep variables for closures
5001 abort = abort || set_ref_in_funcstacks(copyID);
5002
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005003 // loopvars keep variables for loop blocks
5004 abort = abort || set_ref_in_loopvars(copyID);
5005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005007 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005008
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005009 // callbacks in buffers
5010 abort = abort || set_ref_in_buffers(copyID);
5011
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005012 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5013 abort = abort || set_ref_in_insexpand_funcs(copyID);
5014
5015 // 'operatorfunc' callback
5016 abort = abort || set_ref_in_opfunc(copyID);
5017
5018 // 'tagfunc' callback
5019 abort = abort || set_ref_in_tagfunc(copyID);
5020
5021 // 'imactivatefunc' and 'imstatusfunc' callbacks
5022 abort = abort || set_ref_in_im_funcs(copyID);
5023
Bram Moolenaar1dced572012-04-05 16:54:08 +02005024#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005025 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005026#endif
5027
Bram Moolenaardb913952012-06-29 12:54:53 +02005028#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005029 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005030#endif
5031
5032#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005033 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005034#endif
5035
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005036#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005037 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005038 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005039#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005040#ifdef FEAT_NETBEANS_INTG
5041 abort = abort || set_ref_in_nb_channel(copyID);
5042#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005043
Bram Moolenaare3188e22016-05-31 21:13:04 +02005044#ifdef FEAT_TIMERS
5045 abort = abort || set_ref_in_timer(copyID);
5046#endif
5047
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005048#ifdef FEAT_QUICKFIX
5049 abort = abort || set_ref_in_quickfix(copyID);
5050#endif
5051
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005052#ifdef FEAT_TERMINAL
5053 abort = abort || set_ref_in_term(copyID);
5054#endif
5055
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005056#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005057 abort = abort || set_ref_in_popups(copyID);
5058#endif
5059
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005060 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005061 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005062 /*
5063 * 2. Free lists and dictionaries that are not referenced.
5064 */
5065 did_free = free_unref_items(copyID);
5066
5067 /*
5068 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005069 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005072 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005073 else if (p_verbose > 0)
5074 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005075 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005076 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005077
5078 return did_free;
5079}
5080
5081/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005083 */
5084 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005085free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005086{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005087 int did_free = FALSE;
5088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 // Let all "free" functions know that we are here. This means no
5090 // dictionaries, lists, channels or jobs are to be freed, because we will
5091 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005092 in_free_unref_items = TRUE;
5093
5094 /*
5095 * PASS 1: free the contents of the items. We don't free the items
5096 * themselves yet, so that it is possible to decrement refcount counters
5097 */
5098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005100 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005103 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104
5105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // Go through the list of jobs and free items without the copyID. This
5107 // must happen before doing channels, because jobs refer to channels, but
5108 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005109 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5113#endif
5114
5115 /*
5116 * PASS 2: free the items themselves.
5117 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005118 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005119 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005120
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005121#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 // Go through the list of jobs and free items without the copyID. This
5123 // must happen before doing channels, because jobs refer to channels, but
5124 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125 free_unused_jobs(copyID, COPYID_MASK);
5126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005128 free_unused_channels(copyID, COPYID_MASK);
5129#endif
5130
5131 in_free_unref_items = FALSE;
5132
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133 return did_free;
5134}
5135
5136/*
5137 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005138 * "list_stack" is used to add lists to be marked. Can be NULL.
5139 *
5140 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005141 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005142 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005143set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005144{
5145 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005147 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005148 hashtab_T *cur_ht;
5149 ht_stack_T *ht_stack = NULL;
5150 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005151
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 cur_ht = ht;
5153 for (;;)
5154 {
5155 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005157 // Mark each item in the hashtab. If the item contains a hashtab
5158 // it is added to ht_stack, if it contains a list it is added to
5159 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005160 todo = (int)cur_ht->ht_used;
5161 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5162 if (!HASHITEM_EMPTY(hi))
5163 {
5164 --todo;
5165 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5166 &ht_stack, list_stack);
5167 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005168 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005169
5170 if (ht_stack == NULL)
5171 break;
5172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005173 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005174 cur_ht = ht_stack->ht;
5175 tempitem = ht_stack;
5176 ht_stack = ht_stack->prev;
5177 free(tempitem);
5178 }
5179
5180 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005181}
5182
Dominique Pelle748b3082022-01-08 12:41:16 +00005183#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5184 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005185/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005186 * Mark a dict and its items with "copyID".
5187 * Returns TRUE if setting references failed somehow.
5188 */
5189 int
5190set_ref_in_dict(dict_T *d, int copyID)
5191{
5192 if (d != NULL && d->dv_copyID != copyID)
5193 {
5194 d->dv_copyID = copyID;
5195 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5196 }
5197 return FALSE;
5198}
Dominique Pelle748b3082022-01-08 12:41:16 +00005199#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005200
5201/*
5202 * Mark a list and its items with "copyID".
5203 * Returns TRUE if setting references failed somehow.
5204 */
5205 int
5206set_ref_in_list(list_T *ll, int copyID)
5207{
5208 if (ll != NULL && ll->lv_copyID != copyID)
5209 {
5210 ll->lv_copyID = copyID;
5211 return set_ref_in_list_items(ll, copyID, NULL);
5212 }
5213 return FALSE;
5214}
5215
5216/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005218 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5219 *
5220 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005221 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005222 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005223set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005224{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005225 listitem_T *li;
5226 int abort = FALSE;
5227 list_T *cur_l;
5228 list_stack_T *list_stack = NULL;
5229 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 cur_l = l;
5232 for (;;)
5233 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005234 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Mark each item in the list. If the item contains a hashtab
5236 // it is added to ht_stack, if it contains a list it is added to
5237 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005238 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5239 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5240 ht_stack, &list_stack);
5241 if (list_stack == NULL)
5242 break;
5243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005245 cur_l = list_stack->list;
5246 tempitem = list_stack;
5247 list_stack = list_stack->prev;
5248 free(tempitem);
5249 }
5250
5251 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252}
5253
5254/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005255 * Mark the partial in callback 'cb' with "copyID".
5256 */
5257 int
5258set_ref_in_callback(callback_T *cb, int copyID)
5259{
5260 typval_T tv;
5261
5262 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5263 return FALSE;
5264
5265 tv.v_type = VAR_PARTIAL;
5266 tv.vval.v_partial = cb->cb_partial;
5267 return set_ref_in_item(&tv, copyID, NULL, NULL);
5268}
5269
5270/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005271 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 * "list_stack" is used to add lists to be marked. Can be NULL.
5273 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5274 *
5275 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005278set_ref_in_item(
5279 typval_T *tv,
5280 int copyID,
5281 ht_stack_T **ht_stack,
5282 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005285
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005286 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005287 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005288 dict_T *dd = tv->vval.v_dict;
5289
Bram Moolenaara03f2332016-02-06 18:09:59 +01005290 if (dd != NULL && dd->dv_copyID != copyID)
5291 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005292 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005293 dd->dv_copyID = copyID;
5294 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005296 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5297 }
5298 else
5299 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005300 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5301
Bram Moolenaara03f2332016-02-06 18:09:59 +01005302 if (newitem == NULL)
5303 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 else
5305 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005306 newitem->ht = &dd->dv_hashtab;
5307 newitem->prev = *ht_stack;
5308 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005309 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005310 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005311 }
5312 }
5313 else if (tv->v_type == VAR_LIST)
5314 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005315 list_T *ll = tv->vval.v_list;
5316
Bram Moolenaara03f2332016-02-06 18:09:59 +01005317 if (ll != NULL && ll->lv_copyID != copyID)
5318 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 ll->lv_copyID = copyID;
5321 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005322 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005323 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005324 }
5325 else
5326 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005327 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5328
Bram Moolenaara03f2332016-02-06 18:09:59 +01005329 if (newitem == NULL)
5330 abort = TRUE;
5331 else
5332 {
5333 newitem->list = ll;
5334 newitem->prev = *list_stack;
5335 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005336 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005337 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005338 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005339 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005340 else if (tv->v_type == VAR_FUNC)
5341 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005342 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005343 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005344 else if (tv->v_type == VAR_PARTIAL)
5345 {
5346 partial_T *pt = tv->vval.v_partial;
5347 int i;
5348
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005349 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005351 // Didn't see this partial yet.
5352 pt->pt_copyID = copyID;
5353
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005354 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005355
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 if (pt->pt_dict != NULL)
5357 {
5358 typval_T dtv;
5359
5360 dtv.v_type = VAR_DICT;
5361 dtv.vval.v_dict = pt->pt_dict;
5362 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5363 }
5364
5365 for (i = 0; i < pt->pt_argc; ++i)
5366 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5367 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005368 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005369 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005370 }
5371 }
5372#ifdef FEAT_JOB_CHANNEL
5373 else if (tv->v_type == VAR_JOB)
5374 {
5375 job_T *job = tv->vval.v_job;
5376 typval_T dtv;
5377
5378 if (job != NULL && job->jv_copyID != copyID)
5379 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005380 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 if (job->jv_channel != NULL)
5382 {
5383 dtv.v_type = VAR_CHANNEL;
5384 dtv.vval.v_channel = job->jv_channel;
5385 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5386 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005387 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388 {
5389 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005390 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005391 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5392 }
5393 }
5394 }
5395 else if (tv->v_type == VAR_CHANNEL)
5396 {
5397 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005398 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005399 typval_T dtv;
5400 jsonq_T *jq;
5401 cbq_T *cq;
5402
5403 if (ch != NULL && ch->ch_copyID != copyID)
5404 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005405 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005406 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 {
5408 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5409 jq = jq->jq_next)
5410 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5411 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5412 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005413 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005414 {
5415 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005416 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5418 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005419 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005420 {
5421 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005422 dtv.vval.v_partial =
5423 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5425 }
5426 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005427 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005428 {
5429 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005430 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005431 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5432 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005433 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005434 {
5435 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005436 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5438 }
5439 }
5440 }
5441#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005442 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005443}
5444
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 * Return a string with the string representation of a variable.
5447 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005448 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005449 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005450 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005451 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005452 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005453 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5454 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005455 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005457 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005458echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005459 typval_T *tv,
5460 char_u **tofree,
5461 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005462 int copyID,
5463 int echo_style,
5464 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005465 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005467 static int recurse = 0;
5468 char_u *r = NULL;
5469
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005472 if (!did_echo_string_emsg)
5473 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 // Only give this message once for a recursive call to avoid
5475 // flooding the user with errors. And stop iterating over lists
5476 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005477 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005478 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005480 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005481 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005482 }
5483 ++recurse;
5484
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 switch (tv->v_type)
5486 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005487 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005488 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005489 {
5490 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005491 r = tv->vval.v_string;
5492 if (r == NULL)
5493 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494 }
5495 else
5496 {
5497 *tofree = string_quote(tv->vval.v_string, FALSE);
5498 r = *tofree;
5499 }
5500 break;
5501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005503 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005504 char_u buf[MAX_FUNC_NAME_LEN];
5505
5506 if (echo_style)
5507 {
LemonBoya5d35902022-04-29 21:15:02 +01005508 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5509 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005510 buf, MAX_FUNC_NAME_LEN);
5511 if (r == buf)
5512 {
5513 r = vim_strsave(buf);
5514 *tofree = r;
5515 }
5516 else
5517 *tofree = NULL;
5518 }
5519 else
5520 {
5521 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5522 : make_ufunc_name_readable(
5523 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5524 TRUE);
5525 r = *tofree;
5526 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005528 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005529
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005530 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005531 {
5532 partial_T *pt = tv->vval.v_partial;
5533 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005534 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005535 garray_T ga;
5536 int i;
5537 char_u *tf;
5538
5539 ga_init2(&ga, 1, 100);
5540 ga_concat(&ga, (char_u *)"function(");
5541 if (fname != NULL)
5542 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005543 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005544 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005545 && vim_isupper(fname[1]))
5546 {
5547 ga_concat(&ga, (char_u *)"'g:");
5548 ga_concat(&ga, fname + 1);
5549 }
5550 else
5551 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005552 vim_free(fname);
5553 }
5554 if (pt != NULL && pt->pt_argc > 0)
5555 {
5556 ga_concat(&ga, (char_u *)", [");
5557 for (i = 0; i < pt->pt_argc; ++i)
5558 {
5559 if (i > 0)
5560 ga_concat(&ga, (char_u *)", ");
5561 ga_concat(&ga,
5562 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5563 vim_free(tf);
5564 }
5565 ga_concat(&ga, (char_u *)"]");
5566 }
5567 if (pt != NULL && pt->pt_dict != NULL)
5568 {
5569 typval_T dtv;
5570
5571 ga_concat(&ga, (char_u *)", ");
5572 dtv.v_type = VAR_DICT;
5573 dtv.vval.v_dict = pt->pt_dict;
5574 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5575 vim_free(tf);
5576 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005577 // terminate with ')' and a NUL
5578 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005579
5580 *tofree = ga.ga_data;
5581 r = *tofree;
5582 break;
5583 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005585 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005586 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005587 break;
5588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005590 if (tv->vval.v_list == NULL)
5591 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005592 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005593 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005594 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005595 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005596 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5597 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598 {
5599 *tofree = NULL;
5600 r = (char_u *)"[...]";
5601 }
5602 else
5603 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005604 int old_copyID = tv->vval.v_list->lv_copyID;
5605
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005607 *tofree = list2string(tv, copyID, restore_copyID);
5608 if (restore_copyID)
5609 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005610 r = *tofree;
5611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005612 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005615 if (tv->vval.v_dict == NULL)
5616 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005617 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005618 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005619 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005621 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5622 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005623 {
5624 *tofree = NULL;
5625 r = (char_u *)"{...}";
5626 }
5627 else
5628 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005629 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005630
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005631 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005632 *tofree = dict2string(tv, copyID, restore_copyID);
5633 if (restore_copyID)
5634 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005635 r = *tofree;
5636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005637 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005640 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005641 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005643 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005644 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005645 break;
5646
Bram Moolenaar835dc632016-02-07 14:27:38 +01005647 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005648 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005651 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5652 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005653 if (composite_val)
5654 {
5655 *tofree = string_quote(r, FALSE);
5656 r = *tofree;
5657 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005658#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005661 case VAR_INSTR:
5662 *tofree = NULL;
5663 r = (char_u *)"instructions";
5664 break;
5665
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005666 case VAR_FLOAT:
5667 *tofree = NULL;
5668 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5669 r = numbuf;
5670 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005671
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005672 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005673 case VAR_SPECIAL:
5674 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005675 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005676 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678
Bram Moolenaar8502c702014-06-17 12:51:16 +02005679 if (--recurse == 0)
5680 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005681 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005682}
5683
5684/*
5685 * Return a string with the string representation of a variable.
5686 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5687 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005688 * Does not put quotes around strings, as ":echo" displays values.
5689 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5690 * May return NULL.
5691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005693echo_string(
5694 typval_T *tv,
5695 char_u **tofree,
5696 char_u *numbuf,
5697 int copyID)
5698{
5699 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5700}
5701
5702/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005703 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5704 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005705 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005706 */
5707 int
5708buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5709{
5710 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005711 char_u *t;
5712 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005713
5714 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5715 return -1;
5716
5717 if (lnum > buf->b_ml.ml_line_count)
5718 lnum = buf->b_ml.ml_line_count;
5719
5720 str = ml_get_buf(buf, lnum, FALSE);
5721 if (str == NULL)
5722 return -1;
5723
5724 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005725 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005726
Bram Moolenaar91458462021-01-13 20:08:38 +01005727 // count the number of characters
5728 t = str;
5729 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5730 t += mb_ptr2len(t);
5731
5732 // In insert mode, when the cursor is at the end of a non-empty line,
5733 // byteidx points to the NUL character immediately past the end of the
5734 // string. In this case, add one to the character count.
5735 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5736 count++;
5737
5738 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005739}
5740
5741/*
5742 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005743 * byte index. Works only for loaded buffers. Returns -1 on failure.
5744 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005745 */
5746 int
5747buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5748{
5749 char_u *str;
5750 char_u *t;
5751
5752 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5753 return -1;
5754
5755 if (lnum > buf->b_ml.ml_line_count)
5756 lnum = buf->b_ml.ml_line_count;
5757
5758 str = ml_get_buf(buf, lnum, FALSE);
5759 if (str == NULL)
5760 return -1;
5761
5762 // Convert the character offset to a byte offset
5763 t = str;
5764 while (*t != NUL && --charidx > 0)
5765 t += mb_ptr2len(t);
5766
Bram Moolenaar91458462021-01-13 20:08:38 +01005767 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005768}
5769
5770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005772 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005774 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005775var2fpos(
5776 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005777 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005778 int *fnum, // set to fnum for '0, 'A, etc.
5779 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005781 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005783 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005786 if (varp->v_type == VAR_LIST)
5787 {
5788 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005789 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005790 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005791 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005792
5793 l = varp->vval.v_list;
5794 if (l == NULL)
5795 return NULL;
5796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005797 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005798 pos.lnum = list_find_nr(l, 0L, &error);
5799 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005800 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005801 if (charcol)
5802 len = (long)mb_charlen(ml_get(pos.lnum));
5803 else
5804 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005805
Bram Moolenaarec65d772020-08-20 22:29:12 +02005806 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005808 li = list_find(l, 1L);
5809 if (li != NULL && li->li_tv.v_type == VAR_STRING
5810 && li->li_tv.vval.v_string != NULL
5811 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005812 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005813 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005814 }
5815 else
5816 {
5817 pos.col = list_find_nr(l, 1L, &error);
5818 if (error)
5819 return NULL;
5820 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005823 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005824 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005825 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005827 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005828 pos.coladd = list_find_nr(l, 2L, &error);
5829 if (error)
5830 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005831
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005832 return &pos;
5833 }
5834
Bram Moolenaarc5809432021-03-27 21:23:30 +01005835 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5836 return NULL;
5837
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005838 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005839 if (name == NULL)
5840 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005841
5842 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005843 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005844 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005845 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005846 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005847 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005848 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005849 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005850 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005851 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005852 pos = VIsual;
5853 else
5854 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005855 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005856 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005857 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005859 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005860 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5862 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005863 pos = *pp;
5864 }
5865 if (pos.lnum != 0)
5866 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005867 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005868 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5869 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005871
Bram Moolenaara5525202006-03-02 22:52:09 +00005872 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005873
Bram Moolenaar477933c2007-07-17 14:32:23 +00005874 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005875 {
5876 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005877 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005878 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005879 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 // In silent Ex mode topline is zero, but that's not a valid line
5881 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005882 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005883 return &pos;
5884 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005885 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005886 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005887 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005888 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005889 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005890 return &pos;
5891 }
5892 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005895 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 pos.lnum = curbuf->b_ml.ml_line_count;
5898 pos.col = 0;
5899 }
5900 else
5901 {
5902 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005903 if (charcol)
5904 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5905 else
5906 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 return &pos;
5909 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005910 if (in_vim9script())
5911 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return NULL;
5913}
5914
5915/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005916 * Convert list in "arg" into a position and optional file number.
5917 * When "fnump" is NULL there is no file number, only 3 items.
5918 * Note that the column is passed on as-is, the caller may want to decrement
5919 * it to use 1 for the first column.
5920 * Return FAIL when conversion is not possible, doesn't check the position for
5921 * validity.
5922 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005924list2fpos(
5925 typval_T *arg,
5926 pos_T *posp,
5927 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005928 colnr_T *curswantp,
5929 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005930{
5931 list_T *l = arg->vval.v_list;
5932 long i = 0;
5933 long n;
5934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005935 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5936 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005937 if (arg->v_type != VAR_LIST
5938 || l == NULL
5939 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005940 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005941 return FAIL;
5942
5943 if (fnump != NULL)
5944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005946 if (n < 0)
5947 return FAIL;
5948 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005949 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005950 *fnump = n;
5951 }
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005954 if (n < 0)
5955 return FAIL;
5956 posp->lnum = n;
5957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005958 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005959 if (n < 0)
5960 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005961 // If character position is specified, then convert to byte position
5962 if (charcol)
5963 {
5964 buf_T *buf;
5965
5966 // Get the text for the specified line in a loaded buffer
5967 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5968 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5969 return FAIL;
5970
Bram Moolenaar91458462021-01-13 20:08:38 +01005971 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005972 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005973 posp->col = n;
5974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005976 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005977 posp->coladd = 0;
5978 else
5979 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005980
Bram Moolenaar493c1782014-05-28 14:34:46 +02005981 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005983
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984 return OK;
5985}
5986
5987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 * Get the length of an environment variable name.
5989 * Advance "arg" to the first character after the name.
5990 * Return 0 for error.
5991 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 char_u *p;
5996 int len;
5997
5998 for (p = *arg; vim_isIDc(*p); ++p)
5999 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006000 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return 0;
6002
6003 len = (int)(p - *arg);
6004 *arg = p;
6005 return len;
6006}
6007
6008/*
6009 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006010 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 * Return 0 if something is wrong.
6012 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 char_u *p;
6017 int len;
6018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006019 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006021 {
6022 if (*p == ':')
6023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006024 // "s:" is start of "s:var", but "n:" is not and can be used in
6025 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006026 len = (int)(p - *arg);
6027 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6028 || len > 1)
6029 break;
6030 }
6031 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006032 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 return 0;
6034
6035 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006036 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
6038 return len;
6039}
6040
6041/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006042 * Get the length of the name of a variable or function.
6043 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006045 * Return -1 if curly braces expansion failed.
6046 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * If the name contains 'magic' {}'s, expand them and return the
6048 * expanded name in an allocated string via 'alias' - caller must free.
6049 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006051get_name_len(
6052 char_u **arg,
6053 char_u **alias,
6054 int evaluate,
6055 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
6057 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 char_u *p;
6059 char_u *expr_start;
6060 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6065 && (*arg)[2] == (int)KE_SNR)
6066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 *arg += 3;
6069 return get_id_len(arg) + 3;
6070 }
6071 len = eval_fname_script(*arg);
6072 if (len > 0)
6073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 *arg += len;
6076 }
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006079 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006081 p = find_name_end(*arg, &expr_start, &expr_end,
6082 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (expr_start != NULL)
6084 {
6085 char_u *temp_string;
6086
6087 if (!evaluate)
6088 {
6089 len += (int)(p - *arg);
6090 *arg = skipwhite(p);
6091 return len;
6092 }
6093
6094 /*
6095 * Include any <SID> etc in the expanded string:
6096 * Thus the -len here.
6097 */
6098 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6099 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006100 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 *alias = temp_string;
6102 *arg = skipwhite(p);
6103 return (int)STRLEN(temp_string);
6104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006107 // Only give an error when there is something, otherwise it will be
6108 // reported at a higher level.
6109 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006110 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 return len;
6113}
6114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006115/*
6116 * Find the end of a variable or function name, taking care of magic braces.
6117 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6118 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006119 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006120 * Return a pointer to just after the name. Equal to "arg" if there is no
6121 * valid name.
6122 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006123 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124find_name_end(
6125 char_u *arg,
6126 char_u **expr_start,
6127 char_u **expr_end,
6128 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006130 int mb_nest = 0;
6131 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006133 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006134 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006136 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006138 *expr_start = NULL;
6139 *expr_end = NULL;
6140 }
6141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006142 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006143 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6144 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006145 return arg;
6146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 for (p = arg; *p != NUL
6148 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006149 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006150 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006151 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006153 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006155 if (*p == '\'')
6156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006157 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006158 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006159 ;
6160 if (*p == NUL)
6161 break;
6162 }
6163 else if (*p == '"')
6164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006165 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006166 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006167 if (*p == '\\' && p[1] != NUL)
6168 ++p;
6169 if (*p == NUL)
6170 break;
6171 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006172 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006174 // "s:" is start of "s:var", but "n:" is not and can be used in
6175 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006176 len = (int)(p - arg);
6177 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006178 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006179 break;
6180 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 if (*p == '[')
6185 ++br_nest;
6186 else if (*p == ']')
6187 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006189
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006190 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 if (*p == '{')
6193 {
6194 mb_nest++;
6195 if (expr_start != NULL && *expr_start == NULL)
6196 *expr_start = p;
6197 }
6198 else if (*p == '}')
6199 {
6200 mb_nest--;
6201 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6202 *expr_end = p;
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 }
6206
6207 return p;
6208}
6209
6210/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006211 * Expands out the 'magic' {}'s in a variable/function name.
6212 * Note that this can call itself recursively, to deal with
6213 * constructs like foo{bar}{baz}{bam}
6214 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6215 * "in_start" ^
6216 * "expr_start" ^
6217 * "expr_end" ^
6218 * "in_end" ^
6219 *
6220 * Returns a new allocated string, which the caller must free.
6221 * Returns NULL for failure.
6222 */
6223 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224make_expanded_name(
6225 char_u *in_start,
6226 char_u *expr_start,
6227 char_u *expr_end,
6228 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006229{
6230 char_u c1;
6231 char_u *retval = NULL;
6232 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006233
6234 if (expr_end == NULL || in_end == NULL)
6235 return NULL;
6236 *expr_start = NUL;
6237 *expr_end = NUL;
6238 c1 = *in_end;
6239 *in_end = NUL;
6240
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006241 temp_result = eval_to_string(expr_start + 1, FALSE);
6242 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006243 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006244 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6245 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006246 if (retval != NULL)
6247 {
6248 STRCPY(retval, in_start);
6249 STRCAT(retval, temp_result);
6250 STRCAT(retval, expr_end + 1);
6251 }
6252 }
6253 vim_free(temp_result);
6254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006255 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006256 *expr_start = '{';
6257 *expr_end = '}';
6258
6259 if (retval != NULL)
6260 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006261 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006262 if (expr_start != NULL)
6263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006264 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006265 temp_result = make_expanded_name(retval, expr_start,
6266 expr_end, temp_result);
6267 vim_free(retval);
6268 retval = temp_result;
6269 }
6270 }
6271
6272 return retval;
6273}
6274
6275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006280eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006282 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006283}
6284
6285/*
6286 * Return TRUE if character "c" can be used as the first character in a
6287 * variable or function name (excluding '{' and '}').
6288 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006291{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006292 return ASCII_ISALPHA(c) || c == '_';
6293}
6294
6295/*
6296 * Return TRUE if character "c" can be used as the first character of a
6297 * dictionary key.
6298 */
6299 int
6300eval_isdictc(int c)
6301{
6302 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303}
6304
6305/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006306 * Handle:
6307 * - expr[expr], expr[expr:expr] subscript
6308 * - ".name" lookup
6309 * - function call with Funcref variable: func(expr)
6310 * - method call: var->method()
6311 *
6312 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006313 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006314 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006316handle_subscript(
6317 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006318 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006319 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006320 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006321 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006322{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006323 int evaluate = evalarg != NULL
6324 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006325 int ret = OK;
6326 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006327 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006328 int getnext;
6329 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006330
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006331 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006332 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006333 // When at the end of the line and ".name" or "->{" or "->X" follows in
6334 // the next line then consume the line break.
6335 p = eval_next_non_blank(*arg, evalarg, &getnext);
6336 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006337 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006338 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6339 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6340 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006341 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006342 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006343 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006344 check_white = FALSE;
6345 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006346
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006347 if (rettv->v_type == VAR_ANY)
6348 {
6349 char_u *exp_name;
6350 int cc;
6351 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006352 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006353 type_T *type;
6354
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006355 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006356 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006357 if (**arg != '.')
6358 {
6359 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006360 semsg(_(e_expected_dot_after_name_str),
6361 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006362 ret = FAIL;
6363 break;
6364 }
6365 ++*arg;
6366 if (IS_WHITE_OR_NUL(**arg))
6367 {
6368 if (verbose)
6369 emsg(_(e_no_white_space_allowed_after_dot));
6370 ret = FAIL;
6371 break;
6372 }
6373
6374 // isolate the name
6375 exp_name = *arg;
6376 while (eval_isnamec(**arg))
6377 ++*arg;
6378 cc = **arg;
6379 **arg = NUL;
6380
6381 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006382 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006383 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006384
6385 if (idx < 0 && ufunc == NULL)
6386 {
6387 ret = FAIL;
6388 break;
6389 }
6390 if (idx >= 0)
6391 {
6392 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6393 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6394
6395 copy_tv(sv->sv_tv, rettv);
6396 }
6397 else
6398 {
6399 rettv->v_type = VAR_FUNC;
6400 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6401 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006402 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006403 }
6404
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006405 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6406 || rettv->v_type == VAR_PARTIAL))
6407 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006408 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006409 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6410 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006411
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006412 // Stop the expression evaluation when immediately aborting on
6413 // error, or when an interrupt occurred or an exception was thrown
6414 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006415 if (aborting())
6416 {
6417 if (ret == OK)
6418 clear_tv(rettv);
6419 ret = FAIL;
6420 }
6421 dict_unref(selfdict);
6422 selfdict = NULL;
6423 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006424 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006425 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006426 if (in_vim9script())
6427 *arg = skipwhite(p + 2);
6428 else
6429 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006430 if (ret == OK)
6431 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006432 if (VIM_ISWHITE(**arg))
6433 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006434 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006435 ret = FAIL;
6436 }
6437 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006438 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006439 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006440 else
6441 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006442 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006443 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006444 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006445 // "." is ".name" lookup when we found a dict or when evaluating and
6446 // scriptversion is at least 2, where string concatenation is "..".
6447 else if (**arg == '['
6448 || (**arg == '.' && (rettv->v_type == VAR_DICT
6449 || (!evaluate
6450 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006451 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006452 {
6453 dict_unref(selfdict);
6454 if (rettv->v_type == VAR_DICT)
6455 {
6456 selfdict = rettv->vval.v_dict;
6457 if (selfdict != NULL)
6458 ++selfdict->dv_refcount;
6459 }
6460 else
6461 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006462 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006463 {
6464 clear_tv(rettv);
6465 ret = FAIL;
6466 }
6467 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006468 else
6469 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006470 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006472 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6473 // Don't do this when "Func" is already a partial that was bound
6474 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006475 if (selfdict != NULL
6476 && (rettv->v_type == VAR_FUNC
6477 || (rettv->v_type == VAR_PARTIAL
6478 && (rettv->vval.v_partial->pt_auto
6479 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006480 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006481
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006482 dict_unref(selfdict);
6483 return ret;
6484}
6485
6486/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 * Make a copy of an item.
6488 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006489 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6491 * reference to an already copied list/dict can be used.
6492 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006493 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495item_copy(
6496 typval_T *from,
6497 typval_T *to,
6498 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006499 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006500 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501{
6502 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006507 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006509 }
6510 ++recurse;
6511
6512 switch (from->v_type)
6513 {
6514 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006515 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 case VAR_STRING:
6517 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006518 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006519 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006520 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006521 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006522 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006523 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 copy_tv(from, to);
6525 break;
6526 case VAR_LIST:
6527 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006528 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (from->vval.v_list == NULL)
6530 to->vval.v_list = NULL;
6531 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6532 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006533 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 to->vval.v_list = from->vval.v_list->lv_copylist;
6535 ++to->vval.v_list->lv_refcount;
6536 }
6537 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006538 to->vval.v_list = list_copy(from->vval.v_list,
6539 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 if (to->vval.v_list == NULL)
6541 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006542 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006543 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006545 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546 case VAR_DICT:
6547 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006548 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 if (from->vval.v_dict == NULL)
6550 to->vval.v_dict = NULL;
6551 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6552 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006553 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6555 ++to->vval.v_dict->dv_refcount;
6556 }
6557 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006558 to->vval.v_dict = dict_copy(from->vval.v_dict,
6559 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 if (to->vval.v_dict == NULL)
6561 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006562 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006563 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006564 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006566 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006568 }
6569 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006570 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571}
6572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 void
6574echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6575{
6576 char_u *tofree;
6577 char_u numbuf[NUMBUFLEN];
6578 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6579
6580 if (*atstart)
6581 {
6582 *atstart = FALSE;
6583 // Call msg_start() after eval1(), evaluating the expression
6584 // may cause a message to appear.
6585 if (with_space)
6586 {
6587 // Mark the saved text as finishing the line, so that what
6588 // follows is displayed on a new line when scrolling back
6589 // at the more prompt.
6590 msg_sb_eol();
6591 msg_start();
6592 }
6593 }
6594 else if (with_space)
6595 msg_puts_attr(" ", echo_attr);
6596
6597 if (p != NULL)
6598 for ( ; *p != NUL && !got_int; ++p)
6599 {
6600 if (*p == '\n' || *p == '\r' || *p == TAB)
6601 {
6602 if (*p != TAB && *needclr)
6603 {
6604 // remove any text still there from the command
6605 msg_clr_eos();
6606 *needclr = FALSE;
6607 }
6608 msg_putchar_attr(*p, echo_attr);
6609 }
6610 else
6611 {
6612 if (has_mbyte)
6613 {
6614 int i = (*mb_ptr2len)(p);
6615
6616 (void)msg_outtrans_len_attr(p, i, echo_attr);
6617 p += i - 1;
6618 }
6619 else
6620 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6621 }
6622 }
6623 vim_free(tofree);
6624}
6625
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 * ":echo expr1 ..." print each argument separated with a space, add a
6628 * newline at the end.
6629 * ":echon expr1 ..." print each argument plain.
6630 */
6631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006635 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006636 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 int needclr = TRUE;
6638 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006639 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006640 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006641 evalarg_T evalarg;
6642
Bram Moolenaare707c882020-07-01 13:07:37 +02006643 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 if (eap->skip)
6646 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006647 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // If eval1() causes an error message the text from the command may
6650 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006651 need_clr_eos = needclr;
6652
Bram Moolenaar68db9962021-05-09 23:19:22 +02006653 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006654 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 /*
6657 * Report the invalid expression unless the expression evaluation
6658 * has been cancelled due to an aborting error, an interrupt, or an
6659 * exception.
6660 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006661 if (!aborting() && did_emsg == did_emsg_before
6662 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006663 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006664 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 break;
6666 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006667 need_clr_eos = FALSE;
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006670 {
6671 if (rettv.v_type == VAR_VOID)
6672 {
6673 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6674 break;
6675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006677 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 arg = skipwhite(arg);
6681 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006682 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006683 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 if (eap->skip)
6686 --emsg_skip;
6687 else
6688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (needclr)
6691 msg_clr_eos();
6692 if (eap->cmdidx == CMD_echo)
6693 msg_end();
6694 }
6695}
6696
6697/*
6698 * ":echohl {name}".
6699 */
6700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006701ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006703 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
6705
6706/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006707 * Returns the :echo attribute
6708 */
6709 int
6710get_echo_attr(void)
6711{
6712 return echo_attr;
6713}
6714
6715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 * ":execute expr1 ..." execute the result of an expression.
6717 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006718 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006720 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 * Each gets spaces around each argument and a newline at the end for
6722 * echo commands
6723 */
6724 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006728 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 int ret = OK;
6730 char_u *p;
6731 garray_T ga;
6732 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006733 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734
6735 ga_init2(&ga, 1, 80);
6736
6737 if (eap->skip)
6738 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006739 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006741 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006742 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745 if (!eap->skip)
6746 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006747 char_u buf[NUMBUFLEN];
6748
6749 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006750 {
6751 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6752 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006753 semsg(_(e_using_invalid_value_as_string_str),
6754 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006755 p = NULL;
6756 }
6757 else
6758 p = tv_get_string_buf(&rettv, buf);
6759 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006760 else
6761 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006762 if (p == NULL)
6763 {
6764 clear_tv(&rettv);
6765 ret = FAIL;
6766 break;
6767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 len = (int)STRLEN(p);
6769 if (ga_grow(&ga, len + 2) == FAIL)
6770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006771 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 ret = FAIL;
6773 break;
6774 }
6775 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 ga.ga_len += len;
6779 }
6780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006781 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 arg = skipwhite(arg);
6783 }
6784
6785 if (ret != FAIL && ga.ga_data != NULL)
6786 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006787 // use the first line of continuation lines for messages
6788 SOURCING_LNUM = start_lnum;
6789
Bram Moolenaar37fef162022-08-29 18:16:32 +01006790 if (eap->cmdidx == CMD_echomsg
6791 || eap->cmdidx == CMD_echowindow
6792 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006793 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006794 // Mark the already saved text as finishing the line, so that what
6795 // follows is displayed on a new line when scrolling back at the
6796 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006797 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006798 }
6799
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006800 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006801 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006802 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006803 out_flush();
6804 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006805 else if (eap->cmdidx == CMD_echowindow)
6806 {
6807#ifdef HAS_MESSAGE_WINDOW
6808 start_echowindow();
6809#endif
6810 msg_attr(ga.ga_data, echo_attr);
6811#ifdef HAS_MESSAGE_WINDOW
6812 end_echowindow();
6813#endif
6814 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006815 else if (eap->cmdidx == CMD_echoconsole)
6816 {
6817 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6818 ui_write((char_u *)"\r\n", 2, TRUE);
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 else if (eap->cmdidx == CMD_echoerr)
6821 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006822 int save_did_emsg = did_emsg;
6823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006824 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006825 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (!force_abort)
6827 did_emsg = save_did_emsg;
6828 }
6829 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006830 {
6831 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6832
6833 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6834 sticky_cmdmod_flags = cmdmod.cmod_flags
6835 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 do_cmdline((char_u *)ga.ga_data,
6837 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006838 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841
6842 ga_clear(&ga);
6843
6844 if (eap->skip)
6845 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006846 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847}
6848
6849/*
6850 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6851 * "arg" points to the "&" or '+' when called, to "option" when returning.
6852 * Returns NULL when no option name found. Otherwise pointer to the char
6853 * after the option name.
6854 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006855 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006856find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 char_u *p = *arg;
6859
6860 ++p;
6861 if (*p == 'g' && p[1] == ':')
6862 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006863 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 p += 2;
6865 }
6866 else if (*p == 'l' && p[1] == ':')
6867 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006868 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 p += 2;
6870 }
6871 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006872 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 if (!ASCII_ISALPHA(*p))
6875 return NULL;
6876 *arg = p;
6877
6878 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006879 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 else
6881 while (ASCII_ISALPHA(*p))
6882 ++p;
6883 return p;
6884}
6885
6886/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006887 * Display script name where an item was last set.
6888 * Should only be invoked when 'verbose' is non-zero.
6889 */
6890 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006891last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006892{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006893 char_u *p;
6894
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006895 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006896 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006897 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006898 if (p != NULL)
6899 {
6900 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006901 msg_puts(_("\n\tLast set from "));
6902 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006903 if (script_ctx.sc_lnum > 0)
6904 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006905 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006906 msg_outnum((long)script_ctx.sc_lnum);
6907 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006908 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006909 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006910 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006911 }
6912}
6913
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006914#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916/*
6917 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006918 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 * "flags" can be "g" to do a global substitute.
6920 * Returns an allocated string, NULL for error.
6921 */
6922 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923do_string_sub(
6924 char_u *str,
6925 char_u *pat,
6926 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006927 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 int sublen;
6931 regmatch_T regmatch;
6932 int i;
6933 int do_all;
6934 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006935 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 garray_T ga;
6937 char_u *ret;
6938 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006939 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006941 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006943 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 ga_init2(&ga, 1, 200);
6946
6947 do_all = (flags[0] == 'g');
6948
6949 regmatch.rm_ic = p_ic;
6950 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6951 if (regmatch.regprog != NULL)
6952 {
6953 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006954 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006957 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006958 if (regmatch.startp[0] == regmatch.endp[0])
6959 {
6960 if (zero_width == regmatch.startp[0])
6961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006962 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006963 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006964 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6965 (size_t)i);
6966 ga.ga_len += i;
6967 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006968 continue;
6969 }
6970 zero_width = regmatch.startp[0];
6971 }
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 /*
6974 * Get some space for a temporary buffer to do the substitution
6975 * into. It will contain:
6976 * - The text up to where the match is.
6977 * - The substituted text.
6978 * - The text after the match.
6979 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006980 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006981 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6983 {
6984 ga_clear(&ga);
6985 break;
6986 }
6987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006988 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 i = (int)(regmatch.startp[0] - tail);
6990 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006992 (void)vim_regsub(&regmatch, sub, expr,
6993 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6994 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006996 tail = regmatch.endp[0];
6997 if (*tail == NUL)
6998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (!do_all)
7000 break;
7001 }
7002
7003 if (ga.ga_data != NULL)
7004 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7005
Bram Moolenaar473de612013-06-08 18:19:48 +02007006 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008
7009 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7010 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007011 if (p_cpo == empty_option)
7012 p_cpo = save_cpo;
7013 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007015 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007016 // If it's still empty it was changed and restored, need to restore in
7017 // the complicated way.
7018 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007019 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007020 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 return ret;
7024}